⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tictactoeboard.java

📁 tictactoc 井字游戏
💻 JAVA
字号:
package NetworkTicTacToe.client;

import game.basic.XYLocation;
import game.adversary.*;
import java.awt.*;
import javax.swing.*;

public class TicTacToeBoard {
    private String[] topRow =    {" "," "," "};
    private String[] midRow =    {" "," "," "};
    private String[] bottomRow = {" "," "," "};

    private String[] whichRow(int rowNumber) {
        String[] whichRow = null;
        if (rowNumber ==0) {
            whichRow = topRow;
        }
        else if (rowNumber ==1) {
            whichRow = midRow;
        }
        else if (rowNumber ==2) {
            whichRow = bottomRow;
        }
        return whichRow;
    }

    public boolean isEmpty(int row, int col) {
        String[] whichRow = whichRow(row);

        if (whichRow[col]==" ") {
            return true;
        }
        else {
            return false;
        }
    }

    public void markX(int row,int col) {
        String[] whichRow = null;
        whichRow = whichRow(row);
        whichRow[col]="X";
    }


    public void markO(int row,int col) {
        String[] whichRow = null;
        whichRow = whichRow(row);
        whichRow[col]="O";
    }

    public boolean isAnyRowComplete() {
        boolean retVal = false;
        for (int i=0;i<3;i++) {
            String[] whichRow = whichRow(i);
            if ((whichRow[0]!= " " ) && 
                (whichRow[0]==whichRow[1]) && 
                (whichRow[1]==whichRow[2]) ) {

                retVal = true;
                break;
            }
        }

        return retVal;
    }

    public boolean isAnyColumnComplete() {
        boolean retVal = false;
        for (int i=0;i<3;i++) {
            if ((topRow[i]!= " " ) && 
                (topRow[i]==midRow[i]) && 
                (midRow[i]==bottomRow[i]) ) {

                retVal = true;
                break;
            }
        }

        return retVal;
    }

    public boolean isAnyDiagonalComplete() {
        boolean retVal = false;
        if ((topRow[0]!= " ") && 
            (topRow[0]== midRow[1]) && 
            (midRow[1]==bottomRow[2])) {

            retVal =true;
        }
        else if((topRow[2]!= " ") && 
                (topRow[2]== midRow[1]) &&
                (midRow[1]==bottomRow[0])) {

             retVal =true ;
        }

        return retVal;
    }

    public boolean lineThroughBoard() {
        if ((isAnyRowComplete()) || 
            (isAnyColumnComplete()) || 
            (isAnyDiagonalComplete())) {

            return true;
        }
        else {
            return false;
        }
    }

    public String getValue(int row, int col) {
        String[] whichRow = whichRow(row);
        return whichRow[col];
    }

     private void setValue(int row, int col,String val) {
        String[] whichRow = whichRow(row);
        whichRow[col]=val;
    }

    public void  print(Square[][] board) {
        for (int i=0;i<3;i++) {
            for (int j=0;j<3;j++){
                String value = getValue(i,j);
                String printValue;

                board[i][j].setMark(value.charAt(0));
                board[i][j].repaint();
            }
        }
    }

    public TicTacToeBoard cloneBoard() {
        TicTacToeBoard newBoard =  new TicTacToeBoard();
        for (int i=0;i<3;i++) {
            for (int j=0;j<3;j++) {
                 String s = getValue(i,j);
                 newBoard.setValue(i,j,s);
            }
        }
        return newBoard;
    }

    public int getFilledPositions() {
        int retVal = 0;
        for (int i=0;i<3;i++) {
             for (int j=0;j<3;j++) {
                 if (!(isEmpty(i,j))) {
                     retVal++;
                 }
             }
        }
        return retVal;
    }

    public boolean equals(Object anObj) {
        boolean retVal = true;
        TicTacToeBoard anotherBoard = (TicTacToeBoard) anObj;
        boolean secondBreak = false;
         for (int i=0;i<3;i++){
             for (int j=0;j<3;j++) {
                 if (anotherBoard.getValue(i,j) != getValue(i,j)) {
                     retVal = false;
                     secondBreak = true;
                     break;
                 }
             }
             if (secondBreak == false) {
                 break;
             }
         }
       return retVal;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -