/******************************************************** * 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.*; import java.applet.*; 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 boolean sent_my_move = false; private URL base_url; private TextField opponentfield; private TextField textField; private Label userlabel; private Label userlabel2; private Label userlabel3; private int framewidth = 400; private int frameheight = 300; Applet myapplet; GraphicPanel gp; AudioClip mysound; Image Gameimage; // constructor Game (Applet myapplet, URL base_url, clientwindow parent, boolean you_go_first, Image gameimage) { super("Test Game Window"); this.base_url = base_url; this.cw = parent; this.you_go_first = you_go_first; this.Gameimage = gameimage; mysound = myapplet.getAudioClip(base_url, "intelmmx.wav"); 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() { mysound.play(); System.out.println("RUNNING thread"); if (!you_go_first) { sent_my_move = true; } makewindow(); while (true) { try{thread.sleep(20);} catch(InterruptedException e){} // wait for opponent's move if (sent_my_move) { move = this.get_opponent_move(); opponentfield.setText(move); // process the move } } // loop until the game is over } // called by clientwindow Don't change this!!! public synchronized void set_opponent_move(String nextMove) { move_made = true; System.out.println("setting opponent's next move is " + nextMove); opponent_move = nextMove; notifyAll(); } // Don't change this!!! 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; sent_my_move = false; return (opponent_move); } public void send_move (String nextMove) { if (!sent_my_move) { System.out.println("waiting for send my move its false\n"); } cw.sendMove(nextMove); // sends move to other player sent_my_move = true; System.out.println("sent move " + nextMove); } // You re-write all this... public void makewindow() { this.setBackground(Color.black); this.setForeground(Color.black); this.setLayout(new FlowLayout()); gp = new GraphicPanel(Gameimage); gp.setSize(300,100); this.add(gp); userlabel3= new Label(); userlabel3.setText("Type in red box & hit c/r to send move to Opponent"); userlabel3.setFont(new Font("Helvetica",Font.PLAIN,16)); userlabel3.setForeground(Color.white); userlabel3.setBackground(Color.black); this.add(userlabel3); textField=new TextField(41); textField.setFont(new Font("Helvetica",Font.PLAIN,16)); textField.setBackground(Color.red); textField.setForeground(Color.white); this.add(textField); userlabel2= new Label(); userlabel2.setText("This is Opponent's move via socket"); userlabel2.setFont(new Font("Helvetica",Font.PLAIN,16)); userlabel2.setForeground(Color.white); userlabel2.setBackground(Color.black); this.add(userlabel2); opponentfield = new TextField(41); opponentfield.setFont(new Font("Helvetica",Font.PLAIN,14)); opponentfield.setBackground(Color.white); opponentfield.setForeground(Color.black); opponentfield.setEditable(false); this.add(opponentfield); this.add(new Button("QUIT")); this.setSize(framewidth, frameheight); this.show(); } public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { String label = (String)arg; if (label.equals("QUIT")) { cw.quitWindow(); this.dispose(); return true; } } if (evt.target == textField) { nextMove = textField.getText(); send_move(nextMove); return true; } return false; } } // end Game class