Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-16-2007, 10:28 PM
Member
 
Join Date: Nov 2007
Posts: 6
Rep Power: 0
Windoze is on a distinguished road
Default help debugging a dice game
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;
import 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();

// introduction

System.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 ");
else
if (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 turn the dice over to the other user (the computer in this case)");
System.out.println(" If you keep the dice (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 turns

if ( 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 dice is a " + user.facevalue2());
System.out.println("your two dice combined rolled a " + c) ;
total1 = total1 + 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 present");
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 turns

while (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 total is still " + total2);
System.out.println(" the dice are now yours. ");
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 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
// restarting

if (total1 >= 100)
{
System.out.println(" Congragtulations, you win!");
System.out.println(" press 5 to play again ");
b = scan.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 PairOfDice



public 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 die
public 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;
else
numFaces = 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 dice game of PIG
Do you know the rules ? (1 for yes or 2 for no)
1
ok, then lets begin
---------------------------------------------------------------------------------------------------------
press 1 to roll the dice!
1
the first dice is a 1
the second dice is a 3
your two dice combined rolled a 4
you do not get any points for that roll, becuase a 1 was present
your total is still 0
the dice are automatically turned over to the computer
-------------------------------------------------------------------------
the computer is now rolling.....
the computers first dice is a 5
the computers first dice is a 5
the computers combined roll is a 10
the computers total is 10
the computer has turned the dice over to you!
--------------------------------------------------------------------------
the computers first dice is a 5
the computers first dice is a 5
the computers combined roll is a 10
the computer does not recieve any points for that roll, because a 1 was present
the computers total is still 10
the dice are now yours.
--------------------------------------------------------------------------
the computers first dice is a 5
the computers first dice is a 5
the computers combined roll is a 10
the computer does not recieve any points for tht roll, because a 1 was present
Aren'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 roll, 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).
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Roll 2-Dice "Pig" Game Help King8654 AWT / Swing 7 04-07-2008 06:58 PM
Defective code for dice, help please? byron New To Java 10 04-01-2008 06:22 AM
Help debugging a dice game Windoze New To Java 7 11-22-2007 01:01 AM
Implementing "Game Over" in Minesweeper game based on Gridworld framework. JFlash New To Java 0 11-15-2007 11:02 PM
Debugging In NetBeans IDE JavaForums NetBeans 0 07-30-2007 11:13 PM


All times are GMT +2. The time now is 03:57 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org