T Wilko
T Wilko

Reputation: 13

Function isn't being called after the while loop ends

I've created a program where a user can play rock paper scissors.

One of the things that I want to be able to do is ask the user if they want to play the game again and if they do it restarts and if not it just ends.

I have the function all setup and I call the function after the while loop in the class "MainClass"(playAgainn is the function name) but for some reason it isn't called and the program just ends with listing the scores of the CPU and the player and pressing enter does nothing so the program just stays open.

Does anyone know why this is or how I would have to change my code so that I am able to loop the program based on the user input of "y" or "n" - the help would be greatly appreciated. I am coding in C# using Microsoft Visual Studio Community 2015 if that helps. The Code is listed Below.

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

namespace ProgramV3
{
    class RPS
    {
        public static int scorePlayer = 0;
        public static int scoreCPU = 0;
        public static int playerWon = 0;
        public static int cpuWon = 0;
        static string inputPlayer, inputCPU;
        public static int randomInt;
        public static bool playAgain = true;


        Random rnd = new Random();

       public static void userInput()
        {
            Console.Write("Choose between ROCK, PAPER and SCISSORS:    ");
            inputPlayer = Console.ReadLine();
            inputPlayer = inputPlayer.ToUpper();

            Random rnd = new Random();

            randomInt = rnd.Next(1, 4);
        }

        public static void CheckWhoWinsEach()
        {
            switch (randomInt)
            {
                case 1:
                    inputCPU = "ROCK";
                    Console.WriteLine("Computer chose ROCK");
                    if (inputPlayer == "ROCK")
                    {
                        Console.WriteLine("DRAW THIS ROUND!!\n\n");
                    }
                    else if (inputPlayer == "PAPER")
                    {
                        Console.WriteLine("PLAYER WINS THIS ROUND!!\n\n");
                        scorePlayer++;
                    }
                    else if (inputPlayer == "SCISSORS")
                    {
                        Console.WriteLine("CPU WINS THIS ROUND!!\n\n");
                        scoreCPU++;
                    }
                    break;
                case 2:
                    inputCPU = "PAPER";
                    Console.WriteLine("Computer chose PAPER");
                    if (inputPlayer == "PAPER")
                    {
                        Console.WriteLine("DRAW THIS ROUND!!\n\n");
                    }
                    else if (inputPlayer == "ROCK")
                    {
                        Console.WriteLine("CPU WINS THIS ROUND!!\n\n");
                        scoreCPU++;
                    }
                    else if (inputPlayer == "SCISSORS")
                    {
                        Console.WriteLine("PLAYER WINS THIS ROUND!!\n\n");
                        scorePlayer++;
                    }
                    break;
                case 3:
                    inputCPU = "SCISSORS";
                    Console.WriteLine("Computer chose SCISSORS");
                    if (inputPlayer == "SCISSORS")
                    {
                        Console.WriteLine("DRAW THIS ROUND!!\n\n");
                    }
                    else if (inputPlayer == "ROCK")
                    {
                        Console.WriteLine("PLAYER WINS THIS ROUND!!\n\n");
                        scorePlayer++;
                    }
                    else if (inputPlayer == "PAPER")
                    {
                        Console.WriteLine("CPU WINS!!\n\n");
                        scoreCPU++;
                    }
                    break;
                default:
                    Console.WriteLine("Invalid entry!");
                    break;
            }

            Console.WriteLine("\n\nSCORES:\tPLAYER:\t{0}\tCPU:\t{1}", scorePlayer, scoreCPU);
        }

        public static void CheckWhoWon()
        {
            if (scorePlayer == 3)
            {
                Console.WriteLine("Player OVERALL!");
                playerWon++;
                Console.WriteLine("CPU HAS WON: {0} TIMES AND PLAYER HAS WON {1} TIMES", cpuWon, playerWon);
            }
            else if (scoreCPU == 3)
            {
                Console.WriteLine("CPU WON OVERALL!");
                cpuWon++;
                Console.WriteLine("CPU HAS WON: {0} TIMES AND PLAYER HAS WON: {1} TIMES",cpuWon, playerWon);
            }
            else
            {

            }

        }

        public static void playAgainn()
        {
            Console.WriteLine("Do you want to play again?(y/n)");
            string loop = Console.ReadLine();
            if (loop == "y")
            {
                playAgain = true;
                Console.Clear();
            }
            else if (loop == "n")
            {
                playAgain = false;
            }
            else
            {

            }
        }


    }

    class MainClass
    {
        public static void Main(string[] args)
        {

            while (RPS.playAgain)
            {
                while (RPS.scorePlayer < 3 && RPS.scoreCPU < 3)
                {
                    RPS.userInput();
                    RPS.CheckWhoWinsEach();
                    RPS.CheckWhoWon();


                }
            }
            RPS.playAgainn();
        }

    }
}

Upvotes: 1

Views: 77

Answers (4)

Mary
Mary

Reputation: 15091

The problem with your playAgainn() Function is it is not a Function. I don't think it will compile.

public static bool playAgainn()
        {
            Console.WriteLine("Do you want to play again?(y/n)");
            string loop = Console.ReadLine();
            if (loop == "y")
            {
                Console.Clear();
                return true;
            }
            else 
            {
                return false;
            }
        }

Upvotes: 0

JosMelka
JosMelka

Reputation: 21

Have you tried to put the RPS.playAgainn(); exactly before the closing brace } that precedes? I'm reading it on my phone and I think it's out of the RPS.playAgain=true area so it is executed after the while is done.

Upvotes: 0

Orifjon
Orifjon

Reputation: 1097

There are couple of issues in your loops.

  1. You should check and set playAgain inside of outer while loop.
  2. You should reset scores when user selects y.

play again

public static void playAgainn()
{
    Console.WriteLine("Do you want to play again?(y/n)");
    string loop = Console.ReadLine();
    if (loop == "y")
    {
        playAgain = true;
        // reset scores here
        Console.Clear();
    }
    else if (loop == "n")
    {
        playAgain = false;
    }
    else
    {

    }
}

main class

class MainClass
{
  public static void Main(string[] args)
  {
      while (RPS.playAgain)
      {
          while (RPS.scorePlayer < 3 && RPS.scoreCPU < 3)
          {
              RPS.userInput();
              RPS.CheckWhoWinsEach();
              RPS.CheckWhoWon();
          }
            RPS.playAgainn();
      }
  }
}

Upvotes: 0

Leon Bambrick
Leon Bambrick

Reputation: 26306

You need to move the RPS.playAgainn(); up into the loop above it.

Then the mainClass will look like this:

class MainClass
{
    public static void Main(string[] args)
    {
        while (RPS.playAgain)
        {
            while (RPS.scorePlayer < 3 && RPS.scoreCPU < 3)
            {
                RPS.userInput();
                RPS.CheckWhoWinsEach();
                RPS.CheckWhoWon();
            }
            RPS.playAgainn();
        }
    }
}

Upvotes: 1

Related Questions