2021-08-18java五子棋

//五子棋,用red ,yellow两个颜色代替
//实在关不掉下面的高亮。。
//对角线check那里没有进行矩阵移动
//现在改过来了;

import java.util.*;
public class GoBang {
	public static void main(String[] args)
	{
		Scanner input=new Scanner(System.in);
		//记录步数;
		int countr=0;
		int county=0;
		int row=0;
		int column=0;
		char [][]situation=new char[6][7];
		pattern(situation);
		while(county<22&&countr<22)
		{
	    System.out.println("enter the row(0-5)for player Red:");
	    row=input.nextInt();
	    System.out.println("enter the column(0-6)for player Red:");
		column=input.nextInt();
		situation[row][column]='R';
		countr++;
		System.out.println("enter the row(0-5)for player Yellow:");
		row=input.nextInt();
		System.out.println("enter the column(0-6)for player Yellow:");
		column=input.nextInt();
		situation[row][column]='Y';
		county++;
		if(countr>=3||county>=3)
		{
			if(check(situation)!=0)
			{
				if(check(situation)==1)
					{
					pattern(situation);
					System.out.println(" game over ! red win!!");
					break;
					}
				else
					if(check(situation)==-1)
					{   
						pattern(situation);
						System.out.println(" game over ! yellow win!!");
						break;
					}
			}
		}
		pattern(situation);
		}
		input.close();		
	}
	public static void pattern(char [][]arr)
	{
		System.out.println("------------------------------------");
		for(int i=0;i<arr.length;i++)
		{
			for(int j=0;j<arr[i].length;j++)
			{
				System.out.print("|");
				if(arr[i][j]=='R'||arr[i][j]=='Y')
				{ 
					String temp=arr[i][j]+"";
					System.out.printf("%4s",temp);
				}
				else
					System.out.printf("%4s","");
			}
			System.out.print("|");
			System.out.println();
			System.out.println("------------------------------------");
		}
	}
	public static int check(char [][]arr)//return 1 for x win,-1 for o win;
	{
		int row=rowCheck(arr);
		int column=columnCheck(arr);
		int diagonal=DiagonalsInOrderCheck(arr);
		int diagonalR=DiagonalsInReverseOrderCheck(arr);
		if(row==1||column==1||diagonal==1||diagonalR==1)
			return 1;
		else
			if(row==-1||column==-1||diagonal==-1||diagonalR==-1)
				return -1;
			else
				return 0;
		
	}
	public static int columnCheck(char[][]arr)//return 1 for x win,-1 for o win;
	{
		int countr=0;
		int county=0;
		for(int i=0;i<arr[0].length;i++)
		{
			for(int j=0;j<arr.length;j++)
			{
				if(arr[j][i]=='R')
					countr++;
			if(arr[j][i]=='Y')
				county++;
			if(countr==4)
				return 1;
			if(county==4)
				return -1;	
			}
			countr=0;
			county=0;
		}
		
		return 0;		
	}
	public static int rowCheck(char[][]arr)//return 1 for R win,-1 for Y win;
	{
		int countr=0;
		int county=0;
		for(int i=0;i<arr.length;i++)
		{
			for(int j=0;j<arr[i].length;j++)
			{
				if(arr[i][j]=='R')
					countr++;
			if(arr[i][j]=='Y')
				county++;
			if(countr==4)
				return 1;
			if(county==4)
				return -1;	
			}
			countr=0;
			county=0;
		}
				return 0;
    }
	public static int DiagonalsInOrderCheck(char[][]matrix)
	{
		int countr=0;
		int county=0;
		int m=6;
		for(int i=3;i<m;i++)	//控制矩阵大小
		{
			for(int k=0 ;k<=m-3;k++)//控制行移动
         	{    
			for(int d=0;d<=m-3;d++)//控制列移动
			{
				for(int j=0;j<=i&&(k+j+d)<m;j++)//检查一个R方阵
			{
				if(matrix[j+k+d][j+d]=='R')
				{
					countr++;
				}
				if(countr==4)
					return 1;
			}	
			countr=0;
			for(int j=0;j<=i&&(k+j+d)<m;j++)//检查一个Y方阵;
			{
				if(matrix[j+k+d][j+d]=='Y')
				{
					county++;
				}
				if(county==4)
					return -1;
			}			
			county=0;
		}
		}
		}
	return 0;
	}
	public static int DiagonalsInReverseOrderCheck(char[][]matrix)
	{
		int countr=0;
		int county=0;
		int m=6;
		int row=0;
		int column=0;
	    for(int i=6;i>=3;i--)//控制矩阵大小
		{
	    	for(int k=0;k<=m-3;k++)//控制行移动;
	    		for(int d=0;d<=m-3;d++)//控制列移动
		    {
			for(int j=0;j<m&&Math.abs(i-j+d)<m+1;j++)//检查一个R方阵;
			{
				column=Math.abs(i-j+d);
			    row=j+k;
			    if(row==6)
					break;
				if(matrix[row][column]=='R')
				{
					countr++;
				}
				if(countr==4)
					return 1;
			}
			countr=0;
			for(int j=0;j<m&&(i-j+d)<m+1;j++)//检查一个Y方阵;
			{
				column=Math.abs(i-j+d);
			    row=j+k;
			    if(row==6)
					break;
				if(matrix[row][column]=='Y')
				{
					county++;
				}
				if(county==4)
					return -1;
			}
			county=0;
			
		}
		}
	return 0;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无情键修

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

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

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

打赏作者

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

抵扣说明:

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

余额充值