帮我写一个java六子棋
时间: 2023-08-28 22:09:20 浏览: 82
好的,六子棋是一种类似于五子棋的游戏,在一个 14 x 14 的棋盘上进行。以下是一个简单的Java实现,你可以参考一下:
```java
import java.util.Scanner;
public class SixChess {
private static final int ROWS = 14;
private static final int COLS = 14;
private static final int EMPTY = 0;
private static final int BLACK = 1;
private static final int WHITE = 2;
private static final int WIN_COUNT = 6;
private int[][] board = new int[ROWS][COLS];
private int turn = BLACK;
public SixChess() {
// 初始化棋盘
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
board[i][j] = EMPTY;
}
}
}
public void play() {
Scanner scanner = new Scanner(System.in);
while (true) {
// 打印棋盘
printBoard();
// 提示下棋
System.out.print(turn == BLACK ? "Black" : "White");
System.out.println(", please enter row and col: ");
// 获取下棋位置
int row = scanner.nextInt();
int col = scanner.nextInt();
// 检查是否合法
if (!isValidMove(row, col)) {
System.out.println("Invalid move, please try again.");
continue;
}
// 更新棋盘
board[row][col] = turn;
// 检查是否胜利
if (isWin(row, col)) {
printBoard();
System.out.println(turn == BLACK ? "Black" : "White" + " wins!");
break;
}
// 下一位玩家
turn = (turn == BLACK) ? WHITE : BLACK;
}
}
private boolean isValidMove(int row, int col) {
return row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY;
}
private boolean isWin(int row, int col) {
int count = 0;
// 横向
for (int i = Math.max(0, col - WIN_COUNT + 1); i <= Math.min(col, COLS - WIN_COUNT); i++) {
count = 0;
for (int j = i; j < i + WIN_COUNT; j++) {
if (board[row][j] == turn) {
count++;
} else {
count = 0;
}
if (count == WIN_COUNT) {
return true;
}
}
}
// 纵向
for (int i = Math.max(0, row - WIN_COUNT + 1); i <= Math.min(row, ROWS - WIN_COUNT); i++) {
count = 0;
for (int j = i; j < i + WIN_COUNT; j++) {
if (board[j][col] == turn) {
count++;
} else {
count = 0;
}
if (count == WIN_COUNT) {
return true;
}
}
}
// 对角线
int startRow = Math.max(row - WIN_COUNT + 1, 0);
int startCol = Math.max(col - WIN_COUNT + 1, 0);
int endRow = Math.min(row + WIN_COUNT - 1, ROWS - 1);
int endCol = Math.min(col + WIN_COUNT - 1, COLS - 1);
for (int i = startRow; i <= endRow - WIN_COUNT + 1; i++) {
count = 0;
for (int j = startCol; j <= endCol && i + j - row >= 0 && i + j - row < ROWS; j++) {
if (board[i + j - row][j] == turn) {
count++;
} else {
count = 0;
}
if (count == WIN_COUNT) {
return true;
}
}
}
for (int i = startRow; i <= endRow - WIN_COUNT + 1; i++) {
count = 0;
for (int j = endCol; j >= startCol && i + endCol - j >= 0 && i + endCol - j < ROWS; j--) {
if (board[i + endCol - j][j] == turn) {
count++;
} else {
count = 0;
}
if (count == WIN_COUNT) {
return true;
}
}
}
return false;
}
private void printBoard() {
System.out.print(" ");
for (int i = 0; i < COLS; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < ROWS; i++) {
System.out.print(i);
for (int j = 0; j < COLS; j++) {
System.out.print(board[i][j] == EMPTY ? " +" : board[i][j] == BLACK ? " x" : " o");
}
System.out.println();
}
}
public static void main(String[] args) {
SixChess game = new SixChess();
game.play();
}
}
```
这个实现使用了一个二维数组来表示棋盘,其中 `board[i][j]` 表示第 i 行、第 j 列的位置的状态,可以是 EMPTY(空)、BLACK(黑子)或 WHITE(白子)。 `turn` 变量表示当前玩家的颜色,可以是 BLACK 或 WHITE。在 `play()` 方法中,使用 `Scanner` 类获取玩家下棋的位置,并使用 `isValidMove()` 方法检查是否合法。如果合法,就更新棋盘并检查是否胜利。如果有一方胜利,游戏结束。
`isWin()` 方法检查是否胜利。它首先检查横向和纵向是否有连续的 WIN_COUNT 颗同色棋子,然后检查两个方向的对角线是否有连续的 WIN_COUNT 颗同色棋子。
最后,`printBoard()` 方法用于打印当前棋盘的状态。
阅读全文