用Java编写五子棋
时间: 2023-09-08 13:14:56 浏览: 83
用JAVA语言编写五子棋
好的,以下是一个简单的Java五子棋游戏实现示例:
首先,我们需要创建一个棋盘类来表示游戏状态。这个类应该包含一个二维数组来表示每个位置的棋子状态,一个方法来判断游戏是否结束,以及一些方法来放置棋子和计算得分等。
```java
public class ChessBoard {
private int[][] board;
private int size;
private int winLength;
public ChessBoard(int size, int winLength) {
this.board = new int[size][size];
this.size = size;
this.winLength = winLength;
}
public boolean placeChess(int x, int y, int color) {
if (x < 0 || x >= size || y < 0 || y >= size || board[x][y] != 0) {
return false;
}
board[x][y] = color;
return true;
}
public boolean isGameOver() {
return checkHorizontal() || checkVertical() || checkDiagonal();
}
private boolean checkHorizontal() {
for (int i = 0; i < size; i++) {
for (int j = 0; j <= size - winLength; j++) {
int count = 0;
for (int k = 0; k < winLength; k++) {
if (board[i][j+k] == board[i][j]) {
count++;
}
}
if (count == winLength) {
return true;
}
}
}
return false;
}
private boolean checkVertical() {
for (int i = 0; i <= size - winLength; i++) {
for (int j = 0; j < size; j++) {
int count = 0;
for (int k = 0; k < winLength; k++) {
if (board[i+k][j] == board[i][j]) {
count++;
}
}
if (count == winLength) {
return true;
}
}
}
return false;
}
private boolean checkDiagonal() {
for (int i = 0; i <= size - winLength; i++) {
for (int j = 0; j <= size - winLength; j++) {
int count1 = 0;
int count2 = 0;
for (int k = 0; k < winLength; k++) {
if (board[i+k][j+k] == board[i][j]) {
count1++;
}
if (board[i+k][j+winLength-1-k] == board[i][j]) {
count2++;
}
}
if (count1 == winLength || count2 == winLength) {
return true;
}
}
}
return false;
}
public int getScore(int color) {
int score = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == color) {
score++;
}
}
}
return score;
}
}
```
接下来,我们需要创建一个玩家类来放置棋子。这个类可以通过控制台或图形用户界面实现。
```java
public class Player {
private int color;
public Player(int color) {
this.color = color;
}
public int getColor() {
return color;
}
public void makeMove(ChessBoard board) {
Scanner scanner = new Scanner(System.in);
System.out.print("Player " + color + " make a move (x y): ");
int x = scanner.nextInt();
int y = scanner.nextInt();
if (!board.placeChess(x, y, color)) {
System.out.println("Invalid move, please try again.");
makeMove(board);
}
}
}
```
最后,我们需要创建一个游戏控制类来协调棋盘和玩家之间的交互。
```java
public class Game {
private ChessBoard board;
private Player[] players;
private int currentPlayerIndex;
public Game(int size, int winLength, Player[] players) {
this.board = new ChessBoard(size, winLength);
this.players = players;
this.currentPlayerIndex = 0;
}
public void start() {
while (!board.isGameOver()) {
Player currentPlayer = players[currentPlayerIndex];
board.print();
currentPlayer.makeMove(board);
currentPlayerIndex = (currentPlayerIndex + 1) % players.length;
}
board.print();
int winner = getWinner();
if (winner == 0) {
System.out.println("Game ended in a draw.");
} else {
System.out.println("Player " + winner + " wins!");
}
}
private int getWinner() {
int score1 = board.getScore(players[0].getColor());
int score2 = board.getScore(players[1].getColor());
if (score1 > score2) {
return players[0].getColor();
} else if (score2 > score1) {
return players[1].getColor();
} else {
return 0;
}
}
}
```
现在我们可以创建两个玩家并启动游戏了:
```java
public static void main(String[] args) {
Player player1 = new Player(1);
Player player2 = new Player(2);
Game game = new Game(15, 5, new Player[] { player1, player2 });
game.start();
}
```
这是一个非常基本的五子棋游戏实现示例,您可以根据自己的需要和技术水平进行修改和扩展。
阅读全文