j ava五子棋程序代码
时间: 2023-10-22 08:35:04 浏览: 94
以下是一个简单的Java五子棋程序代码示例:
```java
import java.util.Scanner;
public class FiveChess {
private static final int BOARD_SIZE = 15;
private static char[][] board = new char[BOARD_SIZE][BOARD_SIZE];
public static void main(String[] args) {
initializeBoard();
printBoard();
char player = 'X';
while (true) {
System.out.println("Player " + player + ", make your move (row[1-15] column[1-15]):");
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt() - 1;
int col = scanner.nextInt() - 1;
if (isValidMove(row, col)) {
makeMove(row, col, player);
printBoard();
if (isWinningMove(row, col)) {
System.out.println("Player " + player + " wins!");
break;
}
if (isBoardFull()) {
System.out.println("Game is a draw!");
break;
}
player = (player == 'X') ? 'O' : 'X';
} else {
System.out.println("Invalid move! Try again.");
}
}
}
private static void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = '-';
}
}
}
private static void printBoard() {
System.out.print(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print((i + 1) + " ");
}
System.out.println();
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print((i + 1) + " ");
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
private static boolean isValidMove(int row, int col) {
return (row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == '-');
}
private static void makeMove(int row, int col, char player) {
board[row][col] = player;
}
private static boolean isWinningMove(int row, int col) {
return (checkRow(row) || checkCol(col) || checkDiagonal1(row, col) || checkDiagonal2(row, col));
}
private static boolean checkRow(int row) {
for (int i = 0; i < BOARD_SIZE - 4; i++) {
if (board[row][i] != '-' && board[row][i] == board[row][i + 1] && board[row][i] == board[row][i + 2]
&& board[row][i] == board[row][i + 3] && board[row][i] == board[row][i + 4]) {
return true;
}
}
return false;
}
private static boolean checkCol(int col) {
for (int i = 0; i < BOARD_SIZE - 4; i++) {
if (board[i][col] != '-' && board[i][col] == board[i + 1][col] && board[i][col] == board[i + 2][col]
&& board[i][col] == board[i + 3][col] && board[i][col] == board[i + 4][col]) {
return true;
}
}
return false;
}
private static boolean checkDiagonal1(int row, int col) {
int i = row - 4;
int j = col - 4;
while (i <= row && j <= col) {
if (i >= 0 && i + 4 < BOARD_SIZE && j >= 0 && j + 4 < BOARD_SIZE) {
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;
}
}
i++;
j++;
}
return false;
}
private static boolean checkDiagonal2(int row, int col) {
int i = row + 4;
int j = col - 4;
while (i >= row && j <= col) {
if (i >= 4 && i < BOARD_SIZE && j >= 0 && j + 4 < BOARD_SIZE) {
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;
}
}
i--;
j++;
}
return false;
}
private static boolean isBoardFull() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
}
```
该程序包含五个主要功能:
1. 初始化棋盘
2. 打印当前棋盘
3. 验证移动是否有效
4. 移动并更新棋盘
5. 检查是否有获胜者
该程序使用字符数组来表示棋盘,'X'表示玩家1的棋子,'O'表示玩家2的棋子,'-'表示空格。程序在控制台中打印当前棋盘,并提示玩家输入他们的移动。程序会检查移动是否有效,如果有效,它将在棋盘上更新该位置并检查是否有一个获胜者。如果没有获胜者,程序将在下一轮游戏中切换玩家。如果棋盘已满而没有获胜者,游戏将宣布平局并终止。
阅读全文