📄 heibaiqi.java
字号:
} else
{
continueloop = false;
}
nextx += deltax;
nexty += deltay;
if(nextx < 0 || nextx > 7 || nexty < 0 || nexty > 7)
continueloop = false;
}
}
}
}
//电脑下棋
private void mymove()
{
hangon();
boolean computerplaysblack = true;
if(playerPlaysWhite)
computerplaysblack = false;
//此表为各个位置的权值,权值越高则位置越有利
int advantagearray[][] = {
{ 8, 2, 7, 6, 6, 7, 2, 8 },
{ 2, 1, 3, 3, 3, 3, 1, 2 },
{ 7, 3, 5, 4, 4, 5, 3, 7 },
{ 6, 3, 4, 0, 0, 4, 3, 6 },
{ 6, 3, 4, 0, 0, 4, 3, 6 },
{ 7, 3, 5, 4, 4, 5, 3, 7 },
{ 2, 1, 3, 3, 3, 3, 1, 2 },
{ 8, 2, 7, 6, 6, 7, 2, 8 }
};
//将已放置棋子的点权值设为零
for(int x = 0; x < 8; x++)
for(int y = 0; y < 8; y++)
if(gamegrid[x][y] != -1)
advantagearray[x][y] = 0;
//将不能放置棋子的点权值设为零
for(int x=0; x<8; x++)
for(int y = 0; y < 8; y++)
if(advantagearray[x][y] != 0 && !moveisvalid(x, y,
playerPlaysWhite))
advantagearray[x][y] = 0;
//记录权值最大点,此段程序效率不是很好
int maxadvantage = 0;
for(int x = 0; x < 8; x++)
for(int y = 0; y < 8; y++)
if(advantagearray[x][y] > maxadvantage)
maxadvantage = advantagearray[x][y];
if(maxadvantage == 0) { nextMove();}
else
{
int xpos = -1;
int ypos = -1;
for(int x = 0; x < 8; x++)
{
for(int y = 0; y < 8; y++)
if(advantagearray[x][y] == maxadvantage)
{
xpos = x;
ypos = y;
}
}
plantapiece(xpos, ypos, computerplaysblack);
lastMoveWasWhite = computerplaysblack;
nextMove();
}
}
//判断胜利
private void nextMove()
{
if(boardisfull()) checkforwin();
else if(!playercanmove() && !gamecanmove())
{
checkforwin();
}
else
{
if(lastMoveWasWhite && playerPlaysWhite)
if(gamecanmove())
{
showStatus("My move.");
playersMove = false;
mymove();
}
else
{
showStatus("I cannot move. Take another turn.");
playersMove = true;
}
if(lastMoveWasWhite && !playerPlaysWhite)
if(playercanmove())
{
showStatus("Your move.");
playersMove = true;
}
else
{
showStatus("You cannot move. My turn.");
playersMove = false;
mymove();
}
if(!lastMoveWasWhite && playerPlaysWhite)
if(playercanmove())
{
showStatus("Your move.");
playersMove = true;
}
else
{
showStatus("You cannot move. My turn.");
playersMove = false;
mymove();
}
if(!lastMoveWasWhite && !playerPlaysWhite)
if(gamecanmove())
{
showStatus("My move.");
playersMove = false;
mymove();
}
else
{
showStatus("I cannot move. Take another turn.");
playersMove = true;
}
}
}
//判断棋盘是否已经下满
private boolean boardisfull()
{
for(int x = 0; x < 8; x++)
for(int y = 0; y < 8; y++)
{
if(gamegrid[x][y] != -1)
continue;
return false;
}
return true;
}
//当棋盘已放满时,判断胜利方,计算黑白各有多少子 ,玩家press mouse tostart again,最后置playsmove=false
private void checkforwin()
{
allowPlay = false;
int blacktotal = 0;
int whitetotal = 0;
for(int x = 0; x < 8; x++)
{
for(int y = 0; y < 8; y++)
{
if(gamegrid[x][y] == 0)
whitetotal++;
else if(gamegrid[x][y]==1)blacktotal++;
}
}
if(playerPlaysWhite)
{
if(whitetotal > blacktotal)
{
showStatus("You win! Black total = " + blacktotal + ", white total = " + whitetotal);
Font font = new Font("Helvetica", 3, 48);
String winner = new String("You win!");
Graphics g = getGraphics();
g.setColor(Color.red);
g.setFont(font);
g.drawString(winner, 50, 195);
Font font2 = new Font("Helvetica", 1, 14);
String winner2 = new String("To play again, click on the board.");
g.setFont(font2);
g.drawString(winner2, 55, 265);
}
else
if(blacktotal > whitetotal)
{
showStatus("I win. Black total = " + blacktotal + ", white total = " + whitetotal);
Font font = new Font("Helvetica", 3, 48);
String winner = new String("I win!");
Graphics g = getGraphics();
g.setColor(Color.red);
g.setFont(font);
g.drawString(winner, 50, 195);
Font font2 = new Font("Helvetica", 1, 14);
String winner2 = new String("To play again, click on the board.");
g.setFont(font2);
g.drawString(winner2, 55, 265);
}
else
{
showStatus("Tie game. Black total = " + blacktotal + ",whitetotal = " + whitetotal);
Font font = new Font("Helvetica", 3, 48);
String winner = new String("A tie!");
Graphics g = getGraphics();
g.setColor(Color.red);
g.setFont(font);
g.drawString(winner, 50, 195);
Font font2 = new Font("Helvetica", 1, 14);
String winner2 = new String("To play again, click on the board.");
g.setFont(font2);
g.drawString(winner2, 55, 265);
}
}
else
if(whitetotal > blacktotal)
{
Font font = new Font("Helvetica", 3, 48);
String winner = new String("I win!");
Graphics g = getGraphics();
g.setColor(Color.red);
g.setFont(font);
g.drawString(winner, 50, 195);
Font font2 = new Font("Helvetica", 1, 14);
String winner2 = new String("To play again, click on the board.");
g.setFont(font2);
g.drawString(winner2, 55, 265);
showStatus("I win. Black total = " + blacktotal + ", white total =" + whitetotal);
}
else
if(blacktotal > whitetotal)
{
showStatus("You win! Black total = " + blacktotal + ", white total= " + whitetotal);
Font font = new Font("Helvetica", 3, 48);
String winner = new String("You win!");
Graphics g = getGraphics();
g.setColor(Color.red);
g.setFont(font);
g.drawString(winner, 50, 195);
Font font2 = new Font("Helvetica", 1, 14);
String winner2 = new String("To play again, click on the board.");
g.setFont(font2);
g.drawString(winner2, 55, 265);
}
else
{
showStatus("Tie game. Black total = " + blacktotal + ", white total = " + whitetotal);
Font font = new Font("Helvetica", 3, 48);
String winner = new String("A tie!");
Graphics g = getGraphics();
g.setColor(Color.red);
g.setFont(font);
g.drawString(winner, 50, 195);
Font font2 = new Font("Helvetica", 1, 14);
String winner2 = new String("To play again, click on the board.");
g.setFont(font2);
g.drawString(winner2, 55, 265);
}
playersMove = false;
}
//玩家可以下棋
private boolean playercanmove()
{
boolean returnvalue = false;
for(int x = 0; x < 8; x++)
{
for(int y = 0; y < 8; y++)
if(moveisvalid(x, y, !playerPlaysWhite))
returnvalue = true;
}
return returnvalue;
}
//电脑下棋
private boolean gamecanmove()
{
boolean returnvalue = false;
for(int x = 0; x < 8; x++)
{
for(int y = 0; y < 8; y++)
if(moveisvalid(x, y, playerPlaysWhite))
returnvalue = true;
}
return returnvalue;
}
private synchronized void hangon()
{ //其实此处不用synchronized也是可以的;
try { Thread.sleep(600); }//设定电脑的等待时间!
catch(InterruptedException e){}
finally { return;}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -