使用二维数组实现一个简单的五子棋游戏;黑白棋轮流落子;判断输赢.
import java.util.Scanner;
public class Chess001 {
//棋盘的右方及下方的编号建立数组
static char[] num = {'⒈', '⒉', '⒊', '⒋', '⒌', '⒍', '⒎', '⒏', '⒐', '⒑', '⒒', '⒓', '⒔', '⒕', '⒖'};
//黑棋
static char black = '○';
//白棋
static char white = '●';
//制表符"十"
static char line = '十';
//控制棋盘的大小;本代码最大范围仅15行15列
static int count = 15;
//棋盘数组
static char[][] qp = new char[count][count];
//控制黑棋白棋的接替落子
static boolean flag = true;//黑棋 true 白棋 false
static Scanner scanner = new Scanner(System.in);
//主方法
public static void main(String[] args) {
//初始化棋盘
intqp(count);
//游戏
inChess();
}
//开始进行游戏
public static void inChess() {
while (true) {
//黑棋
if (flag) {
System.out.println("黑棋落子;输入黑棋横纵坐标");
boolean res = position();
//无效位置
if (!res) {
continue;
}
//打印棋盘
Printqp();
//判断黑棋输赢
if (isWin()) {
System.out.println("黑棋获胜,游戏结束");
break;
}
flag = false;
}
//白棋
else {
System.out.println("白棋落子:输入白棋横纵坐标");
boolean res = position();
//无效位置
if (!res) {
continue;
}
//打印棋盘
Printqp();
//判断白棋输赢
if (isWin()) {
System.out.println("白棋获胜,游戏结束");
break;
}
flag = true;
}
}
}
//坐标位置的提示
public static boolean position() {
int x = scanner.nextInt();
int y = scanner.nextInt();
//控制输入坐标不能越界
if ((x < 0 || x > count - 1) && (y < 0 || y > count - 1)) {
System.out.println("该位置无效");
return false;
}
//避免两颗棋子放在同一位置
if (qp[x - 1][y - 1] == black || qp[x - 1][y - 1] == white) {
System.out.println("位置重复");
return false;
}
qp[x - 1][y - 1] = flag ? black : white;
return true;
}
//获胜判断方法
public static boolean isWin() {
//黑棋获胜
if (flag) {
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp[i].length; j++) {
//横向5颗黑棋相连获胜
if (qp[i][j] == black && qp[i][j + 1] == black && qp[i][j + 2] == black && qp[i][j + 3] == black && qp[i][j + 4] == black) {
return true;
}
//纵向5颗黑棋相连获胜
if (qp[i][j] == black && qp[i + 1][j] == black && qp[i + 2][j] == black && qp[i + 3][j] == black && qp[i + 4][j] == black) {
return true;
}
//斜向5颗黑棋相连获胜
if (qp[i][j] == black && qp[i + 1][j + 1] == black && qp[i + 2][j + 2] == black && qp[i + 3][j + 3] == black && qp[i + 4][j + 4] == black) {
return true;
}
}
}
return false;
}
//白棋获胜
else {
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp[i].length; j++) {
//横向向5颗白棋相连获胜
if (qp[i][j] == white && qp[i][j + 1] == white && qp[i][j + 2] == white && qp[i][j + 3] == white && qp[i][j + 4] == white) {
return true;
}
//纵向5颗白棋相连获胜
if (qp[i][j] == white && qp[i + 1][j] == white && qp[i + 2][j] == white && qp[i + 3][j] == white && qp[i + 4][j] == white) {
return true;
}
//斜向5颗白棋相连获胜
if (qp[i][j] == white && qp[i + 1][j + 1] == white && qp[i + 2][j + 2] == white && qp[i + 3][j + 3] == white && qp[i + 4][j + 4] == white) {
return true;
}
}
}
return false;
}
}
//初始化棋盘内容
public static void intqp ( int count){
qp = new char[count][count];
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp[i].length; j++) {
qp[i][j] = line;
if (j == qp.length - 1) {
qp[i][count - 1] = num[i];
}
if (i == count - 1) {
qp[i][j] = num[j];
}
}
}
}
//遍历打印棋盘方法
public static void Printqp () {
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp[i].length; j++) {
System.out.print(qp[i][j]);
}
System.out.println();
}
}
}