public class XO {
private String name;
private static int turn = 1;
public XO(String name) {
if (2 % turn == 0) {
turn++;
name = "X";
}
else {
turn++;
name = "O";
}
}
//set method
public void setName(String name) {
this.name = name;
}
//public void setTurn(int turn){
//this.turn=0;
//}
//get method
public String getName() {
return name;
}
public int getTurn() {
return turn;
}
public boolean equals(XO obj) {
return name == obj.getName();
}
public String toString() {
return "You are " + name;
}
}
import java.util. * ;
public class TicTacToeBoard extends Board {
private int turnCnt;
private XO[][] board;
public TicTacToeBoard(int rows, int columns) {
super(rows, columns);
turnCnt = 0;
this.board = new XO[rows][columns];
}
//set method
public void setBoard(XO[][] board) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
board[i][j] = board.getName();
}
}
}
//get methods
public XO[][] getBoard() {
return board;
}
public int getTurnCnt() {
return turnCnt;
}
//print board
public void printBoard() {
for (int i = 0; i < board.length; i++) {
System.out.println("\n");
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j] + " ");
}
}
}
//add X or O
public boolean add(int r, int c) {
XO[][] j = new XO[r][c];
if (r > 2 && c > 2 || this.board.equals(j)) return false;
else
//setBoard(j);
return true;
}
//check win
public boolean win(XO a, XO b) {
//player 1 wins
if (board[0][0] == a && board[1][0] == a && board[2][0] == a) {
return true;
}
if (board[0][1] == a && board[1][1] == a && board[2][1] == a) {
return true;
}
if (board[0][2] == a && board[1][2] == a && board[2][2] == a) {
return true;
}
if (board[0][0] == a && board[0][1] == a && board[0][2] == a) {
return true;
}
if (board[1][0] == a && board[1][1] == a && board[1][2] == a) {
return true;
}
if (board[2][0] == a && board[2][1] == a && board[2][2] == a) {
return true;
}
if (board[0][0] == a && board[1][1] == a && board[2][2] == a) {
return true;
}
if (board[2][0] == a && board[1][1] == a && board[0][2] == a) {
return true;
}
//now player 2 wins
if (board[0][0] == b && board[1][0] == b && board[2][0] == b) {
return true;
}
if (board[0][1] == b && board[1][1] == b && board[2][1] == b) {
return true;
}
if (board[0][2] == b && board[1][2] == b && board[2][2] == b) {
return true;
}
if (board[0][0] == b && board[0][1] == b && board[0][2] == b) {
return true;
}
if (board[1][0] == b && board[1][1] == b && board[1][2] == b) {
return true;
}
if (board[2][0] == b && board[2][1] == b && board[2][2] == b) {
return true;
}
if (board[0][0] == b && board[1][1] == b && board[2][2] == b) {
return true;
}
if (board[2][0] == b && board[1][1] == b && board[0][2] == b) {
return true;
}
else return false;
}
public String toString() {
return board[0][0] + "\t" + board[1][0] + "\t" + board[2][0] + "\n" + board[0][1] + "\t" + board[1][1] + "\t" + board[1][2] + "\n" + board[0][2] + "\t" + board[1][2] + "\t" + board[2][2];
}
}
嗨,我正在做一個有關創造性的戰術遊戲在Java中的任務。這些是我的代碼。該指南指出,XO類應該保存關於「X」或「O」對象的信息。將對象添加到二維數組
該類應該有兩個屬性:一個名稱(它是一個字符串「X」或「O」)和一個靜態int變量,它被設置爲0或1(它應該被初始化爲1)。您將使用此變量來確定在創建對象時要爲其指定的名稱(例如,如果它是1,請將名稱設置爲「X」)。
我不知道如何使用轉向變量,我知道我應該將2個對象的X O傳遞給2d數組,但我對如何將對象傳遞到2d數組有點模糊。同時,我想先打印板就像一個空白的矩陣,但它始終是 [空空空, 空空空 空空空]
任何幫助將.Thanks很多感謝!
我不知道如何將x o對象添加到電路板。是這樣的public boolean add(int r,int c){ \t \t XO [] [] player = new XO [r] [c]; \t \t if(r> 3 && c> 3) \t \t \t return false; \t \t else \t \t \t xo [r] [c] = player; \t \t \t // setBoard(j); \t \t \t turnCnt ++; \t \t \t return true; \t \t \t} – Ratata4