五子棋
1. 定义声明一个二维数组做棋盘
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//定义声明一个二维数组作为棋盘
int[][] game = new int[9][9];
Scanner sc = new Scanner(System.in);
2. 定义变量, 代表落子角色
//定义一个变量, 作为当前落子的角色, 1代表黑子, 2代表白子
int role = 1;
3.开始游戏
1. 用户输入行和列
//游戏开始
while(true) {
//等待获取用户输入的行和列
if(role == 1) {
System.out.println("黑棋落子");
}else {
System.out.println("白棋落子");
}
System.out.println("请输入要落子的行和列(空格或回车隔开)");
int r = sc.nextInt();
int c = sc.nextInt();
2. 将输入设为对应棋子
//将用户输入的行和列, 设置成对应的棋子
if(game[r][c] != 0) {
System.out.println("该位置已经有落子");
//没有continue,下错棋子的一方, 将少一次下棋机会
continue;
}else {
game[r][c] = role;
}
System.out.println("棋盘:");
//后面会经常使用的,打印一下当前棋盘状态的代码块
for(int[]arr : game) {
for(int item : arr) {
System.out.print(item + " ");
}
System.out.println();
}
4. 判断是否结束
1. 横向判断
//横向向左判断
int offset = 1;
int count = 1;
while(c - offset >= 0 && game[r][c - offset] == role) {
count++;
offset++;
}
//横向向右判断
offset = 1;
while(c + offset < game[r].length && game[r][c + offset] == role) {
count++;
offset++;
}
if(count >= 5) {
break;
}
2. 竖向判断
//竖向向下判断
offset = 1;
count = 1;
while(r + offset < game.length && game[r + offset][c] == role) {
count++;
offset++;
}
//竖向向上判断
offset = 1;
while(r - offset >= 0 && game[r - offset][c] == role) {
count++;
offset++;
}
if(count >= 5) {
break;
}
3. 左斜判断
//左斜("\")判断当前游戏是否胜利
//左斜("\")向右下判断
offset = 1;
count = 1;
while(r + offset < game.length && c + offset < game[r].length && game[r + offset][c + offset] == role) {
count++;
offset++;
}
//左斜("\")向左上判断
offset = 1;
while(r - offset >= 0 && c - offset >= 0 && game[r - offset][c - offset] == role) {
count++;
offset++;
}
if(count >= 5) {
break;
}
4.右斜判断
//右斜("/")判断当前游戏是否胜利
//右斜("/")向左下判断
offset = 1;
count = 1;
while(r + offset < game.length && c - offset >=0 && game[r + offset][c - offset] == role) {
count++;
offset++;
}
//右斜("/")向右上判断
offset = 1;
while(r - offset >= 0 && c + offset < game[r].length && game[r - offset][c + offset] == role) {
count++;
offset++;
}
if(count >= 5) {
break;
}
//棋子角色的变换, 如果是1, 则变成2, 如果是2, 则变成1
role = 3 - role;
}
System.out.println(role + "胜利");
}
}
5. 运行结果展示
1.横向胜利
2.竖向胜利
3. 左斜判断
文章来源:https://www.toymoban.com/news/detail-788040.html
4. 右斜判断
文章来源地址https://www.toymoban.com/news/detail-788040.html
6. 完整代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//定义声明一个二维数组作为棋盘
int[][] game = new int[9][9];
Scanner sc = new Scanner(System.in);
//定义一个变量, 作为当前落子的角色, 1代表黑子, 2代表白子
int role = 1;
//游戏开始
while(true) {
//等待获取用户输入的行和列
if(role == 1) {
System.out.println("黑棋落子");
}else {
System.out.println("白棋落子");
}
System.out.println("请输入要落子的行和列(空格或回车隔开)");
int r = sc.nextInt();
int c = sc.nextInt();
//将用户输入的行和列, 设置成对应的棋子
if(game[r][c] != 0) {
System.out.println("该位置已经有落子");
//没有该行,下错棋子的一方, 将少一次下棋机会
continue;
}else {
game[r][c] = role;
}
System.out.println("棋盘:");
//后面会经常使用的,打印一下当前棋盘状态的代码块
for(int[]arr : game) {
for(int item : arr) {
System.out.print(item + " ");
}
System.out.println();
}
//横向判断当前游戏是否结束
//横向向左判断
int offset = 1;
int count = 1;
while(c - offset >= 0 && game[r][c - offset] == role) {
count++;
offset++;
}
//横向向右判断
offset = 1;
while(c + offset < game[r].length && game[r][c + offset] == role) {
count++;
offset++;
}
if(count >= 5) {
break;
}
//竖向判断当前游戏是否结束
//竖向向下判断
offset = 1;
count = 1;
while(r + offset < game.length && game[r + offset][c] == role) {
count++;
offset++;
}
//竖向向上判断
offset = 1;
while(r - offset >= 0 && game[r - offset][c] == role) {
count++;
offset++;
}
if(count >= 5) {
break;
}
//左斜("\")判断当前游戏是否胜利
//左斜("\")向右下判断
offset = 1;
count = 1;
while(r + offset < game.length && c + offset < game[r].length && game[r + offset][c + offset] == role) {
count++;
offset++;
}
//左斜("\")向左上判断
offset = 1;
while(r - offset >= 0 && c - offset >= 0 && game[r - offset][c - offset] == role) {
count++;
offset++;
}
if(count >= 5) {
break;
}
//右斜("/")判断当前游戏是否胜利
//右斜("/")向左下判断
offset = 1;
count = 1;
while(r + offset < game.length && c - offset >=0 && game[r + offset][c - offset] == role) {
count++;
offset++;
}
//右斜("/")向右上判断
offset = 1;
while(r - offset >= 0 && c + offset < game[r].length && game[r - offset][c + offset] == role) {
count++;
offset++;
}
if(count >= 5) {
break;
}
//棋子角色的变换, 如果是1, 则变成2, 如果是2, 则变成1
role = 3 - role;
}
System.out.println(role + "胜利");
}
}
到了这里,关于Java五子棋详细步骤及完整代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!