?? tictactoeboard.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 + -