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


System.exit(0);
break;
case -1 :
// 白棋
g2d.setColor(Color.magenta);
g2d.drawString("白棋win", 200, 520);
JOptionPane pane1 = new JOptionPane("白棋胜利^_^");
JDialog dialog1 = pane1.createDialog(frame, "提示...");
dialog1.show();
System.exit(0);
break;
}
}
void xianShiTiShiXia(Graphics2D g2d)// //显示提示下一个棋子
{
switch (ziShu % 2) {
case 0 :
g2d.setColor(Color.magenta);// 黑棋
g2d.drawString("提示:", 10, 520);
g2d.setColor(Color.orange);
g2d.drawString("白", 150, 520);
g2d.setColor(Color.magenta);
g2d.drawString("棋下", 195, 520);
break;
case 1 :
g2d.setColor(Color.magenta);// 黑棋
g2d.drawString("提示:", 10, 520);
g2d.setColor(Color.black);
g2d.drawString("黑", 150, 520);
g2d.setColor(Color.magenta);
g2d.drawString("棋下", 195, 520);
break;
}
}
int kanFangShuiYing(int fx, int fy) {
int kanx = gbx;
int kany = gby;
int shu = 1;
// zuo
while (kanx + fx = 0kanx + fx11kany + fy = 0 kany + fy11 pans[kany+ fy][kanx + fx] == pans[gby][gbx]) {
shu++;
kany = kany + fy;
kanx = kanx + fx;
}
// you
kanx = gbx;
kany = gby;
while (kanx - fx = 0kanx - fx11kany - fy = 0 kany - fy11 pans[kany -fy][kanx - fx] == pans[gby][gbx]) {
shu++;
kany = kany - fy;
kanx = kanx - fx;
}
return (shu = 5 ? pans[gby][gbx] : 0);
}
};
运行结果:
二维数组做五子棋(代码)package com.test;
import java.util.Scanner;
public classTestMain{
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
char[] name = { '空', '黑', '白' };
int count = 0;// 计数器,用来计算该谁下棋了
char[][] qi = new char[16][16];// 把棋盘情况放进二维数组中
while (true) {
int row = 0;// 初始化从控制台接收到的row和col 的index为0
int col = 0;
if (count == 0) {
print(qi, name[count], row, col);
count++;
}
if (count == 1) {
System.out.println("请黑(@)下棋(i j):");
row = console.nextInt(16)%16;// 从控制台接收到的row的index
col = console.nextInt(16)%16;// 从控制台接收到的col的index
String condition = print(qi, name[count], row, col);
if (condition.length() == 1) {// 下棋正确
count++;
}
if (condition.length() == 2) {// 下棋出现错误 , 要下的位置已经下过棋子
System.out.println("此位置已经有棋子,黑棋重新再下");
}
if (condition.length() == 3) {// 黑棋子赢
System.out.println("黑棋很给力,白棋是浮云,黑棋赢了");
break;
}
if (condition.length() == 4) {
System.out.println("白棋子赢");
break;
}
}
if (count == 2) {
System.out.println("请白(*)下棋(i j):");
row = console.nextInt(16);
col = console.nextInt(16);
String condition = print(qi, name[count], row, col);
if (condition.length() == 1) {// 打印棋盘
count--;
}
if (condition.length() == 2) {// 下棋出现错误,要下的位置已经下过棋子
System.out.println("此位置已经有棋子,白棋重新再下");
}
if (condition.length() == 3) {
System.out.println("黑棋子赢");
break;
}
if (condition.length() == 4) {
System.out.println("白棋很给力,黑棋是浮云,白棋赢了");
break;
}
}
}
}
/**
* @param qi是装棋盘情况
* @param name轮到谁下棋了
*row 行号 col 列号 return true-下棋正确,false-下棋错误,重新下
*/
public static String print(char[][] qi, char name, int row, int col) {

推荐阅读