2016-06-15 6 views
0

私はテキストベースのJavaボードゲーム(非常に基本的)を完成しようとしています。そして、私はこの最後のことに固執しています。 -これを解決できない、Javaで配列を更新する

基本的には、ユーザーが仮想ダイスをロールするタイプの「ロール」を持っている場合です。結果に応じて、配列が更​​新されて 'o'の位置が変更されます(これにより、カウンタが移動されたように見えます)。私は最初のロールのためにこれを作る方法を考え出しました。

しかし、私が理解できないことは、2番目のロールと1番目のロールを追加する方法です(3番目と4番目のロールで追加するなど)。

(例 -

ロール1:プレイヤーはダイスを振り - ボード上の第三位置に移動する(解決)

ロール2から3を取得:ゲームは最後のロールが3だったことを記憶し - プレーヤーは4ロール - ボードを更新し、数7にカウンタを動かす)ここ

は、関連するコードの抜粋である:

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]); 


} 

最後に、ここでコンソール出力は次のとおりです。

​​
+0

配列を使用する必要がありますか、オブジェクトを自由に使用できますか?オブジェクトを使用する場合は、より簡単で洗練されたものになります。 – Tschallacka

+0

@engineer私は、1つのロールの後にカウンタが最後に終わった。 – jordan

+0

@MichaelDibbets私はオブジェクトを使うことができました。私はJavaにはまったく新しく、オブジェクトには慣れていません - 私はそれらをどのようにすることができるか提案できますか? – jordan

答えて

0

次のようにコードを変更することができます:

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; 
         }      
        } 
       } 
      } 
     } 
    } 

しかし、現在でArrayIndexOutOfBoundExceptionを取得するために危険にさらしていることに注意してくださいarray[p1r[c]]、およびアップデートされたバージョンです。したがって、私はそれに対処することをお勧めします。たとえば、モジュール化されたアクセスarray[(p1r[c%p1r.length]%array.length)]を使用します。このような状況では、ゲームの振る舞いに依存します。

希望すると助かります!

0

あなたはオブジェクトにどのように見えるか尋ねました。

このグループにはすべてのプレーヤーが含まれています。
プレイヤーにはボードがあります。
ボードは各プレーヤーの位置を追跡します。

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(); 
     } 
    } 
関連する問題