java实现简单五子棋判断输赢

package com.txt;
import java.util.Scanner;  								

class Gobang 
{
	char[][] chessboard;//生成新的二维数组							
    Gobang()     //构建五子棋函数         				
	{
		chessboard = new char[15][15];	//15x15的棋盘[0~14]			
		for(int i=0;i<15;i++)							
		{
			for(int j=0;j<15;j++)					
				
				chessboard[i][j] = '+'; //循环体构建+型棋盘     
		}
	}
//输赢判断
	boolean judge() {    //布尔类型的判断
		boolean result = false;
		//左右方向判断
		for(int i=0;i<15;i++) {
			for(int j=4;j<15;j++) {
				char ch = chessboard[i][j];
				if(ch == '+') {
					continue;
				}
					if(ch=='*' 
							&& chessboard[i][j-1] == ch 
							&& chessboard[i][j-2] == ch 
							&& chessboard[i][j-3] == ch
							&& chessboard[i][j-4] == ch) {
						System.out.println("您赢了");
						result=true;
					}
				
			}
			
		}
		//上下方向判断
		for(int i=4;i<15;i++) {
			for(int j=0;j<15;j++) {
				char ch = chessboard[i][j];
				if(ch == '+') {
					continue;
				}
					if(ch=='*' 
							&& chessboard[i-1][j] == ch 
							&& chessboard[i-2][j] == ch 
							&& chessboard[i-3][j] == ch
							&& chessboard[i-4][j] == ch) {
						System.out.println("您赢了");
						result=true;
					}
				
			}
			
		}
		//左上右下判断
		for(int i=4;i<15;i++) {
			for(int j=4;j<15;j++) {
				char ch = chessboard[i][j];
				if(ch == '+') {
					continue;
				}
					if(ch=='*' 
							&& chessboard[i-1][j-1] == ch 
							&& chessboard[i-2][j-2] == ch 
							&& chessboard[i-3][j-3] == ch
							&& chessboard[i-4][j-4] == ch) {
						System.out.println("您赢了");
						result=true;
					}
				
			}
			
		}
		//左下右上判断
		for(int i=4;i<15;i++) {
			for(int j=0;j<15;j++) {
				char ch = chessboard[i][j];
				if(ch == '+') {
					continue;
				}
					if(ch=='*' 
							&& chessboard[i-1][j+1] == ch 
							&& chessboard[i-2][j+2] == ch 
							&& chessboard[i-3][j+3] == ch
							&& chessboard[i-4][j+4] == ch) {
						System.out.println("您赢了");
						result=true;
					}
				
			}
			
		}
		return result;
}
	 void start ()
	 {
		 Scanner input = new Scanner(System.in);
	
		
		 while(true) 
		 {
			 System.out.println("游戏开始!");
			 int x=0,y=0;
			 while(true)
			 {
				 System.out.println("请输入落子位置:x,y");
				 String p_input = input.nextLine();
				 String[] p_inputs = p_input.split(",");
				 x = Integer.parseInt(p_inputs[0]);
				 y = Integer.parseInt(p_inputs[1]);
				 if( x >= 0 && x < 15 && y >= 0 && y < 15) {}
				 if(chessboard[x][y] == '+')
				 {
						 chessboard[x][y] = '*';
						 break;
				 }
			}
			 for(int i=0;i<15;i++)
			 {
				 for(int j=0;j<15;j++)
					 System.out.print(chessboard[i][j]+" ");
				 System.out.println();
			 }
			 if(judge()) break;
			 while(true)
			 {
				 System.out.println("机器落子!");
				 x = (int)(Math.random()*15);
				 y =(int)(Math.random()*15);
				 if(x>=0 && x<15 && y>=0 && y<15)
					 if(chessboard[x][y] == '+')
					 {
						 chessboard[x][y] = '$';
						 break;
					 }
				 for(int i=0;i<15;i++)
				 {
					 for(int j=0;j<15;j++)
						 System.out.print(chessboard[i][j]);
					 System.out.println();
				 }
				if(judge()) break;
			 }
			 
		 }
	
	 }
	 public static void main(String[] args) 
		{
			Gobang gb = new Gobang();
			gb.start();
		}
}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值