井字游戏java代码_井字游戏(java)

井字游戏(java)

井字游戏(java)

分析:游戏方式只有赢和平两种方式

赢:有一方有3个连成一条线就赢了(横,竖,对角线)。想要赢填子的数量必须大于等于5,所以填前面4个格子时不需要判断。

考虑赢我把它分为三种情况:横,竖,对角线

横竖其实是同一类 只要判断下一行一列下填的东西是否相同。(如果一行一列有个空没有填就可以直接跳过)

对角线:主对角线和斜对角线  主对角线的特点是横竖坐标相同,斜对角线的特点是横坐标等于2减去竖坐标

平:我偷懒了下,要填满9个格子

好开始写程序。

功能:1、展示填好的九宫格

2、判断是否有人赢了

3、填格子

展示代码:

public static void showMap(char[][] map)

{

System.out.println("-------------");

for(int i=0;i<3;i++)

{

System.out.print("|");

for(int j=0;j<3;j++)

{

if(map[i][j]!='\u0000')

System.out.print(" "+map[i][j]+" |");

else

System.out.print(" |");

}

System.out.println();

System.out.println("-------------");

}

}

88f26259f01fcf43d9caff34db99c29e.png

判断代码

public static boolean isWin(char[][] chess)

{

for(int i=0;i<3;i++)//横着判断

{

boolean row=true;

if(chess[i][0]=='\u0000')

continue;

for(int j=1;j<3;j++)

{

if(chess[i][j]!=chess[i][0]||chess[i][j]=='\u0000')

{

row=false;

break;

}

}

if(row)

{

System.out.println(chess[i][0]+" player won");

return true;

}

}

for(int i=0;i<3;i++)//竖着判断

{

boolean column=true;

if(chess[0][i]=='\u0000')

continue;

for(int j=1;j<3;j++)

{

if(chess[j][i]!=chess[0][i]||chess[j][i]=='\u0000')

{

column=false;

break;

}

}

if(column)

{

System.out.println(chess[0][i]+" player won");

return true;

}

}

boolean mainDiagonal=true;

boolean deputyDiagonal=true;

for(int i=0;i<3;i++)//对角线判断

{

for(int j=0;j<3;j++)

{

if(mainDiagonal&&i==j)

{

if(chess[i][j]=='\u0000'||chess[i][j]!=chess[0][0])

mainDiagonal=false;

}

if(deputyDiagonal&&i==2-j)

{

if(chess[i][j]=='\u0000'||chess[i][j]!=chess[0][2])

deputyDiagonal=false;

}

}

}

if(mainDiagonal)

{

System.out.println(chess[0][0]+" player won");

return true;

}

if(deputyDiagonal)

{

System.out.println(chess[0][2]+" player won");

return true;

}

return false;

}

总代码:

import java.util.Scanner;

class WellGame

{

public static void main(String[] args)

{

char[][] array=new char[3][3];

showMap(array);

int step=0;

int row,column;

Scanner input=new Scanner(System.in);

while(step<5||isWin(array)==false&&step<9)

{

if(step%2==0) //用奇偶来区别XO输入

{

System.out.print("Enter a row(0, 1, or 2) for player X:");

row=input.nextInt();

System.out.print("Enter a column(0, 1, or 2) for player X:");

column=input.nextInt();

array[row][column]='X';

showMap(array);

}else

{

System.out.print("Enter a row(0, 1, or 2) for player O:");

row=input.nextInt();

System.out.print("Enter a column(0, 1, or 2) for player O:");

column=input.nextInt();

array[row][column]='O';

showMap(array);

}

step++;

}

if(step==9) //平局

System.out.println("The game ended in a draw");

}

public static boolean isWin(char[][] chess)

{

for(int i=0;i<3;i++) //行判断

{

boolean row=true;

if(chess[i][0]=='\u0000')

continue;

for(int j=1;j<3;j++)

{

if(chess[i][j]!=chess[i][0]||chess[i][j]=='\u0000')

{

row=false;

break;

}

}

if(row)

{

System.out.println(chess[i][0]+" player won");

return true;

}

}

for(int i=0;i<3;i++) //列判断

{

boolean column=true;

if(chess[0][i]=='\u0000')

continue;

for(int j=1;j<3;j++)

{

if(chess[j][i]!=chess[0][i]||chess[j][i]=='\u0000')

{

column=false;

break;

}

}

if(column)

{

System.out.println(chess[0][i]+" player won");

return true;

}

}

boolean mainDiagonal=true;

boolean deputyDiagonal=true;

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

if(mainDiagonal&&i==j)//主对角线判断

{

if(chess[i][j]=='\u0000'||chess[i][j]!=chess[0][0])

mainDiagonal=false;

}

if(deputyDiagonal&&i==2-j) //斜对角线判断

{

if(chess[i][j]=='\u0000'||chess[i][j]!=chess[0][2])

deputyDiagonal=false;

}

}

}

if(mainDiagonal) //主对角线连成直线

{

System.out.println(chess[0][0]+" player won");

return true;

}

if(deputyDiagonal) //斜对角线连成直线

{

System.out.println(chess[0][2]+" player won");

return true;

}

return false;

}

public static void showMap(char[][] map)//显示九个格

{

System.out.println("-------------");

for(int i=0;i<3;i++)

{

System.out.print("|");

for(int j=0;j<3;j++)

{

if(map[i][j]!='\u0000')

System.out.print(" "+map[i][j]+" |");

else

System.out.print(" |");

}

System.out.println();

System.out.println("-------------");

}

}

}

5c90a6101019ea25c7811312fb621cbc.png

83b1b8ed9b4377b1f4ab991466a74808.png

daf4450c85b3bd051f1eb7c51cbc8a11.png

井字游戏(java)相关教程

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值