简体   繁体   English

尝试在 java 中制作基本的滑动益智游戏,但在尝试测试时遇到问题

[英]Trying to make a basic sliding puzzle game in java but running across a problem when trying to test

I am currently trying to make a basic sliding puzzle game.我目前正在尝试制作一个基本的滑动益智游戏。 However, I am kind of stuck and not too sure on what I need to do to fix my problem.但是,我有点卡住了,不太确定我需要做什么来解决我的问题。

Here is my code below and when trying to test out what I have so far, the method CreateTheGame is not working?下面是我的代码,当尝试测试我到目前为止的内容时,CreateTheGame 方法不起作用? I get the following error in my tester, "the method CreateTheGame in the class Sliding Puzzle cannot be applied to given types: Required: int[][]"我的测试仪出现以下错误,“class 滑动拼图中的 CreateTheGame 方法不能应用于给定类型:必需:int[][]”

Did I mess up pretty bad or is it a simple mistake?我是不是搞砸了,还是一个简单的错误? Thanks!谢谢!

import java.util.Arrays;
import java.util.Random;

public class SlidingPuzzle
{
public static int rows = 4;
public static int cols = 4;
private int EmptyBox;
public static int[][] TheGame;

public SlidingPuzzle(int row, int col)
{  
    rows = row;
    cols = col;
    int[][] TheGame = new int[row][col];

}

**public static void main (String[] args) {

    CreateTheGame();

}**


public static int[][] CreateTheGame(int[][] TheGame)
{
   //Created an array to use to generate random numbers
   //that'll be filled into the 2D array
   Random random = new Random();
   int[] TheNumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
   int count = 0;


   for (int x = 0; x < TheNumbers.length; x++) {
           int ranIndex = random.nextInt(TheNumbers.length);
           int Temp = TheNumbers[ranIndex];
           TheNumbers[ranIndex] = TheNumbers[x];
           TheNumbers[x] = Temp;   
   }

   for (int row = 0; row < TheGame.length; row++)
    for (int col = 0; col < TheGame[row].length; col++)
   { 
       TheGame[row][col] = TheNumbers[count];
       count++;   
   }
   return TheGame;
}

public void printGame () 
{ 
   for (int row = 0; row < TheGame.length; row++)
   for (int col = 0; col < TheGame[row].length; col++)
   {
       System.out.print(TheGame[row][col] + " ");
   }
    System.out.println();

}   

//public int SelectBoxToMove();      

It looks like CreateTheGame() requires an argument of type int[][].看起来 CreateTheGame() 需要一个 int[][] 类型的参数。

But when you call the function here, you do not pass it any argument:但是当你在这里调用 function 时,你没有传递任何参数:

**public static void main (String[] args) {

    CreateTheGame();

}**

Perhaps you meant something like CreateTheGame(TheGame);也许您的意思是CreateTheGame(TheGame);

I think it's better if you call the CreateTheGame() method inside the constructor and you need to add an int[][] argument TheGame in your case so it will be like this CreateTheGame(TheGame) .我认为如果你在构造函数中调用CreateTheGame()方法会更好,并且你需要在你的情况下添加一个 int[][] 参数TheGame ,这样它就会像这样CreateTheGame(TheGame) After int the main method you create an instance of SlidingPuzzle like this:在 int main 方法之后,您创建一个SlidingPuzzle的实例,如下所示:

public static void main (String[] args) {

SlidingPuzzle game= new SlidingPuzzle(4,4);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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