/** * aiPlayer.java */ package boondock.holdem.Ai; import boondock.holdem.Gui.guiCard; import boondock.holdem.Sound.waveObj; import boondock.holdem.Player.OtherPlayer; import boondock.holdem.Network.*; import boondock.holdem.Cards.*; import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import java.util.*; import java.util.StringTokenizer; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.*; /** * The main AI of the game. This is the AI that the user interacts with. * * @author Jonathan O'Keefe * @author Scott Semonian * @author Matt Brinza * @author Hamid R. Tahsildoost * * @author The Boondock Saints * @author No Limit Texas Holdem */ public class aiPlayer extends JFrame implements ActionListener, WindowListener { public static final String DEFAULT_MYSQL_PATH = "jdbc:mysql://localhost/handvalues?user=boondock&password=saints"; public static String mySQLPath = ""; public final String SPACEBUFFER = " "; public JSplitPane splitPane, splitPane2; //the text panes are in splitPane, and then splitPane is put into splitPane2 with the board pane private JTextArea messageArea;//textbox where the text from everyone will be displayed private JTextField inputArea;//textbox where the text from everyone will be displayed private JButton sendButton;//button to send the text from users inputArea private static String userName; // userName to add to outgoing messages // MessageManager for communicating with server private MessageManager messageManager; // MessageListener for receiving incoming messages public MessageListener messageListener; static JFrame frame; static String inputAddress = "localhost"; static Graphics g; int currentPlayer = 0, soundID = 0; boolean isMyTurn = false; boolean notifyMe = false; boolean doingBlinds; boolean execMe = false; static boolean displayedSQLError = false; //BUTTONS/LABELS for 'special effects' ImageIcon foldDownIM = new ImageIcon("images/jpegS/Fold_Pressed.jpg"); ImageIcon foldOverIM = new ImageIcon("images/jpegS/Fold_Hover.jpg"); ImageIcon foldUpIM = new ImageIcon("images/jpegS/Fold_Normal.jpg"); JLabel foldLabel = new JLabel(foldUpIM,JLabel.CENTER); ImageIcon raiseDownIM = new ImageIcon("images/jpegS/Raise_Pressed.jpg"); ImageIcon raiseOverIM = new ImageIcon("images/jpegS/Raise_Hover.jpg"); ImageIcon raiseUpIM = new ImageIcon("images/jpegS/Raise_Normal.jpg"); JLabel raiseLabel = new JLabel(raiseUpIM,JLabel.CENTER); ImageIcon allinDownIM = new ImageIcon("images/jpegS/AllIn_Pressed.jpg"); ImageIcon allinOverIM = new ImageIcon("images/jpegS/AllIn_Hover.jpg"); ImageIcon allinUpIM = new ImageIcon("images/jpegS/AllIn_Normal.jpg"); JLabel allinLabel = new JLabel(allinUpIM,JLabel.CENTER); ImageIcon callDownIM = new ImageIcon("images/jpegS/Call_Pressed.jpg"); ImageIcon callOverIM = new ImageIcon("images/jpegS/Call_Hover.jpg"); ImageIcon callUpIM = new ImageIcon("images/jpegS/Call_Normal.jpg"); JLabel callLabel = new JLabel(callUpIM,JLabel.CENTER); ImageIcon checkDownIM = new ImageIcon("images/jpegS/Check_Pressed.jpg"); ImageIcon checkOverIM = new ImageIcon("images/jpegS/Check_Hover.jpg"); ImageIcon checkUpIM = new ImageIcon("images/jpegS/Check_Normal.jpg"); JLabel checkLabel = new JLabel(checkUpIM,JLabel.CENTER); private JButton refreshButton, exitButton, startGameButton; private JTextField amountAlreadyBet_Field, potSize_Field, amountNeededToCall_Field, raiseAmount_Field, myChipAmount_Field; private JLabel amountInLabel, potSizeLabel, amountCallLabel, amountRaiseLabel, myChipAmountLabel; private JLabel P1info = new JLabel(SPACEBUFFER + "Empty Seat (0)" + SPACEBUFFER), P2info = new JLabel("Empty Seat (0)" + SPACEBUFFER), P3info = new JLabel("Empty Seat (0)" + SPACEBUFFER), P4info = new JLabel("Empty Seat (0)"), P5info = new JLabel("Empty Seat (0)" + SPACEBUFFER), P6info = new JLabel("Empty Seat (0)" + SPACEBUFFER), P7info = new JLabel(SPACEBUFFER + "Empty Seat (0)" + SPACEBUFFER), P8info = new JLabel("Empty Seat (0)"); private String amountAlreadyBet = "0", potSize = "0", amountNeededToCall = "0", raiseAmount = "0", myChipAmount = "0"; private static OtherPlayer allPlayers[] = new OtherPlayer[8]; private static boolean showFlop = false, showTurn = false, showRiver = false; private static String communityCards[] = new String[5]; private static int dealerButton = 0; public void windowOpened(WindowEvent e){} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowGainedFocus(WindowEvent e) {} public void windowLostFocus(WindowEvent e) {} public void windowStateChanged(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowClosing(WindowEvent e) { messageManager.disconnect( messageListener ); System.exit( 0 ); } /** * Constructor for the GUI that the user interacts with. * * @param manager The MessageManager that is assigned to handle sending/recieving messages to/from the GUI */ public aiPlayer(MessageManager manager) { frame.addWindowListener(this); // set the MessageManager messageManager = manager; // create MyMessageListener for receiving messages messageListener = new MyMessageListener(); //setup the basics of the textarea for the messagbox messageArea = new JTextArea(); messageArea.setEditable(false); messageArea.setWrapStyleWord(true); messageArea.setLineWrap(true); JScrollPane messageAreaScrollPane = new JScrollPane(messageArea); //setup the basics of the textarea for the sendbox inputArea = new JTextField(); inputArea.addActionListener( new ActionListener() { // send new message when user activates sendButton public void actionPerformed( ActionEvent event ) { messageManager.sendMessage( userName,inputArea.getText()); inputArea.setText(""); //clear inputArea } } ); //end of ActionListener setup for sendButton JScrollPane inputAreaScrollPane = new JScrollPane(inputArea); //Provide minimum sizes for the two components in the split pane. messageAreaScrollPane.setMinimumSize(new Dimension(800, 110)); messageAreaScrollPane.setPreferredSize(new Dimension(800, 110)); inputAreaScrollPane.setMinimumSize(new Dimension(700, 40)); //from 800 to 700 inputAreaScrollPane.setPreferredSize(new Dimension(800, 40)); //create a message panel for the sendbox/sendbutton JPanel inputAreaPanel = new JPanel(); inputAreaPanel.setLayout(new BoxLayout(inputAreaPanel, BoxLayout.X_AXIS)); inputAreaPanel.setMinimumSize(new Dimension(800, 40)); inputAreaPanel.setPreferredSize(new Dimension(800, 40)); inputAreaPanel.add(inputAreaScrollPane); inputAreaPanel.setBackground(Color.white); //add the 'send' button // create Icon for sendButton Icon sendIcon = new ImageIcon( "images/Send_Icon.jpg" ); sendButton = new JButton("Send", sendIcon); inputAreaPanel.add(sendButton); sendButton.addActionListener( new ActionListener() { // send new message when user activates sendButton public void actionPerformed( ActionEvent event ) { messageManager.sendMessage( userName,inputArea.getText()); inputArea.setText(""); //clear inputArea } } ); //end of ActionListener setup for sendButton //AutoDisconnect on CloseWindow addWindowListener ( new WindowAdapter () { // disconnect from server and exit application public void windowClosing ( WindowEvent event ) { messageManager.disconnect( messageListener ); System.exit( 0 ); } } ); //end of AutoDisconnect on CloseWindow code. //add the text components to the splitpane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, messageAreaScrollPane, inputAreaPanel); splitPane.setOneTouchExpandable(true); //Provide a preferred size for the split pane. splitPane.setPreferredSize(new Dimension(800, 150)); //create the panel which holds the stuff in the main game window JPanel Center_mainGamePanel = new JPanel(); Center_mainGamePanel.setLayout(new BoxLayout(Center_mainGamePanel, BoxLayout.Y_AXIS)); //setup player panels JPanel upperPlayers = new JPanel(); upperPlayers.setLayout(new BoxLayout(upperPlayers, BoxLayout.X_AXIS)); upperPlayers.setBackground(Color.white); upperPlayers.add(P1info); upperPlayers.add(P2info); upperPlayers.add(P3info); JPanel rightPlayer = new JPanel(); rightPlayer.setLayout(new BoxLayout(rightPlayer, BoxLayout.Y_AXIS)); rightPlayer.add(P4info); rightPlayer.setBackground(Color.white); JPanel lowerPlayers = new JPanel(); lowerPlayers.setLayout(new BoxLayout(lowerPlayers, BoxLayout.X_AXIS)); lowerPlayers.add(P7info); lowerPlayers.add(P6info); lowerPlayers.add(P5info); lowerPlayers.setBackground(Color.white); JPanel leftPlayer = new JPanel(); leftPlayer.setLayout(new BoxLayout(leftPlayer, BoxLayout.Y_AXIS)); leftPlayer.add(P8info); leftPlayer.setBackground(Color.white); //add background JLabel imageBackground = new JLabel(new ImageIcon("images/Game-Table.jpg")); Container content = getContentPane(); content.setLayout(new FlowLayout() ); content.setBackground(Color.white); content.add(imageBackground); Center_mainGamePanel.add(upperPlayers); Center_mainGamePanel.add(content); Center_mainGamePanel.add(lowerPlayers); Center_mainGamePanel.setBackground(Color.white); JPanel AllPlayers_mainGamePanel = new JPanel(); AllPlayers_mainGamePanel.setLayout(new BoxLayout(AllPlayers_mainGamePanel, BoxLayout.X_AXIS)); AllPlayers_mainGamePanel.add(leftPlayer); AllPlayers_mainGamePanel.add(Center_mainGamePanel); AllPlayers_mainGamePanel.add(rightPlayer); AllPlayers_mainGamePanel.setBackground(Color.white); JPanel mainGamePanel = new JPanel(); mainGamePanel.setLayout(new BoxLayout(mainGamePanel, BoxLayout.Y_AXIS)); mainGamePanel.setMinimumSize( new Dimension(800,200) ); mainGamePanel.add(AllPlayers_mainGamePanel); mainGamePanel.setBackground(Color.white); Icon refresh = new ImageIcon("images/Observe_Icon.jpg"); Icon exit = new ImageIcon("images/Exit_Icon.jpg"); Icon start = new ImageIcon("images/Start_Icon.jpg"); //setup the buttons refreshButton = new JButton("Refresh",refresh); refreshButton.addActionListener(this); exitButton = new JButton("Exit", exit); exitButton.addActionListener(this); startGameButton = new JButton("",start); startGameButton.addActionListener(this); //setup the text fields amountAlreadyBet_Field = new JTextField(10); amountAlreadyBet_Field.setEditable(false); potSize_Field = new JTextField(10); potSize_Field.setEditable(false); amountNeededToCall_Field = new JTextField(10); amountNeededToCall_Field.setEditable(false); raiseAmount_Field = new JTextField(10); raiseAmount_Field.setEditable(true); myChipAmount_Field = new JTextField(10); myChipAmount_Field.setEditable(false); //setup the labels amountInLabel = new JLabel("Bet So Far:"); potSizeLabel = new JLabel("Pot Size:"); amountCallLabel = new JLabel("Call Amount:"); amountRaiseLabel = new JLabel("Raise Amount:"); myChipAmountLabel = new JLabel("Your Chip Amount:"); //create minipanels JPanel foldPanel = new JPanel(); foldPanel.setBackground(Color.white); foldPanel.setLayout(new BoxLayout(foldPanel, BoxLayout.Y_AXIS)); foldPanel.add(amountInLabel); foldPanel.add(amountAlreadyBet_Field); JPanel customFold = new JPanel(); customFold.setBackground(Color.white); customFold.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent event) {foldLabel.setIcon(foldDownIM);} public void mouseReleased(MouseEvent event) {foldLabel.setIcon(foldUpIM);} public void mouseExited(MouseEvent event) {foldLabel.setIcon(foldUpIM);} public void mouseEntered(MouseEvent event) {foldLabel.setIcon(foldOverIM);} public void mouseClicked(MouseEvent event) { if(isMyTurn) { String sendCommand = "COMMAND:"; //fold sendCommand = sendCommand + "fold"; messageManager.sendMessage( userName,sendCommand); updateAllValues();//always update new values last } } } ); customFold.add(foldLabel); foldPanel.add(customFold); JPanel checkPanel = new JPanel(); checkPanel.setBackground(Color.white); checkPanel.setLayout(new BoxLayout(checkPanel, BoxLayout.Y_AXIS)); checkPanel.add(potSizeLabel); checkPanel.add(potSize_Field); JPanel customCheck = new JPanel(); customCheck.setBackground(Color.white); customCheck.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent event) {checkLabel.setIcon(checkDownIM);} public void mouseReleased(MouseEvent event) {checkLabel.setIcon(checkUpIM);} public void mouseExited(MouseEvent event) {checkLabel.setIcon(checkUpIM);} public void mouseEntered(MouseEvent event) {checkLabel.setIcon(checkOverIM);} public void mouseClicked(MouseEvent event) { if(isMyTurn) { String sendCommand = "COMMAND:"; //check if(amountNeededToCall.equals("0")) { sendCommand = sendCommand + "check"; messageManager.sendMessage( userName,sendCommand); updateAllValues();//always update new values last } } } } ); customCheck.add(checkLabel); checkPanel.add(customCheck); JPanel callPanel = new JPanel(); callPanel.setBackground(Color.white); callPanel.setLayout(new BoxLayout(callPanel, BoxLayout.Y_AXIS)); callPanel.add(amountCallLabel); callPanel.add(amountNeededToCall_Field); JPanel customCall = new JPanel(); customCall.setBackground(Color.white); customCall.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent event) {callLabel.setIcon(callDownIM);} public void mouseReleased(MouseEvent event) {callLabel.setIcon(callUpIM);} public void mouseExited(MouseEvent event) {callLabel.setIcon(callUpIM);} public void mouseEntered(MouseEvent event) {callLabel.setIcon(callOverIM);} public void mouseClicked(MouseEvent event) { if(isMyTurn) { //call if((Integer.parseInt(amountNeededToCall) < Integer.parseInt(myChipAmount)) && ((Integer.parseInt(amountNeededToCall) > 0))) { String sendCommand = "COMMAND:"; sendCommand = sendCommand + "call" + amountNeededToCall; messageManager.sendMessage( userName,sendCommand); updateAllValues();//always update new values last } } } } ); customCall.add(callLabel); callPanel.add(customCall); JPanel raisePanel = new JPanel(); raisePanel.setBackground(Color.white); raisePanel.setLayout(new BoxLayout(raisePanel, BoxLayout.Y_AXIS)); raisePanel.add(amountRaiseLabel); raisePanel.add(raiseAmount_Field); JPanel customRaise = new JPanel(); customRaise.setBackground(Color.white); customRaise.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent event) {raiseLabel.setIcon(raiseDownIM);} public void mouseReleased(MouseEvent event) {raiseLabel.setIcon(raiseUpIM);} public void mouseExited(MouseEvent event) {raiseLabel.setIcon(raiseUpIM);} public void mouseEntered(MouseEvent event) {raiseLabel.setIcon(raiseOverIM);} public void mouseClicked(MouseEvent event) { if(isMyTurn) { //check raiselabel raiseAmount = raiseAmount_Field.getText(); if( (Integer.parseInt(raiseAmount) < Integer.parseInt(myChipAmount)) && (Integer.parseInt(raiseAmount) > 0) && (Integer.parseInt(myChipAmount) - Integer.parseInt(raiseAmount) > 0) ) { String sendCommand = "COMMAND:"; //amountAlreadyBet = String.valueOf(Integer.parseInt(amountAlreadyBet) + Integer.parseInt(raiseAmount)); sendCommand = sendCommand + "raise" + raiseAmount; messageManager.sendMessage( userName,sendCommand); updateAllValues();//always update new values last } } } } ); customRaise.add(raiseLabel); raisePanel.add(customRaise); JPanel allInPanel = new JPanel(); allInPanel.setBackground(Color.white); allInPanel.setLayout(new BoxLayout(allInPanel, BoxLayout.Y_AXIS)); allInPanel.add(myChipAmountLabel); allInPanel.add(myChipAmount_Field); JPanel customAllIn = new JPanel(); customAllIn.setBackground(Color.white); customAllIn.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent event) {allinLabel.setIcon(allinDownIM);} public void mouseReleased(MouseEvent event) {allinLabel.setIcon(allinUpIM);} public void mouseExited(MouseEvent event) {allinLabel.setIcon(allinUpIM);} public void mouseEntered(MouseEvent event) {allinLabel.setIcon(allinOverIM);} public void mouseClicked(MouseEvent event) { if(isMyTurn) { String sendCommand = "COMMAND:"; //put em all in sendCommand = sendCommand + "allin" + myChipAmount; messageManager.sendMessage( userName,sendCommand); updateAllValues();//always update new values last //SWITCH THE UPDATE ALL VALUES WITH SENDMESSAGE??? } } } ); customAllIn.add(allinLabel); allInPanel.add(customAllIn); JPanel miscPanel1 = new JPanel(); miscPanel1.setBackground(Color.white); miscPanel1.setLayout(new BoxLayout(miscPanel1, BoxLayout.Y_AXIS)); JPanel SGBPanel = new JPanel(); SGBPanel.setBackground(Color.white); SGBPanel.setMinimumSize( new Dimension(120,37) ); SGBPanel.setPreferredSize( new Dimension(120,37) ); SGBPanel.setMaximumSize( new Dimension(120,37) ); SGBPanel.add(startGameButton); JPanel RBPanel = new JPanel(); RBPanel.setBackground(Color.white); RBPanel.setMinimumSize( new Dimension(120,37) ); RBPanel.setPreferredSize( new Dimension(120,37) ); RBPanel.setMaximumSize( new Dimension(120,37) ); RBPanel.add(refreshButton); miscPanel1.add(SGBPanel); miscPanel1.add(RBPanel); miscPanel1.setMinimumSize( new Dimension(120,75) ); miscPanel1.setPreferredSize( new Dimension(120,75) ); miscPanel1.setMaximumSize( new Dimension(120,75) ); //setup the buttons panel JPanel buttonsPanel = new JPanel(); buttonsPanel.setBackground(Color.white); buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)); buttonsPanel.setMinimumSize(new Dimension(600, 75)); buttonsPanel.setPreferredSize(new Dimension(600, 75)); buttonsPanel.setMaximumSize(new Dimension(800, 75)); //add the buttons to the buttons panel buttonsPanel.add(foldPanel); buttonsPanel.add(checkPanel); buttonsPanel.add(callPanel); buttonsPanel.add(raisePanel); buttonsPanel.add(allInPanel); buttonsPanel.add(miscPanel1); buttonsPanel.add(exitButton); //add the buttonspanel to the main game panel (this puts all the buttons on the mainGamePanel) mainGamePanel.add(buttonsPanel); //put main panel into scrollPane JScrollPane mainGameScrollPane = new JScrollPane(mainGamePanel); mainGameScrollPane.setMinimumSize( new Dimension(800,450) ); mainGameScrollPane.setPreferredSize( new Dimension(800,450) ); //puts everything into splitPane2 which holds the entire game (this can be put into a panel later or something) splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, mainGameScrollPane, splitPane); splitPane2.setOneTouchExpandable(true); splitPane2.setDividerLocation(450); splitPane2.setPreferredSize(new Dimension(900, 625)); frame.getContentPane().add(splitPane2); frame.pack(); frame.setVisible(true); connectToGame(); //connect player to the game updateAllValues(); //initiate all values using the update function } //end of constructor public static void initPropertiesSetup() { try { // get System.properties Properties systemProperties = System.getProperties(); // append project properties to System.properties FileInputStream propertyFile = new FileInputStream( "holdem.properties" ); systemProperties.load( propertyFile ); propertyFile.close(); String key = "BOONDOCK.holdem.MySQL.PATH"; mySQLPath = systemProperties.getProperty( key ); } catch(Exception e) { System.out.println("Unable to setup properties file which was setup when the server was created. Reverting to default mysql path."); mySQLPath = DEFAULT_MYSQL_PATH; } } /** * The main function of the game. * For now, this sets the address that the user connects to. * Creates the JFrame and uses the gui constructor to draw layout. */ public aiPlayer() { initPropertiesSetup(); JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); frame = new JFrame("No Limit Texas Holdem Game - by The Boondock Saints"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // prompt for userName JOptionPane.showMessageDialog(frame,"AI player created, watch all the action on this GUI!"); userName = "AI v1.2"; inputAddress = "localhost"; SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } //end of main method private static String executeMySQLCommand(String myQuery) { String retVal = ""; //JFrame smallFrame = new JFrame("MySQL Query Results");//CONNECT TO MYSQL! try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch(Exception ex) { System.out.println("Weird exception caught."); } try { Connection conn = DriverManager.getConnection(mySQLPath); Statement stmt = null; ResultSet rs = null; try { stmt = conn.createStatement(); rs = stmt.executeQuery(myQuery); StringBuffer results = new StringBuffer(); ResultSetMetaData metaData = rs.getMetaData(); int numberOfColumns = metaData.getColumnCount(); for(int i = 1; i <= numberOfColumns; i++) results.append( metaData.getColumnName(i) + "\t"); results.append("\n"); while(rs.next()) { for(int i = 1; i <= numberOfColumns; i++) { if(!retVal.equals("")) retVal = retVal + ":"; results.append(rs.getObject(i) + "\t"); retVal = retVal + rs.getObject(i); } results.append("\n"); } } finally { if(rs != null) { try{ rs.close(); } catch (SQLException sqlEx) { //ignore } rs = null; } if(stmt != null) { try { stmt.close(); } catch(SQLException sqlEx) { //ignore } stmt = null; } } } catch (SQLException ex) { if(!displayedSQLError) { System.out.println("WARNING: You dont have MySQL properly installed. Additional information provided below:"); System.out.println(" SQLException: " + ex.getMessage()); System.out.println(" SQLState: " + ex.getSQLState()); System.out.println(" VendorError: " + ex.getErrorCode()); displayedSQLError = true; } } return retVal; } private static void createAndShowGUI() { MessageManager messageManager; messageManager = new SocketMessageManager(inputAddress); aiPlayer holdemGui = new aiPlayer(messageManager); } /** * MyMessageListener listens for new messages from MessageManager and * displays messages in messageArea using MessageDisplayer. */ private class MyMessageListener implements MessageListener { // when received, display new messages in messageArea public void messageReceived( String from, String message ) { if(from.equals("COMMAND_NEWHAND")) { repaint(); updateAllValues(); repaint(); drawBoard(); repaint(); } if(from.equals("COMMAND_SERVER")) { StringTokenizer tokenizer = new StringTokenizer(message, ":"); OtherPlayer otherPlayer = new OtherPlayer(); otherPlayer.setIndex(tokenizer.nextToken()); currentPlayer = Integer.parseInt(tokenizer.nextToken()); int newSoundID = Integer.parseInt(tokenizer.nextToken()); otherPlayer.setName(tokenizer.nextToken()); otherPlayer.setChipCount(tokenizer.nextToken()); String tempString = tokenizer.nextToken(); if(tempString.equals("F")) otherPlayer.setIsInHand(false); else otherPlayer.setIsInHand(true); //Dealer info: tempString = tokenizer.nextToken(); if(tempString.equals("T")) { dealerButton = otherPlayer.getIndex(); } //end dealer info otherPlayer.setFirstCard(tokenizer.nextToken()); otherPlayer.setSecondCard(tokenizer.nextToken()); for(int i = 0; i < 5; i++) { communityCards[i] = tokenizer.nextToken(); } tempString = tokenizer.nextToken(); if(tempString.equals("F")) showFlop = false; else showFlop = true; tempString = tokenizer.nextToken(); if(tempString.equals("F")) showTurn = false; else showTurn = true; tempString = tokenizer.nextToken(); if(tempString.equals("F")) showRiver = false; else showRiver = true; execMe = false; if(userName.equals(otherPlayer.getName())) { doSound(newSoundID); //soundID is global so its already there :) myChipAmount = String.valueOf(otherPlayer.getChipCount()); potSize = tokenizer.nextToken(); amountAlreadyBet = tokenizer.nextToken(); amountNeededToCall = String.valueOf(Integer.parseInt(tokenizer.nextToken())); otherPlayer.setIsMyPlayer(true); execMe = true; } allPlayers[otherPlayer.getIndex()] = otherPlayer; updateAllValues(); tempString = otherPlayer.getName() + "(" + otherPlayer.getChipCount() + ")"; if(!otherPlayer.isInHand()) tempString = tempString + "\n[Folded]"; switch(otherPlayer.getIndex()) { case 0: if(currentPlayer == 0) { if(otherPlayer.getName().equals(userName)) { isMyTurn = true; if(!notifyMe) notifyMe(); } else isMyTurn = false; P1info.setForeground(Color.blue); } else P1info.setForeground(Color.black); P1info.setText(SPACEBUFFER + tempString + SPACEBUFFER); break; case 1: if(currentPlayer == 1) { if(otherPlayer.getName().equals(userName)) { isMyTurn = true; if(!notifyMe) notifyMe(); } else isMyTurn = false; P2info.setForeground(Color.blue); } else P2info.setForeground(Color.black); P2info.setText(tempString + SPACEBUFFER); break; case 2: if(currentPlayer == 2) { if(otherPlayer.getName().equals(userName)) { isMyTurn = true; if(!notifyMe) notifyMe(); } else isMyTurn = false; P3info.setForeground(Color.blue); } else P3info.setForeground(Color.black); P3info.setText(tempString + SPACEBUFFER); break; case 3: if(currentPlayer == 3) { if(otherPlayer.getName().equals(userName)) { isMyTurn = true; if(!notifyMe) notifyMe(); } else isMyTurn = false; P4info.setForeground(Color.blue); } else P4info.setForeground(Color.black); P4info.setText(tempString); break; case 4: if(currentPlayer == 4) { if(otherPlayer.getName().equals(userName)) { isMyTurn = true; if(!notifyMe) notifyMe(); } else isMyTurn = false; P5info.setForeground(Color.blue); } else P5info.setForeground(Color.black); P5info.setText(tempString + SPACEBUFFER); break; case 5: if(currentPlayer == 5) { if(otherPlayer.getName().equals(userName)) { isMyTurn = true; if(!notifyMe) notifyMe(); } else isMyTurn = false; P6info.setForeground(Color.blue); } else P6info.setForeground(Color.black); P6info.setText(tempString + SPACEBUFFER); break; case 6: if(currentPlayer == 6) { if(otherPlayer.getName().equals(userName)) { isMyTurn = true; if(!notifyMe) notifyMe(); } else isMyTurn = false; P7info.setForeground(Color.blue); } else P7info.setForeground(Color.black); P7info.setText(SPACEBUFFER + tempString + SPACEBUFFER); break; case 7: if(currentPlayer == 7) { if(otherPlayer.getName().equals(userName)) { isMyTurn = true; if(!notifyMe) notifyMe(); } else isMyTurn = false; P8info.setForeground(Color.blue); } else P8info.setForeground(Color.black); P8info.setText(tempString); break; default: return; } tempString = tokenizer.nextToken(); if(tempString.equals("T")) { doingBlinds = true; } else doingBlinds=false; //IT IS MY TURN, I AM THE AI, I MAKE A CHOICE HERE: if(isMyTurn && !doingBlinds && execMe) { String probVals = ""; Card c1 = new Card(); c1.toCard(allPlayers[currentPlayer].getFirstCard()); Card c2 = new Card(); c2.toCard(allPlayers[currentPlayer].getSecondCard()); if(c1.getNumSuit() == c2.getNumSuit()) { probVals = executeMySQLCommand("SELECT * FROM hands_suited"); } else { probVals = executeMySQLCommand("SELECT * FROM hands_unsuited"); } StringTokenizer probValTokens = new StringTokenizer(probVals, ":"); String temp = ""; for(int i = 0; i < (c1.getNumRank()); i++) { if(!probValTokens.hasMoreTokens()) break; temp = probValTokens.nextToken(); } for(int i = 0; i < ((c2.getNumRank()-1)*13); i++) {//POSSIBLE BUG, ADDED '-13' TO THIS LOOP'S COND. if(!probValTokens.hasMoreTokens()) break; temp = probValTokens.nextToken(); } System.out.println("HAND: " + c1.getNumRank() + "+" + c2.getNumRank() + " PROB: " + temp); if(Integer.parseInt(amountNeededToCall) > 0) { if((Double.parseDouble(amountNeededToCall) / Double.parseDouble(myChipAmount)) <= Double.parseDouble(temp) ) { //call (has additional sanity checks which should hold true no matter what if((Integer.parseInt(amountNeededToCall) < Integer.parseInt(myChipAmount)) && ((Integer.parseInt(amountNeededToCall) > 0))) { String sendCommand = "COMMAND:"; sendCommand = sendCommand + "call" + amountNeededToCall; messageManager.sendMessage( userName,sendCommand); updateAllValues();//always update new values last } else { System.out.println("Sanity check failed."); } } else {//or fold String sendCommand = "COMMAND:"; //fold sendCommand = sendCommand + "fold"; messageManager.sendMessage( userName,sendCommand); updateAllValues();//always update new values last } } else { //check/raise if((Double.parseDouble(amountNeededToCall) / Double.parseDouble(myChipAmount)) <= Double.parseDouble(temp) && (Double.parseDouble(temp) > 0.2) ) { //raise String sendCommand = "COMMAND:"; sendCommand = sendCommand + "raise" + (int)(Double.parseDouble(myChipAmount) * ((Double.parseDouble(temp)-.15) * .25) ); messageManager.sendMessage( userName,sendCommand); updateAllValues();//always update new values last } else { String sendCommand = "COMMAND:"; //check if(amountNeededToCall.equals("0")) { sendCommand = sendCommand + "check"; messageManager.sendMessage( userName,sendCommand); updateAllValues();//always update new values last } } } } //END OF AI STUFF return; } // append message using MessageDisplayer and // invokeLater, ensuring thread-safe access messageArea SwingUtilities.invokeLater( new MessageDisplayer( from, message ) ); } } /** * MessageDisplayer displays a new message by appending the message to * the messageArea JTextArea. This Runnable object should be executed * only on the Event thread, because it modifies a live Swing component */ private class MessageDisplayer implements Runnable { private String fromUser; private String messageBody; // MessageDisplayer constructor public MessageDisplayer( String from, String body ) { fromUser = from; messageBody = body; } // display new message in messageArea public void run() { // append new message messageArea.append( "\n" + fromUser + "> " + messageBody ); // move caret to end of messageArea to ensure new // message is visible on screen messageArea.setCaretPosition( messageArea.getText().length() ); } } // end MessageDisplayer inner class /** * Connects this player to the game automatically. */ public void connectToGame() { // connect to server and route messages to messageListener messageManager.connect( messageListener, userName ); messageManager.sendMessage("CONNECT_SERVER", userName); // clear messageArea messageArea.setText( "" ); // update GUI components inputArea.setEditable( true ); inputArea.requestFocus(); } /** * Handles actions when they are performed by gui. * * @param e An ActionEvent which allows to function to know the source that triggered the event. */ public void actionPerformed (ActionEvent e) { String sendCommand = "COMMAND:"; repaint(); if(e.getSource() == exitButton) { messageManager.disconnect( messageListener ); System.exit( 0 ); } //exit if exit command clicked if(e.getSource() == startGameButton) { sendCommand = sendCommand + "STARTGAME"; messageManager.sendMessage( userName,sendCommand); updateAllValues();//always update new values last }//end of startgame event if(e.getSource() == refreshButton) { repaint(); }//refresh the board repaint(); drawBoard(); } /** * Updates all the fields/values on the screen. */ public void updateAllValues() { amountAlreadyBet_Field.setText(amountAlreadyBet); potSize_Field.setText(potSize); amountNeededToCall_Field.setText(amountNeededToCall); raiseAmount_Field.setText(raiseAmount); myChipAmount_Field.setText(myChipAmount); repaint(); drawBoard(); } public void paint(Graphics g) { super.paint(g); drawBoard(); } private static void drawBoard() { g = frame.getGraphics(); // that's why you should put this code here, //NOW USE G TO DRAW! Graphics2D g2 = (Graphics2D)g; guiCard firstCard, secondCard, firstComm, secondComm, thirdComm, fourthComm, fifthComm; final int xCardLoc[] = {270,432,594,675,594,432,270,166}; final int yCardLoc[] = {120,120,120,200,260,260,260,200}; final int xCommLoc[] = {377,409,441,473,505}; final int yCommLoc = 196; for(int i = 0; i < 8; i++) { if(allPlayers[i] != null) { if(allPlayers[i].isInHand()) { firstCard = new guiCard(xCardLoc[i],yCardLoc[i],allPlayers[i].getFirstCard(),allPlayers[i].isMyPlayer()); secondCard = new guiCard(xCardLoc[i]+32,yCardLoc[i],allPlayers[i].getSecondCard(),allPlayers[i].isMyPlayer()); firstCard.draw(g2); secondCard.draw(g2); } } } if(showFlop) { firstComm = new guiCard(xCommLoc[0],yCommLoc,communityCards[0],true); secondComm = new guiCard(xCommLoc[1],yCommLoc,communityCards[1],true); thirdComm = new guiCard(xCommLoc[2],yCommLoc,communityCards[2],true); firstComm.draw(g2); secondComm.draw(g2); thirdComm.draw(g2); } if(showTurn) { fourthComm = new guiCard(xCommLoc[3],yCommLoc,communityCards[3],true); fourthComm.draw(g2); } if(showRiver) { fifthComm = new guiCard(xCommLoc[4],yCommLoc,communityCards[4],true); fifthComm.draw(g2); } //DRAW DEALER g.setColor(Color.white); g.fillOval(xCardLoc[dealerButton] - 40, yCardLoc[dealerButton] + 30, 20, 20); g.setColor(Color.black); g.drawString("D", xCardLoc[dealerButton] - 35, yCardLoc[dealerButton] + 45); }//end of drawBoard method public void notifyMe() { notifyMe = true; } public void doSound(int newSoundID) { if(newSoundID < soundID) { soundID = newSoundID; } else if(newSoundID > soundID) { soundID = newSoundID; } else { //if soundID is same, donothing } }//not fully implemented for AI because its not practical for AI to play sounds for itself! }//end of gui class