Java Swing Tic-Tac-Toe

I actually wrote this code to show off some of the really cool features of Java to one of my friends who also wrote the same application in a “C++”-esque style. And btw that friend of mine even developed code for the computer player. But after completing his code he sadly realized the basic fact that you cannot win in TicTacToe if you play perfectly!! Hehe
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.logging.Logger; /** * TicTacToe Application * @author Steve Robinson * @version 1.0 */ class TicTacToeFrame extends JFrame { JButton [][] buttons= new JButton[ 3 ][ 3 ]; JTextField statusBar; GamePanel panel; Integer turn; GameListener listener= new GameListener(); Integer count; public TicTacToeFrame() { setLayout( new BorderLayout()); panel= new GamePanel(); add(panel,BorderLayout.CENTER); statusBar= new JTextField( "Player1's Turn" ); statusBar.setEditable( false ); add(statusBar,BorderLayout.SOUTH); setTitle( "Tic Tac Toe!" ); setVisible( true ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds( 400 , 400 , 300 , 300 ); } class GamePanel extends JPanel { public GamePanel() { setLayout( new GridLayout( 3 , 3 )); turn = 1 ; count= 0 ; for ( int i= 0 ;i< 3 ;i++) for ( int j= 0 ;j< 3 ;j++) { buttons[i][j]= new JButton(); buttons[i][j].putClientProperty( "INDEX" , new Integer[]{i,j}); buttons[i][j].putClientProperty( "OWNER" , null ); buttons[i][j].addActionListener(listener); add(buttons[i][j]); } } } class GameListener implements ActionListener { public void actionPerformed(ActionEvent e) { count++; JButton b=(JButton)e.getSource(); Integer[]index=(Integer[]) b.getClientProperty( "INDEX" ); //System.out.println(turn); //turn // //System.out.println("["+index[0]+"]"+"["+index[1]+"]"); // b.putClientProperty( "OWNER" , turn); Icon ico= new ImageIcon(turn.toString()+ ".gif" ); b.setIcon(ico); b.setEnabled( false ); boolean result=checkVictoryCondition(index); if (result) { JOptionPane.showMessageDialog( null , "Player " +turn.toString()+ " Wins" ); initComponents(); } else { if (turn== 1 ) { turn= 2 ; statusBar.setText( "Player2's Turn" ); } else { turn= 1 ; statusBar.setText( "Player1's Turn" ); } } if (count== 9 ) { JOptionPane.showMessageDialog( null , "Match is a draw!" ); initComponents(); } } Integer getOwner(JButton b) { return (Integer)b.getClientProperty( "OWNER" ); } //PrintButtonMap for Diagnostics void printbuttonMap(Integer [][]bMap) { for ( int i= 0 ;i for ( int j= 0 ;j System.out.print(bMap[i][j]+ " " ); System.out.println( "" ); } } boolean checkVictoryCondition(Integer [] index) { /*Integer[][]buttonMap=new Integer[][] { { getOwner(buttons[0][0]),getOwner(buttons[0][1]),getOwner(buttons[0][2])}, { getOwner(buttons[1][0]),getOwner(buttons[1][1]),getOwner(buttons[1][2])}, { getOwner(buttons[2][0]),getOwner(buttons[2][1]),getOwner(buttons[2][2])} }; printbuttonMap(buttonMap); */ Integer a=index[ 0 ]; Integer b=index[ 1 ]; int i; //check row for (i= 0 ;i< 3 ;i++) { if (getOwner(buttons[a][i])!=getOwner(buttons[a][b])) break ; } if (i== 3 ) return true ; //check column for (i= 0 ;i< 3 ;i++) { if (getOwner(buttons[i][b])!=getOwner(buttons[a][b])) break ; } if (i== 3 ) return true ; //check diagonal if ((a== 2 &&b== 2 )||(a== 0 &&b== 0 )||(a== 1 &&b== 1 )||(a== 0 &&b== 2 )||(a== 2 &&b== 0 )) { //left diagonal for (i= 0 ;i if (getOwner(buttons[i][i])!=getOwner(buttons[a][b])) break ; if (i== 3 ) return true ; //right diagonal if ((getOwner(buttons[ 0 ][ 2 ])==getOwner(buttons[a][b]))&&(getOwner(buttons[ 1 ][ 1 ])==getOwner(buttons[a][b]))&&(getOwner(buttons[ 2 ][ 0 ])==getOwner(buttons[a][b]))) return true ; } return false ; } } void initComponents() { for ( int i= 0 ;i< 3 ;i++) for ( int j= 0 ;j< 3 ;j++) { buttons[i][j].putClientProperty( "INDEX" , new Integer[]{i,j}); buttons[i][j].putClientProperty( "OWNER" , null ); buttons[i][j].setIcon( null ); buttons[i][j].setEnabled( true ); turn= 1 ; count= 0 ; statusBar.setText( "Player1's Turn" ); } } } class TicTacToe { public static void main(String[] args) { EventQueue.invokeLater( new Runnable(){ public void run() { TicTacToeFrame frame= new TicTacToeFrame(); } }); } } |
The code is rather straightforward. Ive used two properties in the Buttons to store some information used for checking the winning condition. One is the “OWNER” property which indicates which user currently owns the square and the “INDEX” property which indicates the square’s index in the grid (ie [1,1], [1,2]… etc) Once any player clicks on a square, the OWNER property is updated and the victoryCondition is checked by using the OWNER properties of all the buttons. The rest of the code is self explanatory.
And adding keyboard support for the second player is a pretty easy job. As they say… “I leave that as an exercise”! Hahaha
Cheers,
Steve.
—–
I forgot to attach the image icon files that will be used by the application. You can download it from here
http://www.mediafire.com/?d7d93v2342dxind
Just extract the contents to the folder that contains the code. Thanks to my friend “Gur Png” for telling me about this.
Reference: Java TicTacToe from our JCG partner Steve Robinson at the Footy ‘n’ Tech blog.
Hi, I also create TicTacToe Game With AI:
http://algojava.blogspot.com/2012/05/tic-tac-toe-game-swingjava.html
I think you pasted your code wrong… look at line 108:
for(int i=0;i for(int j=0;j System.out.print(bMap[i][j]+” “);