// mrAppl.java // Raja Sooriamurthi import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class mrAppl extends Applet { // ************************* // Score panel // ************************* Panel pScore; Label lYou, lYouScore, lComp, lCompScore; // ************************* // Message panel // ************************* Panel pMesg; Label lPred; Label lMesg; // ************************* // Button Panel // ************************* Panel pButton; Button btnHead, btnTail; // ************************* // Dialog panel // ************************* Panel pDialog; MindReader mr; char guess, prediction; int right = 0, wrong = 0; // ************************* // init // ************************* public void init () { mr = new MindReader (); // setup the Applet setLayout (new GridLayout (3,1)); setSize(500,300); setBackground(Color.pink); setFont (new Font ("serif", Font.BOLD, 20)); // ************************* // Score panel // ************************* pScore = new Panel(); ((FlowLayout)pScore.getLayout()).setVgap(30); // pScore.setLayout (new GridLayout(1,4)); pScore.setBackground(Color.red); lYou = new Label ("You:"); lYouScore = new Label ("0"); lComp = new Label ("Computer:"); lCompScore = new Label ("0"); pScore.add (lYou); pScore.add (lYouScore); pScore.add (lComp); pScore.add (lCompScore); add(pScore); // ************************* // Message panel // ************************* pMesg = new Panel(); pMesg.setBackground(Color.green); pMesg.setLayout (new GridLayout(2,1)); lPred = new Label (" ? ", Label.CENTER); lMesg = new Label("I've made my prediction", Label.CENTER); pMesg.add(lPred); pMesg.add(lMesg); add(pMesg); // ************************* // Button Panel // ************************* pButton = new Panel(); pButton.setLayout(new FlowLayout()); pButton.setBackground(Color.blue); btnHead = new Button ("Head"); btnTail = new Button ("Tail"); pButton.add (btnHead); pButton.add (btnTail); ((FlowLayout)pButton.getLayout()).setHgap(60); ((FlowLayout)pButton.getLayout()).setVgap(30); add(pButton); btnHead.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { System.out.println ("Head"); foobar ('h'); }} ); btnTail.addActionListener ( new ActionListener() { public void actionPerformed (ActionEvent e) { System.out.println ("Tail"); foobar ('t'); }} ); // ************************* // Dialog panel // ************************* pDialog = new Panel(); play(); } public void play () { prediction = mr.getPrediction(); System.out.println ("My prediction: " + prediction); lPred.setText(" ? "); lMesg.setText("I've made my prediction"); // repaint(); } void foobar (char guess) { lPred.setText(prediction == 'h' ? "Head" : "Tail"); if (guess == prediction) { System.out.println ("Yes!. I too predicted " + prediction); right++; lMesg.setText("I'm right!"); lCompScore.setText (""+right); } else { System.out.println ("No. I predicted " + prediction); wrong++; lMesg.setText("Missed."); lYouScore.setText (""+wrong); } mr.storePlayerGuess (guess); System.out.println ("Score = " + right + " | " + wrong); repaint(); try { Thread.sleep (1000); } catch (InterruptedException e) {} play(); } }