Can anyone help me improve my code please?
I have created this tic-tac-toe applet in university and I have been asked to try and replace some of the if statements with switch statements.
But I am unsure which set of if statements would be more suitable as a switch.
An I do not have a lot of experience with switch statements, I have only been using Java for 2 months.
Here is my code. If anyone can suggest which set of if statements would be more suitable as a switch statement I would really appreciate it.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TicTacToe extends JApplet implements ActionListener{ JButton squares[]; JButton newGameButton; JLabel score; JLabel wins; JLabel loses; JLabel ties; int emptySquaresLeft=9; int gamesWon=0; int gamesLost=0; int gamesTied=0; //Declares the colours that will be used, using the RGB values Color bgColour = new Color(30, 114, 165); // Medium blue Color squareColour = new Color(252, 248, 163); // Light yellow public static void main(String[] args) { //... Create an initialize the applet. JApplet theApplet = new TicTacToe(); theApplet.init(); // Needed if overridden in applet theApplet.start(); // Needed if overridden in applet //... Create a window (JFrame) and make applet the content pane. JFrame window = new JFrame("Tic Tac Toe Applet and Application"); window.setContentPane(theApplet); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Arrange the components. window.setSize(300,300); // Sets the window size window.setVisible(true); // Make the window visible } //======================================== Applet constructor public TicTacToe() { add(new JLabel("This is both an Applet and Application!")); } /** * init method is the applet's constructor */ public void init(){ // Get the applet's content pane - // all window components go there Container appletContent = this.getContentPane(); resize(400,400); //Set the applet's layout manager, font and background color appletContent.setLayout(new BorderLayout()); appletContent.setBackground(bgColour); // Create the button New Game and register it // with the action listener newGameButton=new JButton("New Game"); newGameButton.addActionListener(this); // Creates the won, lost and ties labels wins=new JLabel("Won: "); loses=new JLabel("Lost: "); ties=new JLabel("Ties: "); // Creates the top panel and adds the new game button to the North JPanel topPanel=new JPanel(); topPanel.setLayout(new BorderLayout()); topPanel.add(newGameButton,"North"); // Creates the top lower panel, sets the colour and grid layout JPanel topLowerPanel=new JPanel(); topLowerPanel.setLayout(new GridLayout(1,0)); topLowerPanel.setBackground(bgColour); // Adds the wins label to the panel topLowerPanel.add(wins); Font labelFont = wins.getFont(); wins.setFont(new Font(labelFont.getFontName(), labelFont.getStyle(), 16)); wins.setForeground(Color.WHITE); // Adds the loses label to the panel topLowerPanel.add(loses); loses.setFont(new Font(labelFont.getFontName(), labelFont.getStyle(), 16)); loses.setForeground(Color.WHITE); // Adds the ties label to the panel topLowerPanel.add(ties); ties.setFont(new Font(labelFont.getFontName(), labelFont.getStyle(), 16)); ties.setForeground(Color.WHITE); // Adds the top lower panel to the center of the top panel topPanel.add(topLowerPanel,"Center"); // Adds the top panel to the North of the applet content appletContent.add(topPanel,"North"); //Creates the center panel JPanel centerPanel=new JPanel(); centerPanel.setLayout(new GridLayout(3,3)); // Adss the center panel to the Center of the applet content appletContent.add(centerPanel,"Center"); // Gets the current font style of the score and sets the font size to 16 //Creates the score label an adds it to the bottom of the interface score=new JLabel("Your turn!"); score.setForeground(squareColour); score.setFont(new Font(labelFont.getFontName(), labelFont.getStyle(), 16)); appletContent.add(score,"South"); // create an array to hold references to 9 buttons squares=new JButton[9]; // Instantiate the buttons, store the references // to them in the array, register them with the // listeners, paint them in orange and add to panel for(int i=0;i<9;i++){ squares[i]=new JButton(); squares[i].addActionListener(this); squares[i].setBackground(squareColour); // Gets the current font style of the squares and sets the font size to 36 Font currentFont = squares[i].getFont(); squares[i].setFont(new Font(currentFont.getFontName(), currentFont.getStyle(), 36)); centerPanel.add(squares[i]); // Create the the frame and set it's content pane JFrame frame = new JFrame("TicTacToe"); } } /** * This method will process all action events * @param ActionEvent object */ public void actionPerformed(ActionEvent e) { JButton theButton = (JButton) e.getSource(); // Is this a New Game button? if (theButton ==newGameButton){ for(int i=0;i<9;i++){ squares[i].setEnabled(true); squares[i].setText(""); squares[i].setBackground(squareColour); } emptySquaresLeft=9; score.setText("Your turn!"); newGameButton.setEnabled(false); return; // exit the method here } String winner = ""; // Is this one of the squares? for ( int i=0; i<9; i++ ) { if ( theButton == squares[i] ) { squares[i].setText("X"); squares[i].setEnabled(false); winner = lookForWinner(); if(!"".equals(winner)){ endTheGame(); } else { computerMove(); winner = lookForWinner(); if ( !"".equals(winner)) { endTheGame(); } } break; } } // end loop if ( winner.equals("X") ) { score.setText("You won!"); // increment win counter gamesWon+=1; wins.setText(" Won: " +gamesWon); } else if (winner.equals("O")){ score.setText("You lost!"); // increment lost counter gamesLost+=1; loses.setText(" Lost: "+gamesLost); } else if (winner.equals("T")){ score.setText("It's a tie!"); // increment tie counter gamesTied+=1; ties.setText(" Ties: " +gamesTied); } } // end actionPerformed /** * This method is called after every move to see * if we have a winner. * It checks every row, column and diagonal to find out * three squares with the same label (other than blank) * @return "X", "O", "T" for tie or "" for no winner */ String lookForWinner() { String theWinner = ""; emptySquaresLeft--; if (emptySquaresLeft==0){ return "T"; // it's a tie } // Check the row 1 - array elements 0,1,2 if (!squares[0].getText().equals("") & squares[0].getText().equals(squares[1].getText()) & squares[0].getText().equals(squares[2].getText()) ) { theWinner = squares[0].getText(); highlightWinner(0,1,2); } // Check the row 2 - array elements 3,4,5 if (!squares[3].getText().equals("") & squares[3].getText().equals(squares[4].getText()) & squares[3].getText().equals(squares[5].getText()) ) { theWinner = squares[3].getText(); highlightWinner(3,4,5); } // Check the row 3 - - array elements 6,7,8 if (!squares[6].getText().equals("") & squares[6].getText().equals(squares[7].getText()) & squares[6].getText().equals(squares[8].getText()) ) { theWinner = squares[6].getText(); highlightWinner(6,7,8); } // Check the column 1 - array elements 0,3,6 if (!squares[0].getText().equals("") & squares[0].getText().equals(squares[3].getText()) & squares[0].getText().equals(squares[6].getText()) ) { theWinner = squares[0].getText(); highlightWinner(0,3,6); } // Check the column 2 - array elements 1,4,7 if (!squares[1].getText().equals("") & squares[1].getText().equals(squares[4].getText()) & squares[1].getText().equals(squares[7].getText()) ) { theWinner = squares[1].getText(); highlightWinner(1,4,7); } // Check the column 3 - array elements 2,5,8 if (!squares[2].getText().equals("") & squares[2].getText().equals(squares[5].getText()) & squares[2].getText().equals(squares[8].getText()) ) { theWinner = squares[2].getText(); highlightWinner(2,5,8); } // Check the first diagonal - array elements 0,4,8 if (!squares[0].getText().equals("") & squares[0].getText().equals(squares[4].getText()) & squares[0].getText().equals(squares[8].getText()) ) { theWinner = squares[0].getText(); highlightWinner(0,4,8); } // Check the second diagonal - array elements 2,4,6 if (!squares[2].getText().equals("") & squares[2].getText().equals(squares[4].getText()) & squares[2].getText().equals(squares[6].getText()) ) { theWinner = squares[2].getText(); highlightWinner(2,4,6); } return theWinner; } /** * This method applies a set of rules to find * the best computer's move. If a good move * can't be found, it picks a random square. */ void computerMove() { int selectedSquare; // Computer first tries to find an empty // square next the two squares with "O" to win selectedSquare = findEmptySquare("O"); // if can't find two "O", at least try to stop the // opponent from making 3 in a row by placing // "O" next to 2 "X". if ( selectedSquare == -1 ) selectedSquare = findEmptySquare("X"); // if the selectedSquare is still -1, at least // try to occupy the central square if ( (selectedSquare == -1)&&(squares[4].getText().equals("")) ) selectedSquare=4; // no luck with the central either... // just get a random square if ( selectedSquare == -1 ) selectedSquare = getRandomSquare(); squares[selectedSquare].setText("O"); squares[selectedSquare].setEnabled(false); } /** * This method checks every row, column and diagonal * to see if there are two squares with the same label * and an empty square. * @param give X - for the user, and O for the computer * @return the number of the empty square to use, * or the negative 1 could not find 2 square * with the same label */ int findEmptySquare(String player) { int weight[] = new int[9]; for ( int i = 0; i < 9; i++ ) { if ( squares[i].getText().equals("O") ) weight[i] = -1; else if ( squares[i].getText().equals("X") ) weight[i] = 1; else weight[i] = 0; } int twoWeights = player.equals("O") ? -2 : 2; // See if row 1 has the same 2 squares and a blank if ( weight[0] + weight[1] + weight[2] == twoWeights ) { if ( weight[0] == 0 ) return 0; else if ( weight[1] == 0 ) return 1; else return 2; } // See if row 2 has the same 2 squares and a blank if ( weight[3] +weight[4] + weight[5] == twoWeights ) { if ( weight[3] == 0 ) return 3; else if ( weight[4] == 0 ) return 4; else return 5; } // See if row 3 has the same 2 squares and a blank if ( weight[6] + weight[7] +weight[8] == twoWeights ) { if ( weight[6] == 0 ) return 6; else if ( weight[7] == 0 ) return 7; else return 8; } // See if column 1 has the same 2 squares and a blank if ( weight[0] + weight[3] + weight[6] == twoWeights ) { if ( weight[0] == 0 ) return 0; else if ( weight[3] == 0 ) return 3; else return 6; } // See if column 2 has the same 2 squares and a blank if ( weight[1] +weight[4] + weight[7] == twoWeights ) { if ( weight[1] == 0 ) return 1; else if ( weight[4] == 0 ) return 4; else return 7; } // See if column 3 has the same 2 squares and a blank if ( weight[2] + weight[5] + weight[8] == twoWeights ) { if ( weight[2] == 0 ) return 2; else if ( weight[5] == 0 ) return 5; else return 8; } // See if diagonal 1 has the same 2 squares and a blank if ( weight[0] + weight[4] + weight[8] == twoWeights ) { if ( weight[0] == 0 ) return 0; else if ( weight[4] == 0 ) return 4; else return 8; } // See if diagonal has the same 2 squares and a blank if ( weight[2] + weight[4] + weight[6] == twoWeights ) { if ( weight[2] == 0 ) return 2; else if ( weight[4] == 0 ) return 4; else return 6; } // do not have the same two neighbors return -1; } // end of findEmptySquare /** * This method selects any empty square * @return a randomly selected square number */ int getRandomSquare() { boolean gotEmptySquare = false; int selectedSquare = -1; do { selectedSquare = (int) (Math.random() * 9 ); if (squares[selectedSquare].getText().equals("")) { gotEmptySquare = true; // the looping will end } } while (!gotEmptySquare ); return selectedSquare; } // end getRandomSquare() /** * This method highlights the winning line * @param first square to highlight * @param second square to highlight * @param third square to highlight */ void highlightWinner(int win1, int win2, int win3) { squares[win1].setBackground(Color.CYAN); squares[win2].setBackground(Color.CYAN); squares[win3].setBackground(Color.CYAN); } /** * Disables squares and enable New Game button */ void endTheGame(){ for(int i=0;i<9;i++){ squares[i].setEnabled(false); } // Enable the new game button newGameButton.setEnabled(true); } }