控制台五子棋游戏

import java.util.*;
import java.io.*;

/**
  *Description:控制台五子棋游戏,用户自定义BOARD_SIZE的大小,
  *即棋盘的大小,用户为黑棋●,PC为白棋○,
  *用户输入黑旗的坐标x,y PC随机产生坐标x1,y1
  *注意:不能重复下棋,每次下棋,需要扫描谁赢了
  *扫描:横、竖、右斜、左斜
  *@author	MechanicalDog
  *@version 1.0
  */
public class Gobang
{
	private static int BOARD_SIZE = 20;
	String[][] board;

	public static void main(String[] args) throws Exception
	{
		//无static修饰的类
		Gobang gb = new Gobang();
		gb.initBoard();
		gb.printBoard();
		Scanner s = new Scanner(System.in);
//		String str = s.nextLine();
		String str ;


		//当检测到一方胜利时,跳出循环
		outer:
		while( (str = s.nextLine())!= null )
		{
			//将输入的字符串x,y分成两个字符串x和y,并且以","作为分界
			String[] cordinate =  str.split(",");
			//将String字符转换为int类型,不是转换为ASCII
			int x = Integer.parseInt(cordinate[0]);
			int y = Integer.parseInt(cordinate[1]);
			
			gb.board[x-1][y-1] = "●";

			//电脑随机生成两个随机数,取值范围0~15
			int x1 = (int)( Math.random() * BOARD_SIZE );
			int y1 = (int)( Math.random() * BOARD_SIZE );
			//下棋的点不能重复下棋
			while( gb.board[x1][y1] != "╂" )
			{
				 x1 = (int)( Math.random() * BOARD_SIZE );
				 y1 = (int)( Math.random() * BOARD_SIZE );
			}
			gb.board[x1][y1] = "○";
			gb.printBoard();
			
			//标记
			int black = 0;
			int white = 0;
			int blackWin = 0;
			int whiteWin = 0;

			//扫描谁赢了,4次循环扫描,横、竖、右斜、左斜
			//横着扫描
			outer1:
			for(int i = 0 ; i < BOARD_SIZE ; i++ )
			{				
				white = 0;
				black = 0;
				for (int j = 0 ; j < BOARD_SIZE ; j++ )
				{
					if(gb.board[i][j] == "●")
					{
						black++;
					}
					else if(gb.board[i][j] == "○")
					{
						white++;
					}

					if( black == 5)
					{
						System.out.println("黑棋胜利");
						blackWin = 1;
						break outer1;
					}
					if( white == 5)
					{
						System.out.println("白棋胜利");
						whiteWin = 1;
						break outer1;
					}
				}
			}



			//竖着扫描
			outer2:
			for(int j = 0 ; j < BOARD_SIZE ; j++ )
			{
				white = 0;
				black = 0;
				for (int i = 0 ; i < BOARD_SIZE ; i++ )
				{
					if(gb.board[i][j] == "●")
					{
						black++;
					}
					else if(gb.board[i][j] == "○")
					{
						white++;
					}

					if( black == 5)
					{
						System.out.println("黑棋胜利");
						blackWin = 1;
						break outer2;
					}
					if( white == 5)
					{
						System.out.println("白棋胜利");
						whiteWin = 1;
						break outer2;
					}
				}
			}



			//右斜扫描(从右上角开始判断)

			int temp = BOARD_SIZE ;
			int k = 0;
			int t = 0;
			//斜线共有2*BOARD_SIZE - 1 条
			outer3:
			for( int num = 1 ; num < (2*BOARD_SIZE); num++)
			{
				white = 0;
				black = 0;
				temp--;

			    if( temp >= 0)
				{
					t = temp;
					k = BOARD_SIZE - t ;
					for( int i = 0 ; i < k ; i++)
					{
						if(gb.board[i][ t + i ] == "●")
						{
							black++;
						}
						else if(gb.board[i][ t + i ] == "○")
						{
							white++;
						}

						if( black == 5)
						{
							System.out.println("黑棋胜利");
							blackWin = 1;
							break outer3;
						}
						if( white == 5)
						{
							System.out.println("白棋胜利");
							whiteWin = 1;
							break outer3;
						}
					}
				}
				else
				{
					t = temp;
					t = -t;
					k = BOARD_SIZE - t ;
					for( int j = 0 ; j < k ; j++ )
					{
						if(gb.board[ t + j ][ j ]== "●")
						{
							black++;
						}
						else if(gb.board[ t + j ][ j ] == "○")
						{
							white++;
						}

						if( black == 5)
						{
							System.out.println("黑棋胜利");
							blackWin = 1;
							break outer3;
						}
						if( white == 5)
						{
							System.out.println("白棋胜利");
							whiteWin = 1;
							break outer3;
						}
					}
				}				
			}



			//左斜扫描(从左上角开始)
			 temp = BOARD_SIZE ;
			 k = 0;
			 t = 0;
			 int count = 0 ;
			//斜线共有2*BOARD_SIZE - 1 条
			outer4:
			for( int num = 1 ; num < (2*BOARD_SIZE); num++)
			{
				white = 0;
				black = 0;
				temp--;

			    if( temp >= 0)
				{
					t = temp;
					k = BOARD_SIZE - t ;
					for( int i = 0 ; i < k ; i++)
					{
						if(gb.board[ i ][count - i]== "●")
						{
							black++;
						}
						else if(gb.board[ i ][ count -i ] == "○")
						{
							white++;
						}

						if( black == 5)
						{
							System.out.println("黑棋胜利");
							blackWin = 1;
							break outer4;
						}
						if( white == 5)
						{
							System.out.println("白棋胜利");
							whiteWin = 1;
							break outer4;
						}
					}
				}
				else
				{
					t = temp;
					t = -t;
					k = BOARD_SIZE - t ;
					int p = 0;
					for( int j = BOARD_SIZE - 1  ; p < k ; j -- , p++ )
					{
						if(gb.board[ count - j ][ j ]== "●")
						{
							black++;
						}
						else if(gb.board[ count - j ][ j ] == "○")
						{
							white++;
						}

						if( black == 5)
						{
							System.out.println("黑棋胜利");
							blackWin = 1;
							break outer4;
						}
						if( white == 5)
						{
							System.out.println("白棋胜利");
							whiteWin = 1;
							break outer4;
						}
					}
				}	
				count++;
			 }
			
			//判断一方有没有获得胜利
			if( blackWin != 1 && whiteWin != 1)
			{
				System.out.println("请输入您的下棋坐标,应以x,y格式");
			}
			else
			{
				System.out.println("游戏结束");
				//跳出整个循环
				break outer;
			}

		}
	}
	/**
	  *定义棋盘,用"+"表示
	  */
	public void initBoard()
	{
		//初始化棋盘数组,一定要有!!!
		board = new String[BOARD_SIZE][BOARD_SIZE];
		for( int i = 0 ; i < BOARD_SIZE ; i++ )
		{
			for( int j = 0 ; j < BOARD_SIZE ; j++ )
			{
				board[i][j] = "╂";
			}
		}
	}

	/**
	  *打印棋盘
	  */
	public void printBoard()
	{
		for( int i = 0 ; i < BOARD_SIZE ; i++ )
		{
			for( int j = 0 ; j < BOARD_SIZE ; j++ )
			{
				System.out.print(board[i][j]);
			}
			//换行
			System.out.print('\n');
		}
		
	}

}

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值