赞
踩
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,下错棋子的一方, 将少一次下棋机会 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 + "胜利"); } }
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 + "胜利"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。