我正在做一个项目,我在这个游戏中有一个 5x5 的网格,其中有随机放置的镜子,玩家试图通过发射激光并查看它的出口位置或猜测坐标来猜测镜子的位置。
网格看起来像这样:
ABCDE
T.....F
S.....G
R.....H
Q.....I
P.....J
ONMLK
我被困在如何让 shootLaser() 方法按预期工作。
我想让用户看到激光在什么字母处退出。这是输出产生的:
Enter a letter (A-T) to shoot the laser: A
Shot exited maze at 0
老师给了我小费:
When the laser exits (looping stops) you will need to use the X&Y coordinates (row? Col?) to CALCULATE where it exited… this is the inverse of starting the shot, (where we take ‘C’ and get X=2,y=0, DOWN…)
// class
import java.util.*;
import java.io.*;
import java.lang.*;
public class LazerMaze5 {
// Initialiaze variables used for the game
static Scanner input = new Scanner(System.in); // Scanner object
static int numOfMirrors;
static String userMenu; // Menu choice
static int numOfShots = 0;
static int numOfCorrectGuesses = 0;
static int numOfIncorrectGuesses = 0;
static String[][] maze = new String[5][5]; // Array created for maze
static boolean[][] guessedMirrors = new boolean[5][5]; // Array created for guessed mirrors
// skipping down to the shootlaser() method
public static void shootLaser(){
// Validate the shot
System.out.print("Enter a letter (A-T) to shoot the laser: ");
char letter = input.next().charAt(0);
if (letter < 'A' || letter > 'T') {
System.out.println("Invalid letter entered. Please enter a letter from A to T.");
return;
}
int currentRow = 0;
int currentCol = 0;
// Loop through steps until shot exits maze
while (currentRow >= 0 && currentRow < maze.length && currentCol >= 0 && currentCol < maze[0].length) {
// Update coordinates based on current direction
switch (letter) {
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
currentRow--;
// If new coordinates are outside maze, stop looping and display exit point
if (currentRow < 0 || currentRow >= maze.length || currentCol < 0
|| currentCol >= maze[0].length) {
System.out.println("Shot exited maze at " + (currentCol));
break;
// If new coordinates are a mirror, change direction
}else if (maze[currentRow][currentCol] == "/") {
if (letter == 'F' || letter == 'G' || letter == 'H' ||
letter == 'I' || letter == 'J') {
currentCol++;
}
} else if (maze[currentRow][currentCol] == "\\") {
if (letter == 'F' || letter == 'G' || letter == 'H' ||
letter == 'I' || letter == 'J') {
currentCol--;
}
}
break;
case 'T':
case 'S':
case 'R':
case 'Q':
case 'P':
currentRow++;
if (currentRow < 0 || currentRow >= maze.length || currentCol < 0
|| currentCol >= maze[0].length) {
System.out.println("Shot exited maze at " + (currentCol));
break;
} else if (maze[currentRow][currentCol] == "/") {
if (letter == 'T' || letter == 'S' || letter == 'R' ||
letter == 'Q' || letter == 'P') {
currentCol--;
}
} else if (maze[currentRow][currentCol] == "\\") {
if (letter == 'T' || letter == 'S' || letter == 'R' ||
letter == 'Q' || letter == 'P') {
currentCol++;;
}
}
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
currentCol++;
if (currentRow < 0 || currentRow >= maze.length || currentCol < 0
|| currentCol >= maze[0].length) {
System.out.println("Shot exited maze at " + currentRow);
break;
} else if (maze[currentRow][currentCol] == "/") {
if (letter == 'A' || letter == 'B' || letter == 'C'
|| letter == 'D' || letter == 'E') {
currentRow--;
}
} else if (maze[currentRow][currentCol] == "\\") {
if (letter == 'A' || letter == 'B' || letter == 'C'
|| letter == 'D' || letter == 'E') {
currentRow++;
}
}
break;
case 'O':
case 'N':
case 'M':
case 'L':
case 'K':
currentCol--;
if (currentRow < 0 || currentRow >= maze.length || currentCol < 0
|| currentCol >= maze[0].length) {
System.out.println("Shot exited maze at " + currentRow);
break;
} else if (maze[currentRow][currentCol] == "/") {
if (letter == 'O' || letter == 'N' || letter == 'M' ||
letter == 'L' || letter == 'K') {
currentRow++;
}
} else if (maze[currentRow][currentCol] == "\\") {
if (letter == 'O' || letter == 'N' || letter == 'M' ||
letter == 'L' || letter == 'K') {
currentRow--;
}
}
break;
}
}
}