此程序中用了五个方法,play1()和play2()分别用于接收用户12的输入然后更新棋盘,deal()用于判断用户的输入是否有效,print()用于在用户操作后打印棋盘格,judge()用于判断是否有某一种棋子连成一行,即判断游戏是否要继续进行。
代码如下:
import java.util.*;
public class tic-tac-toe {
public static void main(String[] args) {
System.out.println("Let's play tic-tac-toe");
// 构建一个3*3的二维数组(初始棋盘)
char[][] checkerboard = new char[3][3];
Scanner in = new Scanner(System.in);
System.out.println("Who want to first?(please enter player1 or player2)");
String first = in.next();
if(first.equals("player1")){
int round = 1;
boolean judge = false;
while(judge == false){
if(round%2 == 1){
checkerboard = play1(checkerboard);
print(checkerboard);
if(judge(checkerboard)==true){
break;
}
round ++;
}else{
checkerboard = play2(checkerboard);
print(checkerboard);
if(judge(checkerboard)==true){
break;
}
round++;
}
}
}else {
int round = 2;
boolean judge = false;
while (judge == false) {
if (round % 2 == 1) {
checkerboard = play1(checkerboard);
print(checkerboard);
if (judge(checkerboard) == true) {
break;
}
round++;
} else {
checkerboard = play2(checkerboard);
print(checkerboard);
if (judge(checkerboard) == true) {
break;
}
round++;
}
}
}
}
// 方法print()用于打印操作后的棋盘格
public static void print(char[][] checkerboard){
System.out.println("-------------");
for(int i = 0;i <= 2; i++){
System.out.print("|");
for(int j = 0;j <= 2; j++){
if(checkerboard[i][j]=='X'||checkerboard[i][j]=='O') {
System.out.print(" "+checkerboard[i][j]+" ");
System.out.print("|");
}else{
System.out.print(" ");
System.out.print("|");
}
}
System.out.println();
System.out.println("-------------");
}
}
// 方法play1用于处理用户1输入的选择,此处设置用户1的棋子为字符'X'
public static char[][] play1(char[][] checkerboard){
Scanner in = new Scanner(System.in);
System.out.print("Player1's move?(Enter the number of rows and columns, separated by spaces.)");
int line = in.nextInt();
int row = in.nextInt();
while(deal(line,row,checkerboard) == false){
System.out.print("Player1's move?(Enter the number of rows and columns, separated by spaces.)");
line = in.nextInt();
row = in.nextInt();
}
checkerboard[line-1][row-1] = 'X';
return checkerboard;
}
// 方法play2用于处理用户1输入的选择,此处设置用户1的棋子为字符'O'
public static char[][] play2(char[][] checkerboard){
Scanner in = new Scanner(System.in);
System.out.print("Player2's move?(Enter the number of rows and columns, separated by spaces.)");
int line = in.nextInt();
int row = in.nextInt();
while(deal(line,row,checkerboard) == false){
System.out.print("Player2's move?(Enter the number of rows and columns, separated by spaces.)");
line = in.nextInt();
row = in.nextInt();
}
checkerboard[line-1][row-1] = 'O';
return checkerboard;
}
// 方法deal用于处理用户的输入,检测输入是否有效
public static boolean deal(int line,int row,char[][] checkerboard){
if(line == 0 || line > 3 || row == 0 || row > 3){ // 输入行列数不在范围内的情况
System.out.println("Please enter a valid number of rows and columns.");
return false;
}else if(checkerboard[line-1][row-1]=='X' || checkerboard[line-1][row-1]=='O'){ // 输入位置已有棋子的情况
System.out.println("There is already a piece, please re-enter your choice.");
return false;
}else{
return true;
}
}
// 方法judge用于判断是否有三个棋子连成一行
public static boolean judge(char[][] checkerboard){
// 判断一行上是否有棋子相连
for (int i = 0; i <= 2; i++){
int continuity_x = 0;
int continuity_o = 0;
for (int j = 0; j <= 2;j++){
if (checkerboard[i][j] == 'X'){
continuity_x++;
}else if (checkerboard[i][j] == 'O'){
continuity_o++;
}
}
if(continuity_x == 3){
System.out.println("Player1 wins!");
return true;
}else if(continuity_o == 3){
System.out.println("Player2 wins!");
return true;
}
}
// 判断一列上是否有棋子相连
for (int j = 0; j <= 2; j++){
int continuity_x = 0;
int continuity_o = 0;
for (int i = 0; i <= 2;i++){
if (checkerboard[i][j] == 'X'){
continuity_x++;
}else if (checkerboard[i][j] == 'O'){
continuity_o++;
}
}
if(continuity_x == 3){
System.out.println("Player1 wins!");
return true;
}else if(continuity_o == 3){
System.out.println("Player2 wins!");
return true;
}
}
// 判断对角线上是否有棋子相连
if(checkerboard[0][0]=='X'&checkerboard[1][1]=='X'&checkerboard[2][2]=='X'){
System.out.println("Player1 wins!");
return true;
}else if(checkerboard[0][0]=='O'&checkerboard[1][1]=='O'&checkerboard[2][2]=='O'){
System.out.println("Player2 wins!");
return true;
}
return false;
}
}
运行结果测试:在测试多种可能后,会发现有一种可能双方的棋子都没有连成一行
所以在judge方法中我添加了对这一类情况的判断,修改后代码如下:
import java.util.*;
public class tic_tac_toe {
public static void main(String[] args) {
System.out.println("Let's play tic-tac-toe");
// 构建一个3*3的二维数组(初始棋盘)
char[][] checkerboard = new char[3][3];
Scanner in = new Scanner(System.in);
System.out.println("Who want to first?(please enter player1 or player2)");
String first = in.next();
if(first.equals("player1")){
int round = 1;
boolean judge = false;
while(judge == false){
if(round%2 == 1){
checkerboard = play1(checkerboard);
print(checkerboard);
if(judge(checkerboard)==true){
break;
}
round ++;
}else{
checkerboard = play2(checkerboard);
print(checkerboard);
if(judge(checkerboard)==true){
break;
}
round++;
}
}
}else {
int round = 2;
boolean judge = false;
while (judge == false) {
if (round % 2 == 1) {
checkerboard = play1(checkerboard);
print(checkerboard);
if (judge(checkerboard) == true) {
break;
}
round++;
} else {
checkerboard = play2(checkerboard);
print(checkerboard);
if (judge(checkerboard) == true) {
break;
}
round++;
}
}
}
}
// 方法print()用于打印操作后的棋盘格
public static void print(char[][] checkerboard){
System.out.println("-------------");
for(int i = 0;i <= 2; i++){
System.out.print("|");
for(int j = 0;j <= 2; j++){
if(checkerboard[i][j]=='X'||checkerboard[i][j]=='O') {
System.out.print(" "+checkerboard[i][j]+" ");
System.out.print("|");
}else{
System.out.print(" ");
System.out.print("|");
}
}
System.out.println();
System.out.println("-------------");
}
}
// 方法play1用于处理用户1输入的选择,此处设置用户1的棋子为字符'X'
public static char[][] play1(char[][] checkerboard){
Scanner in = new Scanner(System.in);
System.out.print("Player1's move?(Enter the number of rows and columns, separated by spaces.)");
int line = in.nextInt();
int row = in.nextInt();
while(deal(line,row,checkerboard) == false){
System.out.print("Player1's move?(Enter the number of rows and columns, separated by spaces.)");
line = in.nextInt();
row = in.nextInt();
}
checkerboard[line-1][row-1] = 'X';
return checkerboard;
}
// 方法play2用于处理用户1输入的选择,此处设置用户1的棋子为字符'O'
public static char[][] play2(char[][] checkerboard){
Scanner in = new Scanner(System.in);
System.out.print("Player2's move?(Enter the number of rows and columns, separated by spaces.)");
int line = in.nextInt();
int row = in.nextInt();
while(deal(line,row,checkerboard) == false){
System.out.print("Player2's move?(Enter the number of rows and columns, separated by spaces.)");
line = in.nextInt();
row = in.nextInt();
}
checkerboard[line-1][row-1] = 'O';
return checkerboard;
}
// 方法deal用于处理用户的输入,检测输入是否有效
public static boolean deal(int line,int row,char[][] checkerboard){
if(line == 0 || line > 3 || row == 0 || row > 3){ // 输入行列数不在范围内的情况
System.out.println("Please enter a valid number of rows and columns.");
return false;
}else if(checkerboard[line-1][row-1]=='X' || checkerboard[line-1][row-1]=='O'){ // 输入位置已有棋子的情况
System.out.println("There is already a piece, please re-enter your choice.");
return false;
}else{
return true;
}
}
// 方法judge用于判断是否有三个棋子连成一行
public static boolean judge(char[][] checkerboard){
// 判断一行上是否有棋子相连
for (int i = 0; i <= 2; i++){
int continuity_x = 0;
int continuity_o = 0;
for (int j = 0; j <= 2;j++){
if (checkerboard[i][j] == 'X'){
continuity_x++;
}else if (checkerboard[i][j] == 'O'){
continuity_o++;
}
}
if(continuity_x == 3){
System.out.println("Player1 wins!");
return true;
}else if(continuity_o == 3){
System.out.println("Player2 wins!");
return true;
}
}
// 判断一列上是否有棋子相连
for (int j = 0; j <= 2; j++){
int continuity_x = 0;
int continuity_o = 0;
for (int i = 0; i <= 2;i++){
if (checkerboard[i][j] == 'X'){
continuity_x++;
}else if (checkerboard[i][j] == 'O'){
continuity_o++;
}
}
if(continuity_x == 3){
System.out.println("Player1 wins!");
return true;
}else if(continuity_o == 3){
System.out.println("Player2 wins!");
return true;
}
}
// 判断对角线上是否有棋子相连
if(checkerboard[0][0]=='X'&checkerboard[1][1]=='X'&checkerboard[2][2]=='X'){
System.out.println("Player1 wins!");
return true;
}else if(checkerboard[0][0]=='O'&checkerboard[1][1]=='O'&checkerboard[2][2]=='O'){
System.out.println("Player2 wins!");
return true;
}
// 判断棋盘是否已满
for (int j = 0; j <= 2; j++){
int num = 0;
for (int i = 0; i <= 2;i++){
if (checkerboard[i][j] == 'X'|| checkerboard[i][j] == 'O'){
num++;
}
}
if(num == 9){
System.out.println("tie");
return true;
}
}
return false;
}
}
运行测试: