public class XOGame {
//打印棋盘 给定一个二维数组 (行列相同)
// -1则为 X 0表示未落子* 1表示O
public static void bordDisp(int[][] arr){
for (int i= 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if(arr[i][j] == -1) {
System.out.print("X");
}else if (arr[i][j] == 1) {
System.out.print("O");
}else {
System.out.print("*");
}
System.out.print(" ");
}
System.out.println();
}
}
//让用户输入 (落子)
// player表示当前哪个玩家
// 返回一个数组 记录本次输入的坐标(行 和列)
public static int[] playerDrop(Scanner scanner, int player, int[][] bord){
int[] arr = new int[2];
while(true){
System.out.println("请玩家"+player+"落子");
System.out.print("x:");
arr[0] = Integer.parseInt(scanner.nextLine());
System.out.print("y:");
arr[1] = Integer.parseInt(scanner.nextLine());
//如果检测成功则return
if(checkPosition(arr, bord)){
return arr;
}
}
}
//检查落子是否合法
//int[] arr 行列数组 int[][] bord当前棋盘
//返回true false
public static boolean checkPosition(int[] position,int[][] bord){
int x = position[0]; //行数
int y = position[1]; //列数
//行数 列数 1~3
if (!(x>=1 && x<=3 && y>=1 && y<=3) ) {
System.out.println("位置超范围,请重输!");
return false;
}
//落子的位置 必须是*
// bord[x-1][y-1] == 0
if (bord[x-1][y-1] != 0) {
System.out.println("该位置以经有棋子!");
return false;
}
return true;
}
//修改棋盘
//参数: int[][] bord棋盘, int[] positon, int player
public static void bordChange(int[][] bord, int[] position, int player){
//获取坐标
int x = position[0]-1;
int y = position[1]-1;
//根据坐标 和 当前用户修改棋盘
bord[x][y] = (player==1)?-1:1;
}
//棋局是否结束
//参数: int[][] bord
//返回: true false
public static boolean isFinish(int[][] bord){
//玩家1赢了
if(
(bord[0][0]==-1 && bord[0][1]==-1 && bord[0][2]==-1) ||
(bord[1][0]==-1 && bord[1][1]==-1 && bord[1][2]==-1) ||
(bord[2][0]==-1 && bord[2][1]==-1 && bord[2][2]==-1) ||
(bord[0][0]==-1 && bord[1][0]==-1 && bord[2][0]==-1) ||
(bord[0][1]==-1 && bord[1][1]==-1 && bord[2][1]==-1) ||
(bord[0][2]==-1 && bord[1][2]==-1 && bord[2][2]==-1) ||
(bord[0][0]==-1 && bord[1][1]==-1 && bord[2][2]==-1) ||
(bord[0][2]==-1 && bord[1][1]==-1 && bord[2][0]==-1)
){
System.out.println("玩家1获得胜利! Game Over");
return true;
//玩家2赢了
}else if(
(bord[0][0]==1 && bord[0][1]==1 && bord[0][2]==1) ||
(bord[1][0]==1 && bord[1][1]==1 && bord[1][2]==1) ||
(bord[2][0]==1 && bord[2][1]==1 && bord[2][2]==1) ||
(bord[0][0]==1 && bord[1][0]==1 && bord[2][0]==1) ||
(bord[0][1]==1 && bord[1][1]==1 && bord[2][1]==1) ||
(bord[0][2]==1 && bord[1][2]==1 && bord[2][2]==1) ||
(bord[0][0]==1 && bord[1][1]==1 && bord[2][2]==1) ||
(bord[0][2]==1 && bord[1][1]==1 && bord[2][0]==1)
){
System.out.println("玩家2获得胜利! Game Over");
return true;
//没下完
}else if (findZero(bord)) {
return false;
//和局
}else{
System.out.println("和局");
return true;
}
}
//判定一个int[][] arr 中有没有0
public static boolean findZero(int[][] bord){
for (int i = 0; i < bord.length; i++) {
for (int j = 0; j < bord.length; j++) {
if (bord[i][j] == 0) {
return true;
}
}
}
return false;
}
int[] a={1,2,3,4};
static public void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//作业:若用char[][] 来记录棋盘 修改代码
int n = 3; //棋盘的行列数 (正方的)
int[][] bord = new int[n][n]; //记录棋盘现状
int player = 1;//记录当前是哪个玩家 1表示玩家1 2表示玩家2
while(true){
//当前棋盘
bordDisp(bord);
//落子操作 直到成功
int[] position =playerDrop(scanner, player,bord);
//更新棋盘
bordChange(bord, position, player);
//判定棋局状况 (有人赢了或和局 则结束整个循环)
if (isFinish(bord)) {
break;
}
//切换玩家
player = player==1?2:1;
System.out.println("-------------------\n");
}
}
}
//打印棋盘 给定一个二维数组 (行列相同)
// -1则为 X 0表示未落子* 1表示O
public static void bordDisp(int[][] arr){
for (int i= 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if(arr[i][j] == -1) {
System.out.print("X");
}else if (arr[i][j] == 1) {
System.out.print("O");
}else {
System.out.print("*");
}
System.out.print(" ");
}
System.out.println();
}
}
//让用户输入 (落子)
// player表示当前哪个玩家
// 返回一个数组 记录本次输入的坐标(行 和列)
public static int[] playerDrop(Scanner scanner, int player, int[][] bord){
int[] arr = new int[2];
while(true){
System.out.println("请玩家"+player+"落子");
System.out.print("x:");
arr[0] = Integer.parseInt(scanner.nextLine());
System.out.print("y:");
arr[1] = Integer.parseInt(scanner.nextLine());
//如果检测成功则return
if(checkPosition(arr, bord)){
return arr;
}
}
}
//检查落子是否合法
//int[] arr 行列数组 int[][] bord当前棋盘
//返回true false
public static boolean checkPosition(int[] position,int[][] bord){
int x = position[0]; //行数
int y = position[1]; //列数
//行数 列数 1~3
if (!(x>=1 && x<=3 && y>=1 && y<=3) ) {
System.out.println("位置超范围,请重输!");
return false;
}
//落子的位置 必须是*
// bord[x-1][y-1] == 0
if (bord[x-1][y-1] != 0) {
System.out.println("该位置以经有棋子!");
return false;
}
return true;
}
//修改棋盘
//参数: int[][] bord棋盘, int[] positon, int player
public static void bordChange(int[][] bord, int[] position, int player){
//获取坐标
int x = position[0]-1;
int y = position[1]-1;
//根据坐标 和 当前用户修改棋盘
bord[x][y] = (player==1)?-1:1;
}
//棋局是否结束
//参数: int[][] bord
//返回: true false
public static boolean isFinish(int[][] bord){
//玩家1赢了
if(
(bord[0][0]==-1 && bord[0][1]==-1 && bord[0][2]==-1) ||
(bord[1][0]==-1 && bord[1][1]==-1 && bord[1][2]==-1) ||
(bord[2][0]==-1 && bord[2][1]==-1 && bord[2][2]==-1) ||
(bord[0][0]==-1 && bord[1][0]==-1 && bord[2][0]==-1) ||
(bord[0][1]==-1 && bord[1][1]==-1 && bord[2][1]==-1) ||
(bord[0][2]==-1 && bord[1][2]==-1 && bord[2][2]==-1) ||
(bord[0][0]==-1 && bord[1][1]==-1 && bord[2][2]==-1) ||
(bord[0][2]==-1 && bord[1][1]==-1 && bord[2][0]==-1)
){
System.out.println("玩家1获得胜利! Game Over");
return true;
//玩家2赢了
}else if(
(bord[0][0]==1 && bord[0][1]==1 && bord[0][2]==1) ||
(bord[1][0]==1 && bord[1][1]==1 && bord[1][2]==1) ||
(bord[2][0]==1 && bord[2][1]==1 && bord[2][2]==1) ||
(bord[0][0]==1 && bord[1][0]==1 && bord[2][0]==1) ||
(bord[0][1]==1 && bord[1][1]==1 && bord[2][1]==1) ||
(bord[0][2]==1 && bord[1][2]==1 && bord[2][2]==1) ||
(bord[0][0]==1 && bord[1][1]==1 && bord[2][2]==1) ||
(bord[0][2]==1 && bord[1][1]==1 && bord[2][0]==1)
){
System.out.println("玩家2获得胜利! Game Over");
return true;
//没下完
}else if (findZero(bord)) {
return false;
//和局
}else{
System.out.println("和局");
return true;
}
}
//判定一个int[][] arr 中有没有0
public static boolean findZero(int[][] bord){
for (int i = 0; i < bord.length; i++) {
for (int j = 0; j < bord.length; j++) {
if (bord[i][j] == 0) {
return true;
}
}
}
return false;
}
int[] a={1,2,3,4};
static public void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//作业:若用char[][] 来记录棋盘 修改代码
int n = 3; //棋盘的行列数 (正方的)
int[][] bord = new int[n][n]; //记录棋盘现状
int player = 1;//记录当前是哪个玩家 1表示玩家1 2表示玩家2
while(true){
//当前棋盘
bordDisp(bord);
//落子操作 直到成功
int[] position =playerDrop(scanner, player,bord);
//更新棋盘
bordChange(bord, position, player);
//判定棋局状况 (有人赢了或和局 则结束整个循环)
if (isFinish(bord)) {
break;
}
//切换玩家
player = player==1?2:1;
System.out.println("-------------------\n");
}
}
}