Java——利用二维数组实现五子棋

Java——利用二维数组实现五子棋

一、游戏规则

当某一方的棋子在横向、纵向或者在与棋盘对角线平行的方向上有5颗连续的棋子即可获胜

二、分析

  1. 五子棋棋盘是二维的,每个位置都可以由x,y唯一表示,因此用长宽相等的二维数组来模拟。
  2. 每下一步后需要判断对局是否结束,因此,需要对棋盘(即二维数组)进行判断,判断是否有连续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();
		}
	}

}

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生命中有太多不确定

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值