JAVA:(游戏:四子连)

题目描述:

四子连是一个两个人玩的棋盘游戏,在游戏中,玩家轮流将有颜色的棋子放在一个六行七列的垂直悬挂的网格中,如下图所示。
在这里插入图片描述
这个游戏的目的是在对手实现一行、一列或者一条对角线上有四个相同颜色的棋子之前,你能先做到。程序提示两个玩家交替地下红子 Red 或黄子 Yellow。当放下一子时,程序在控制台重新显示这个棋盘,然后确定游戏的状态(贏、平局还是继续)。下面是一个运行示例:
在这里插入图片描述


在这里插入图片描述

思路:

  1. 创建二维数组chess[ ][ ]存放棋盘以及落子情况
  2. 创建方法judge( )判断游戏是否产生输赢

代码:

import java.util.Scanner;

public class Test31 {
	static Scanner in = new Scanner(System.in);
	
	public static void main(String[] args) {
		
		char[][] chess = new char[6][15];
		for(int i = 0; i < chess.length; i++)	//初始化棋盘
		{
			for(int j = 0; j < chess[i].length; j++)
			{
				if(j % 2 == 0)
					chess[i][j] = '|';
				else
					chess[i][j] = ' ';
			}
		}
		
		beginGame(chess);
		
		in.close();
	}
	
	
	//开始比赛
	public static void beginGame(char[][] chess)
	{
		char disk;
		for(int i = 0; i < 42; i++)
		{
			if(i % 2 == 0)
				disk = 'R';
			else 
				disk = 'Y';
			printChessBoard(chess);
			System.out.println("——————————————");
			if(dropDisk(chess,disk) == 0)
			{
				i--;
				continue;
			}
			if(judge(chess) == 0)
			{
				System.out.println("The red player won");
				return;
			}
			else if(judge(chess) == 1)
				{
					System.out.println("The yellow player won");
					return;
				}
		}
		System.out.println("No winner");
	}
	
	
	//落子
	public static int dropDisk(char[][] chess,char disk)
	{
		if(disk == 'R')
			System.out.print("Drop a red disk at column(0-6):");
		else
			System.out.print("Drop a yellow disk at column(0-6):");
		int key = in.nextInt()*2+1;
		if(key < 0 || key > 6)
		{
			System.out.println("Enter error!");
			return 0;
		}
		for(int row = chess.length - 1; row >= 0; row--)
		{
			if(chess[row][key] == ' ')
			{
				chess[row][key] = disk;
				return 1;
			}
		}
		System.out.println("This colum is no more position!");
		return 0;
	}
	
	
	//打印棋盘
	public static void printChessBoard(char[][] chess)
	{
		for(int i = 0; i < chess.length; i++)
		{
			for(int j = 0; j < chess[i].length; j++)
				System.out.print(chess[i][j]);
			System.out.println();
		}
	}
	
	
	//判断输赢,返回0'R'赢,返回1'Y'赢,返回2未分出胜负
	public static int judge(char[][] chess)
	{
		if(judgeRow(chess) != 2)
			return judgeRow(chess);
		if(judgeCol(chess) != 2)
			return judgeCol(chess);
		if(judgeMainDiagonal(chess) != 2)
			return judgeMainDiagonal(chess);
		if(judgeSubDiagonal(chess) != 2)
			return judgeSubDiagonal(chess);
		return 2;
	}
	
	
	//判断行是否有四个相等的
	public static int judgeRow(char[][] chess)
	{
		for(int i = 0; i < chess.length; i++)
			for(int j = 1; j < chess[i].length - 6; j+=2)
			{
				if(chess[i][j] == chess[i][j+2] && chess[i][j] == chess[i][j+4] && chess[i][j] == chess[i][j+6] && chess[i][j] != ' ')
					if(chess[i][j] == 'R')
						return 0;
					else
						return 1;
			}
		return 2;
	}
	
	
	//判断列是否有四个相等的
	public static int judgeCol(char[][] chess)
	{
		for(int i = 0; i < chess.length - 3; i++)
			for(int j = 1; j < chess[i].length; j+=2)
			{
				if(chess[i][j] == chess[i+1][j] && chess[i][j] == chess[i+2][j] && chess[i][j] == chess[i+3][j] && chess[i][j] != ' ')
					if(chess[i][j] == 'R')
						return 0;
					else
						return 1;
			}
		return 2;
	}
	
	
	//判断主对角线是否有四个相等的
	public static int judgeMainDiagonal(char[][] chess)
	{
		for(int i = 0; i < chess.length - 3; i++)//主对角线
			for(int j = 1; j < chess[i].length - 6; j+=2)
			{
				if(chess[i][j] == chess[i+1][j+2] && chess[i][j] == chess[i+2][j+4] && chess[i][j] == chess[i+3][j+6] && chess[i][j] != ' ')
					if(chess[i][j] == 'R')
						return 0;
					else
						return 1;
			}
		return 2;
	}
	
	
	//判断副对角线是否有四个相等的
	public static int judgeSubDiagonal(char[][] chess)
	{
		for(int i = 3; i < chess.length; i++)//副对角线
			for(int j = 1; j < chess[i].length - 6; j+=2)
			{
				if(chess[i][j] == chess[i-1][j+2] && chess[i][j] == chess[i-2][j+4] && chess[i][j] == chess[i-3][j+6] && chess[i][j] != ' ')
					if(chess[i][j] == 'R')
						return 0;
					else
						return 1;
			}
		return 2;
	}
	
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值