Search code examples
javaswingjframecell

How Would I Draw on Second Board For Tic Tac Toe


I am trying to create a 'Shadow TicTacToe' game for a final project for an Intro to Programming class.

The way this game is supposed to work is that a user clicks a cell in the first board and an X or O will be drawn. That X will also be drawn in the same cell, but different spot on the second game board. By different spot I mean if cell(2,3) is on the center tile on the first game board that same cell would be on the top right tile(or on some other tile) on the second game board.

I have created the first and second board, each in a JPanel. I am just having trouble creating a way for whatever token is in one cell on the first game board to be in the same cell on the second game board. I created 2 cells, one for each game board, let me know if that is just complicating things or I really do need 2 cells. I am just stuck on this one part of the project and any help is appreciated.

Description of final project for clarification:

Final Project Description

Code(so far, still have a few things to do):

    import javax.swing.*;
   import java.awt.event.*;
   import java.awt.*;
   import javax.swing.border.LineBorder;
   import java.util.Random;
   public class Almost extends JFrame{
   // Initialzies cell array which will be the cells in grid 1
   private Cell[][] cell = new Cell[3][3];
   // Initalizes the cells array which will be the cells in grid 2
   private Cells[][] cells = new Cells[3][3];
   // Creates a boolean array
   private boolean t[][] = new boolean[3][3];
   // Intializes btnReset which will be a button that resets the game
   private JButton btnReset;
   //Initializes Random
   Random rand = new Random();
   // Initializes variables which will be used to create random ints
   int f;
   int g;
   // Initializes JlblStatus
   private JLabel jlblStatus = new JLabel(" ");
   // Initializes the labels of the indexes for the cells on grid 1
   private JLabel jlblIndex1 = new JLabel("(" + 0 + "," + 0 + ")");
   private JLabel jlblIndex2 = new JLabel("(" + 0 + "," + 1 + ")");
   private JLabel jlblIndex3 = new JLabel("(" + 0 + "," + 2 + ")");
   private JLabel jlblIndex4 = new JLabel("(" + 1 + "," + 0 + ")");
   private JLabel jlblIndex5 = new JLabel("(" + 1 + "," + 1 + ")");
   private JLabel jlblIndex6 = new JLabel("(" + 1 + "," + 2 + ")");
   private JLabel jlblIndex7 = new JLabel("(" + 2 + "," + 0 + ")");
   private JLabel jlblIndex8 = new JLabel("(" + 2 + "," + 1 + ")");
   private JLabel jlblIndex9 = new JLabel("(" + 2 + "," + 2 + ")");

   // Initializes the labels of the indexes of the cells on grid 2
   private JLabel jlblCells1 = new JLabel("(" + 0 + "," + 0 + ")");
   private JLabel jlblCells2 = new JLabel("(" + 0 + "," + 1 + ")");
   private JLabel jlblCells3 = new JLabel("(" + 0 + "," + 2 + ")");
   private JLabel jlblCells4 = new JLabel("(" + 1 + "," + 0 + ")");
   private JLabel jlblCells5 = new JLabel("(" + 1 + "," + 1 + ")");
   private JLabel jlblCells6 = new JLabel("(" + 1 + "," + 2 + ")");
   private JLabel jlblCells7 = new JLabel("(" + 2 + "," + 0 + ")");
   private JLabel jlblCells8 = new JLabel("(" + 2 + "," + 1 + ")");
   private JLabel jlblCells9 = new JLabel("(" + 2 + "," + 2 + ")");

   private char currentPlayer = ' ';
   // Method that builds the JFrame and JPanels

   public Almost(){
      // Title of the JFrame
      JFrame frame = new JFrame("Shadow Tic Tac Toe Game");
      // Makes the JFrame full screen
      frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
      // If x button is clicked than the JFrame closes
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // Initializes JPanel1
      JPanel panel1 = new JPanel();
      // Initializes JPanel2
      JPanel panel2 = new JPanel();
      // Adds panel1 which will hold the first TicTacToe game.
      panel1.setLayout(new GridLayout(3,3,0,0));
      for(int d = 0; d < 3; d++){
         for(int c = 0; c < 3; c++){
            panel1.add(cell[d][c] = new Cell());
            // Sets size of the cells in JPanel1
            cell[d][c].setPreferredSize(new Dimension(250,250));
         }
      }
      // Visibly labels grid 1 cells with their cell index
      cell[0][0].add(jlblIndex1);
      cell[0][1].add(jlblIndex2);
      cell[0][2].add(jlblIndex3);
      cell[1][0].add(jlblIndex4);
      cell[1][1].add(jlblIndex5);
      cell[1][2].add(jlblIndex6);
      cell[2][0].add(jlblIndex7);
      cell[2][1].add(jlblIndex8);
      cell[2][2].add(jlblIndex9);      


      // Puts the cells on grid 2          
      panel2.setLayout(new GridLayout(3,3,0,0));
      int n = 0;
      while(n < 9){
      f=rand.nextInt(3);
      g=rand.nextInt(3);
      // If the index f & g are not already in boolean array t then teh while loop will run
      // It will also place the cell index f & g on grid 2
      while(t[f][g] == false){
         t[f][g] = true;
         panel2.add(cells[f][g] = new Cells());
         cells[f][g].setPreferredSize(new Dimension(250,250));
         n++;
        }
       }
       // Visibly labels grid 2 cells with their cell index
       cells[0][0].add(jlblCells1);
       cells[0][1].add(jlblCells2);
       cells[0][2].add(jlblCells3);
       cells[1][0].add(jlblCells4);
       cells[1][1].add(jlblCells5);
       cells[1][2].add(jlblCells6);
       cells[2][0].add(jlblCells7);
       cells[2][1].add(jlblCells8);
       cells[2][2].add(jlblCells9);

      // Adds Panel1 to the JFrame
      frame.add(panel1, BorderLayout.WEST);
      // Adds Panel2 to the JFrame
      frame.add(panel2, BorderLayout.EAST);
      // Updates the status of the game here (win/lose/draw/whose turn it is
      frame.add(jlblStatus, BorderLayout.SOUTH);
      // Sets size of the message area at the bottom of the frame
      jlblStatus.setPreferredSize(new Dimension(100,100));
      // Adds a Reset button to the JFrame
      buildReset();
      // Puts the reset button on the top of the JFrame
      frame.add(btnReset, BorderLayout.NORTH);
      // Shows the Instructions of the game
      Instructions();
      // Calls method Chose() which allows the player to chose which token they will play as
      Chose();
      frame.pack();
      // Sets it so the JFrame is visible to the user
      frame.setVisible(true);
   }  
   // Method that creates the Instructions for the game. Will be shown to the user prior to the user picking his token
   public void Instructions(){
      JOptionPane.showMessageDialog(null,"INSTRUCTIONS" + "This game is called a 'Shadow Tic Tac Toe Game'. In this game there will be two Tic Tac Toe game boards, though only one is playable. \nBut you can win on either board. Lets say you place your token on the center tile at cell(2,3). \nAn X will be drawn on that spot on board 1 and on a randomized spot on the second game board at cell(2,3). \nYou will be able to see the cell indexes before you click on a cell so you can form a strategy");
   }
   // Method that lets the user chose his/her token
   public void Chose(){
      int f = 2;
      // While f == 2 the loop will run
      while(f == 2){
         String input = JOptionPane.showInputDialog("CHOSE" + "\nPlease select your token. \nPress 1 for X and 2 for O.");
         // Reads in the user input. Input put into a variable called pawn
         int pawn = Integer.parseInt(input);
         // If user input 1 his/her token will be X. F will equal 3 so the loop does not run again
         if(input.equals("1")){
            currentPlayer = 'X';
            f = 3;
         // If user input 2 his/her token will be O. F will equal 3 so the loop does not run again
         }else if(input.equals("2")){
            currentPlayer = 'O';
            f = 3;
         // If user does not enter in either a 1 or 2 an error message will appear. f wil equal 2 so the loop runs again and asks the user to chose his/her token again
         }else{
            JOptionPane.showMessageDialog(null,"ERROR INVALID RESPONSE");
            f = 2;
      }
     }
   }
   // Builds the button btnReset
   public void buildReset(){
      btnReset = new JButton("Reset");
      btnReset.addActionListener(new BtnListener());     
   }
   // If btnReset if clicked it resets the Almost() method and starts the game over 
   private class BtnListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
         Almost b = new Almost();
      }
   }
   // The class for the cells on grid 2
   // Responsible for setting the tokens for grid 2 and drawing the tokens in the cells in grid 2
   public class Cells extends JPanel{
      // Initalizes tok and initailly sets it to nothing
      private char tok = ' ';
      public Cells(){
         // Sets the border for the cells to the color black
         setBorder(new LineBorder(Color.black,1));
      }
      // Sets tok to char d, which will be currentPlayer
      public void setTok(char d){
         tok = d;
         repaint();
      }
      // Responsible for getting the tokens in cells in grid 2. Mainly used in Full2() and Won2()
      public char getTok(){
         return tok;
      }
      // Paints the token in the cells in grid 2
      protected void paintComponent(Graphics g){
         super.paintComponent(g);
         // If tok equals X it will draw an X
         if(tok == 'X'){
            g.drawLine(10,10,getWidth() - 10, getHeight()-10);
            g.drawLine(getWidth()-10,10,10,getHeight()-10);
         // If tok equals O an oval will be drawn
         }else if (tok == 'O'){
            g.drawOval(10,10,getWidth()-20, getHeight()-20);
         }
      }
   }
    // Class for the cells in grid 1
    public class Cell extends JPanel{
      private char token = ' ';
      String Id ="";
      //write getter setter for it  and do same code as you have done for to
      public void setToken(char c){
         token = c;
         repaint();
      }
      // When called upon it gets the token in the cells specified and return them
      // Called in Full1() and Won1()
      public char getToken(){
         return token;
      }
      public Cell(){ 
         // Sets the border for the cells to the color black
         setBorder(new LineBorder(Color.black, 1));
         addMouseListener(new MyMouseListener());
      }
      // Paints the token in the cell on grid 1 that was clicked
       protected void paintComponent(Graphics g) {
         super.paintComponent(g);
         // If token equals X it paints an X
         if (token == 'X') {
           g.drawLine(10,10, getWidth() - 10, getHeight() - 10);
           g.drawLine(getWidth() - 10,10,10, getHeight() - 10);
         }
         // If token equals O it will draw an oval
         else if (token == 'O') {
           g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
         }
      } 
      private class MyMouseListener extends MouseAdapter{
         public void mouseClicked(MouseEvent e){
        // If cell is empty and game is not over
        if (token == ' ' && currentPlayer != ' ') {
          // Sets the token in the cell that was clicked in grid 1
          setToken(currentPlayer);
          // Sets the token in the corresponding cell in grid 2
          PlaceGrid2();
                // Win Solutions
                  if (Won1(currentPlayer)){
                     jlblStatus.setText(currentPlayer + " wins on board1!");
                     Change();

                  }else if (Won2(currentPlayer)){
                     jlblStatus.setText(" Player X wins on board 2!");

               }else{
               Change();
               }
             }
         }
      }  
   }
   // Changes the players turn
   public void Change(){
      if(currentPlayer == 'X'){
                     currentPlayer = 'O';
                   }else{
                     currentPlayer = 'X';
                   }

                  jlblStatus.setText("It is " + currentPlayer + " 's turn"); 
   }
   // Places token on grid 2
   public void PlaceGrid2(){
      for(int c = 0; c < 3; c++){
         for(int d = 0; d < 3; d++){
            if(cell[c][d].getToken() == 'X'){
               cells[c][d].setTok('X');
            }
            if(cell[c][d].getToken() == 'O'){
               cells[c][d].setTok('O');
            }
         }
       }
   }
   // Checks to see if grid 1 is full
   public boolean Full1(){
      for(int d = 0; d < 3; d++)
         for(int c = 0; c < 3; c++)
            if(cell[d][c].getToken() == ' ')
               return false;
            return true;

    }
    // Checks to see if grid 2 is full
    public boolean Full2(){
      for(int d = 0; d < 3; d++)
         for(int c = 0; c < 3; c++)
            if(cells[d][c].getTok() == ' ')
               return false;

            return true;


    }
   // Checks to see if anyone won on grid 1
   public boolean Won1(char token){
      for(int d = 0; d < 3; d++){
         if(cell[d][0].getToken() == token && cell[d][1].getToken() == token && cell[d][2].getToken() == token){
            return true;
         }
      }
      for(int c = 0; c < 3; c++){
         if(cell[0][c].getToken() == token && cell[1][c].getToken() == token && cell[2][c].getToken() == token){
            return true;
         }
       }
      if(cell[0][0].getToken() == token && cell[1][1].getToken() == token && cell[2][2].getToken() == token){
         return true;
      }
      if(cell[0][2].getToken() == token && cell[1][1].getToken() == token && cell[2][0].getToken() == token){
         return true;
      }
      return false;
   }
   // Checks to see if anyone won on grid 2
   public boolean Won2(char tok){
      for(int d = 0; d < 3; d++){
         if(cells[d][0].getTok() == tok && cells[d][1].getTok() == tok && cells[d][2].getTok() == tok){
            return true;
         }
      }
      for(int c = 0; c < 3; c++){
         if(cells[0][c].getTok() == tok && cells[1][c].getTok() == tok && cells[2][c].getTok() == tok){
            return true;
         }
      }
      if(cells[0][0].getTok() == tok && cells[1][1].getTok() == tok && cells[2][2].getTok() == tok){
         return true;
      }
      if(cells[0][2].getTok() == tok && cells[1][1].getTok() == tok && cells[2][0].getTok() == tok){
         return true;
      }
      return false;
    }
     // Main method
     public static void main(String [] args){
      new Almost();
   }
}

Solution

  • You need to add an index to each Cell and that index will map your cells from board 1 and board 2. You will draw the token, in the second board, on the Cell that has the same index as the Cell on the first Board. Just as you see in your image. Because if you go through the coordinates system you loose track and the boards will be equal.