关键词

Java 五子棋游戏

编写五子棋小游戏的Java源代码

五子棋是一款非常经典的棋类游戏,它可以锻炼玩家的思维能力和判断能力。在这篇文章中,我们将介绍如何使用 Java 语言编写一个简单的五子棋游戏,包含完整的源代码。

游戏规则

五子棋的规则非常简单:两个玩家轮流将棋子下在棋盘上,先在横向、纵向或者斜向形成连续的五个棋子的一方获胜。

程序设计

为了实现五子棋游戏,我们需要设计以下几个类:

  • Chessboard 类:表示棋盘,包含棋盘大小和存储棋子的数组。
  • Player 类:表示玩家,包含玩家姓名和棋子颜色。
  • Game 类:表示游戏,包含两个玩家和棋盘对象,并提供下棋、判断胜负等方法。

下面是完整的 Java 源代码:

import java.util.Scanner;

public class Chessboard {
    int size;  // 棋盘大小
    char[][] board;  // 存储棋子的数组
    
    public Chessboard(int size) {
        this.size = size;
        board = new char[size][size];
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                board[i][j] = '-';
            }
        }
    }
    
    public void printBoard() {
        System.out.println(" ");
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }
}

public class Player {
    String name;
    char color;
    
    public Player(String name, char color) {
        this.name = name;
        this.color = color;
    }
}

public class Game {
    Chessboard board;
    Player player1, player2;
    boolean gameOver;
    Scanner scanner;
    
    public Game(int size) {
        board = new Chessboard(size);
        player1 = new Player("Player 1", 'x');
        player2 = new Player("Player 2", 'o');
        gameOver = false;
        scanner = new Scanner(System.in);
    }
    
    public void playGame() {
        Player currentPlayer = player1;
        while (!gameOver) {
            board.printBoard();
            System.out.println(currentPlayer.name + "'s turn:");
            int row = scanner.nextInt();
            int col = scanner.nextInt();
            if (isValidMove(row, col)) {
                board.board[row][col] = currentPlayer.color;
                if (isWinningMove(row, col)) {
                    System.out.println(currentPlayer.name + " wins!");
                    gameOver = true;
                } else if (isBoardFull()) {
                    System.out.println("It's a tie!");
                    gameOver = true;
                } else {
                    currentPlayer = (currentPlayer == player1) ? player2 : player1;
                }
            } else {
                System.out.println("Invalid move, try again!");
            }
        }
    }
    
    public boolean isValidMove(int row, int col) {
        return (row >= 0 && row < board.size && col >= 0 && col < board.size && board.board[row][col] == '-');
    }
    
    public boolean isWinningMove(int row, int col) {
        char c = board.board[row][col];
        int count = 1;
        int i, j;
        
        // 横向判断
        i = row;
        j = col - 1;
        while (j >= 0 && board.board[i][j] == c) {
            count++;
            j--;
        }
        j = col + 1;
        while (j < board.size && board.board[i][j] == c) {
            count++;
            j++;
        }
        if (count >= 5) {
            return true;
        }
        
        // 纵向判断
        count = 1;
        i = row - 1;
        j = col;
        while(i >= 0 && board.board[i][j] == c) {
        count++;
        i--;
		}
		i = row + 1;
		while (i < board.size && board.board[i][j] == c) {
			count++;
			i++;
		}
		if (count >= 5) {
			return true;
		}
		
		// 左上到右下斜向判断
		count = 1;
		i = row - 1;
		j = col - 1;
		while (i >= 0 && j >= 0 && board.board[i][j] == c) {
			count++;
			i--;
			j--;
		}
		i = row + 1;
		j = col + 1;
		while (i < board.size && j < board.size && board.board[i][j] == c) {
			count++;
			i++;
			j++;
		}
		if (count >= 5) {
			return true;
		}
		
		// 右上到左下斜向判断
		count = 1;
		i = row - 1;
		j = col + 1;
		while (i >= 0 && j < board.size && board.board[i][j] == c) {
			count++;
			i--;
			j++;
		}
		i = row + 1;
		j = col - 1;
		while (i < board.size && j >= 0 && board.board[i][j] == c) {
			count++;
			i++;
			j--;
		}
		if (count >= 5) {
			return true;
		}
		
		return false;
	}
	
	public boolean isBoardFull() {
		for (int i = 0; i < board.size; i++) {
			for (int j = 0; j < board.size; j++) {
				if (board.board[i][j] == '-') {
					return false;
				}
			}
		}
		return true;
	}
	}
	
	public class Main {
	public static void main(String[] args) {
	Game game = new Game(15);
	game.playGame();
	}
}

程序运行

在编写完上述 Java 源代码后,我们可以使用 Java 编译器将其编译成字节码文件,并运行程序。

在命令行中进入源码文件所在的目录,使用以下命令编译 Java 源代码:

javac Main.java

使用以下命令运行程序:

java Main

程序将显示棋盘和当前玩家,玩家输入下棋的坐标即可进行游戏。

结论

通过编写五子棋游戏的 Java 源代码,我们学习了如何使用面向对象的思想设计游戏程序,并掌握了 Java 语言的基本语法知识。这种学习方式不仅提高了我们的编程能力,还增强了我们的逻辑思维和创造性思维能力。

本文链接:http://task.lmcjl.com/news/7345.html

展开阅读全文