五子棋代码java随机 五子棋代码js(14)


System.exit(0);
}
});// 关闭窗体
}
public void keyPressed(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_ENTER :
if (ziShu % 2 == 0)// 子数来判断谁赢
{
heiXia();
} else {
baiXia();
}
shui = shuiYing();
break;
case KeyEvent.VK_ESCAPE :
System.exit(1);
break;
case KeyEvent.VK_LEFT :
guangBiaoDong(-1, 0);// 光标动的坐标
break;
case KeyEvent.VK_RIGHT :
guangBiaoDong(1, 0);// 光标动的坐标
break;
case KeyEvent.VK_DOWN :
guangBiaoDong(0, 1);// 光标动的坐标
break;
case KeyEvent.VK_UP :
guangBiaoDong(0, -1);// 光标动的坐标
break;
}
frame.repaint();
}
@SuppressWarnings("serial")
class Screen extends JComponent {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
xianShi(g2d);// 画图
}
}
public static void main(String[] args) {
new Five();
}
void xianShi(Graphics2D g2d)// 显示
{
xianShiQiPan(g2d);// 显示棋盘
xianShiQiZis(g2d);// 显示旗子
xianShiGuangBiao(g2d);// 显示光标
g2d.setFont(new Font("STXingkai", 1, 30));// 0708第二小组
g2d.setColor(Color.black);
g2d.drawString("欢乐五子棋Java版", 280, 580);
g2d.setFont(new Font("stCaiyun", 1, 40));
if (shui == 0)// 判断
{
g2d.setColor(Color.black);
xianShiTiShiXia(g2d);// 显示谁下棋
} else {
g2d.setColor(Color.black);
xianShiTiShiYing(g2d); // 显示谁赢棋
}
}
void guangBiaoDong(int fangX, int fangY)// //光标动
{
if (gbx + fangX10 || gbx + fangX0 || gby + fangY10|| gby + fangY0) {
} else {
gbx = gbx + fangX;
gby = gby + fangY;
}
}
void heiXia()// 黑棋
{
if (pans[gby][gbx] == 0) {
pans[gby][gbx] = -1;
ziShu++;
}
}
void baiXia()// 白棋
{
if (pans[gby][gbx] == 0) {
pans[gby][gbx] = 1;
ziShu++;
}
}
int shuiYing()// 0: 1:bai -1:hei谁赢拉
{
return kanFangShuiYing(1, 0) + kanFangShuiYing(0, 1)
+ kanFangShuiYing(1, 1) + kanFangShuiYing(1, -1);
}
// ---------------------------------------------
void xianShiQiPan(Graphics2D g2d)// 显示棋盘
{
g2d.setColor(Color.pink);
g2d.fillRect(0, 0, 40 * 12, 40 * 12);// 0,0,40*12,40*12
g2d.setColor(Color.BLACK);
// 行格
for (int i = 0; i11; i++)// 40,行*40+40,440,行*40+40
{
g2d.drawLine(40, i * 40 + 40, 440, i * 40 + 40);
}
// 列格
for (int k = 0; k11; k++)// 列*40+40,40,列*40+40,440
{
g2d.drawLine(k * 40 + 40, 40, k * 40 + 40, 440);
}
}
void xianShiQiZis(Graphics2D g2d)// 显示旗子
{
for (int h = 0; h11; h++) {
for (l = 0; l11; l++) {
switch (pans[h][l]) {
case 1 :
g2d.setColor(Color.BLACK);
g2d.fillOval(40 - 15 + l * 40, 40 - 15 + h * 40, 30, 30);// (40-15+l*40,40-15+h*40,30,30);
break;
case -1 :
g2d.setColor(Color.white);
g2d.fillOval(40 - 15 + l * 40, 40 - 15 + h * 40, 30, 30);// (40-15+l*40,40-15+h*40,30,30);
break;
case 0 :
break;
}
}
}
}
void xianShiGuangBiao(Graphics2D g2d)// 显示光标
{
g2d.setColor(Color.blue);
g2d.fillOval(40 - 5 + gbx * 40, 40 - 5 + gby * 40, 10, 10);
}
@SuppressWarnings("deprecation")
void xianShiTiShiYing(Graphics2D g2d)// 显示谁赢
{
switch (shui) {
case 1 :
// 黑棋
g2d.setColor(Color.black);
g2d.drawString("黑棋win", 200, 520);
JOptionPane pane = new JOptionPane("黑棋胜利^_^");
JDialog dialog = pane.createDialog(frame, "提示...");
dialog.show();

推荐阅读