⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 othellogame.java

📁 JCreator下开发
💻 JAVA
字号:
// 游戏过程

public class OthelloGame extends Thread
{
	private OthelloBoard currentBoard;
	private boolean gameIsOver;
	private boolean blackWin;
	private boolean whiteWin;
	private boolean blackIsAI;
	private boolean whiteIsAI;
	private boolean blackMove;
	private CyberthelloFrame UI;
	private OthelloAI blackAI;
	private OthelloAI whiteAI;
	public OthelloGame(boolean blackIsHuman,
	                   boolean whiteIsHuman,
		               CyberthelloFrame frame)
	{
		currentBoard = new OthelloBoard();
		gameIsOver = false;
		blackWin = false;
		whiteWin = false;
		blackIsAI = !blackIsHuman;
		whiteIsAI = !whiteIsHuman;
		blackMove = true;
		UI = frame;
		if (blackIsAI)//设定电脑所执棋子的颜色
			blackAI = new OthelloAI(Cyberthello.BLACK);
		if (whiteIsAI)
			whiteAI = new OthelloAI(Cyberthello.WHITE);
	}
	public void run()//开始下棋
	{
		while (!gameIsOver)
		{
			if (blackMove)//轮到黑方下棋
			{
				if (blackIsAI)//黑方是电脑,那么调用最大食子算法下棋
				{
					Move m = blackAI.chooseMove(currentBoard);
					if (m == Move.pass)//走棋合法
					{
						if (currentBoard.blackHasMove())
							throw new RuntimeException("Black AI passed when" +
								" it did have a legal move.");
					}
					else//非法走棋
					{
						if (!currentBoard.move(m, Cyberthello.BLACK))
							throw new RuntimeException("Black AI selected an" +
								" invalid move: " + m);
					}
				}
				else//玩家执黑棋,那么等待玩家落子
				{
					try {synchronized(UI) {UI.wait();}}
					catch (InterruptedException e) { }
				}
			}
			else//轮到白方下棋,处理过程与上类似
			{
				if (whiteIsAI)
				{
					Move m = whiteAI.chooseMove( currentBoard);
					if (m == Move.pass)
					{
						if (currentBoard.whiteHasMove())
							throw new RuntimeException("White AI passed when" +
								" it did have a legal move.");
					}
					else
					{
						if (!currentBoard.move(m, Cyberthello.WHITE))
							throw new RuntimeException("White AI selected an" +
								" invalid move: " + m);
					}
				}
				else
				{
					try {synchronized(UI) {UI.wait();}}
					catch (InterruptedException e) { }
				}
			}
			if (blackMove)//黑方下完棋,然后看看白方的情况
			{
				if (currentBoard.hasMove(Cyberthello.WHITE))
					blackMove = false;  // 白方有路可走,则现在轮到白方下棋
				else//白方无路可走
					if (currentBoard.hasMove(Cyberthello.BLACK))//黑方有路可走,则继续让黑方下棋
						System.out.println("白方无路可走,等待黑方走棋");
					else//双方都无路可走,游戏结束
					{
						gameIsOver = true;
					}
			}
			else//白方下完棋,看看黑方的情况
			{
				if (currentBoard.hasMove(Cyberthello.BLACK))
					blackMove = true;  // 轮到黑方下棋
				else
					if (currentBoard.hasMove(Cyberthello.WHITE))//黑方无路可走,让白方继续下棋
						System.out.println("黑方无路可走了,等待白方走棋");
					else//双方都无路可走,游戏结束
					{
						gameIsOver = true;
					}
			}

			UI.refreshUI();//刷新
			{
			    try	{Thread.sleep(500);}
			    catch (InterruptedException e) { }
			}
		}
		//游戏结束,刷新一下		
        if(getWhiteScore()>getBlackScore())//判断哪方获胜
            whiteWin=true;
        else if(getWhiteScore()<getBlackScore())
            blackWin=true;
		
		UI.refreshUI();
	    return;
	}
	public void endGame()
	{
		gameIsOver = true;
	}
	public boolean isGameOver()
	{
		return gameIsOver;
	}  
    public boolean blackWin()
    {
    	return blackWin;
    }  
    public boolean whiteWin()
    {
    	return whiteWin;
    }
	public boolean isBlacksMove()
	{
		return blackMove;
	}
	public boolean makeHumanMove(int row, int col, int color)
	{
		if (gameIsOver)  //没有游戏正在进行
		{
			System.err.println("makeHumanMove called when game is Over.");
			System.exit(0);
		}
		return currentBoard.move(new Move(row, col), color);
	}
	public OthelloBoard getCurrentBoard()
	{
		return currentBoard;
	}
	public boolean blackIsHuman()
	{
		return !blackIsAI;
	}
	public boolean whiteIsHuman()
	{
		return !whiteIsAI;
	}
	public int getBlackScore()
	{
		return currentBoard.getBlackScore();
	}
	public int getWhiteScore()
	{
		return currentBoard.getWhiteScore();
	}
	public int getSquareContents(int row, int col)
	{
		return currentBoard.getSquareContents(row, col);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -