Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Rock, Paper, Scissors and looping dilemma.

$
0
0

Bare with me, this might be kind of sloppy. But first off, I've basically built the majority of the game, but now all I have left is to figure out a better way to get the game to continuously loop without a preset counter.

I have to apply the same loop technique to get the invalid entry messages to correctly reprompt the user and return to the original function had they entered the correct input. These two things are where my brain has shut off. :(

If anyone has suggestions, please let me know! I also put PlayAgain(); method in there, but I'm thinking that might be unnecessary if I can just figure out what kind of loop or whatever I can use to get the game to reset. And would need it to figure out how to get the invalid entries to work. Just this and two really confusing array exercises left! But anyway, thanks in advance if anyone is willing to help. I'll post the code below, and it may be kind of sloppy but I'll try my best.

using System;


namespace Project3
{
    class RPSGameTest
    {
        static void Main(string[] args)
        {

            
            // creating the game object
            RPSGame rpsGame = new RPSGame();
            
            Console.WriteLine("Welcome to Rock, Paper, Scissors!");
            //prompting user with necessary input to begin or end game sequence
            Console.WriteLine("Would you like to play? Enter 'yes', 'y', 'Yes', or 'Y' for yes.\nEnter 'no', 'No', 'n', 'N' for no.\nDecision?");
            string playerChoice = Console.ReadLine();
            if (playerChoice == "yes" || playerChoice == "y" || playerChoice == "Yes" || playerChoice == "Y")
            {
                //calling method to start game
                rpsGame.StartGame();
                //calling method to display results of game
                rpsGame.DetermineWinner();
            }

            else if (playerChoice == "no" || playerChoice == "n" || playerChoice == "No" || playerChoice == "N")
            {
                Console.WriteLine("See ya!");
            }

            else
            {
                Console.WriteLine("Invalid choice.");
                Console.WriteLine("Yes or no?");
                playerChoice = Console.ReadLine();
            }

            //beginning game again.
            rpsGame.PlayAgain();
          
       
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project3
{
    class RPSGame
    {
        //initializing randomgenerator computer move and player move
        string computerChoice;
        string playerMove;

        public void PlayAgain()
        {
            Console.WriteLine("Would you like to play?");
            Console.WriteLine();
            string playerChoice = Console.ReadLine();

            if (playerChoice == "yes" || playerChoice == "y" || playerChoice == "Yes" || playerChoice == "Y")
            {
                StartGame();
                DetermineWinner();
            }

            else if (playerChoice == "no" || playerChoice == "n" || playerChoice == "No" || playerChoice == "N")
            {
                //player does not want to play, ending game
                Console.WriteLine("See ya!");
            }

            else
            {
                //prompting player for another entry.
                Console.WriteLine("Invalid entry.");
                Console.WriteLine("Please try again: ");
                playerChoice = Console.ReadLine();
                StartGame();
                DetermineWinner();
            }
        }



        
        //method to determine the winner and display results
        public void DetermineWinner()
        {

            if (computerChoice == playerMove)
            {
                Console.WriteLine("Tie game.");
                Console.WriteLine("Your hand was: {0}", playerMove);
                Console.WriteLine("The computer's hand was: {0}", computerChoice);
            }

            else if (computerChoice == "Rock" && playerMove == "Scissors" || computerChoice == "Scissors" && playerMove == "Paper" || computerChoice == "Paper"&& playerMove == "Rock")
            { 
                Console.WriteLine("You lose!");
                Console.WriteLine("Your hand was: {0}", playerMove);
                Console.WriteLine("The computer's hand was: {0}", computerChoice);
            }

            else if (computerChoice == "Rock" && playerMove == "Paper" || computerChoice == "Scissors" && playerMove == "Rock" || computerChoice == "Paper"&& playerMove == "Scissors")
            { 
                Console.WriteLine("You win!");
                Console.WriteLine("Your hand was: {0}", playerMove);
                Console.WriteLine("The computer's hand was: {0}", computerChoice);
            }

            else
            {
                Console.WriteLine("Invalid Entry: Must be 'Rock', 'Paper', or 'Scissors'.");
                Console.WriteLine("Please try again: ");
                playerMove = Console.ReadLine();
            }



        }
        //method to begin game
        public void StartGame()
        {

            Random RandomNumber = new Random();
            //random generator that decides computer's hand
            int x = RandomNumber.Next(0, 3);
            if (x == 0)
            { computerChoice = "Rock"; }
            else if (x == 1)
            { computerChoice = "Paper"; }
            else if (x == 2)
            { computerChoice = "Scissors"; }

            Console.WriteLine("Rock, Paper, or Scissors?");
            playerMove = Console.ReadLine();
            //reading input to decide player's hand

         }

        
    }
}


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>