/******************************************************** * Filename : Game.java * Version : Beta V1.0 * Author : Roger Webster, Ph.D. * This class is what you fill in to write the game. **********************************************************/ import java.awt.*; import java.net.*; class Game extends Frame implements Runnable { private clientwindow cw; private Thread thread; private String nextMove = "test"; private String opponent_move; private String move; private boolean you_go_first; private boolean move_made = false; private URL base_url; // constructor Game (URL base_url, clientwindow parent, boolean you_go_first) { super("XXX Game Window"); this.base_url = base_url; this.cw = parent; this.you_go_first = you_go_first; this.start(); } // gets the thread going public void start() { System.out.println("starting thread"); thread = new Thread(this,"Game"); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } public void run() { System.out.println("RUNNING thread"); while (true) { try{thread.sleep(20);} // yield instead ? catch(InterruptedException e){} if (you_go_first) { // more code here probably // when client makes move then cw.sendMove(nextMove); // sends move to other player // wait for opponent's move move = this.get_opponent_move(); // process the move } else { // wait for opponent's move move = this.get_opponent_move(); // process the move // when client makes move then cw.sendMove(nextMove); // sends move to other player // more code here probably } } // loop until the game is over } // called by clientwindow public synchronized void set_opponent_move(String nextMove) { move_made = true; System.out.println("setting opponent's next move is " + nextMove); opponent_move = nextMove; notify(); } public synchronized String get_opponent_move() { if (!move_made) { try { wait(); } catch (Exception e) { System.out.println("Exception in wait " + e.toString()); } } System.out.println("get opponent's next move " + opponent_move); move_made = false; return (opponent_move); } } // end Game class