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; // ************************* // Dialog panel // ************************* Panel pDialog; // ************************* // Query Panel // ************************* Panel pQuery Label lMesg2; Button bYes, bNo; // ************************* // Message panel // ************************* Panel pMesg; Label lPred; Label lMesg; // ************************* // Button Panel // ************************* Panel pButton; Button btnHead, btnTail; MindReader m; char guess, prediction; int right = 0, wrong = 0; // ************************* // init // ************************* public void init () { m = new MindReader (); // setup the Applet setLayout (new GridLayout (3,1)); setSize(300,500); setBackground(Color.pink); setFont (new Font ("serif", Font.BOLD, 20); // ************************* // Score panel // ************************* pScore = new Panel(); // 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); lPred = new Label (" ? "); lMesg = new Label("I've made my prediction", Label.CENTER); pMesg.add(lPred); pMesg.add(lMesg); add(pMesg); // ************************* // Button Panel // ************************* pButton = new Panel(); // pButton.setBackground(Color.blue); btnHead = new Button ("Head"); btnTail = new Button ("Tail"); pButton.add (btnHead); pButton.add (btnTail); ((FlowLayout)pButton.getLayout()).setHgap(60); 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'); }} ); // ************************* // Query Panel // ************************* pQuery = new Panel(); // ************************* // Dialog panel // ************************* pDialog = new Panel(); p.Dialog.setLayout (new CardLayout()); pDialog.add ("one", pMesg); pDialog.add ("two", pQuery); play(); } // ************************* // play // ************************* public void play () { prediction = m.getPrediction(); System.out.println ("My prediction: " + prediction); lPred.setText(" ? "); msgLabel.setText("I've made my prediction"); } void foobar (char guess) { lPred.setText(prediction == 'h' ? "Head" : "Tail"); if (guess == prediction) { System.out.println ("Yes!. I too predicted " + prediction); right++; msgLabel.setText("I'm right!"); lCompScore.setText (""+right); Dialog dl = new Dialog((Frame) getParent()); // dl.show(); } else { System.out.println ("No. I predicted " + prediction); wrong++; msgLabel.setText("Missed."); lYouScore.setText (""+wrong); } m.storePlayerGuess (guess); sb.shift (guess); System.out.println ("Score = " + right + " | " + wrong); try { Thread.sleep (1000); } catch (InterruptedException e) {} play(); } }