java五子棋
时间: 2023-08-10 09:05:55 浏览: 175
以下是一个简单的 Java 五子棋游戏的实现,希望能够帮助到你:
```java
import java.util.Scanner;
public class GobangGame {
private static final int BOARD_SIZE = 15;
private String[][] board = new String[BOARD_SIZE][BOARD_SIZE];
private String currentPlayer;
private Scanner scanner;
public GobangGame() {
scanner = new Scanner(System.in);
currentPlayer = "X";
initializeBoard();
}
public void start() {
printBoard();
while (true) {
System.out.println("It's " + currentPlayer + "'s turn.");
int row = getRow();
int col = getCol();
if (isValidMove(row, col)) {
board[row][col] = currentPlayer;
printBoard();
if (isWinningMove(row, col)) {
System.out.println("Congratulations, " + currentPlayer + " wins!");
break;
}
if (isBoardFull()) {
System.out.println("The game is a tie!");
break;
}
currentPlayer = currentPlayer.equals("X") ? "O" : "X";
} else {
System.out.println("Invalid move, please try again.");
}
}
scanner.close();
}
private int getRow() {
System.out.print("Enter row number (1-" + BOARD_SIZE + "): ");
return scanner.nextInt() - 1;
}
private int getCol() {
System.out.print("Enter column number (1-" + BOARD_SIZE + "): ");
return scanner.nextInt() - 1;
}
private boolean isValidMove(int row, int col) {
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
return false;
}
return board[row][col].equals("-");
}
private boolean isWinningMove(int row, int col) {
String target = board[row][col];
// Check horizontal
for (int i = Math.max(col - 4, 0); i <= Math.min(col, BOARD_SIZE - 5); i++) {
if (board[row][i].equals(target) &&
board[row][i + 1].equals(target) &&
board[row][i + 2].equals(target) &&
board[row][i + 3].equals(target) &&
board[row][i + 4].equals(target)) {
return true;
}
}
// Check vertical
for (int i = Math.max(row - 4, 0); i <= Math.min(row, BOARD_SIZE - 5); i++) {
if (board[i][col].equals(target) &&
board[i + 1][col].equals(target) &&
board[i + 2][col].equals(target) &&
board[i + 3][col].equals(target) &&
board[i + 4][col].equals(target)) {
return true;
}
}
// Check diagonal (top-left to bottom-right)
for (int i = Math.max(row - 4, 0), j = Math.max(col - 4, 0); i <= Math.min(row, BOARD_SIZE - 5) && j <= Math.min(col, BOARD_SIZE - 5); i++, j++) {
if (board[i][j].equals(target) &&
board[i + 1][j + 1].equals(target) &&
board[i + 2][j + 2].equals(target) &&
board[i + 3][j + 3].equals(target) &&
board[i + 4][j + 4].equals(target)) {
return true;
}
}
// Check diagonal (bottom-left to top-right)
for (int i = Math.min(row + 4, BOARD_SIZE - 1), j = Math.max(col - 4, 0); i >= Math.max(row, 4) && j <= Math.min(col, BOARD_SIZE - 5); i--, j++) {
if (board[i][j].equals(target) &&
board[i - 1][j + 1].equals(target) &&
board[i - 2][j + 2].equals(target) &&
board[i - 3][j + 3].equals(target) &&
board[i - 4][j + 4].equals(target)) {
return true;
}
}
return false;
}
private boolean isBoardFull() {
for (String[] row : board) {
for (String cell : row) {
if (cell.equals("-")) {
return false;
}
}
}
return true;
}
private void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = "-";
}
}
}
private 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();
}
}
public static void main(String[] args) {
GobangGame game = new GobangGame();
game.start();
}
}
```
实现的五子棋游戏比较简单,玩家可以通过控制台输入行列坐标来下棋,程序会检查输入是否合法,并判断胜负和平局。你可以根据需要进行修改和优化。
阅读全文