简体   繁体   中英

Loops, 2D arrays

Why does this program print the following output?

import java.util.Arrays;
import java.util.Collections;

public class FifteenModel implements Boardgame
{


  private String currentMessage = "No message yet";
  private String[][] status = new String[4][4];  // spelplanen
  private int iemp, jemp;   



  public void FiftenModel()
  {
      createGameBoard();
  }

  private void createGameBoard()
  {
      String[] rutor = new String[16];  //Skapar matrisen
      for (int i = 1; i <= 16; i++)
      {
          rutor[i-1] = String.valueOf(i);
          if (i == 16)
          {
              rutor[i-1] = null;
          }
      }
      shuffleGameBoard(rutor);
  }

  private void shuffleGameBoard(String[] rutor)
  {
    Collections.shuffle(Arrays.asList(rutor));  
    saveArray(rutor);
  }



  private void saveArray(String[] rutor)
  {
      for (int i = 0; i < 4; i++)
      {
          for (int j = 0; j < 4; j++)
          {
            status[i][j] = rutor[4 * i + j ];

            if (status[i][j] == null)
            {
                status[i][j] = "";
                iemp = i;
                jemp = j;
            }
          }
      }
  }


  public boolean move(int i, int j)
  {

      if (((iemp - 1 <= i) && (i <= iemp + 1)) && ((jemp - 1 <= j) && (j <= jemp + 1)))

          if ((iemp == i) ^ (jemp == j))
          {
              currentMessage = "Elements is changed!";
              status[iemp][jemp] = status[i][j];
              status[i][j] = "";
              iemp = i;
              jemp = j;
              return true;

          }
        currentMessage = "Choose the right element!";
        return false;     

  }

  public String getStatus(int i, int j){
      return status[i][j];
  }


  public String getMessage(){
      return currentMessage;
  }


  public boolean gameDone(){

      for (int j = 0; j < 4; j++)
          for (int i = 0; i < 4; i++)
            try {
                if (Integer.valueOf(getStatus(i,j)) == (j * 4 + i + 1))
                {
                    currentMessage = "Congrats!";
                    return true;
                }
            }   catch (Exception e){
            }
     return false;
  }

}

With this output:

Welcome to fifteen puzzle

  null  null  null  null
  null  null  null  null
  null  null  null  null
  null  null  null  null

Your move: 

Why is it null? It's suppose to be 1 to 15 and the last one as null, and then the program is suppose to shuffle that matrix. Now, I think the problem is in the shuffle method, but it's hard for me to find it since I am a beginner.

!Edit! The other file.

class Text15 {
    public static void main(String[] u) {
        Scanner scan = new Scanner(System.in);
        Boardgame thegame = new FifteenModel();                 // Model object is created
        System.out.println("\nWelcome to fifteen puzzle\n");
        while (true) {
            // Print the current board
            for (int i=0; i<4; i++) {
                for (int j=0; j<4; j++)
                    System.out.print("  " + thegame.getStatus(i,j)); // getStatus
                System.out.println();
            }
            System.out.println();
            System.out.print("Your move: ");
            int i = scan.nextInt();  // get an int number from terminal window
            int j = scan.nextInt();
            thegame.move(i,j);                               // move
            System.out.println(thegame.getMessage());        // getMessage
        }
    }
}

You have create a method called FiftenModel

but what you want is this to be the constructor

public FifteenModel () // fix spelling mistake as well
{
  createGameBoard();
}

Also try debugging your code to find out whether status[i][j] = rutor[4 * i + j ]; is correct as it looks like it will the an out of bounds exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM