משימה איקס עיגול

המחלקה TicBoard

package proj;  // שימו לב, יש לשנות את שם החבילה לשם החבילה שלכם

import java.awt.Color;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.*;

import java.util.function.IntConsumer;

import javax.swing.*; 

 

public class TicBoard {

    

     int cellRow,cellCol;

     private JFrame frame;

     private JButton [][] btnArr;

     private IntConsumer clickButton;

    

     public TicBoard(int [][] arr){

        

         cellRow = -1;

         cellCol = -1;

         frame = new JFrame();//creating instance of JFrame 

     frame.setSize(arr.length*200+arr.length*3,arr[0].length*200+arr[0].length*4);//400 width and 500 height 

         btnArr = new JButton[arr.length][arr[0].length];

         for (int row=0;row<btnArr.length;row++){

              for (int col=0;col<btnArr[0].length;col++){

                  btnArr[row][col] = new JButton("");

                   btnArr[row][col].setBounds(row*200+row, col*200+col, 200, 200);

                   btnArr[row][col].addActionListener(listener);

                  frame.add(btnArr[row][col]);

              }

         }

         updateBoard(arr);       

         frame.setLayout(null);//using no layout managers 

         frame.setVisible(true);//making the frame visible 

        

     frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

         frame.addWindowListener(new java.awt.event.WindowAdapter() {

              @Override

              public void windowClosing(java.awt.event.WindowEvent windowEvent) {

                  if (JOptionPane.showConfirmDialog(frame,

                            "Are you sure you want to end the game?", "End Game?",

                            JOptionPane.YES_NO_OPTION,

                            JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){

                      

                       System.exit(0);

                  }

              }

         });

        

     }

     public void dansMethod(int x, IntConsumer aMethod) {

        

         clickButton = aMethod;

     }

     public void messagBox(String message,String title){

         JOptionPane.showMessageDialog(null, message,title,1);

     }

     public int getCellRow() {

         return cellRow;

     }

     public int getCellCol() {

         return cellCol;

     }

     public void close() {

         frame.dispose();

     }

     public void setColor(int row,int col,Color color) {

         btnArr[row][col].setBackground(color);        

     }

    

     public void updateBoard(int[][] arr) {

         for (int row=0;row<btnArr.length;row++){

              for (int col=0;col<btnArr[0].length;col++){

                  btnArr[row][col].setFont(new Font("Serif",Font.BOLD,60));

                 

                  if (arr[row][col] == 1){

                       btnArr[row][col].setText("X");

                       btnArr[row][col].setForeground(Color.RED);

                  }

                  else if (arr[row][col] == 2){

                       btnArr[row][col].setText("O");

                       btnArr[row][col].setForeground(Color.black);

                  }

                  else{

                       btnArr[row][col].setBackground(Color.lightGray);

                       btnArr[row][col].setText("");

                  }

              }

         }

        

     }

     private int[] getBtnIdx(JButton b){

         int [] ret = {-1,-1};

         for (int row=0;row<btnArr.length;row++){

              for (int col=0;col<btnArr[0].length;col++){

                  if (btnArr[row][col] == b){

                       ret[0] = row;

                       ret[1] = col;

                       return ret;

                  }

              }

         }

         return ret;

 

     }

     ActionListener listener = new ActionListener() {

         @Override

         public void actionPerformed(ActionEvent e) {

              int [] idx;

              if (e.getSource() instanceof JButton) {

                  idx = getBtnIdx((JButton)e.getSource());

                  String text = "row = "+idx[0]+" col = "+idx[1];

                 

                  cellRow = idx[0];

                  cellCol = idx[1];

                  clickButton.accept(0);

              }

         }

     };

}

 

 

MAIN

public static TicBoard board;

     public static int [][] boardArr;

     public static boolean turn;

    

     public static void main(String[] args) {

         boardArr = new int[3][3];

         turn = false;

         board = new TicBoard(boardArr);

         board.dansMethod(0, x -> buttonClick(x));

 

     }

     public static void buttonClick(int x){

         int row,col;

         col = board.getCellCol();

         row = board.getCellRow();

         if(boardArr[row][col] == 0) {

              if (!turn)

                  boardArr[row][col] = 1;

              else

                  boardArr[row][col] = 2;

              turn = !turn;

         }

         else

              return;

         board.updateBoard(boardArr);

         System.out.println("row = "+row+" col = "+col);

        

         if (checkStatus()) {

              board.close();

         }

     }