Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Don't understand Pig Java game.... I can't use "scanner

$
0
0
Directions:
1. the player rolls the die
2. if the die shows one, the player gets no points for that turn and player's turn ends.
3. if the die shows 2 - 6:
the number on the die is added to the turnPoints
the player now has the option to either:
ROLL AGAIN or HOLD
4. The player continues to roll the die until either, the player decides to hold or the player rolls a 1. The points for each roll are added to the turnPoints, but of course at any point if the plyer rolls a 1 before the player hold, the turnPoints go to zero.

I can only use loops like while and nested loops. We have not learned "scanner" yet, so I cannot use that.

I just don't know where to go from here.... I have put MY code below, but feel free to edit it if you would like, or share your own code, if you have done this project and are WILLING to share.

Thank you!

Here is the code that I have come up with.
import cs1.Keyboard;

public class PigGameYa
{
	public static void main(String[] args)
	{
		int humanTot = 0;
		int compTot = 0;
		int roundTot = 0;
		Die die1 = new Die();
		Die die2 = new Die();
		int num1, num2;
		int rollagain = 1;
		String possesor = new String();

		System.out.println("Welcome to the game PIG, where you gamble your luck.  You will be playing against the computer.");
		System.out.println("To play, you roll two dice, and add the values to a total.  You DO NOT want to roll ANY 1's");

		System.out.println("You will be using some shortcuts for this game... Hit the 1 key to roll for both you and the computer, and the 2 key to hand your die off.");
		System.out.println("Well Its your turn! Best of luck!");

		while (humanTot < 100 && compTot <100)
		{
				while (rollagain == 1)
				{
					possesor = ("Human");
					num1 = die1.roll();
					num2 = die2.roll();

					if (num1 != 1 && num2 != 1)
					{
						roundTot = roundTot + num1 + num2;

						System.out.println("Press 1 to roll again... or 2 to hand off!");
						rollagain = Keyboard.readInt();
					}
					else if (num1 == 1 || num2 == 1)
					{
						if (num1 == 1 && num2 == 1)
						{
							roundTot = 0;
							humanTot = 0;
							rollagain = 2;
						}
						else
						{
							roundTot = 0;
							rollagain = 2;
						}
					}
					humanTot = humanTot + roundTot;
					System.out.println("The player's total so far is... " + humanTot);
					System.out.println("The computer's total so far is... " + compTot);
					System.out.println("Dice Possesion: " + possesor);
					System.out.println("The round total is... " + roundTot);
					System.out.println("Current Roll... " + num1 + "   " + num2);

				}
				while (rollagain == 2 && roundTot <= 20)
				{
					possesor = ("Computer");
					num1 = die1.roll();
					num2 = die2.roll();

					System.out.println("The two dice were " + num1 + " " + num2);

					if (num1 != 1 && num2 != 1)
						{
							roundTot = roundTot + (num1 + num2);

							if (roundTot >= 20)
								rollagain = 1;
							else
								rollagain = 2;

						}
						else if (num1 == 1 || num2 == 1)
						{
							if (num1 == 1 && num2 == 1)
							{
								roundTot = 0;
								humanTot = 0;
								rollagain = 1;
							}
							else
							{
								roundTot = 0;
								rollagain = 1;
							}
						}
					compTot = compTot + roundTot;
					System.out.println("The player's total so far is... " + humanTot);
					System.out.println("The computer's total so far is... " + compTot);
					System.out.println("Dice Possesion: " + possesor);
					System.out.println("The round total is... " + roundTot);
					System.out.println("Current Roll... " + num1 + "   " + num2);
				}

		}
	}
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



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