none
Stuck with this Pigs game! Need some tips please!! RRS feed

  • Question

  • Hey there, I've been having a major issue with my math for this specific Pigs game that I need to create for my class. I've coded everything else for it and have the two methods created and a while loop so that they can bounce back and forth. It's the humans turn first and then it will rotate to the robots turn and will keep looping until the max points (100) are reached and they're the winner. The code is looping the way it should and everything is working but the math isn't right. I've had to dig into these equations over a dozen times in the past few days and can't seem to figure it out. I just tested it and the humans turn total was 12 and it said they're total points later on after the robots turn were now at 22 or something like that. They're just very random and it's hard to tell where I'm going wrong here. I'm not asking you guys to finish my project for me but if you could please point me in the right direction before I lose my mind that would be swell lol thanks!!

            static void humanPigs()
            {
                int humanDiceNumber = 0;//dice roll for the human user
                int maxPoints;//max amount of points to play for
                int humanPoints = 0;//point value for each human dice roll
                int humanRoundPoints = 0;//total amount of points for the humans round
                int totalHumanPoints = 0;//total amount of points for the human for all rounds

                bool isPlaying = true;

                Random rand = new Random();

                Console.WriteLine("---------------------");
                Console.WriteLine("--Pigs (Human Turn)--");
                Console.WriteLine("---------------------");

                Console.WriteLine("It's your turn!!");

                maxPoints = 100;

                while (isPlaying)
                {


                    for (int i = 0; i < 1; i++)//random num generator x1
                    {
                        humanDiceNumber = rand.Next(1, 7);//generator for numbers between 1-6

                    }
                    Console.WriteLine($"You rolled a {humanDiceNumber}");

                    if (humanDiceNumber == 1)
                    {
                        humanPoints = 0;
                        humanRoundPoints = 0;

                        Console.WriteLine($"Your turn score is {humanRoundPoints}");

                        isPlaying = false;
                        computerPigs();

                    }
                    else
                    {
                        humanPoints = humanDiceNumber;
                        humanRoundPoints += humanPoints;
                        totalHumanPoints += humanRoundPoints;

                        Console.WriteLine("Enter r to roll or h to hold (r/h): ");

                        switch (Console.ReadLine())
                        {
                            case "r":
                                Console.WriteLine($"Your turn point total is {humanRoundPoints}");
                                break;
                            case "h":
                                isPlaying = false;
                                Console.WriteLine("You HOLD");
                                Console.WriteLine($"Your turn point total is {humanRoundPoints}");
                                Console.WriteLine("");
                                Console.WriteLine($"Your total points: {totalHumanPoints}");
                                computerPigs();
                                break;
                            default:
                                isPlaying = false;
                                Console.WriteLine("Sorry that isn't a valid entry! I'll hold this for you!");
                                Console.WriteLine("You HOLD");
                                Console.WriteLine($"Your turn point total is {humanRoundPoints}");
                                Console.WriteLine("");
                                Console.WriteLine($"Your total points: {totalHumanPoints}");
                                computerPigs();
                                break;
                        }
                        if (totalHumanPoints >= maxPoints)
                        {
                            
                            Console.WriteLine($"Your turn point total is {humanRoundPoints}");
                            Console.WriteLine("");
                            Console.WriteLine("YOU WIN!!");
                            Console.WriteLine($"Your total points: {totalHumanPoints}");

                            isPlaying = false;
                            SimMenu();
                        }
                    }
                }           
            }
            static void computerPigs()
            {
                int computerDiceNumber = 0;
                int maxPoints;//max amount of points to play for
                int computerPoints = 0;//point value for each computer dice roll
                int computerRoundPoints = 0;//total amount of points for the computers round
                int totalComputerPoints = 0;//total amount of points for the computer for all rounds

                maxPoints = 100;

                Random rand = new Random();

                bool isPlaying = true;

                Console.WriteLine("------------------------");
                Console.WriteLine("--Pigs (Computer Turn)--");
                Console.WriteLine("------------------------");

                Console.WriteLine("It's the computer's turn!!");

                while (isPlaying)
                {

                    for (int i = 0; i < 1; i++)//random num generator x1
                    {
                        computerDiceNumber = rand.Next(1, 7);//generator for numbers between 1-6

                    }
                    Console.WriteLine($"Computer rolled a {computerDiceNumber}");

                    if (computerDiceNumber == 1)
                    {
                        computerPoints = 0;
                        computerRoundPoints = 0;

                        Console.WriteLine($"Computer's turn score is {computerRoundPoints}");

                        isPlaying = false;
                        humanPigs();

                    }
                    if (computerDiceNumber != 1)
                    {
                        computerPoints = computerDiceNumber;
                        computerRoundPoints += computerPoints;
                        totalComputerPoints += computerRoundPoints;

                        if (computerRoundPoints >= 10)
                        {
                            
                            Console.WriteLine("Computer HOLDS");
                            Console.WriteLine($"Computer's turn point total is {computerRoundPoints}");
                            Console.WriteLine("");
                            Console.WriteLine($"Computer's total points: {totalComputerPoints}");
                            isPlaying = false;
                            humanPigs();
                        }
                        if (totalComputerPoints >= maxPoints)
                        {
                            Console.WriteLine($"Computer's turn point total is {computerRoundPoints}");
                            Console.WriteLine("");
                            Console.WriteLine("COMPUTER WINS!!");
                            Console.WriteLine($"Computer's total points: {totalComputerPoints}");
                            isPlaying = false;
                            SimMenu();
                        }                   
                    }
                }           
            }

    Monday, November 11, 2019 2:30 AM

Answers

  • Greetings OhhBnertt.

    I think (not 100% sure) your problem is that humanPigs() and computerPigs() shouldn't call each other. Every time one of those methods is called, it starts with a new points total (totalHumanPoints or totalComputerPoints respectively) for that call of the method. And if one calls the other which calls the first one again, you get recursion where the number of points in each call is different, and, well... I think it gets a bit tangled.

    You should set the points totals outside of the methods so they have class scope, then call the methods in a non-recursive way.

    int humanTotalPoints = 0; int computerTotalPoints = 0;

    void RunTheGame()

    { while(no-one has won) { while(it's the human's turn) { humanPigs(); } while{it's the computer's turn) { computerpigs(); } }

    }

    Or something like that. I hope that helps.

    • Marked as answer by OhhBnertt Tuesday, November 12, 2019 9:17 PM
    Monday, November 11, 2019 11:03 AM

All replies

  • Greetings OhhBnertt.

    I think (not 100% sure) your problem is that humanPigs() and computerPigs() shouldn't call each other. Every time one of those methods is called, it starts with a new points total (totalHumanPoints or totalComputerPoints respectively) for that call of the method. And if one calls the other which calls the first one again, you get recursion where the number of points in each call is different, and, well... I think it gets a bit tangled.

    You should set the points totals outside of the methods so they have class scope, then call the methods in a non-recursive way.

    int humanTotalPoints = 0; int computerTotalPoints = 0;

    void RunTheGame()

    { while(no-one has won) { while(it's the human's turn) { humanPigs(); } while{it's the computer's turn) { computerpigs(); } }

    }

    Or something like that. I hope that helps.

    • Marked as answer by OhhBnertt Tuesday, November 12, 2019 9:17 PM
    Monday, November 11, 2019 11:03 AM
  • Good day Ante,

    Thanks for your reply! I looked into what you mentioned and it seems like it'll work just fine! Thanks for all your help!

    Monday, November 11, 2019 9:42 PM