|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
RockPaperScissor program troubles w/ threads
Ok, I'm working on a programming project, we basicaly need to make a program to simulate two players playing rock paper scissor, with each player being a thread. I belive I'm having trouble with the synchronization (one player is racing ahead of the other). So here is my code, and then the output that I get.
package rockscissorpaper; /** * * @author Alex */ import java.util.*; //includes random() public class RockScissorPaper { /** * @param args the command line arguments */ public static void main(String[] args) { ScoreCard scorecard; RoundCompare roundcompare; Player player1, player2; int totalrounds = 10; scorecard = new ScoreCard(); roundcompare = new RoundCompare(scorecard); player1 = new Player(1, totalrounds, roundcompare); player2 = new Player(2, totalrounds, roundcompare); player1.start(); player2.start(); try{ player1.join(); } catch(InterruptedException e) {} try{ player2.join(); } catch(InterruptedException e) {} scorecard.finalscore(); } }//end Class RockScissorPaper class Player extends Thread { int id, choice, totalrounds; RoundCompare roundcompare; Random generator; public Player(int id, int totalrounds, RoundCompare roundcompare) { this.roundcompare = roundcompare; this.id = id; this.totalrounds = totalrounds; generator = new Random(); } private void makechoice() { this.choice = generator.nextInt(3) + 1; } private void tellusyourchoice() { System.out.println("Player " + this.id + " chose " + choice); } public void run() { for(int i = 1; i <= totalrounds; i++) { makechoice(); tellusyourchoice(); roundcompare.submitchoice(this.id, this.choice); } } } //end class Player class RoundCompare { volatile int p1choice, p2choice; volatile boolean p1gavechoice, p2gavechoice; int whoissleeping; //for debugging purposes... ScoreCard scorecard; public RoundCompare(ScoreCard scorecard) { p1choice = 0; p2choice = 0; p1gavechoice = false; p2gavechoice = false; whoissleeping = 0; this.scorecard = scorecard; } synchronized public void submitchoice(int id, int choice) { switch(id) { case 1: this.p1choice = choice; p1gavechoice = true; case 2: this.p2choice = choice; p2gavechoice = true; } if(p1gavechoice && p2gavechoice) { compare(id); resetvalues(); notifyAll(); } else { System.out.println("Player " + id + " is going to sleep."); //debugging try { wait(); } catch(InterruptedException e) {} System.out.println("Player woke up."); //debugging } } synchronized private void compare(int id) { switch(this.p1choice - this.p2choice){ case -2: scorecard.winner(2, this.p2choice); case -1: scorecard.winner(1, this.p1choice); case 0: scorecard.winner(0, 0); case 1: scorecard.winner(2, this.p2choice); case 2: scorecard.winner(1, this.p1choice); } } synchronized private void resetvalues() { p1choice = 0; p2choice = 0; p1gavechoice = false; p2gavechoice = false; whoissleeping = 0; } } //end class RoundCompare class ScoreCard { private int p1wins, p2wins, rockwins, scissorwins, paperwins, draws; public ScoreCard() { p1wins = 0; p2wins = 0; rockwins = 0; scissorwins = 0; paperwins = 0; draws = 0; } public void winner(int id, int choice) { switch(id) { case 0: draws++; case 1: p1wins++; case 2: p2wins++; } switch(choice) { case 1: rockwins++; case 2: scissorwins++; case 3: paperwins++; } } public void finalscore() { System.out.println("Summary Statistics"); System.out.println("Number of Draws:" + draws); System.out.println("Number of Player 1 wins:" + p1wins); System.out.println("Number of Player 2 wins:" + p2wins); System.out.println("Number of Rock wins:" + rockwins); System.out.println("Number of Scissor wins:" + scissorwins); System.out.println("Number of Paper wins:" + paperwins); } } //end class ScoreCard //*********************************************// Here is the output init: deps-jar: compile: run: Player 1 chose 3 Player 1 chose 2 Player 1 chose 1 Player 1 chose 1 Player 1 chose 1 Player 1 chose 1 Player 1 chose 1 Player 1 chose 3 Player 1 chose 3 Player 1 chose 1 Player 2 chose 3 Player 2 is going to sleep. |
|
#2
|
|||
|
|||
|
Here, I made it simpler for you.
You forgot a bunch of 'breaks' on the switch statements. I also made some minor changes: Code:
class ScoreCard {
private int p1wins, p2wins, rockwins, scissorwins, paperwins, draws;
public ScoreCard() {
p1wins = 0;
p2wins = 0;
rockwins = 0;
scissorwins = 0;
paperwins = 0;
draws = 0;
}
public void winner(int id, int choice) {
switch(id) {
case 0: draws++; break;
case 1: p1wins++; break;
case 2: p2wins++; break;
}
switch(choice) {
case 1: rockwins++; break;
case 2: scissorwins++; break;
case 3: paperwins++; break;
}
}
public void finalscore() {
System.out.println("Summary Statistics");
System.out.println("Number of Draws:" + draws);
System.out.println("Number of Player 1 wins:" + p1wins);
System.out.println("Number of Player 2 wins:" + p2wins);
System.out.println("Number of Rock wins:" + rockwins);
System.out.println("Number of Scissor wins:" + scissorwins);
System.out.println("Number of Paper wins:" + paperwins);
}
} //end class ScoreCard
Code:
class RoundCompare {
volatile int p1choice, p2choice;
volatile boolean p1gavechoice, p2gavechoice;
int whoissleeping; //for debugging purposes...
ScoreCard scorecard;
public RoundCompare(ScoreCard scorecard) {
p1choice = 0;
p2choice = 0;
p1gavechoice = false;
p2gavechoice = false;
whoissleeping = 0;
this.scorecard = scorecard;
}
synchronized public void submitchoice(int id, int choice) {
switch(id) {
/*
* Comments:
* Added break to your switch cases
*/
case 1: this.p1choice = choice; p1gavechoice = true; break;
case 2: this.p2choice = choice; p2gavechoice = true; break;
}
if(p1gavechoice && p2gavechoice) {
compare(id);
resetvalues();
notifyAll();
}
else {
System.out.println("Player " + id + " is going to sleep."); //debugging
try {
wait();
}
catch(Exception e) {}
System.out.println("Player woke up."); //debugging
}
}
synchronized private void compare(int id) {
switch(this.p1choice - this.p2choice){
case -2: scorecard.winner(2, this.p2choice); break;
case -1: scorecard.winner(1, this.p1choice); break;
case 0: scorecard.winner(0, 0); break;
case 1: scorecard.winner(2, this.p2choice); break;
case 2: scorecard.winner(1, this.p1choice); break;
}
}
synchronized private void resetvalues() {
p1choice = 0;
p2choice = 0;
p1gavechoice = false;
p2gavechoice = false;
whoissleeping = 0;
}
} //end class RoundCompare
Code:
import java.util.Random;
class Player extends Thread {
int id, choice, totalrounds;
RoundCompare roundcompare;
Random generator;
public Player(int id, int totalrounds, RoundCompare roundcompare) {
this.roundcompare = roundcompare;
this.id = id;
this.totalrounds = totalrounds;
generator = new Random();
}
private void makechoice() {
this.choice = generator.nextInt(3) + 1;
}
private void tellusyourchoice() {
System.out.println("Player " + this.id + " chose " + choice);
}
public void run() {
for(int i = 1; i <= totalrounds; i++) {
makechoice();
tellusyourchoice();
roundcompare.submitchoice(this.id, this.choice);
}
}
} //end class Player
Code:
/**
*
* @author Alex
*/
import java.util.*; //includes random()
public class RockScissorPaper {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ScoreCard scorecard;
RoundCompare roundcompare;
Player player1, player2;
int totalrounds = 10;
scorecard = new ScoreCard();
roundcompare = new RoundCompare(scorecard);
player1 = new Player(1, totalrounds, roundcompare);
player2 = new Player(2, totalrounds, roundcompare);
player1.start();
player2.start();
try{
player1.join();
}
catch(InterruptedException e) {}
try{
player2.join();
}
catch(InterruptedException e) {}
scorecard.finalscore();
}
}//end Class RockScissorPaper
Still not perfect, but I think you should be able to fix any other bugs Last edited by daniel_g : April 19th, 2007 at 06:59 PM. |
|
#3
|
|||
|
|||
|
Well thank you very much! I'll have to remember the /code code trick you used to make the displaying of the code much neater. On an even bigger note thank you for taking the time to look over and look for mistakes in my code, I can't belive I forgot the breaks for the switches. You really saved me here, I only had a week left to finishes this project and I just couldn't figure out what was going wrong!
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > RockPaperScissor program troubles w/ threads |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|