java实现控制台五子棋

//package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException ;
/*五子棋实现在控制台打印输出用+表示棋盘,用●表示玩家○表示电脑*/
public class FiveChess 
{
    public static int BoardSize = 10;
    public static String[][] board;
	//给数组board赋值+表示棋盘
    public static void initBoard()           
	{
		board = new String[BoardSize][BoardSize];
		for(int i = 0; i < BoardSize ; i++)
		{
			for(int j = 0; j < BoardSize; j++)
			{
				board[i][j] = "+ ";
				
			}
		}
	}	
	//打印输出棋盘
   public static void printBoard()
	{
		for(int i = 0; i < BoardSize; i++)
		{
			for (int j = 0; j < BoardSize; j++)
			{
				System.out.print(board[i][j]);
			}
			System.out.println();
		}
	}
	//判断输入范围是否合法
	public static boolean isLegal(int i, int j)
	{
		//输入范围不能超过棋盘范围
    if(i >= 10 || i <= 0 || j >= 10 || j <= 0 )
	  { 
         return false;
	  }
    System.out.println(i+""+j);
	//判断输入的●是否以前被放过棋子
	 else if(board[i][j] != "+ ")
	  {
		return false;
		
	  }
	else return true;
	}
	//电脑随机产生随机数○
	public static int[] computer()
	{
		int[] corrdinate = new int[2];
		corrdinate[0] = (int)(Math.random() * BoardSize + 1);
		corrdinate[1] = (int)(Math.random() * BoardSize + 1);
		while(!isLegal(corrdinate[0],corrdinate[1]))
		{
		corrdinate[0] = (int)(Math.random()*BoardSize);
		corrdinate[1] = (int)(Math.random()* BoardSize + 1);
		}
		return corrdinate;
	}
	/*判断输赢四个方向
	*/
	public static  int isWin()
	{
		//横向
		for(int i = 1; i <= BoardSize - 4 ; i++)
		{
			for(int j = 1; j <= BoardSize - 4; j++)
			{
				if(board[i][j].equals("●") && board[i + 1][j].equals("●") && board[i + 2][j].equals("●") && board[i + 3][j].equals("●") && board[i + 4][j].equals("●"))
				{
					return 1;
				}
				else if(board[i][j] == "○" && board[i + 1][j] == "○" && board[i + 2][j] == "○" && board[i + 3][j] == "○"  && board[i + 4][j] == "○")
				{
					return 0;
				}
			
			}
		}
		//纵向
		for(int i = 1; i <= BoardSize - 4 ; i++)
		{
			for(int j = 1; j <= BoardSize - 4; j++)
			{
				if(board[i][j] == "●" && board[i][j + 1] == "●" && board[i][j + 2] == "●" && board[i][j + 3] == "●"  && board[i][j + 4] == "●")
				{
					return 1;
				}
				else if(board[i][j] == "○" && board[i][j + 1] == "○" && board[i][j + 2] == "○" && board[i][j + 3] == "○"  && board[i][j + 4] == "○" )
				{
					return 0;
				}
				
			}
		}
		//斜向
		for(int i = 1; i <= BoardSize - 4; i++)
		{
			for(int j = 1; j <= BoardSize - 4; j++)
			{
				if(board[i][j].equals("●") && board[i - 1][j - 1].equals("●") && board[i - 2][j - 2].equals("●") && board[i - 3][j - 3].equals("●")&& board[i - 4][j - 4].equals("●"))
				{
					
					return 1;
				}
				else if(board[i][j] == "○" && board[i - 1][j - 1] == "○" && board[i - 2][j - 2] == "○" && board[i - 3][j - 3] == "○"  && board[i - 4][j - 4] == "○")
				{
					return 0;
				}
			}
			
		}
		//反斜向判断
		for(int i = 1; i <= BoardSize - 4; i++)
		{
			for(int j = 1; j <= BoardSize - 4; j++)
			{
				if(board[i][j].equals("●") && board[i + 1][j + 1].equals("●") && board[i + 2][j + 2].equals("●")  && board[i + 3][j + 3].equals("●")&& board[i + 4][j + 4] .equals("●"))
				{
					return 1;
				}
				else if(board[i][j] == "○" && board[i + 1][j + 1] == "○" && board[i + 2][j + 2] == "○" && board[i + 3][j + 3] == "○" && board[i +4][j + 4] == "○")
				{
					return 0;
				}
			}
			
		}
		return -1;
	}
	//主函数
	public static void main(String[] args)
	{
		int m = BoardSize;
		FiveChess play = new FiveChess();
		play.initBoard();    //棋盘赋值
		play.printBoard();   //打印棋盘
		do
            
	{
		System.out.println("请输入您下棋的坐标,应按照x,y的格式");	
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String inputStr;	
		 
	  try{
		  inputStr = br.readLine();
			if(inputStr != null)      //判断输入的内容是否为空
			{
				// 将用户输入的字符串用,分割成两个字符串
				String[] posStrArr = inputStr.split(",");
				//将两个字符串转换成用户下棋的坐标
				int xPos = Integer.parseInt(posStrArr[0]);
				int yPos = Integer.parseInt(posStrArr[1]);
				if(!isLegal(xPos,yPos))
				{
					System.out.println("输入非法,请重新输入下子坐标");
					continue;
				}
				play.board[yPos-1][xPos-1] = "●";
				play.printBoard();
				System.out.println("电脑下棋子"  );
				int corrdinate[] = computer();
				int c1 = corrdinate[0];
				int c2 = corrdinate[1];
				board[c1 - 1][c2 - 1] ="○";
				play.printBoard();
				
		
			}
		}
		catch(IOException e)
	  	{
			System.out.println("你输入的字符串为空");
		}
	  m--;
	  }while(m > 0 );
		
		//判断输赢
		if(isWin() == 1)
		{
			System.out.println("恭喜您赢了");
			
		}
		if(isWin() == 0)
		{
			System.out.println("很遗憾您输了");
		    
		}
		else
		{
			System.out.println("平局");
	    }
			
		
    }
		
	
}

你可能感兴趣的:(java)