Java编编程程实实现现五五子子棋棋人人人人对对战战代代码码示示例例
利用Java,在控制台操作下,编写的五子棋,作为复习二维 组,面向对象等基础知识。w表示白棋,b表示黑棋
import java.util.Scanner;
public class MainMethod {
public static char[][] c = new char[10][10];
public static void main(Strin [] ar s) {
MainMethod mainMethod = new MainMethod();
mainMethod.init();
int i;
boolean color;
int[] point = new int[2];
Scanner scanner = new Scanner(System.in);
for (i = 1; i < 100; i++) {
mainMethod.draw();// 绘制棋盘
System.out.println("请输入坐标,范围为 (0,0)到 (9,9)");
int x = scanner.nextInt();
int y = scanner.nextInt();
if (i % 2 == 1)
color = true;
else
color = false;
point[0] = x;
point[1] = y;
if (mainMethod.paint(point, color) == false) {//
System.out.println("下的位置不合理,请重新开始");
break;
}
if (mainMethod.win() == 1) {
mainMethod.draw();
System.out.println("恭喜白棋赢!!");
break;
} else if (mainMethod.win() == -1) {
mainMethod.draw();
System.out.println("恭喜黑棋赢!!");
break;
}
}
if (i == 100)
System.out.println("平局");
}
public char[][] init() {// 初始化棋盘
for (int i = 0; i < c.len th; i++) {
for (int j = 0; j < c.len th; j++) {
c[i][j] = '+ ';
}
}
return c;
}
public boolean paint(int[] point, boolean colr) {// 判断下的位置是否正确,若正确改变棋盘
boolean b = true;
int x = point[0];
int y = point[1];
if (inBoard(point) == false || c[x][y] == 'w ' || c[x][y] == 'b' || x < 0) {
b = false;
return b;
}
else if (colr == true) {// 白棋下
if (c[x][y] == '+ ') {
c[x][y] = 'w ';
}
}
else if (colr == false) {// 黑棋下
if (c[x][y] == '+ ') {
c[x][y] = 'b';
}
}
return b;
}
public boolean inBoard(int[] point) {// 判断point是否在棋盘上
int x = point[0];
int y = point[1];
if (x < 0 || y < 0 || x >= c.len th || y >= c.len th