2016-10-09 11 views
-1

プログラムの出力に問題があります。私はグリッドを生成し、パクマンの向きを変えて移動させることができますが、それは最初の位置から移動した後、方向付けまたは移動を停止します。何かが間違っている必要がありますプログラムが繰り返す方法で、私は何がわからない。java pacman - ループは配列を通して1回だけ反復する

package javaapplication2; 
import java.util.*; 

public class JavaApplication2 { 
    int index; 
    int index2; 
    final int ARRAY_SIZE = 10; 
    final int ARRAY_SIZE2 = 10; 
    String [][] myArray = new String [ARRAY_SIZE][ARRAY_SIZE2]; 
    String output; 

public static void main(String[] args) 
{ 

    JavaApplication2 pacman = new JavaApplication2(); 
    pacman.createPacman(); 
    pacman.generateArray(); 
    Scanner input = new Scanner(System.in); 
    int m = input.nextInt(); 
    pacman.orientPacman(m); 



} 
public void generateArray(){ 


    for (index = 0; index < ARRAY_SIZE; index++) 
    { 
     for (index2 = 0; index2 < ARRAY_SIZE2; index2++) 
     { 
      Random rand = new Random(); 
      int value = rand.nextInt(50); 
      if (myArray[index][index2] == "<") 
       myArray[index][index2] = "<"; 
      else if (myArray[index][index2] == " ") 
       myArray[index][index2] = " "; 
      else if (value < 45) 
       myArray[index][index2] = "."; 
      else myArray[index][index2] = "0"; 
      System.out.print(" " + myArray[index][index2]); 
        if (index2 + 1 == ARRAY_SIZE) 
        { 
    System.out.println(); 
        } 



}} 
} 


    public void printArray(String myArray[][]){ 
       for (index = 0; index < ARRAY_SIZE; index++) 
    { 
     for (index2 = 0; index2 < ARRAY_SIZE2; index2++) 
     { 
      System.out.print(" " + myArray[index][index2]); 
        if (index2 + 1 == ARRAY_SIZE) 
        { 
    System.out.println(); 
        } 
    }} 
    Scanner input = new Scanner(System.in); 
    int m = input.nextInt(); 
    orientPacman(m);} 

    public void createPacman(){ 
     myArray[0][0] = "<"; 



    } 

    public void orientPacman(int m){ 
     for (index = 0; index < ARRAY_SIZE; index++) 
    { 
     for (index2 = 0; index2 < ARRAY_SIZE2; index2++) 
     { 
    if ((myArray[index][index2] == "<" && m == 1) | 
    (myArray[index][index2] == ">" && m == 2) | 
    (myArray[index][index2] == "^" && m == 3) | 
    (myArray[index][index2] == "v" && m == 4) ) 
    { 
    movePacman(); 
    break; 
    } 
    else if (myArray[index][index2] == ">" | myArray[index][index2] == "^"  | myArray[index][index2] == "v" && m == 1) 
    myArray[index][index2] = "<"; 
    else if (myArray[index][index2] == "<" | myArray[index][index2] == "^" | myArray[index][index2] == "v" && m == 2) 
    myArray[index][index2] = ">"; 
    else if (myArray[index][index2] == "<" | myArray[index][index2] == ">" | myArray[index][index2] == "v" && m == 3) 
    myArray[index][index2] = "^"; 
    else if (myArray[index][index2] == "<" | myArray[index][index2] == ">" | myArray[index][index2] == "^" && m == 4) 
    myArray[index][index2] = "v"; 
    printArray(myArray); 

     } 

    }} 
    public void movePacman(){ 
     for (index = 0; index < ARRAY_SIZE; index++) 
    { 
     for (index2 = 0; index2 < ARRAY_SIZE2; index2++) 
     { 
    if (myArray[index][index2] == "<") 
    { 
    myArray[index][index2+1] = "<"; 
    myArray[index][index2] = " "; 
    } 
    if (myArray[index][index2] == ">") 
    { 
    myArray[index][index2-1] = ">"; 
    myArray[index][index2] = " "; 
    } 
    if (myArray[index][index2] == "^") 
    { 
    myArray[index+1][index2] = "^"; 
    myArray[index][index2] = " "; 
    } 
     if (myArray[index][index2] == "v") 
    { 
    myArray[index-1][index2] = "v"; 
    myArray[index][index2] = " "; 
    } 
    printArray(myArray); 


}}}} 

答えて

0

最初の繰り返しで実行されているかどうかを確認します。

また、2番目のインデックスと3番目のインデックス値が空であるかどうかを確認してください。

+0

少しデバッグしてみてください –

関連する問題