📄 ex_5_26_chessgameof3.java
字号:
// 090320 P163 Ex5.26 三子棋
// 按位置选择结果检查(行、列、正反对角线)
import javax.swing.JOptionPane;
public class Ex_5_26_ChessGameOf3 {
/**
* @param args
*/
public static boolean rowCheck(int row, char[][] Chess){
if(Chess[row-1][0] == Chess[row-1][1] && Chess[row-1][1] == Chess[row-1][2])
return true;
else
return false;
}
public static boolean columnCheck(int column, char[][] Chess){
if(Chess[0][column-1] == Chess[1][column-1] && Chess[1][column-1] == Chess[2][column-1])
return true;
else
return false;
}
public static boolean diagonalCheck(char[][] Chess){
if(Chess[0][0] == Chess[1][1] && Chess[1][1] == Chess[2][2])
return true;
else
return false;
}
public static boolean reverseDiagonalCheck(char[][] Chess){
if(Chess[0][2] == Chess[1][1] && Chess[1][1] == Chess[2][0])
return true;
else
return false;
}
public static void printBoard(char[][] Chess){
String chessString = "";
for(int m = 0; m < 3; m++){
for(int n = 0; n < 3; n++) {
chessString += " "+Chess[m][n];
// System.out.print(Chess[m][n]);
}
chessString += "\n";
// System.out.println();
}
JOptionPane.showMessageDialog(null, chessString, "Ex5.26 3 Chess Game", JOptionPane.INFORMATION_MESSAGE);
}
public static boolean resultCheck(int row, int column, char[][] Chess){
boolean flag = false;
//分三种类型,对输入位置进行检查
if((row + column) % 2 == 1){ //(1,2)(2,1)(2,3)(3,2)
if(rowCheck(row, Chess))
flag = true;
else if(columnCheck(column, Chess))
flag = true;
}
else if(row == column){ //(1,1)(2,2)(3,3)
if(rowCheck(row, Chess))
flag = true;
else if(columnCheck(column, Chess))
flag = true;
else if(diagonalCheck(Chess))
flag = true;
}
else{ //(1,3)(2,2)(3,1)
if(rowCheck(row, Chess))
flag = true;
else if(columnCheck(column, Chess))
flag = true;
else if(reverseDiagonalCheck(Chess))
flag = true;
}
return flag;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] Chess = {{'*','*','*'}, {'*','*','*'}, {'*','*','*'}};
int term;
int row, column;
printBoard(Chess);
for(term = 1; term <= 4; term++){ // 前两个回合跳过检查
String rowString;
if(term%2 == 1){
rowString = JOptionPane.showInputDialog(null, "it's the prior's term(X),enter the row: ", "Ex5.26 3 Chess Game", JOptionPane.QUESTION_MESSAGE);
row = Integer.parseInt(rowString);
String columnString = JOptionPane.showInputDialog(null, "it's the prior's term(X),enter the column: ", "Ex5.26 3 Chess Game", JOptionPane.QUESTION_MESSAGE);
column = Integer.parseInt(columnString);
// if(Chess[row-1][column-1] == '*')
// JOptionPane.showMessageDialog(null, "A used position! Reenter the position.", "Ex5.26 3 Chess Game", JOptionPane.INFORMATION_MESSAGE);
// continue label0;
Chess[row-1][column-1] = 'X';
}
else{
// label1:
rowString = JOptionPane.showInputDialog(null, "it's the posterior's term(O),enter the row: ", "Ex5.26 3 Chess Game", JOptionPane.QUESTION_MESSAGE);
row = Integer.parseInt(rowString);
String columnString = JOptionPane.showInputDialog(null, "it's the posterior's term(O),enter the column: ", "Ex5.26 3 Chess Game", JOptionPane.QUESTION_MESSAGE);
column = Integer.parseInt(columnString);
// if(Chess[row-1][column-1] == '*')
// JOptionPane.showMessageDialog(null, "A used position! Reenter the position.", "Ex5.26 3 Chess Game", JOptionPane.INFORMATION_MESSAGE);
// continue label1;
Chess[row-1][column-1] = 'O';
}
printBoard(Chess);
System.out.println();
}
// System.out.println(term);
for(; term <= 9; term++){
if(term%2 == 1){
String rowString = JOptionPane.showInputDialog(null, "it's the prior's term(X),enter the row: ", "Ex5.26 3 Chess Game", JOptionPane.QUESTION_MESSAGE);
row = Integer.parseInt(rowString);
String columnString = JOptionPane.showInputDialog(null, "it's the prior's term(X),enter the column: ", "Ex5.26 3 Chess Game", JOptionPane.QUESTION_MESSAGE);
column = Integer.parseInt(columnString);
Chess[row-1][column-1] = 'X';
}
else{
String rowString = JOptionPane.showInputDialog(null, "it's the posterior's term(X),enter the row: ", "Ex5.26 3 Chess Game", JOptionPane.QUESTION_MESSAGE);
row = Integer.parseInt(rowString);
String columnString = JOptionPane.showInputDialog(null, "it's the posterior's term(X),enter the column: ", "Ex5.26 3 Chess Game", JOptionPane.QUESTION_MESSAGE);
column = Integer.parseInt(columnString);
Chess[row-1][column-1] = 'O';
}
printBoard(Chess);
System.out.println();
if(resultCheck(row, column, Chess)){
if(term%2 == 1){
JOptionPane.showMessageDialog(null, "Winner is prior!", "Ex5.26 3 Chess Game", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else{
JOptionPane.showMessageDialog(null, "Winner is posterior!", "Ex5.26 3 Chess Game", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}
JOptionPane.showMessageDialog(null, "End in a tie!", "Ex5.26 3 Chess Game", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
// 示例输入 2 2 1 2 2 3 2 1 3 3 1 1 1 3 游戏结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -