疯狂java讲义第四章课后习题第6题答案

6、编写控制台的五子棋游戏。
这里主要是检测是否有胜利的地方代码比较多,可以进行进一步的精简,还有就是这里的机器人只实现了随机下棋,如果想要实现AI可以在AI方法中进行扩展,比如检测一下行列以及斜向的,我会进行简单地扩展

import java.util.*;
public class Test
{
	private int BROAD_SIZE = 15;
	private String[][] chess = new String[BROAD_SIZE][BROAD_SIZE];
	private Scanner scanner = new Scanner(System.in);//也可以用 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  br.readLine()效果是一样的
	private Random rand = new Random();
	private void initGame()
	{
		for(int i = 0 ; i < BROAD_SIZE; i++)
		{
			for( int j = 0; j < BROAD_SIZE; j++)
			{
				chess[i][j] = " +";//在前面加一个空格,打印出来更好看
				System.out.print(" +");
			}
			System.out.println("");
		}
	}
	private void refreshChess()
	{
		for(int i = 0 ; i < BROAD_SIZE; i++)
		{
			for(int j = 0 ; j < BROAD_SIZE; j++)
			{
				System.out.print(chess[i][j]);
			}
			System.out.println("");
		}
	}
	public void start()
	{
		System.out.println("\n——————游戏开始!——————\n");
		initGame();
	}
	public void play()
	{
		
		System.out.println("\n请输入落子坐标,以0为起始位置(x,y),输入q或quit退出:");
		String locate = scanner.next();
		if(locate.equals("q") || locate.equals("quit"))
		{
			System.exit(0);
		}
		try
		{
			int x = Integer.valueOf(locate.split(",")[0]);
			int y = Integer.valueOf(locate.split(",")[1]);
			if(x<0 || x>14 || y <0 || y>14)//如果
			{
				throw new Exception("落子位置不正确!");
			}
			else if( !chess[x][y].equals(" +"))
			{
				throw new Exception("已经有棋子了,请换一个地方下!");
			}
			else//输入的格式正确而且是数字
			{
				chess[x][y] = " *";
				
				//检测结果
				int result = checkResult();

				if(result == -1)
				{
					//机器人下棋
					//先做一个简单的机器人,最差的机器人,能够随机下棋
				
					int[] axy = AI();
				
					chess[axy[0]][axy[1]] = " o";
				
					//检测结果
					checkResult();
				}
				

				
			}	
		}	
		catch(ArrayIndexOutOfBoundsException ae)
		{
			ae.printStackTrace();
			System.out.println("请输入正确的格式!");
		}
		catch(NumberFormatException ne)
		{
			System.out.println("请输入数字!");
		}
		catch(Exception e)
		{
			
			System.out.println(e.getMessage());
		}
		
	}

	
	/**
	* 检测是否有人赢了
	* @return int 1的话是玩家获胜,0的话是电脑获胜。 -1的话是还没有任何一方胜利
	*/
	public int checkResult() 
	{

		int result = -1;
		int m = 0, c = 0;
		//首先,横向检测
		for(int i = 0 ; i < BROAD_SIZE; i ++)
		{
			for( int j = 0 ; j<BROAD_SIZE ; j ++)
			{
				//检测玩家棋子
				if( chess[i][j].equals(" *"))
				{
					m++;
					if( m >= 5 )
					{
						result = 1;//玩家获胜
					}
				}
				else
				{
					m=0;//如果不连续了,就重置为0
				}
				//检测电脑棋子
				if( chess[i][j].equals(" o"))
				{
					c++;
					if( c >= 5 )
					{
						result = 0;//电脑获胜
					}
				}
				else
				{
					c=0;//如果不连续了,就重置为0
				}
			}

		}
		//重置m和c
		m = 0;c = 0;
		//如果横向上,没有任何一方获胜,就检测纵向
		for(int i = 0 ; i < BROAD_SIZE; i ++)
		{
			for( int j = 0 ; j<BROAD_SIZE ; j ++)
			{
				//检测玩家棋子
				if( chess[j][i].equals(" *"))
				{
					m++;
					if( m >= 5 )
					{
						result = 1;//玩家获胜
					}
				}
				else
				{
					m=0;//如果不连续了,就重置为0
				}
				//检测电脑棋子
				if( chess[j][i].equals(" o"))
				{
					c++;
					if( c >= 5 )
					{
						result = 0;//电脑获胜
					}
				}
				else
				{
					c=0;//如果不连续了,就重置为0
				}
			}

		}
		//重置m和c
		m = 0; c = 0;
		//横向和纵向都没有任何一方获胜,就再检测斜的,形状类似\的上半部分
		for( int i = 0 ; i < 15 ; i ++)
		{ 
			int x = 0, y = i;// 0,0~14,14|| 0,1~13,14
			while(x < 15 && y < 15)
			{
				//检测玩家棋子
				if( chess[x][y].equals(" *"))
				{
					m++;
					if( m >= 5 )
					{
						result = 1;//玩家获胜
					}
				}
				else
				{
					m=0;//如果不连续了,就重置为0
				}
				//检测电脑棋子
				if( chess[x][y].equals(" o"))
				{
					c++;
					if( c >= 5 )
					{
						result = 0;//电脑获胜
					}
				}
				else
				{
					c=0;//如果不连续了,就重置为0
				}
				x++;y++;
			}
		}
		//重置m和c
		m = 0; c = 0;
		//横向和纵向都没有任何一方获胜,就再检测斜的,形状类似\的下半部分
		for( int i = 0 ; i < 15 ; i ++)
		{ 
			int x = i, y = 0;// 0,0~14,14|| 1,0~14~
			while(x < 15 && y < 15)
			{
				//检测玩家棋子
				if( chess[x][y].equals(" *"))
				{
					m++;
					if( m >= 5 )
					{
						result = 1;//玩家获胜
					}
				}
				else
				{
					m=0;//如果不连续了,就重置为0
				}
				//检测电脑棋子
				if( chess[x][y].equals(" o"))
				{
					c++;
					if( c >= 5 )
					{
						result = 0;//电脑获胜
					}
				}
				else
				{
					c=0;//如果不连续了,就重置为0
				}
				x++;y++;
			}
		}
		//重置m和c
		m = 0; c = 0;
		//斜的另一边的上半部分/
		for( int i = 14 ; i > 0 ; i --)
		{ 
			int x = i, y = 0;// 0,0~14,14|| 1,0~14~
			while(x >= 0 && y < 15)
			{
				//检测玩家棋子
				if( chess[x][y].equals(" *"))
				{
					m++;
					if( m >= 5 )
					{
						result = 1;//玩家获胜
					}
				}
				else
				{
					m=0;//如果不连续了,就重置为0
				}
				//检测电脑棋子
				if( chess[x][y].equals(" o"))
				{
					c++;
					if( c >= 5 )
					{
						result = 0;//电脑获胜
					}
				}
				else
				{
					c=0;//如果不连续了,就重置为0
				}
				x--;y++;
			}
		}	
		//重置m和c
		m = 0; c = 0;
		//斜的另一边的下半部分/
		for( int i = 14 ; i > 0 ; i --)
		{ 
			int x = i, y = 14;// 0,0~14,14|| 1,0~14~
			while(x >= 0 && y <15)
			{
				//检测玩家棋子
				if( chess[x][y].equals(" *"))
				{
					m++;
					if( m >= 5 )
					{
						result = 1;//玩家获胜
					}
				}
				else
				{
					m=0;//如果不连续了,就重置为0
				}
				//检测电脑棋子
				if( chess[x][y].equals(" o"))
				{
					c++;
					if( c >= 5 )
					{
						result =  0;//电脑获胜
					}
				}
				else
				{
					c=0;//如果不连续了,就重置为0
				}
				x--;y++;
			}
		}
		refreshChess();
		//进行结果解读
		if( result == 1)
		{
			System.out.println("恭喜你,获得了胜利!");
					
		}
		else if( result == 0)
		{
			System.out.println("很遗憾,你输了!");
		}
		
			
		if(result == 0 || result == 1)
		{
			System.out.println("您是否还要继续游戏(y/anything to out):");
			String nextGame = scanner.next();
			if(nextGame.equals("y") | nextGame.equals("Y"))
			{
				initGame();
			}
			else
			{
				System.exit(1);
			}
				
		}

		return result;		


		
	}
	//五子棋机器人的AI
	public int[]  AI() 
	{
		//最简单的五子棋机器人,随机下棋
		int x = rand.nextInt(15);
		int y = rand.nextInt(15);
		
		while(!chess[x][y].equals(" +"))//如果这个随机其位置已经有棋子了就直到挑到没有棋子的
		{
			x = rand.nextInt(15);
			y = rand.nextInt(15);
		}
		//横向检查,记录连续的o的长度,和对应最后一个或者第一个的x,y
		System.out.println("机器人落子于("+x+","+y+")");
		return new int[]{x,y};
	}

	public static void main(String[] args)
	{
		Test test = new Test();
		test.start();
		while (true)
		{
			test.play();
		}

		
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

细水长流cpu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值