// Debug the TicTacToe program below. import import import import import javax.swing.*; java.awt.

*; java.awt.event.*; java.io.*; java.util.*;

public class TicTacToe implements ActionListener { final String VERSION = "2.0"; //Setting up ALL the variables JFrame window = new JFrame("Tic-Tac-Toe " + VERSION); JMenuBar mnuMain = new JMenuBar(); JMenuItem mnuNewGame = new JMenuItem("New Game"), mnuInstruction = new JMenuItem("Instruct ions"), mnuExit = new JMenuItem("Exit"), mnuAbout = new JMenuItem("About"); JButton btn1v1 = new JButton("Player vs Player"), btn1vCPU = new JButton("Player vs Computer"), btnQuit = new JButton("Quit"), btnSetName = new JButton("Set Player Names"), btnContinue = new JButton("Continue..."), btnTryAgain = new JButton("Try Again?"); JButton btnEmpty[] = new JButton[10]; JPanel pnlNewGame = new JPanel(), pnlMenu = new JPanel(), pnlMain = new JPanel(), pnlTop = new JPanel(), pnlBottom = new JPanel(), pnlQuitNTryAgain = new JPanel(), pnlPlayingField = new JPanel(); JLabel lblTitle = new JLabel("Tic-Tac-Toe"), lblTurn = new JLabel(), lblStatus = new JLabel("", JLabel.CENTER); JTextArea txtMessage = new JTextArea(); final int winCombo[][] = new int[][] {1, 2, 3}, }, {4, 5, 6}, }, {7, 8, 9}, {3, 6, 9} /*Horizontal Wins*/ /*Vertical Wins*/ /*Diagonal Wins*/ }; final int X = 535, Y = 342, mainColorR = 190, mainColorG = 50, mainColorB = 50, btnColorR = 70, btnColorG = 70, btnColorB = 70; Color clrBtnWonColor = new Color(190, 190, 190); int turn = 1, player1Won = 0, player2Won = 0, wonNumber1 = 1, wonNumber2 = 1, wonNumber3 = 1; int option; {2, 5, 8}, {3, 5, 7 { {1, 4, 7}, {1, 5, 9

setBackground(new Color(mainColorR.add(mnuNewGame).// Menu Bar is Complete //Adding buttons to NewGame panel pnlNewGame. 1. 2)). pnlMenu.setEnabled(false). mnuMain.add(btnContinue). mainColo rB)). (mainColorB. pnlQuitNTryAgain.setLayout(new FlowLayout(FlowLayout.setBackground(new Color((mainColorR . window.setEditable(false).setLayout(new GridLayout(4.add(btn1v1).50)).setBackground(new Color(mainColorR . .add(btnTryAgain). pnlMain. pnlBottom. Player1 = "Player 1". pnlNewGame.setBackground(new Color(mainColorR. txtMessage.setLocation(350.boolean inGame = false. btnContinue. String message.setLayout(new FlowLayout(FlowLayout. mainColorG 50. mnuMain.CENTER)). mainColorG. 10)). //Setting Menu. mainColorG. boolean btnEmptyClicked = false.add(mnuAbout).add(btn1vCPU).add(btnSetName).white).CENTER)). //Setting Button propertied btnTryAgain. (mainColorG 50). 260). mainColorB-30)). mainColorG-30.setLayout(new BorderLayout()). 2. window. whoTurn.setLayout(new FlowLayout(FlowLayout. pnlBottom. public TicTacToe() { //Setting game properties and layout and sytle.setDefaultCloseOperation(JFrame. boolean win = false. pnlNewGame. pnlTop.setBackground(new Color(mainColorR.setSize(X. //Setting up Panel QuitNTryAgain pnlQuitNTryAgain. mnuMain. Main.EXIT_ON_CLOSE)... 2.add(btnQuit). window. pnlNewGame.add(mnuExit).setResizable(false). txtMessage.add(mnuInstruction). pnlNewGame.50). //Setting window properties: window. pnlNewGame. pnlQuitNTryAgain. //Adding menu items to menu bar mnuMain.CENTER)).50. pnlTop.setForeground(Color. mainColor B)). //Setting txtMessage Properties txtMessage.setLayout(new GridLayout(1. mainCo lorB)). Bottom Panel Layout/Backgrounds pnlMenu. mainColorB.50))). window. 2. Y).setBackground(new Color(mainColorR-30. mainColorG.setEnabled(false). Top. Player2 = "Player 2".

btnContinue.setLayout(new BorderLayout()).add(lblTitle).addActionListener(this). BorderLayout.add(pnlPlayingField). pnlPlayingField.CENTER).setLayout(new GridLayout(3. pnlTop.addActionListener(this).EAST). BorderLayout.CENTER). BorderLayout.add(btnEmpty[i]).addActionListener(this).addActionListener(this). pnlMain. mnuAbout.setBackground(Color.add(pnlMenu.addActionListener(this).addActionListener(this). pnlMain. pnlMain.setLayout(new BorderLayout()). 3.add(pnlQuitNTryAgain. 2. BorderLayout.add(pnlMain.NORTH). ------------------------*/ public void showGame() { // Shows the Playing Field // *IMPORTANT*. mnuExit.addActionListener(this). btnColorG . pnlBottom. btn1vCPU. BorderLayout.setLayout(new BorderLayout()). btnColorB)).CENTER).addActionListener(this).//Adding Action Listener to all the Buttons and Menu Items mnuNewGame. pnlMain.WEST). btnQuit. mnuInstruction.add(pnlBottom. btnTryAgain. for(int i=1.// Calling the cl*** construtor.addActionListener(this). 2)). pnlBottom. //Setting up the playing field pnlPlayingField.addActionListener(this).addActionListener(this). . window. btnEmpty[i].add(lblStatus. pnlBottom. pnlBottom.add(pnlTop.setBackground(new Color(btnColorR. window. i<=9. btnSetName. btnEmpty[i].add(lblTurn. pnlPlayingField. btn1v1. BorderLayout.SOUTH).// Playing Field is Compelte } //Adding everything needed to pnlMenu and pnlMain pnlMenu.black). //Adding to window and Showing window window. i++) { btnEmpty[i] = new JButton(). BorderLayout.add(mnuMain). // STARTS HERE! } PROGRAM /* ------------------------Start of all METHODS. pnlTop. } public static void main(String[] args) { new TicTacToe().setVisible(true).Does not start out brand new (meaning just shows what it ha d before) clearPanelSouth().

} turn = 1.setText(""). btnColorB)). i<10. or horizontally. btnEmpty[wonNumber3].setLayout(new FlowLayout(FlowLayout. checkTurn().pnlPlayingField.getText()) && // if {1 == 2 && 2 == 3} btnEmpty[winCombo[i][1]].getText(). pnlMain. showGame().requestFocus(). inGame = true. checkWinStatus(). btnEmpty[wonNumber2]. i<8. (Basically: Starts a new 1v1 Game) btnEmpty[wonNumber1]. } //---------------------------------------------------------------------------------------------------------------------------------public void new1v1Game() { // Sets all the game requir ed variables to default // // and then shows the playing field.getText())) { /* The way this checks the if someone won i s: First: it checks if the btnEmpty[x] is n ot equal to an empty stringx being the array number inside the multi-dementional arr ay winCombo[checks inside each of the 7 sets][the first number] & . for(int i=0. If the game is over then it a if you want to play again. btnEmpty[i].equals(btnEmp ty[winCombo[i][1]]. btnColor G.setBackground(new Color(btnColorR. i++) { if( !btnEmpty[winCombo[i][0]]. } //---------------------------------------------------------------------------------------------------------------------------------public void checkWin() { // checks if there are 3 symbols in a row vertically.getText(). } //---------------------------------------------------------------------------------------------------------------------------------public void goBack() { clearPanelSouth().setBackground(new Color(btnColorR. btnColorB)).CENTER)). btnColor G. pnlMain.equals(btnEmp ty[winCombo[i][2]].setEnabled(true). for(int i=1. btnColor G.getText(). diagonally.equals("") & btnEmpty[winCombo[i][0]]. btnColorB)).setBackground(new Color(btnColorR.add(lblTitle). // sks // then shows a message and disables buttons. i++) { btnEmpty[i]. win = false.

setBackground(clrBtnWonColo r). i<=9. for(int i=1. } else { whoTurn = Player2 + " [O]". } else checkTurn(). } //------------------------------------------------------------------------------ . } lblTurn. player2Won++.x being winCombo[each set][the first number] y being winCombo[each set the sa me as x][the second number] (So basically checks if the first and second number in each set is equ al to each other) Third: it checks if btnEmtpy[y] is eual to btnEmpty[z].setText("Turn: " + whoTurn).setBackground(clrBtnWonColo r). wonNumber3 = winCombo[i][2]. } else { message = Player2 + " has won". btnEmpty[wonNumber2].y being the same y as last time and z being winCombo[each set as y][the thir d number] Conclusion: So basically it checks i f it is equal to the btnEmpty is equal to each set of numbers */ win = true. } //---------------------------------------------------------------------------------------------------------------------------------public void checkTurn() { if(!(turn % 2 == 0)) { whoTurn = Player1 + " [X]". } else if(!win && turn>9) message = "Both players have tied!\nBetter luck next time.Secong: it checks if btnEmpty[x] is equa l to btnEmpty[y]. wonNumber1 = winCombo[i][0]. btnEmpty[wonNumber1]. } } if(win (!win && turn>9)) { if(win) { if(turn % 2 == 0) { message = Player1 + " has won". btnEmpty[wonNumber3]. break.setEnabled(false). i++) { btnEmpty[i]. checkWinStatus(). player1Won++. } win = false. showMessage(message).setBackground(clrBtnWonColo r).".setEnabled(true). } btnTryAgain. wonNumber2 = winCombo[i][1].

pnlMain. String setText) { return JOptionPane.showInputDialog(null. pnlMain. JOptionPane. pnlTop.equals(Player2)) { option = askMessage("Player 1 name matches Player 2's\nD o you want to continue?".equals("")) showMessage("Invalid Name!"). else if(temp. } //---------------------------------------------------------------------------------------------------------------------------------public int askMessage(String message.remove(pnlPlayingField). pnlTop. int option) { return JOptionPane. "Name Match". 2. } //---------------------------------------------------------------------------------------------------------------------------------public String getInput(String message. setText). } else if(temp != null) { Player2 = temp.remove(pnlTop). pnlBottom. String title.showConfirmDialog(null. optio n). temp = getInput("Enter player 1 name:".CENTER)). } } //---------------------------------------------------------------------------------------------------------------------------------public void setDefaultLayout() { pnlMain. .YES_NO_OPTION). message.showMessageDialog(null. 1. pnlBottom //could have. title. Player1).setText(Player1 + ": " + player1Won + " " + Player2 + ": " + player2Won). if(option == JOptionPane.remove(lblTurn).----------------------------------------------------public void checkWinStatus() { lblStatus.remove(lblTitle).remove(txtMessage).CENTER)). pnlTop.setLayout(new FlowLayout(FlowLayout. } //---------------------------------------------------------------------------------------------------------------------------------public void askUserForPlayerNames() { String temp = Player2.YES_OPTION) Player1 = temp. message). message. if(temp == null) {/*Do Nothing*/} else if(temp. pnlMain. 5)). pnlBottom.setLayout(new GridLayout(2. pnlTop. } //---------------------------------------------------------------------------------------------------------------------------------public void showMessage(String message) { JOptionPane.setLayout(new FlowLayout(FlowLayout. } //---------------------------------------------------------------------------------------------------------------------------------public void clearPanelSouth() { //Removes all the possible panel s //that pnlMain.remove(pnlBottom).

(h orizontally. } txtMessage. diagonally. turn++.pnlBottom. } if(source == mnuNewGame source == mnuInstruction source == mnuAbout) { clearPanelSouth().add(pnlNewGame).setText(message).getSource().add(txtMessage).setText("X"). } /* ------------------------------------End of all non-Abstract METHODS. pnlTop. i++) { if(source == btnEmpty[i] && turn < 10) { btnEmptyClicked = false. else btnEmpty[i]. } pnlMain. } . } else if(source == mnuInstruction source == mnuAbout) { if(source == mnuInstruction) {// Instructions message = "Instructions:\n\n" + "Your go al is to be the first player to get 3 X's or O's in a\n" + "row. pnlPlayingField.remove(pnlQuitNTryAgain). ------------------------------------*/ //-------------------ACTION PERFORMED METHOD (Button Click --> Action?)------------------------// public void actionPerformed(ActionEvent click) { Object source = click. if(!(turn % 2 == 0)) btnEmpty[i].setText("O"). setDefaultLayout(). } } if(btnEmptyClicked) { checkWin(). i<=9. }else {//About message = "About:\n\n" + "Title: Tic-Tac-Toe\n" + "Creator : Blmaster\n" + "Version : " + VERSION + "\n".requestFocus().setEnabled(true). for(int i=1. btnEmptyClicked = false. btnEmpty[i]. if(source == mnuNewGame) {//NewGame pnlTop.add(pnlTop). or vertically)\n" + Player1 + ": X\n" + Player2 + ": O\n".

. } else if(source == mnuExit) { option = askMessage("Are you sure you want to exit?".YES_NO_OPTION).removed Back Button. if(option == JOptionPane.8.7. n" + } else {// 1 v CPU Game showMessage("Coming Soon!"). pnlMain.0.setEnabled(true).setEnabled(false). } else if(source == btnSetName) { askUserForPlayerNames(). Added Try Again Button 1. } pnlMain.fixed bug: TryAgain Button takes over Status Label 1. btnTryAgain." + "your current game will be lost.changes below: bug fixes [Stable] 1. } } } else if(source == btnContinue) { checkTurn(). } else if(source == btnQuit) { inGame = false.YES_OPTION) inGame = false.added Label that shows Player 1 and Player 2 wins 1.9.setVisible(true).. goBack(). new1v1Game().setVisible(false).removed Try Again pop-up. btnContinue. } else if(source == btnTryAgain) { new1v1Game(). JOptionPane.exit(0).YES_OPT ION) System. fixed bug: Same name for both players.else if(source == btn1v1 source == btn1vCPU) { if(inGame) { option = askMessage("If you start a new game.JOptionPane. } //-------------------END OF ACTION PERFORMED METHOD-------------------------// } /* Future Plans: -Add AI */ /* Changes: 2. "E xit Game". . showGame().6fixed bugs below. } if(!inGame) { if(source == btn1v1) {// 1 v 1 Game btnContinue. Added Continue Button 1.setEnabled(false)..YES_NO_OPTION ). "Quit Game?" ..95. if(option == JOptionPane." + "\ "Are you sure you want to continue?".

0. 0.5added function to set Player Names.35. added turn status to status bar. 1.9added back button.4program more efficient/faster.0- fixed bug: Names updated in game if user changes. changes below: (problems gone!) [Stable] 0. 1. 0.5basic functions with menu and nice GUI.6changed menu layout.3added Status bar with quit button during gameplay. 1.1added play again function.2changed theme. */ /* LAYOUT OF THE GAME: THE WINDOW: (pnlMenu/pnlMain) pnlMenu: (THE MENU) pnlMain: (pnlTop/pnlBottom) pnlTop: (pnlPlayingField/INSTRUCTIONS/ABOUT/NEW GAME) pnlBottom:(STATUS BAR/BACK BUTTON) /////////////////////////////////////////////////-------------/ pnlMenu / / /////////////////////////////////////////////////----/ / / / / / / / / / / / / / / / / / / / / / / / ///////////////////////////////////////////////// pnlMain . fixed bug: Names are null if user presses cancel. 1.8added dynamic win message. 0.4program more efficient/faster. 1.game playable. added comments. 1.15.fixed bug: one win combo not working. 1.5added function to set Player Names. 1.fixed bugs.MAIN WINDOW pnlTop . 1.7added game function.1.

-------*/ ./ / pnlBottom /////////////////////////////////////////////////----.

Sign up to vote on this title
UsefulNot useful