2017-12-11 4 views
0

Intro to Programming 클래스의 최종 프로젝트를위한 'Shadow TicTacToe'게임을 만들려고합니다.Tic Tac Toe의 두 번째 보드에서 어떻게 그리겠습니까

이 게임이 작동하는 방식은 사용자가 첫 번째 보드에서 셀을 클릭하면 X 또는 O가 그려지는 것입니다. 그 X도 같은 셀에 그려지지만 두 번째 게임 보드에서는 다른 지점에 그려집니다. 다른 지점으로는 셀 (2,3)이 첫 번째 게임 보드의 가운데 타일에 있고 두 번째 게임 보드의 오른쪽 상단 타일 (또는 다른 타일)에있을 것입니다.

저는 각각 JPanel에서 첫 번째와 두 번째 보드를 만들었습니다. 나는 두 번째 게임 보드의 동일한 셀에있는 첫 번째 게임 보드의 한 셀에있는 토큰에 대한 방법을 만드는 데 어려움을 겪고 있습니다. 각 게임 보드에 대해 하나씩 2 개의 셀을 만들었습니다. 그게 단지 복잡한 작업인지 아니면 실제로 2 개의 셀이 필요한지를 알려주십시오. 나는이 프로젝트의 한 부분에 막 붙어있어 도움이된다. 설명에 대한 최종 프로젝트의

설명 : 각 셀에 인덱스를 추가 할 필요가

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(); 
    } 
} 
+4

시각적 **에서 완전히 분리 된 논리 (예 : MVC 패턴 사용). 게임 로직은 비주얼을 신경 쓰지 않아야합니다.'int [] []'와 같은 2 차원 배열을 사용합니다. 여기서 '1'은 플레이어 '1'이 마크를, '2 '는 다른 플레이어가 마크를 설정하는 것을 의미합니다. ** visual part **에서이 int [] [] 보드를 기반으로 무언가 좋은 것을 그리는 방법이 필요합니다. 그러면 논리는'setMark (int player, int x, int y)'와 같은 메서드를 가질 것이고, 시각적 인 곳에서 (여러분은 MVC의 컨트롤러에서 정확하게) 그 메서드를 올바르게 전달할 것입니다. – Zabuza

답변

0

:

Final Project Description

코드 (지금까지 아직 몇 가지 할 일이 있습니다) 이 인덱스는 보드 1과 보드 2의 셀을 매핑합니다. 첫 번째 보드의 셀과 동일한 인덱스를 갖는 셀의 두 번째 보드에 토큰을 그립니다. 이미지에서 보는 것처럼. 좌표계를 통과하면 느슨한 선로가되어 보드가 평등 해집니다.

+0

또한 버튼이있는 솔루션에서 작업 중이므로 어느 것이 가장 단순한 지 확인할 수 있습니다. –

+0

내 승리 솔루션 중 일부가 작동하지 않는 이유를 알고 계십니까? 나는 이해하지 못한다. 그들은 일해야합니다. – fzk

+0

오, 내가 생각했던 다른 게시물에 있다고 생각했습니다. – fzk