帮我写一个下五子棋的java代码
时间: 2023-03-26 12:00:49 浏览: 135
javawuziqi.rar_五子棋代码 -java
好的,我可以回答这个问题。下面是一个简单的五子棋Java代码:
import java.util.Scanner;
public class FiveInARow {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char[][] board = new char[15][15];
boolean isBlack = true;
int x, y;
while (true) {
// 打印棋盘
for (int i = ; i < 15; i++) {
for (int j = ; j < 15; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
// 判断胜负
if (isWin(board)) {
System.out.println(isBlack ? "黑方胜利!" : "白方胜利!");
break;
}
// 下棋
System.out.println(isBlack ? "黑方下棋:" : "白方下棋:");
x = scanner.nextInt();
y = scanner.nextInt();
if (x < || x >= 15 || y < || y >= 15 || board[x][y] != ) {
System.out.println("无效的位置,请重新输入!");
continue;
}
board[x][y] = isBlack ? '●' : '○';
isBlack = !isBlack;
}
}
// 判断胜负
private static boolean isWin(char[][] board) {
// 判断横向
for (int i = ; i < 15; i++) {
for (int j = ; j < 11; j++) {
if (board[i][j] != && board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2] && board[i][j] == board[i][j + 3] && board[i][j] == board[i][j + 4]) {
return true;
}
}
}
// 判断纵向
for (int i = ; i < 11; i++) {
for (int j = ; j < 15; j++) {
if (board[i][j] != && board[i][j] == board[i + 1][j] && board[i][j] == board[i + 2][j] && board[i][j] == board[i + 3][j] && board[i][j] == board[i + 4][j]) {
return true;
}
}
}
// 判断斜向
for (int i = ; i < 11; i++) {
for (int j = ; j < 11; j++) {
if (board[i][j] != && board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] == board[i + 3][j + 3] && board[i][j] == board[i + 4][j + 4]) {
return true;
}
}
}
// 判断反斜向
for (int i = ; i < 11; i++) {
for (int j = 4; j < 15; j++) {
if (board[i][j] != && board[i][j] == board[i + 1][j - 1] && board[i][j] == board[i + 2][j - 2] && board[i][j] == board[i + 3][j - 3] && board[i][j] == board[i + 4][j - 4]) {
return true;
}
}
}
return false;
}
}
阅读全文