?? othellogame.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)//設(shè)定電腦所執(zhí)棋子的顏色
blackAI = new OthelloAI(Cyberthello.BLACK);
if (whiteIsAI)
whiteAI = new OthelloAI(Cyberthello.WHITE);
}
public void run()//開始下棋
{
while (!gameIsOver)
{
if (blackMove)//輪到黑方下棋
{
if (blackIsAI)//黑方是電腦,那么調(diào)用最大食子算法下棋
{
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//玩家執(zhí)黑棋,那么等待玩家落子
{
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; // 白方有路可走,則現(xiàn)在輪到白方下棋
else//白方無路可走
if (currentBoard.hasMove(Cyberthello.BLACK))//黑方有路可走,則繼續(xù)讓黑方下棋
System.out.println("白方無路可走,等待黑方走棋");
else//雙方都無路可走,游戲結(jié)束
{
gameIsOver = true;
}
}
else//白方下完棋,看看黑方的情況
{
if (currentBoard.hasMove(Cyberthello.BLACK))
blackMove = true; // 輪到黑方下棋
else
if (currentBoard.hasMove(Cyberthello.WHITE))//黑方無路可走,讓白方繼續(xù)下棋
System.out.println("黑方無路可走了,等待白方走棋");
else//雙方都無路可走,游戲結(jié)束
{
gameIsOver = true;
}
}
UI.refreshUI();//刷新
{
try {Thread.sleep(500);}
catch (InterruptedException e) { }
}
}
//游戲結(jié)束,刷新一下
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) //沒有游戲正在進(jìn)行
{
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);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -