Checkers game

  1. /*
  2.  * Player.java
  3.  *
  4.  * Created on 26. november 2004, 10:10
  5.  */
  6.  
  7. package checkers;
  8.  
  9. /**
  10.  * The design to implement the player was inspired from the <U>Object-Oriented Software Development Using java</U> book in chapter 11.3. Two player objects implements an abstract method play() from the Player class. Both of them extend the Thread Java Foundation Class. Consequently, the player that has not the turn wait() until he is notify() by it opponent. This “passing the baton” technique prevents both threads to access the critical section (the board) at the same time. Mutual exclusion is then ensured. When a player object is constructed, his name and his colour are passed in argument to the constructor.
  11.  * @author k380h11
  12.  */
  13. abstract public class Player extends Thread{
  14.    
  15.     Player turn;
  16.     protected Player next;
  17.     protected String id;
  18.     protected boolean color;
  19.    
  20.     /**
  21.      * Creates a new instance of Player
  22.      * @param id name of the player
  23.      * @param color color of the player
  24.      */
  25.     public Player(String id, boolean color) {
  26.         super();
  27.         this.id = id;
  28.         this.color = color;
  29.         start();
  30.     }
  31.         /**
  32.          * This method is an abstract form with is used by Jess and Human
  33.          * to tell where to move their pieces.
  34.          */    
  35.  
  36.     abstract public void play();
  37.    
  38.     /**
  39.      * run the new thread
  40.      */    
  41.     public synchronized void run() {
  42.         while (!Checker.getInstance().isOver()) {
  43.             while (turn !=  this) {
  44.                 try {
  45.                     wait();
  46.                 } catch (InterruptedException e) { return ;}
  47.             }
  48.             Checker.getInstance().displayMessage("Player (" + (color?"white":"red")+ ") "+ id + " `s turn");
  49.             if (Board.getInstance().count(!color) == 0)
  50.                 {Checker.getInstance().win(color);}
  51.             else if (Board.getInstance().count(!color) == 0)
  52.                 {Checker.getInstance().win(color);}
  53.             play();
  54.             turn = null;
  55.         }
  56.     }
  57.         /**
  58.          * This method is used to synchronized the players so that only one player gets the next turn.
  59.          * @param p next player
  60.          */
  61.  
  62.     public synchronized void setNext(Player p) {
  63.         next = p;
  64.     }
  65.         /**
  66.          * This method is used to synchronized the hasturn so that only one player can move a piece.
  67.          */
  68.     public synchronized void hasTurn(){
  69.         turn = this;
  70.         Checker.getInstance().turn = color;
  71.         turn.notify();
  72.     }
  73.         /**
  74.          * This method is used to synchronized the getturn so that only get the turn and has the ablity to move a piece.
  75.          * @return the player who's got the turn
  76.          */
  77.     public synchronized Player getTurn(){
  78.         return turn;
  79.     }
  80.         /**
  81.          * This method is used to convert the player informaation into a string.
  82.          * @return a string containing the information of the player
  83.          */
  84.     public String toString() {
  85.         return turn.id + " " + (turn.color ? "white" : "red");
  86.     }
  87. }
  88.  

contact - link to this site