【问题标题】:Unable to solve this, updating arrays in java无法解决这个问题,更新java中的数组
【发布时间】:2016-06-15 12:25:41
【问题描述】:

我即将完成一个基于文本的 Java 棋盘游戏(非常基础)。我被困在最后一件事上 -

基本上,当用户使用“roll”类型来掷虚拟骰子时会发生这种情况。然后根据结果更新数组以更改 'o' 的位置(这使得计数器看起来像已移动)。我已经想出了如何为第一卷制作这个。

但我不知道如何使第二卷和第一卷相加(以此类推,第三和第四卷)。

(示例 -

掷骰子 1:玩家掷骰子 - 得到 3 - 移动到棋盘上的第三个位置(已解决)

掷出 2:游戏记住最后掷出的是 3 - 玩家掷出 4 - 更新棋盘并将计数器移至数字 7)

这里是相关代码的sn-p:

    public void play(String p1Name2, String p2Name2){

    Scanner user_input = new Scanner(System.in);
    Random rn = new Random();
    String p1Roll = null,p2Roll;
    int[] p1r = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};

    int b= 0,c= 0,d = 0,e = 0,g = 0;

    while(array1[10] != 'o'){
        c++;

            if(b==0) {
                int i =1;
                System.out.println("Player 1, type roll to roll the die. ");
                p1Roll = user_input.next();

                    if(p1Roll.equalsIgnoreCase("roll")){
                        p1r[c] = rn.nextInt(6);
                        p1r[c] += 1;
                        System.out.println("You rolled a: " + p1r[c]);


                        for(int f = 0; f< 10; f++ ){
                            if(array1[f] == 'o'){
                              array1[f] = 'x';
                              array1[p1r[c]] = 'o';
                              if(i ==1){
                                  this.board();
                                  i = 0;

                            }
                        }                       
                }
            }


            if(b==1) {      

            }

    }   
    }
}

    public void board(){


    System.out.println("           1  2  3  4  (5)  6  7  8  9  10  11");
    System.out.println("Player 1:  " + array1[0]+ "  " + array1[1] + "  " + array1[2]+ "  " + array1[3] + "   " + array1[4]+ "   " + array1[5] + "  " + array1[6]+ "  " + array1[7] + "  " + array1[8]+ "  " + array1[9] + "   " + array1[10]);
    System.out.println("Player 2:  " + array2[0]+ "  " + array2[1] + "  " + array2[2]+ "  " + array2[3] + "   " + array2[4]+ "   " + array2[5] + "  " + array2[6]+ "  " + array2[7] + "  " + array2[8]+ "  " + array2[9] + "   " + array2[10]);


}

最后,这是控制台输出:

Player 1, type roll to roll the die. 
roll
You rolled a: 4
           1  2  3  4  (5)  6  7  8  9  10  11
Player 1:  x  x  x  x   o   x  x  x  x  x   x
Player 2:  o  x  x  x   x   x  x  x  x  x   x
Player 1, type roll to roll the die. 
roll
You rolled a: 4
           1  2  3  4  (5)  6  7  8  9  10  11
Player 1:  x  x  x  x   o   x  x  x  x  x   x
Player 2:  o  x  x  x   x   x  x  x  x  x   x
Player 1, type roll to roll the die. 
roll
You rolled a: 2
           1  2  3  4  (5)  6  7  8  9  10  11
Player 1:  x  x  o  x   x   x  x  x  x  x   x
Player 2:  o  x  x  x   x   x  x  x  x  x   x
Player 1, type roll to roll the die.

【问题讨论】:

  • 是否有使用数组的要求,或者您也可以随意使用对象?如果您使用对象,那对您来说会更容易和更清洁。
  • @engineer 我试过了,一卷之后计数器就到最后了。
  • @MichaelDibbets 我可以使用对象,我是 java 新手,不熟悉对象 - 你能建议我如何使用它们吗?
  • 给我一点时间来建立一些东西@jordan

标签: java arrays loops


【解决方案1】:

您可以按如下方式更改代码:

public void play(String p1Name2, String p2Name2){

    Scanner user_input = new Scanner(System.in);
    Random rn = new Random();
    String p1Roll = null,p2Roll;
    int[] p1r = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};

    int b= 0,c= 0,d = 0,e = 0,g = 0;

    while(array1[10] != 'o'){
        c++;
        if(b==0) {
            int i =1;
            System.out.println("Player 1, type roll to roll the die. ");
            p1Roll = user_input.next();

                if(p1Roll.equalsIgnoreCase("roll")){
                    p1r[c] = rn.nextInt(6);
                    p1r[c] += 1;
                    System.out.println("You rolled a: " + p1r[c]);


                    for(int f = 0; f< 10; f++ ){
                        if(array1[f] == 'o'){
                          array1[f] = 'x';
                          /** Update Current Position **/
                          array1[p1r[f] + p1r[c]] = 'o';
                          if(i == 1){
                              this.board();
                              i = 0;
                          }
                          break;
                        }                       
                    }
                }
            }   
       }
   }

但是,请注意,您目前正冒着在array[p1r[c]] 以及更新版本中获取 ArrayIndexOutOfBoundException 的风险。因此,我建议您解决这个问题,例如使用模块化访问 array[(p1r[c%p1r.length]%array.length)]。这取决于您希望在这种情况下的游戏行为。

希望对你有帮助!

【讨论】:

    【解决方案2】:

    你问我它在物体中的样子。它看起来像这样:

    该小组拥有所有玩家。
    玩家有一个棋盘。
    棋盘跟踪每个玩家的位置。

    import java.util.*;
    import java.lang.*;
    import java.io.*;
    
    
    class Game
    {
        public static void main (String[] args) throws java.lang.Exception
        {
            Group group = new Group();
            group.createPlayer("Michael");
            group.createPlayer("Jeff");
            group.createPlayer("Fred"); 
            group.renderBoard();
            int round = 1;
            while(true) {
                System.out.println("Game round "+round);
                round++;
                group.getInputs();
                group.renderBoard();
                if(group.isGameFinished()) {
                    Player winner = group.getWinner();
                    System.out.println("Player "+winner.getName() + " is the winner! Congratulations");
                    break;
                }
    
            }
        }
    
    }
    class Group {
        private ArrayList<Player> group;
        public Group() {
            this.group =  new ArrayList<Player>();
        }
        public void createPlayer(String name) {
            this.group.add(new Player(name));
        }
        public void renderBoard() {
            Iterator<Player> it = this.group.iterator();
            while(it.hasNext()) {
                Player current = it.next();
                current.renderBoard();
            }
        }
        public void getInputs() {
            Iterator<Player> it = this.group.iterator();
            while(it.hasNext()) {
                Player currentMove = it.next();
                currentMove.getDiceRoll();
            }
        }
        public boolean isGameFinished() {
            Iterator<Player> it = this.group.iterator();
            while(it.hasNext()) {
                Player currentMove = it.next();
                if(currentMove.isFinished()) {
                    return true;
                }
            }
            return false;
        }
        public Player getWinner() {
            Iterator<Player> it = this.group.iterator();
            while(it.hasNext()) {
                Player currentMove = it.next();
                if(currentMove.isFinished()) {
                    return currentMove;
                }
            }
            return new Player("Nobody");
        }
    }
    class Player {
            private Scanner user_input;
            private Random rn;
            private String name;
            private Board board;
            public Player(String name) {
                this.name = name;
                this.user_input = new Scanner(System.in);
                this.rn = new Random();
                this.board = new Board(11);
            }
            public void getDiceRoll() {
                System.out.println("Player "+this.name+", type roll to roll the die. ");
                String input = user_input.next();
                if(input.equalsIgnoreCase("roll")) {
                    int dieRoll = rn.nextInt(6);
                    System.out.println("You rolled a "+dieRoll+"!");
                    this.board.updateLocation(dieRoll);
                }
            }
            public String getName() {
                return this.name;
            }
            public boolean isFinished() {
                return this.board.isFinished();
            }
            public void renderBoard() {
                System.out.println(this.name +" > "+this.board.renderBoard());
            }
        }
        class Board {
            private int size;
            private int location;
            public Board(int size) {
                 this.size = size;
                 this.location = size;
            }
            public void reset() {
                this.location = this.size;
            }
            public boolean isFinished() {
                return this.location <= 0;
            }
            public void updateLocation(int amount) {
                this.location -= amount;
                if(this.location < 0) {
                    this.location = 0;
                }
            }
            public String renderBoard() {
                StringBuilder builder = new StringBuilder();
                for(int c=0;c<=this.size;c++) {
                    if(c == this.location) {
                        builder.append('o');
                    }
                    else {
                        builder.append('x');
                    }
                    builder.append(' ');
                }
                return builder.toString();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-06-19
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      相关资源
      最近更新 更多