import java.awt.*; import javax.swing.*; import java.awt.event.*; /** * This class draws the JPanel, the X's and O's, and keeps track of the players and the score. */ public class Game extends JPanel implements ActionListener { //these variables are for placement of the X's and O's private int zero = 0; private int oneh = 100; private int twoh = 200; private int threeh = 300; private int fifty = 50; private int tf = 25; //these variables are for game outputs private JLabel status; private JLabel nextplayer; private JButton reset; private int score = 0; // if the value is 0, it hasn't been clicked on // if the value is 1, it was clicked by player 1 // if the value is 2, it was clicked by player 2 private int topleft = 0; private int topmiddle = 0; private int topright = 0; private int middleleft = 0; private int middlemiddle = 0; private int middleright = 0; private int bottomleft = 0; private int bottommiddle = 0; private int bottomright = 0; //this boolean tells if the game is over or not. Initially, it is not over. private boolean gameover = false; //this constructor shows which player is next and the status of the game public Game (JLabel nextplayer2, JLabel status2, JButton reset2) { nextplayer = nextplayer2; status = status2; reset = reset2; } /** * This method draws the X's and O's */ public void paintComponent(Graphics g) { super.paintComponent(g); //draw the # diagram g.setColor(Color.black); g.drawLine(oneh, zero, oneh, threeh); // left vertical g.drawLine(twoh, zero, twoh, threeh); // right vertical g.drawLine(zero, oneh, threeh, oneh); // top horizontal g.drawLine(zero, twoh, threeh, twoh); // bottom horizontal //this code tells to draw an X or an O depending on the value of a location //1,3 if (topleft == 1) { //an X at 1,3 g.setColor(Color.black); g.drawLine(zero, zero, oneh, oneh); g.drawLine(zero, oneh, oneh, zero); } else if (topleft == 2) { //an O at 1,3 g.setColor(Color.red); g.fillOval(zero, zero, oneh, oneh); g.setColor(Color.white); g.fillOval(zero+tf, zero+tf, fifty, fifty); } //2,3 if (topmiddle == 1) { //an X at 2,3 g.setColor(Color.black); g.drawLine(oneh, zero, twoh, oneh); g.drawLine(oneh, oneh, twoh, zero); } else if (topmiddle == 2) { //an O at 2,3 g.setColor(Color.red); g.fillOval(oneh, zero, oneh, oneh); g.setColor(Color.white); g.fillOval(oneh+tf, zero+tf, fifty, fifty); } //3,3 if (topright == 1) { //an X at 3,3 g.setColor(Color.black); g.drawLine(twoh, zero, threeh, oneh); g.drawLine(twoh, oneh, threeh, zero); } else if (topright == 2) { //an O at 3,3 g.setColor(Color.red); g.fillOval(twoh, zero, oneh, oneh); g.setColor(Color.white); g.fillOval(twoh+tf, zero+tf, fifty, fifty); } //1,2 if (middleleft == 1) { //an X at 1,2 g.setColor(Color.black); g.drawLine(zero, oneh, oneh, twoh); g.drawLine(zero, twoh, oneh, oneh); } else if (middleleft == 2) { //an O at 1,2 g.setColor(Color.red); g.fillOval(zero, oneh, oneh, oneh); g.setColor(Color.white); g.fillOval(zero+tf, oneh+tf, fifty, fifty); } //2,2 if (middlemiddle == 1) { //an X at 2,2 g.setColor(Color.black); g.drawLine(oneh, oneh, twoh, twoh); g.drawLine(oneh, twoh, twoh, oneh); } else if (middlemiddle == 2) { //an O at 2,2 g.setColor(Color.red); g.fillOval(oneh, oneh, oneh, oneh); g.setColor(Color.white); g.fillOval(oneh+tf, oneh+tf, fifty, fifty); } //3,2 if (middleright == 1) { //an X at 3,2 g.setColor(Color.black); g.drawLine(twoh, oneh, threeh, twoh); g.drawLine(twoh, twoh, threeh, oneh); } else if (middleright == 2) { //an O at 3,2 g.setColor(Color.red); g.fillOval(twoh, oneh, oneh, oneh); g.setColor(Color.white); g.fillOval(twoh+tf, oneh+tf, fifty, fifty); } //1,1 if (bottomleft == 1) { //an X at 1,1 g.setColor(Color.black); g.drawLine(zero, twoh, oneh, threeh); g.drawLine(zero, threeh, oneh, twoh); } else if (bottomleft == 2) { //an O at 1,1 g.setColor(Color.red); g.fillOval(zero, twoh, oneh, oneh); g.setColor(Color.white); g.fillOval(zero+tf, twoh+tf, fifty, fifty); } //2,1 if (bottommiddle == 1) { //an X at 2,1 g.setColor(Color.black); g.drawLine(oneh, twoh, twoh, threeh); g.drawLine(oneh, threeh, twoh, twoh); } else if (bottommiddle == 2) { //an O at 2,1 g.setColor(Color.red); g.fillOval(oneh, twoh, oneh, oneh); g.setColor(Color.white); g.fillOval(oneh+tf, twoh+tf, fifty, fifty); } //3,1 if (bottomright == 1) { //an X at 3,1 g.setColor(Color.black); g.drawLine(twoh, twoh, threeh, threeh); g.drawLine(twoh, threeh, threeh, twoh); } else if (bottomright == 2) { //an O at 3,1 g.setColor(Color.red); g.fillOval(twoh, twoh, oneh, oneh); g.setColor(Color.white); g.fillOval(twoh+tf, twoh+tf, fifty, fifty); } //g.drawString(""+score, 250, 250); //Will display the value of "score" in the JPanel } /** * This method gets the x and y coordinates from the MListener class. * * @param mx4 - x coordinate * @param my4 - y coordinate */ public void click(int mx4, int my4) { if (!gameover) { //this code keeps track of the score and if a player won //1,3 if (0 < mx4 && mx4 < 100 && 0 < my4 && my4 < 100) { if (topleft == 0) { score = score + 1; if (score%2 != 0) { topleft = 1; } else { topleft = 2; } } } //2,3 if (100 < mx4 && mx4 < 200 && 0 < my4 && my4 < 100) { if (topmiddle == 0) { score = score + 1; if (score%2 != 0) { topmiddle = 1; } else { topmiddle = 2; } } } //3,3 if (200 < mx4 && mx4 < 300 && 0 < my4 && my4 < 100) { if (topright == 0) { score = score + 1; if (score%2 != 0) { topright = 1; } else { topright = 2; } } } //1,2 if (0 < mx4 && mx4 < 100 && 100 < my4 && my4 < 200) { if (middleleft == 0) { score = score + 1; if (score%2 != 0) { middleleft = 1; } else { middleleft = 2; } } } //2,2 if (100 < mx4 && mx4 < 200 && 100 < my4 && my4 < 200) { if (middlemiddle == 0) { score = score + 1; if (score%2 != 0) { middlemiddle = 1; } else { middlemiddle = 2; } } } //3,2 if (200 < mx4 && mx4 < 300 && 100 < my4 && my4 < 200) { if (middleright == 0) { score = score + 1; if (score%2 != 0) { middleright = 1; } else { middleright = 2; } } } //1,1 if (0 < mx4 && mx4 < 100 && 200 < my4 && my4 < 300) { if (bottomleft == 0) { score = score + 1; if (score%2 != 0) { bottomleft = 1; } else { bottomleft = 2; } } } //2,1 if (100 < mx4 && mx4 < 200 && 200 < my4 && my4 < 300) { if (bottommiddle == 0) { score = score + 1; if (score%2 != 0) { bottommiddle = 1; } else { bottommiddle = 2; } } } //3,1 if (200 < mx4 && mx4 < 300 && 200 < my4 && my4 < 300) { if (bottomright == 0) { score = score + 1; if (score%2 != 0) { bottomright = 1; } else { bottomright = 2; } } } } if (score%2 == 0) { nextplayer.setText("Player 1 (X)"); } else { nextplayer.setText("Player 2 (O)"); } //check to see if player 1 won //you can't win without clicking at least 5 times combined //the large if statements check for wins horizontally, vertically, and diagonally if (score >= 5 && score <= 9) { if ( (topleft == 1 && topmiddle == 1 && topright == 1) || (middleleft == 1 && middlemiddle == 1 && middleright == 1) || (bottomleft == 1 && bottommiddle == 1 && bottomright == 1) || (topleft == 1 && middleleft == 1 && bottomleft == 1) || (topmiddle == 1 && middlemiddle == 1 && bottommiddle == 1) || (topright == 1 && middleright == 1 && bottomright == 1) || (topleft == 1 && middlemiddle == 1 && bottomright == 1) || (topright == 1 && middlemiddle == 1 && bottomleft == 1) ) { status.setText("Player 1 is the winner!"); gameover = true; } //check to see if player 2 won else if ( (topleft == 2 && topmiddle == 2 && topright == 2) || (middleleft == 2 && middlemiddle == 2 && middleright == 2) || (bottomleft == 2 && bottommiddle == 2 && bottomright == 2) || (topleft == 2 && middleleft == 2 && bottomleft == 2) || (topmiddle == 2 && middlemiddle == 2 && bottommiddle == 2) || (topright == 2 && middleright == 2 && bottomright == 2) || (topleft == 2 && middlemiddle == 2 && bottomright == 2) || (topright == 2 && middlemiddle == 2 && bottomleft == 2) ) { status.setText("Player 2 is the winner!"); gameover = true; } else if (score == 9 && status.getText() != "Player 1 is the winner!" && status.getText() != "Player 2 is the winner!") { status.setText("Draw!"); } } repaint(); } public void actionPerformed(ActionEvent event){ gameover = false; score = 0; topleft = 0; topmiddle = 0; topright = 0; middleleft = 0; middlemiddle = 0; middleright = 0; bottomleft = 0; bottommiddle = 0; bottomright = 0; status.setText("Game hasn't ended yet!"); nextplayer.setText("Player 1 (X)"); repaint(); } }