AI游戏的Java代码

时间:2015-05-04 10:10:36

标签: java arrays artificial-intelligence chess

我目前正在开发类似于跳棋的Java游戏。

有两个玩家,player Xplayer Y。我不想要这个游戏的GUI,只是下面的输出。在游戏中,玩家X[0,0]开始,玩家Y[7,7]开始。

我的第一个问题是我不确定如何输入[3,6],让玩家X转移到[3,6]

当玩家X[0,0]移出时,[0,0]应该被标记为不可用,因此,任何玩家都无法前往该空间或跳过它,这是我的第二个问题因为我不确定如何做到这一点。

游戏的目的是让其中一名玩家处于无法移动的位置。我们打算在其中添加不同的AI,但如果我得到基础工作,我可以尝试开发AI。

我不是最强大的程序员,因此如果我的代码可以进行编辑以使事情变得更容易,那么任何帮助都将受到赞赏。谢谢

我的代码如下:

public static AIProject {

    public static String [][] board = new String[9][9];

    public static void addPiece(int x, int y, String r) {
        board [x][y] = r;
    }

    public static void showBoard() {
        for(int row = 0; row < board.length; row++) {
            System.out.println(" ");
            System.out.println("-------------------");

            for (int col = 0; col < board[row].length; col++) {
                System.out.print("I");
                if(board[col][row] == null) {
                    System.out.print(" ");
                } else {
                    System.out.print(board[col][row]);
                }
            }
        }

        System.out.println(" ");
        System.out.println("-------------------");
    }   

    public static void mainn(String[] args) {
        System.out.println(board.length);

        addPiece(0,0,"0");
        addPiece(0,1,"1");
        addPiece(0,2,"2");
        addPiece(0,3,"3");
        addPiece(0,4,"4");
        addPiece(0,5,"5");
        addPiece(0,6,"6");
        addPiece(0,7,"7");
        addPiece(0,8,"8");
        addPiece(1,0,"1");
        addPiece(2,0,"2");
        addPiece(3,0,"3");
        addPiece(4,0,"4");
        addPiece(5,0,"5");
        addPiece(6,0,"6");
        addPiece(7,0,"7");
        addPiece(8,0,"8");

        addPiece(1,1,"X");
        addPiece(8,8,"Y");

        showBoard();
     }
}

1 个答案:

答案 0 :(得分:1)

  

我的第一个问题是我不知道如何输入即[3,6],让玩家X转移到[3,6]。

你可以使用一些有意义的坐标 - 例如,如果用户想要去[3,6],那么一个进入36等。坐标的标签应该在船上可见。

  

当玩家X从[0,0]移动时,[0,0]应该被标记为不可用,因此,任何一个玩家都不能去那个空间或跳过它,这是我的第二个问题,因为我不是确定如何做到这一点。

您可以创建新类,例如BoardItem - 它将包含isOccupied和标签字段 - 如果字段被占用,则标签将显示播放器标签,或者字段未被占用的坐标。

我还建议将数组从1开始计数,而不是从0开始。这对用户来说更有意义 - “当我想要进入1行时,1列我必须输入11”。

我建议的解决方案:

public static AIProject {

private static int BOARD_SIZE = 8;

static BoardItem[][] board = new BoardItem[BOARD_SIZE][BOARD_SIZE];

// available players
public static String[] players = { "X", "Y" };

// x,y are the coordinates, label is the string, it can take coordinates or
// player name if the field is occupied
public static void addPiece(int x, int y, String label, boolean isOccupied)    {
// here we are assigning to a specific board piece a BoardItem - label      and if it
// is occupied
    board[x][y] = new BoardItem(label, isOccupied);
}

public static void showBoard() {
    for (int row = 0; row < BOARD_SIZE; row++) {
        for (int col = 0; col < BOARD_SIZE; col++) {
            if (board[col][row] == null) {
                System.out.print("\t");
            } else {
                System.out.print(board[col][row] + "\t");
            }
        }
        System.out.println("\n_____________________________________________________________");
    }
}

public static void main(String[] args) {
    // board creating
    for (int x = 1; x < BOARD_SIZE ; x++) {
        for (int y = 1; y < BOARD_SIZE ; y++) {
          // as you can see in method definition we are passing coordinates 
         // as integers in 2 first arguments, next coordinates as string
         // this "label" would be displayed on board
         // the last parameter is indicating that the field are not occupied
            addPiece(x, y, x + "" + y, false);
        }
    }
    // however, you want to point that 1,1 is occupied by player 'X'...
    addPiece(1, 1, players[0], true);
    // and 7,7 is occupied by player 'Y'
    addPiece(7, 7, players[1], true);

    // 'X' is active player now
    String activePlayer = players[0];
    Scanner keyboard = new Scanner(System.in);

    // infinite loop - in the loop condition you should check
    // if someone lose/win to end the game
    while (true) {
        // clearing output
        clearScreen();
        // showing board            
        showBoard();
        // moving player - waiting for input from player and then 
        // mark chosen coordinates as occupied by active player
        playerMove(activePlayer, keyboard);
        // changing active player. First X, then Y, then X and so on.
        activePlayer = changeActivePlayer(activePlayer);
    }

}

private static void playerMove(String player, Scanner keyboard) {

    BoardItem boardItem = getBoardItemForPosition(keyboard.next());
    // it should be isNotValid
    while (boardItem.isOccupied()) {
        System.out.println("Chosen position is occupied! Please choose another one");
        boardItem = getBoardItemForPosition(keyboard.next());
    }
    boardItem.setOccupied(true);
    boardItem.setLabel(player);
}

private static BoardItem getBoardItemForPosition(String position) {
    int x = Integer.parseInt(position.substring(0, 1));
    int y = Integer.parseInt(position.substring(1, 2));
    return board[x][y];
}

private static String changeActivePlayer(String activePlayer) {
    if (activePlayer.equals(players[0])) {
        return players[1];
    }
    return players[0];
}

private static void clearScreen() {
    for (int i = 0; i < 50; ++i)
        System.out.println();
}
}

BoardItem类:

public class BoardItem {

private String label;

private boolean isOccupied;

public BoardItem(String label, boolean isOccupied) {
    this.label = label;
    this.isOccupied = isOccupied;
}

public String getLabel() {
    return label;
}

public void setLabel(String label) {
    this.label = label;
}

public boolean isOccupied() {
    return isOccupied;
}

public void setOccupied(boolean isOccupied) {
    this.isOccupied = isOccupied;
}

@Override
public String toString() {
    return label;
}

}

另一个提示:

  • 使用main方法的类应该只启动应用程序,整个逻辑应该在别处;
  • 您应该为用户输入编写验证,并检查用户是否输入了正确的输入 - 适当范围内的2个数字,如果字段是否被占用以及用户是否可以移动到指定字段;
  • 我会将所有数组更改为新类,但这只是一个建议:)一个类应该负责一件事:启动应用程序,创建和引用视图,用于适当的验证的玩家移动。