Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 18th, 2007, 10:24 PM
RainHawk RainHawk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 2 RainHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 57 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old April 19th, 2007, 06:03 PM
daniel_g daniel_g is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 60 daniel_g User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 3 m 50 sec
Reputation Power: 3
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.

Reply With Quote
  #3  
Old April 19th, 2007, 09:11 PM
RainHawk RainHawk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 2 RainHawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 57 sec
Reputation Power: 0
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!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > RockPaperScissor program troubles w/ threads


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT