Java——利用二维数组实现五子棋
一、游戏规则
当某一方的棋子在横向、纵向或者在与棋盘对角线平行的方向上有5颗连续的棋子即可获胜
二、分析
- 五子棋棋盘是二维的,每个位置都可以由x,y唯一表示,因此用长宽相等的二维数组来模拟。
- 每下一步后需要判断对局是否结束,因此,需要对棋盘(即二维数组)进行判断,判断是否有连续5个一样的棋子。
三、代码实现
主要复杂的部分是在判断是否有5个连续的棋子,而且每次判断会分成 横向、纵向和对角线平行方向三种情况
代码如下:
import java.util.Scanner;
/*
目标: 编程实现控制台版并支持两人对战的五子棋游戏
(1)绘制棋盘
(2)提示黑方(用 1 表示)和白方(用 2 表示)分别下棋(X,Y 轴位
置)并重新绘制棋盘。
(3)每当一方下棋后判断是否获胜 。
(4)提示:采用二维数组来模拟棋盘
*/
public class WuZiQi {
public static void main(String[] args) {
byte size = 10; //棋盘尺寸大小
int[][] chessboard = new int[size][size];
//初始化二维数组
for(int i=0;i<size;i++) {
for(int j=0;j<size;j++) {
chessboard[i][j] = 0;
}
}
boolean isEnded = false; //判断对局是否结束
int RoundCount = 1;
int order;
Scanner input = new Scanner(System.in);
while(!isEnded) {
order = RoundCount%2;
switch(order) {
case 1 :
System.out.println("黑方的回合,请输入要棋子的X,Y坐标,以空格隔开:");
int x1 = input.nextInt();
int y1 = input.nextInt();
chessboard[x1-1][y1-1] = 1;
printboard(chessboard); //打印实时棋盘
break;
case 0 :
System.out.println("白方的回合,请输入要棋子的X,Y坐标,以空格隔开:");
int x2 = input.nextInt();
int y2 = input.nextInt();
chessboard[x2-1][y2-1] = 2;
printboard(chessboard); //打印实时棋盘
break;
default :
break;
}
switch(cheakboard(chessboard)) {
case 1 :
System.out.println("游戏结束。黑方胜!!!");
isEnded = true;
break;
case 2 :
System.out.println("游戏结束。白方胜!!!");
isEnded = true;
break;
default :
break;
}
RoundCount++;
}
input.close();
}
/**
* 黑方胜返回1,白方胜返回2,默认返回0
* @param chessboard
* @return
*/
private static int cheakboard(int[][] chessboard) {
int result = 0; //返回结果
//逐行扫描
for(int i=0;i<chessboard.length;i++) {
int count = 1;
int tempValue = chessboard[i][0];
for(int j=1;j<10;j++) {
if(chessboard[i][j]==tempValue) {
count++;
}
if(chessboard[i][j]!=tempValue) {
count = 1;
tempValue = chessboard[i][j];
}
if(count==5 && tempValue==1) { //5个1相连
result = 1;
}
if(count==5 && tempValue==2) { //5个2相连
result = 2;
}
}
}
//逐列扫描
for(int j=0;j<chessboard.length;j++) {
int count = 1;
int tempValue = chessboard[j][0];
for(int i=1;i<10;i++) {
if(chessboard[i][j]==tempValue) {
count++;
}
if(chessboard[i][j]!=tempValue) {
count = 1;
tempValue = chessboard[i][j];
}
if(count==5 && tempValue==1) { //5个1相连
result = 1;
}
if(count==5 && tempValue==2) { //5个2相连
result = 2;
}
}
}
//斜线扫描
for(int i=0;i<chessboard.length-4;i++) {
int count = 1;
int tempValue = chessboard[0][i];
int x = 0;
int y = i;
for(int j=1;j<chessboard.length-i;j++,x++,y++) {
if(chessboard[x+1][y+1]==tempValue) {
count++;
}
if(chessboard[x+1][y+1]!=tempValue) {
count = 1;
tempValue = chessboard[x+1][y+1];
}
if(count==5 && tempValue==1) { //5个1相连
result = 1;
}
if(count==5 && tempValue==2) { //5个2相连
result = 2;
}
}
}
for(int i=1;i<chessboard.length-4;i++) {
int count = 1;
int tempValue = chessboard[i][0];
int x = i;
int y = 0;
for(int j=1;j<chessboard.length-i;j++,x++,y++) {
if(chessboard[x+1][y+1]==tempValue) {
count++;
}
if(chessboard[x+1][y+1]!=tempValue) {
count = 1;
tempValue = chessboard[x+1][y+1];
}
if(count==5 && tempValue==1) { //5个1相连
result = 1;
}
if(count==5 && tempValue==2) { //5个2相连
result = 2;
}
}
}
return result;
}
/**
* 打印棋盘二维数组
* @param chessboard
* @return void
*/
private static void printboard(int[][] chessboard) {
System.out.println("棋盘:");
for(int i=0;i<chessboard.length;i++) {
for(int j=0;j<chessboard.length;j++) {
System.out.print(chessboard[i][j] + " ");
}
System.out.println();
}
}
}