📄 turncoat2.java
字号:
}
}
}
private void flippiece(int x, int y, int state, Graphics g)
{
Color black = new Color(0);
Color grey90 = new Color(0x191919);
Color grey80 = new Color(0x333333);
Color grey60 = new Color(0x666666);
Color grey40 = new Color(0x999999);
Color grey20 = new Color(0xcccccc);
Color grey15 = new Color(0xd8d8d8);
Color grey10 = new Color(0xe5e5e5);
Color white = new Color(0xffffff);
x = x * 50 + 25;
y = y * 50 + 25;
switch(state)
{
case 10: // '\n'
g.setColor(grey80);
g.fillOval(x - 15, y - 6, 40, 36);
g.setColor(grey15);
g.fillOval(x - 20, y - 17, 40, 36);
g.setColor(black);
g.fillOval(x - 20, y - 18, 40, 36);
g.setColor(grey90);
g.fillOval(x - 20, y - 19, 40, 36);
break;
case 11: // '\013'
g.setColor(grey80);
g.fillOval(x - 15, y - 6, 40, 36);
g.setColor(grey15);
g.fillOval(x - 20, y - 11, 40, 30);
g.setColor(black);
g.fillOval(x - 20, y - 15, 40, 30);
g.setColor(grey90);
g.fillOval(x - 20, y - 19, 40, 30);
break;
case 12: // '\f'
g.setColor(grey80);
g.fillOval(x - 15, y - 6, 40, 36);
g.setColor(grey15);
g.fillOval(x - 20, y, 40, 15);
g.fillOval(x - 20, y - 3, 40, 15);
g.setColor(black);
g.fillOval(x - 20, y - 6, 40, 15);
g.fillOval(x - 20, y - 9, 40, 15);
g.setColor(grey90);
g.fillOval(x - 20, y - 12, 40, 15);
break;
case 13: // '\r'
g.setColor(grey80);
g.fillOval(x - 15, y - 6, 40, 36);
g.setColor(black);
g.fillRect(x - 20, y - 6, 40, 6);
g.setColor(grey10);
g.fillRect(x - 20, y, 40, 6);
break;
case 14: // '\016'
g.setColor(grey80);
g.fillOval(x - 15, y - 6, 40, 36);
g.setColor(black);
g.fillOval(x - 20, y - 12, 40, 15);
g.fillOval(x - 20, y - 9, 40, 15);
g.setColor(grey20);
g.fillOval(x - 20, y - 6, 40, 15);
g.fillOval(x - 20, y - 3, 40, 15);
g.setColor(grey10);
g.fillOval(x - 20, y, 40, 15);
break;
case 15: // '\017'
g.setColor(grey80);
g.fillOval(x - 15, y - 6, 40, 36);
g.setColor(black);
g.fillOval(x - 20, y - 19, 40, 30);
g.setColor(grey20);
g.fillOval(x - 20, y - 15, 40, 30);
g.setColor(grey10);
g.fillOval(x - 20, y - 11, 40, 30);
break;
case 16: // '\020'
g.setColor(grey80);
g.fillOval(x - 15, y - 6, 40, 36);
g.setColor(black);
g.fillOval(x - 20, y - 19, 40, 36);
g.setColor(grey20);
g.fillOval(x - 20, y - 18, 40, 36);
g.setColor(grey10);
g.fillOval(x - 20, y - 17, 40, 36);
break;
}
}
//电脑下棋
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, playerplays
white))
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()
{
boolean returnvalue = true;
for(int x = 0; x < 8; x++)
{
for(int y = 0; y < 8; y++)
{
if(gamegrid[x][y] != -1)
continue;
returnvalue = false;
break;
}
}
return returnvalue;
}
//当棋盘已放满时,判断胜利方,计算黑白各有多少子 ,玩家press mouse to sta
rt 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++;
if(gamegrid[x][y] == 1)
blacktotal++;
}
}
if(playerplayswhite)
{
if(whitetotal > blacktotal)
{
showStatus("You win! Black total = " + blacktotal + ", white t
otal = " + 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 tot
al = " + 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 + ", 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);
}
} 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 tota
l = " + 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()
{
try
{
Thread.sleep(1500);
}catch(InterruptedException e){}
finally
{
return;
}
}
public Turncoat2()
{
BASE = 15;
gamegrid = new int[8][8];
playing = false;
playersmove = true;
playerplayswhite = false;
lastmovewaswhite = false;
allowplay = true;
stolen = false;
}
private Image gameboard;
private static boolean largeboard;
private int BASE;
private int gamegrid[][];
private boolean playing;
private boolean playersmove;
private boolean playerplayswhite;
private boolean lastmovewaswhite;
private boolean allowplay;
private boolean stolen;
private static final boolean LOCKED = false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -