1: 2: package tictactoe; 3: 4: abstract public class Player extends Thread { 5: 6: public Player(Game game, int id) { 7: this.game = game; 8: this.id = id; 9: turn = null; 10: next = null; 11: } 12: 13: public int getId() { 14: return id; 15: } 16: 17: public synchronized void setNext(Player p){ 18: next = p; 19: } 20: 21: public synchronized void turn() { 22: turn = this; 23: game.turn = this; 24: notifyAll(); 25: } 26: 27: abstract public Move makeMove(); 28: 29: public void selectCell(int x, int y) {} 30: 31: public synchronized void run() { 32: while (!game.isOver()) { 33: while (turn != this) { 34: try { 35: // wait until it is your turn 36: wait(); 37: // wake up from turn method 38: } 39: catch (InterruptedException ex) { return; } 40: } 41: if (game.isOver()) 42: break; 43: game.displayMessage("Player " + id + "'s turn"); 44: while (true) { 45: Move move = makeMove(); 46: if (game.makeMove(move)) { 47: break; 48: } else { 49: game.displayMessage("Illegal move!"); 50: } 51: } 52: turn = null; 53: next.turn(); 54: } 55: } 56: 57: protected Player next; // the other player 58: protected Player turn; // go if turn==this 59: 60: protected Game game; 61: protected int id; 62: 63: }