// // Tim Hutcheson // COP 4331 Mon. & Wed. 3:30 // Stud. ID. 999-99-9999 // Homework #4 // Main.java // --------- import java.applet.*; import java.util.*; import java.io.*; import java.awt.event.*; import java.awt.Color; import java.awt.Button; import java.awt.CardLayout; import java.awt.BorderLayout; import java.awt.TextField; import java.awt.Font; import java.awt.Panel; // Carnac encapsulates a MindReader instance known as "Carnac: the Magnificent", // (from the old Johhny Carson show), who possess the ability to read minds. public class Main extends Applet { // --------------------- Applet Implementation ---------------------- // // These objects need class scope for the ActionListeners... MindReader carnac; Button headsButton, tailsButton, yesButton, noButton; TextField t1, t2, t3; Panel cPanel, bPanel; CardLayout cLayout, bLayout; boolean thinking; // The FlashCard class creates red on yellow card objects with uniform font... class FlashCard extends TextField { FlashCard( String text ) { setText( text ); setFont( new Font( "Times New Roman", Font.PLAIN, 20 ) ); setBackground(Color.yellow); setForeground(Color.red); } } public void init() { carnac = new MindReader( 4 ); setBackground (Color.lightGray); setForeground (Color.red); // Setup North Panel, it contains the scoreboard... t2 = new TextField( 18 ); t2.setText( " My Score / Your Score" ); Panel nPanel = new Panel(); nPanel.add( t2 ); // Setup South Panel, it has buttons to pick heads or tails and shows Carnac's pick... headsButton = new Button( "heads" ); headsButton.addActionListener(new PlayListener("heads")); tailsButton = new Button( " tails " ); tailsButton.addActionListener(new PlayListener("tails")); t1 = new TextField( 15 ); t1.setText( " <--Make your pick-->" ); Panel sPanel = new Panel(); sPanel.add(headsButton); sPanel.add(t1); sPanel.add(tailsButton); // Setup Replay Panel, it overlays the South Panel at the end of game so // the player can select yes or no... yesButton = new Button( " yes" ); yesButton.addActionListener( new YesListener() ); noButton = new Button( " no " ); noButton.addActionListener( new NoListener() ); t3 = new TextField( 15 ); t3.setText( " <--Make your pick-->" ); Panel rPanel = new Panel(); rPanel.add(yesButton); rPanel.add(t3); rPanel.add(noButton); // Setup Center Card Panel, it contains textfield cards to flash at various times... cLayout = new CardLayout(20,20); cPanel = new Panel(); cPanel.setLayout( cLayout ); cPanel.add("Start",new FlashCard("\n\n\n Carnac will guess your moves."+ "\n\n Pick heads or tails." + "\n\n Match play rules, best of 25 wins!"+ "\n\n Would you like to play?" ) ); cPanel.add("Heads",new FlashCard("\n\n\n\n\n\n I predicted Heads!")); cPanel.add("Tails",new FlashCard("\n\n\n\n\n\n I predicted Tails!")); cPanel.add("Think",new FlashCard("\n\n\n\n\n\n Thinking!")); cPanel.add("Pick", new FlashCard("\n\n\n\n OK!"+ "\n\n I have my choice"+ "\n\n Pick heads or tails.")); cPanel.add("I Win",new FlashCard("\n\n\n\n I Win!" + "\n\n\n Want to play again?")); cPanel.add("You Win",new FlashCard("\n\n\n\n You Win!"+ "\n\n\n Want to play again?")); cPanel.add("End",new FlashCard( "\n\n\n\n Thank you for playing!"+ "\n\n\n Want to play again?")); // Setup Bottom Card Panel, this allows swapping the play and replay panels... bLayout = new CardLayout(); bPanel = new Panel(); bPanel.setLayout( bLayout ); bPanel.add( "Play", sPanel ); bPanel.add( "Replay", rPanel ); // Now attach all the panels to the layout... setLayout( new BorderLayout() ); add( "North", nPanel ); add( "Center", cPanel ); add( "South", bPanel ); cLayout.show( cPanel, "Start" ); bLayout.show( bPanel, "Replay" ); } // Simulate the act of thinking by putting up the "Thinking" card up for // some msecs and then replacing it with the "Pick" card. Implemented // as a seperate Thread to get the cards to swap correctly // in my Visual J++ environment at home. class Thinker extends Thread { String picked; Thinker( String firstCard ) { thinking = true; picked = firstCard; start(); } public void run() { // Show results of last prediction, if any... if( picked != null ) { cLayout.show( cPanel, picked ); try { sleep( 500 ); } catch( InterruptedException ie ) {} } // Think a bit and then direct player to pick his selection... cLayout.show( cPanel, "Think" ); try { sleep( 1000 ); } catch( InterruptedException ie ) {} cLayout.show( cPanel, "Pick" ); // Re-enable all the buttons by signalling end of Thinker... thinking = false; } } // The PlayHandler class is the interface to the original MindReader implementation. // It handles the play, display of scores and the simulation of the "thinking" aspect // of the applet in response to the button clicks of the player. class PlayListener implements ActionListener { String playersPick; public PlayListener( String pick ) { playersPick = pick; } public void actionPerformed (ActionEvent ae) { // Ignore player actions while thinking... if( !thinking ) { // Get a prediction and highlight the appropriate button... char prediction = carnac.play( playersPick ); // Update the scores... int playerScore = carnac.playerScore(), Score = carnac.Score(); t1.setText( " I predicted " + ((prediction=='h') ? "heads" : "tails") ); t2.setText( " " + carnac ); headsButton.setForeground( prediction == 'h' ? Color.red : Color.black ); tailsButton.setForeground( prediction == 't' ? Color.red : Color.black ); // Check for the end of a game and select the appropriate winner card. if( Math.abs(Score - playerScore) > 25 - (Score + playerScore) ) { cLayout.show( cPanel, Score > playerScore ? "I Win" : "You Win" ); bLayout.show( bPanel, "Replay" ); } else // Pretend to "think" a bit and then show cards... new Thinker( prediction == 'h' ? "Heads" : "Tails" ); } } } // If button was a yes or no button, must be at end of game. Swap in the // appropriate cards to allow replay of game. class YesListener implements ActionListener { public void actionPerformed (ActionEvent ae) { // Ignore player actions while thinking... if( !thinking ) { carnac.reset(); t2.setText( " " + carnac ); bLayout.show( bPanel, "Play" ); t1.setText( " <--Make your pick-->" ); // Start up a Thinker object to flip the cards and begin the game... new Thinker( null ); } } } // If player says no, just wait for him/her to say yes... class NoListener implements ActionListener { public void actionPerformed (ActionEvent ae) { // Ignore player actions while thinking... if( !thinking ) { // Leave the player with the option to start at anytime... cLayout.show( cPanel, "Start" ); } } } // ------------------- End Applet Implementation -------------------- // // ------------------ Command Line Implementation ------------------- // public Main( int size ) { carnac = new MindReader( size ); } // cmdLinePlay() handles cmdline play using the guess and predicition // variables to separate interface from implementation. boolean cmdLinePlay() { String guess; char prediction; String yes = "Yes, ", no = "No, "; String item, cmd = "What is your guess [h/t] ?"; try { System.out.println( "\nGuess head or tails and i'll predict your guess." ); System.out.write(cmd.getBytes()); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); String cmdline = br.readLine(); StringTokenizer st = new StringTokenizer( cmdline, " ," ); while( st.hasMoreTokens() ) { guess = st.nextToken(); if( guess.equals("h") || guess.equals("t") ) { prediction = carnac.play( guess ); if( guess.charAt(0) == prediction ) System.out.write( yes.getBytes() ); else System.out.write( no.getBytes() ); System.out.println( "I predicted " + ((prediction=='h') ? "heads" : "tails") ); System.out.println( "Score = " + carnac.playerScore() + " | " + carnac.Score() ); } else if( guess.equals("quit") || guess.equals("q") ) { if( st.hasMoreTokens() ) System.out.println( " Extra characters in command line." ); return false; } else System.out.println( " Did not understand request: "+guess+"." ); } } // Catch possible exceptions and ignore them. catch( IOException e ) { return false; } return true; } public static void main (String[] args) { Main myCarnac = new Main(4); System.out.println( " Welcome to MindReader" ); while( myCarnac.cmdLinePlay() ); } // ---------------- End Command Line Implementation ----------------- // }