首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于AI游戏的Java代码

用于AI游戏的Java代码
EN

Stack Overflow用户
提问于 2015-05-04 18:10:36
回答 1查看 350关注 0票数 2

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

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

我的第一个问题是我不确定如何进入[3,6],让播放器X移动到[3,6]

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

这个游戏的目的实际上是让其中一个玩家处于不能移动的位置。我们打算将不同的AI添加到其中,但如果我得到了基础工作,我可以尝试开发AI的。

我不是最强的程序员,因此,如果我的代码可以编辑在任何方式使事情变得更容易,任何帮助将不胜感激。谢谢

我的代码如下:

代码语言:javascript
复制
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();
     }
}
EN

回答 1

Stack Overflow用户

发布于 2015-05-04 19:35:10

我的第一个问题是我不确定如何进入,即3,6,让玩家X移动到3,6。

你可以使用一些有意义的坐标-例如,如果用户想要转到3,6,然后输入36,等等。坐标的标签应该在船上可见。

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

您可以创建新的类,例如BoardItem -它将包含isOccupied和label字段- label将显示玩家标签,如果该字段将被占用或该字段未被占用的坐标。

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

我提出的解决方案:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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方法的

  • 类应该只启动应用程序,整个逻辑应该在其他地方;
  • 应该为用户输入编写验证,并检查用户是否在适当的范围内输入正确的数字,字段是否被占用,以及用户是否可以移动到指定的字段;
  • 我会将所有数组更改为新的类,但这只是一个建议:)一个类应该负责一件事:启动应用程序,创建和打印视图,使用适当的验证进行玩家移动。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30027540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档