Hello. I am new to Java. I use BlueJ as my compiler. Currently i am working on a project that creates a game between 1 user and the computer you roll the dice and add points anyway almost all the rules are in the source below./* user plays a game with the computer that has to do with rolling dice* first to 100 wins* rolling a 1 means you lose all points for that round* rolling two 1's means you lose all points for the game*/import java util. Scanner;merchandise java util. Random;public class Pig{public static void main(String[] args){Scanner scan = new Scanner(System in);Random gen = new Random();int a = 0;int d = 0;int b = 1;int c = 0;int total1 = 0;int total2 = 0;PairOfDice user = new PairOfDice();PairOfDice computer = new PairOfDice();// introductionSystem out println(" Welcome to the dice game of PIG ");System out println(" Do you know the rules ? (1 for yes or 2 for no)");d = scan nextInt();if (d == 1)System out println(" ok then lets begin ");elseif (d == 2){System out println(" In this version you will play against the computer ");System out println(" to play you roll the dice and add up your total ");System out println(" If you roll a one during your roll you lose all points during that roll.(and forfeit dice to other user)");System out println(" If you roll two one's during a single roll you lose all your points for the entire game (and forfeit dice to other user)");System out println(" After rolling you can roll again or move the dice over to the other user (the computer in this case)");System out println(" If you keep the cut (be a pig) you risk losing points");System out println(" If you turn them over you risk allowing your opponent to gain points or win");System out println(" The choice is yours!");}while (b == 1){System out println(" ---------------------------------------------------------------------------------------------------------");System out println(" press 1 to roll the dice!");b = scan nextInt();// user turnsif ( b == 1 )c = user roll2();if (user facevalue1() != 1 && user facevalue2() != 1){System out println(" the first dice is a " + user facevalue1());System out println(" the second cut is a " + user facevalue2());System out println("your two cut combined rolled a " + c) ;total1 = be1 + c;System out println(" you get " + c + " points for this round ");System out println(" your total for the entire game is " + total1);System out println(" press 1 to keep the dice or press 2 to turn the dice over to the computer");System out println(" --------------------------------------------------------------------------");b = scan nextInt();}if (user facevalue1() == 1 || user facevalue2() == 1 ){System out println(" the first dice is a " + user facevalue1());System out println(" the second dice is a " + user facevalue2());System out println(" your two dice combined rolled a " + c) ;System out println(" you do not get any points for that roll becuase a 1 was show");System out println(" your total is still " + total1);System out println(" the dice are automatically turned over to the computer ");System out println(" -------------------------------------------------------------------------");b = 2;}if (user facevalue1() == 1 && user facevalue2() == 1){System out println(" the first dice is a " + user facevalue1());System out println(" the second dice is a " + user facevalue2());System out println(" your two dice combined rolled a " + c);System out println(" you do not get any points for that roll because a 1 was present");total1 = 0;System out println(" Uh Oh! you rolled two ones you lose all your points!");System out println(" Your total for the game is now " + total1 );System out println(" --------------------------------------------------------------------------");}}// computers turnswhile (b == 2){System out println(" the computer is now rolling..... ");a = computer roll2();if (computer facevalue1() != 1 && computer facevalue2() != 1){System out println(" the computers first dice is a " + computer facevalue1());System out println(" the computers first dice is a " + computer facevalue2());System out println(" the computers combined roll is a " + a);total2 = total2 + a;System out println(" the computers total is " + total2);System out println(" the computer has turned the dice over to you!");System out println(" --------------------------------------------------------------------------");b = 1;}if (computer facevalue1() == 1 || computer facevalue2() == 1);{System out println(" the computers first dice is a " + computer facevalue1());System out println(" the computers first dice is a " + computer facevalue2());System out println(" the computers combined roll is a " + a);System out println(" the computer does not recieve any points for that roll because a 1 was present");System out println(" the computers be is still " + total2);System out println(" the cut are now yours. ");System out println(" --------------------------------------------------------------------------");b = 1;}if (computer facevalue1() == 1 && computer facevalue2() == 1);{System out println(" the computers first cut is a " + computer facevalue1());System out println(" the computers first dice is a " + computer facevalue2());System out println(" the computers combined roll is a " + a);System out println(" the computer does not recieve any points for tht roll because a 1 was present");System out println(" Aren't you a lucky one the computer rolled two 1's the computer now has 0 points.");System out println(" the dice are now yours. ");System out println(" --------------------------------------------------------------------------");b = 1;}}// victory conditions// restartingif (total1 >= 100){System out println(" Congragtulations you win!");System out println(" press 5 to play again ");b = examine nextInt();}if (total2 >= 100){System out println(" Game Over! the computer wins");System out println(" press 5 to play again and get revenge " );b = scan nextInt();}if (b == 5){total1 = 0;total2 = 0;System out println(" press 1 to begin ! ");b = scan nextInt();}}}--------------------------------------------------------------------------that is the main code this is the controller class that setups the PairOfDicepublic class PairOfDice{Die die1 = new Die();Die die2 = new Die();public int roll2(){die1 roll();die2 roll();return die1 getFaceValue() + die2 getFaceValue();}public int facevalue1(){return die1 getFaceValue();}public int facevalue2(){return die2 getFaceValue();}}-------------------------------------------------------------------------and this is the class that actually creates the die import java util. Random;public class Die{private final int MIN_FACES = 4;private static Random generator = new Random();private int numFaces; // number of sides on the diepublic int faceValue; // current value showing on the die//-----------------------------------------------------------------// Defaults to a six-sided die. Initial face value is 1.//-----------------------------------------------------------------public Die (){numFaces = 6;faceValue = 1;}//-----------------------------------------------------------------// Explicitly sets the size of the die. Defaults to a size of// six if the parameter is invalid. Initial face value is 1.//-----------------------------------------------------------------public Die (int faces){if (faces < MIN_FACES)numFaces = 6;elsenumFaces = faces;faceValue = 1;}//-----------------------------------------------------------------// Rolls the die and returns the result.//-----------------------------------------------------------------public int roll (){faceValue = generator nextInt(numFaces) + 1;return faceValue;}//-----------------------------------------------------------------// Returns the current die value.//-----------------------------------------------------------------public int getFaceValue (){return faceValue;}}------------------------------------------------------------------------Everything compiles fine but there is a glitch in the game that i can't seem to fix in the code. when i run the game the output is usually similar to this--------------------------------------------------------------Welcome to the cut game of PIGDo you know the rules ? (1 for yes or 2 for no)1ok then lets begin---------------------------------------------------------------------------------------------------------press 1 to turn the dice!1the first dice is a 1the back up dice is a 3your two dice combined rolled a 4you do not get any points for that roll becuase a 1 was presentyour total is still 0the dice are automatically turned over to the computer-------------------------------------------------------------------------the computer is now rolling.... the computers first dice is a 5the computers first dice is a 5the computers combined roll is a 10the computers total is 10the computer has turned the dice over to you!--------------------------------------------------------------------------the computers first dice is a 5the computers first dice is a 5the computers combined roll is a 10the computer does not recieve any points for that roll because a 1 was presentthe computers total is still 10the dice are now yours.--------------------------------------------------------------------------the computers first dice is a 5the computers first cut is a 5the computers combined roll is a 10the computer does not recieve any points for tht roll because a 1 was presentAren't you a lucky one the computer rolled two 1's the computer now has 0 points the dice are now yours.-----------------------------------------------------------------------------------------------------------------------------------------------------when the user rolls a 1 or two 1's the dice are automatically turned over to the computer (like they are suppose to) but when the computer rolls it prints it 3 times and automatically says it has two 1's everytime after the computers roll when the user hits one the computers rolls again.(but the user is suppose to) and when you hit 2 after the computer turn the user rolls (user is only suppose to roll if 1 is hit)so.... if anyone can help me figure out the bug in the code it would be greatly appreciated.=)If you would like to contact me VIA Aol Instant Messanger you can at: berube52991 (i would prefer this method because it would be easier to relay messages).
Forex Groups - Tips on Trading
Related article:
http://www.java-forums.org/advanced-java/3880-help-debugging-dice-game.html
comments | Add comment | Report as Spam
|