Click here to Skip to main content
15,894,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i run this application, it just displays "Please choose rock, paper or scissors" for as many times as the user inputs for the numPlays. I also need to store the results in an array and display all of the outcomes at the end. Can someone please point me in the right direction?

What I have tried:

Main Class:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Game rps = new Game();
            rps.start();
            rps.gameStart();
        }
    }
}




Game Class:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Game
    {
        string name;
        int numPlays;
        int game;

        public void start()
        {
            Console.WriteLine("Welcome to rock, paper, scissors");
            this.userSettings();
        }

        public void userSettings()
        {
            Console.WriteLine("What is your name: ");
            name = Console.ReadLine();

            Console.WriteLine("How many games would you like to play?: ");
            Int32.TryParse(Console.ReadLine(), out numPlays);
            if (numPlays < 10)
            {
                while (numPlays % 2 == 0)
                {
                    Console.WriteLine("\nNumber is not odd try again.");
                    Console.WriteLine("How many games would you like to play?: ");
                    Int32.TryParse(Console.ReadLine(), out numPlays);
                }

                Console.ReadLine();
            }

            else
            {
                Console.WriteLine("Please insert number less than or equal to 9");
                this.userSettings();
            }


        }

        public void gameStart()
        {

        for(game = 1; game <= numPlays; game++)

            Console.WriteLine("Please choose Rock, Paper, or Scissors");
            string userSelection = Console.ReadLine();

            Random r = new Random();
            int computerSelection = r.Next(4);

            if (computerSelection == 1)
            {
                if (userSelection == "rock")
                {
                    Console.WriteLine("Computer Choice: Rock\n");
                    Console.WriteLine("This round is a tie");
                }
                else if (userSelection == "paper")
                {
                    Console.WriteLine("Computer Choice: Paper\n");
                    Console.WriteLine("This round is a tie");
                }
                else if (userSelection == "scissors")
                {
                    Console.WriteLine("Computer Choice: Scissors\n");
                    Console.WriteLine("This round is a tie");
                }
                else
                {
                    Console.WriteLine("You must choose either rock, paper or scissors");
                }
                Console.ReadLine();
            }

            else if (computerSelection == 2)
            {
                if (userSelection == "rock")
                {
                    Console.WriteLine("Computer Choice: Paper\n");
                    Console.WriteLine("You lose this round");

                }
                else if (userSelection == "paper")
                {
                    Console.WriteLine("Computer Choice: Scissors\n");
                    Console.WriteLine("You lose this round");

                }
                else if (userSelection == "scissors")
                {
                    Console.WriteLine("Computer Choice: Rock\n");
                    Console.WriteLine("You lose this round");
                }
                else
                {
                    Console.WriteLine("You must choose either rock, paper or scissors");
                }
                Console.ReadLine();
            }
            

            else if (computerSelection == 3)
            {
                if (userSelection == "rock")
                {
                    Console.WriteLine("The computer chose scissors");
                    Console.WriteLine("You win this round, rock beats scissors");

                }
                else if (userSelection == "paper")
                {
                    Console.WriteLine("The computer chose rock");
                    Console.WriteLine("You win this round,paper beats rock");

                }
                else if (userSelection == "scissors")
                {
                    Console.WriteLine("The computer chose paper");
                    Console.WriteLine("You win this round, scissors beats paper!");

                }
                else
                {
                    Console.WriteLine("You must choose either rock, paper or scissors");

                }
                Console.ReadLine();
            }
        }
    }
}
Posted
Updated 15-Aug-16 14:36pm
v3
Comments
George Swan 15-Aug-16 17:05pm    
I would suggest using lookup tables to both find and store the results.
https://en.wikipedia.org/wiki/Lookup_table
Perić Željko 21-Aug-16 5:58am    
What would happend if computer have selected number zero.

1 solution

Learning programming is like learning to drive a car. When you learn how to drive a car, you are required to learn all road signs before putting your hands on the wheel, no matter what. It is the same for programming, there is a set of techniques you need to learn before starting your own projects.

My advices:
- Forget this project for now.

- Learn the language by reading documentation.

- Follow tutos to get familiar with common techniques and concepts.

- Learn the debugger as soon as possible, it is a great tool to learn how your code behave.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

- Learn some analyze methods/techniques, , Dijkstra Top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]

Remember the exercises and little projects of tutos are not here to make something useful, they are here to teach you programming.

Advice: add a display of computer choices and you will see that your program is completely wrong.
C#
Random r = new Random();
int computerSelection = r.Next(4);
Console.WriteLine(computerSelection);
Console.WriteLine("\n");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900