新手java五子棋完整代码
javaimport java.util.Scanner;
public class Gobang {
private static final int BOARD_SIZE = 15;
private static final char EMPTY = '.';
private static final char BLACK = 'X';
private static final char WHITE = 'O';
private char[][] board;
public Gobang() {
board = new char[BOARD_SIZE][BOARD_SIZE];
initializeBoard();
}
private void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
private void printBoard() {
System.out.println(" A B C D E F G H I J K L M N O");
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 boolean makeMove(int row, int col, char player) {
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE || board[row][col] != EMPTY) {
System.out.println("Invalid move. Try again.");
return false;
}
board[row][col] = player;
return true;
}
private boolean checkWin(int row, int col, char player) {
// Check horizontal
for (int i = 0; i < 5; i++) {
if (col + i < BOARD_SIZE && board[row][col + i] == player) {
return true;
}
}
// Check vertical
for (int i = 0; i < 5; i++) {
if (row + i < BOARD_SIZE && board[row + i][col] == player) {
return true;
}
}
// Check diagonal
for (int i = 0; i < 5; i++) {
if (row + i < BOARD_SIZE && col + i < BOARD_SIZE && board[row + i][col + i] == player) {
return true;
}
}
// Check reverse diagonal
for (int i = 0; i < 5; i++) {
if (row - i >= 0 && col + i < BOARD_SIZE && board[row - i][col + i] == player) {
return true;
}
}
return false;
}
public void playGame() {
Scanner scanner = new Scanner(System.in);
char currentPlayer = BLACK;
while (true) {
printBoard();
System.out.println("Player " + currentPlayer + "'s turn.");
System.out.print("Enter your move (e.g., A3): ");
String move = scanner.nextLine().toUpperCase();
if (move.length() != 2) {
System.out.println("Invalid move format. Try again.");
continue;
}
int col = move.charAt(0) - 'A';
int row = move.charAt(1) - '1';
if (makeMove(row, col, currentPlayer)) {
if (checkWin(row, col, currentPlayer)) {
printBoard();
System.out.println("Player " + currentPlayer + " wins!");
break;
}
if (currentPlayer == BLACK) {
currentPlayer = WHITE;
} else {
currentPlayer = BLACK;
}
}
}
scanner.close();
}
public static void main(String[] args) {
Gobang game = new Gobang();
game.playGame();
}
}
此代码实现了一个简单的控制台五子棋游戏。玩家轮流输入他们的移动,程序会检查是否有一方获胜。此代码仅供学习和演示目的,可能需要根据实际需求进行修改和扩展。
javaimport java.util.Scanner;
public class Gobang {
private static final int BOARD_SIZE = 15;
private static final char EMPTY = '.';
private static final char BLACK = 'X';
private static final char WHITE = 'O';
private char[][] board;
public Gobang() {
board = new char[BOARD_SIZE][BOARD_SIZE];
initializeBoard();
}
private void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
private void printBoard() {
System.out.println(" A B C D E F G H I J K L M N O");
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 boolean makeMove(int row, int col, char player) {
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE || board[row][col] != EMPTY) {
System.out.println("Invalid move. Try again.");
return false;
}
board[row][col] = player;
return true;
}
private boolean checkWin(int row, int col, char player) {
// Check horizontal
for (int i = 0; i < 5; i++) {
if (col + i < BOARD_SIZE && board[row][col + i] == player) {
return true;
}
}
// Check vertical
for (int i = 0; i < 5; i++) {
if (row + i < BOARD_SIZE && board[row + i][col] == player) {
return true;
}
}
// Check diagonal
for (int i = 0; i < 5; i++) {
if (row + i < BOARD_SIZE && col + i < BOARD_SIZE && board[row + i][col + i] == player) {
return true;
}
}
// Check reverse diagonal
for (int i = 0; i < 5; i++) {
if (row - i >= 0 && col + i < BOARD_SIZE && board[row - i][col + i] == player) {
return true;
}
}
return false;
}
public void playGame() {
Scanner scanner = new Scanner(System.in);
char currentPlayer = BLACK;
while (true) {
printBoard();
System.out.println("Player " + currentPlayer + "'s turn.");
System.out.print("Enter your move (e.g., A3): ");
String move = scanner.nextLine().toUpperCase();
if (move.length() != 2) {
System.out.println("Invalid move format. Try again.");
continue;
}
int col = move.charAt(0) - 'A';
int row = move.charAt(1) - '1';
if (makeMove(row, col, currentPlayer)) {
if (checkWin(row, col, currentPlayer)) {
printBoard();
System.out.println("Player " + currentPlayer + " wins!");
break;
}
if (isBoardFull()) {
printBoard();
System.out.println("It's a draw!");
break;
}
if (currentPlayer == BLACK) {
currentPlayer = WHITE;
} else {
currentPlayer = BLACK;
}
}
}
scanner.close();
}
private boolean isBoardFull() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
Gobang game = new Gobang();
game.playGame();
}
}
这个版本增加了检查平局的逻辑,当棋盘满了且没有玩家获胜时,它将宣布平局。同样,