此代码是模拟15个谜题的游戏。输出应为:
我目前正在尝试执行switch语句,以便根据用户输入将数字移至空白处。这是我尝试过的。谢谢。
import java.util.ArrayList;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
int rowNum=4;
int colNum=4;
int[][] gameboard = new int[rowNum][colNum];
ArrayList<Integer> used = new ArrayList<Integer>();
int emptySlot = (int) (1 + Math.random() * 16);
for(int row = 0; row < rowNum; row++) {
for(int col = 0; col < colNum; col++) {
if(row*gameboard.length + col == emptySlot) {
System.out.print(" ");
continue; //skip empty slot
}
int number;
while(used.contains(number = (int) (1 + Math.random() * 15)));
used.add(number);
gameboard[row][col] = number;
System.out.printf("%-4d",gameboard[row][col]);
}
System.out.println();
}
System.out.println();
System.out.print("Enter a move: (l)eft, (u)p, (r)ight, (d)own, or (exit):");
Scanner sc = new Scanner(System.in);
int px=0;
int py=0;
String move = sc.nextLine();
switch (move) {
case "l":
px -= 1;
break;
case "u":
py +=1;
break;
case "r":
px += 1;
break;
case "d":
py -=1;
break;
case "exit":
break;
}
sc.close();
}
}
答案 0 :(得分:0)
这是一个游戏,您应该添加一个游戏循环,我建议您阅读此书以全面了解什么是游戏循环:game loop
第二,我建议使用“ 0”作为谜题上的空白位置,这使事情变得更容易,找到0并通过switch语句及其在列表中的邻居更改它的位置。
很遗憾,您的问题基于意见,与stackoverflow策略相反,我希望能够回答所有问题。
答案 1 :(得分:0)
如果无法单步实现所需的结果,则还需要将用户选项置于循环中。我将游戏板类型更改为字符串,以缩短显示代码。我要移动的是空白空间,而不是旁边的数字-但这只是一个细节。
public class ShiftGame
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
final int rowNum = 4,
colNum = 4;
int row = 0,
col = 0,
number = 0;
String[][] gameboard = new String[rowNum][colNum];
ArrayList<Integer> used = new ArrayList<Integer>();
int emptySlot = (int) (1 + Math.random() * 16);
int py = emptySlot / 4, // Empty row
px = emptySlot % 4, // Empty col
oldpy, // For old row num
oldpx; // For old col num
String move = "x",
copy;
for (row = 0; row < rowNum; row++) // Generate & display starting number table ...
{
for (col = 0; col < colNum; col++)
{
if (row * rowNum + col != emptySlot)
{
while (used.contains(number = (int) (1 + Math.random() * 15)));
used.add(number);
gameboard[row][col] = "" + number;
}
else
gameboard[row][col] = " ";
System.out.printf("%-4s", gameboard[row][col]);
}
System.out.printf("\n");
}
// ... overwriting the empty slot
System.out.println();
while(!move.equals("e")) // Cycle game loop till user ends it ...
{
System.out.print("Enter a move: l(eft), u(p), r(ight), d(own), or e(xit):");
oldpx = px;
oldpy = py;
move = sc.nextLine();
switch (move) // Change vacancy position ...
{
case "l":
if (px != 0)
px -= 1;
else
System.out.println("\nAlready at leftmost edge - can't move empty slot left!");
break;
case "u":
if (py != 0)
py -= 1;
else
System.out.println("\nAlready at top edge - can't move empty slot up!");
break;
case "r":
if (px != 3)
px += 1;
else
System.out.println("\nAlready at rightmost edge - can't move empty slot right!");
break;
case "d":
if (py != 3)
py += 1;
else
System.out.println("\nAlready at bottom edge - can't move empty slot down!");
break;
case "e":
break;
default: System.out.println("You have entered an invalid claracter. "
+ "\nOnly (l)eft, (u)p, (r)ight, (d)own, or (e)xit) are valid entries.");
}
if (!move.equals("e")) // Interchange new & old positions ...
{
copy = gameboard[py][px];
gameboard[oldpy][oldpx] = copy;
gameboard[py][px] = " ";
System.out.println();
for(row = 0; row < rowNum; row++) // ... and display new table
{
for(col = 0; col < colNum; col++)
System.out.printf("%-4s", gameboard[row][col]);
System.out.printf("\n");
}
}
}
System.out.printf("\n\nGame over. Good Day !"); // Close game.
sc.close();
}
}