프로젝트 용 자바 미로 게임을 만들고 있습니다. 미로는 애플릿이 아닌 표준 출력으로 콘솔에 표시됩니다. 필자는 필요한 대부분의 코드를 만들었지 만, 한 가지 문제점에 봉착했습니다. 즉, 마지막 게임을 다시 플레이 할 수 있어야합니다. 즉, 미로를 사용자 이동으로 다시 그릴 수 있지만 사용자가 입력하지 않아도됩니다.자바 미로 게임의 마지막 게임을 재생하는 방법에 대한 도움이 필요합니다.
어떤 조치를 취해야할지 모르겠습니다. 플레이어의 위치를 유지하는 두 개의 변수가 있음을 알 수 있듯이 각 사용자의 이동 또는 각 배열의 위치를 다른 배열로 복사하는 것에 대해 생각하고있었습니다. plyrX와 plyrY는 각각의 이동이이 문제를 해결할 때마다 이러한 값을 새로운 배열에 복사한다고 생각합니까? 어떻게해야합니까?
나는 textIO.java 클래스에 대한 사과는 exept 아래 TextIO.java [TextIO.java] [1]
내 코드에 링크를 게시 해결하는 방법을 잘 존재하지없는 내 코드를 업데이트 원래 미로 (텍스트 파일에서 읽고 유니 코드 문자를 사용하여 표시)에서 값을 보유하고 또한 plyrX 및 plyrY 및 copy 값을 보유해야한다고 생각하고있는 새로운 변수 c_plyrX 및 c_plyrY에 char 유형의 새 배열로 업데이트됩니다 새 배열로
replayGame();을 호출하려고 할 때; 메뉴에서 방법은 두 번째의 미로를로드 한 후 콘솔 내가
public class MazeGame {
//unicode characters that will define the maze walls,
//pathways, and in game characters.
final static char WALL = '\u2588'; //wall
final static char PATH = '\u2591'; //pathway
final static char PLAYER = '\u25EF'; //player
final static char ENTRANCE = 'E'; //entrance
final static char EXIT = '\u2716'; //exit
//declaring member variables which will hold the maze co-ordinates
//X = rows, Y = columns
static int entX = 0; //entrance X co-ordinate
static int entY = 1; //entrance y co-ordinate
static int plyrX = 0;
static int plyrY = 1;
static int exitX = 24; //exit X co-ordinate
static int exitY = 37; //exit Y co-ordinate
//static member variables which hold maze values
//used so values can be accessed from different methods
static int rows; //rows variable
static int cols; //columns variable
static char[][] maze; //defines 2 dimensional array to hold the maze
//variables that hold player movement values
static char dir; //direction
static int spaces; //amount of spaces user can travel
//variable to hold amount of moves the user has taken;
static int movesTaken = 0;
//new array to hold player moves for replaying game
static char[][] mazeCopy;
static int c_plyrX;
static int c_plyrY;
/** userMenu method for displaying the user menu which will provide various options for
* the user to choose such as play a maze game, get instructions, etc.
*/
public static void userMenu(){
TextIO.putln("Maze Game");
TextIO.putln("*********");
TextIO.putln("Choose an option.");
TextIO.putln("");
TextIO.putln("1. Play the Maze Game.");
TextIO.putln("2. View Instructions.");
TextIO.putln("3. Replay the last game.");
TextIO.putln("4. Exit the Maze Game.");
TextIO.putln("");
int option; //variable for holding users option
TextIO.put("Type your choice: ");
option = TextIO.getlnInt(); //gets users option
//switch statement for processing menu options
switch(option){
case 1: playMazeGame(); break;
case 2: instructions(); break;
case 3: if (c_plyrX == plyrX && c_plyrY == plyrY)replayGame();
else {
TextIO.putln("Option not available yet, you need to play a game first.");
TextIO.putln();
userMenu();
}
break;
case 4: System.exit(0); break;//exits the user out of the console
default: TextIO.put("Option must be 1, 2, 3 or 4");break;
}
} //end of userMenu
/**main method, will call the userMenu and get the users choice and call
* the relevant method to execute the users choice.
*/
public static void main(String[]args){
userMenu(); //calls the userMenu method
} //end of main method
/**instructions method, displays instructions on how to play
* the game to the user/
*/
public static void instructions(){
TextIO.putln("To beat the Maze Game you have to move your character");
TextIO.putln("through the maze and reach the exit in as few moves as possible.");
TextIO.putln("");
TextIO.putln("Your characer is displayed as a " + PLAYER);
TextIO.putln("The maze exit is displayed as a " + EXIT);
TextIO.putln("Reach the exit and you have won escaped the maze.");
TextIO.putln("To control your character type the direction you want to go");
TextIO.putln("and how many spaces you want to move");
TextIO.putln("for example 'D3' will move your character");
TextIO.putln("down 3 spaces.");
TextIO.putln("Remember you can't walk through walls!");
boolean insOption; //boolean variable
TextIO.putln("");
TextIO.put("Do you want to play the Maze Game now? (Y or N) ");
insOption = TextIO.getlnBoolean();
if (insOption == true)playMazeGame();
else userMenu();
} //end of instructions method
/**playMazeGame method, calls the loadMaze method and the charMove method
* to start playing the Maze Game.
*/
public static void playMazeGame(){
loadMaze();
plyrMoves();
} //end of playMazeGame method
/**loadMaze method, loads the 39x25 maze from the MazeGame.txt text file
* and inserts values from the text file into the maze array and
* displays the maze on screen using the unicode block characters.
* plyrX and plyrY variables are set at their staring co ordinates so that when
* a game is completed and the user selects to play a new game
* the player character will always be at position 01.
*/
public static void loadMaze(){
plyrX = 0;
plyrY = 1;
TextIO.readFile("MazeGame.txt"); //now reads from the external MazeGame.txt file
rows = TextIO.getInt(); //gets the number of rows from text file to create X dimensions
cols = TextIO.getlnInt(); //gets number of columns from text file to create Y dimensions
maze = new char[rows][cols]; //creates maze array of base type char with specified dimnensions
//loop to process the array and read in values from the text file.
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
maze[i][j] = TextIO.getChar();
}
TextIO.getln();
} //end for loop
TextIO.readStandardInput(); //closes MazeGame.txt file and reads from
//standard input.
//loop to process the array values and display as unicode characters
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
if (i == plyrX && j == plyrY){
plyrX = i;
plyrY = j;
TextIO.put(PLAYER); //puts the player character at player co-ords
}
else{
if (maze[i][j] == '0') TextIO.putf("%c",WALL); //puts wall block
if (maze[i][j] == '1') TextIO.putf("%c",PATH); //puts path block
if (maze[i][j] == '2') {
entX = i;
entY = j;
TextIO.putf("%c",ENTRANCE); //puts entrance character
}
if (maze[i][j] == '3') {
exitX = i; //holds value of exit
exitY = j; //co-ordinates
TextIO.putf("%c",EXIT); //puts exit character
}
}
}
TextIO.putln();
} //end for loop
} //end of loadMaze method
/**redrawMaze method, method for redrawing the maze after each move.
*
*/
public static void redrawMaze(){
TextIO.readFile("MazeGame.txt"); //now reads from the external MazeGame.txt file
rows = TextIO.getInt(); //gets the number of rows from text file to create X dimensions
cols = TextIO.getlnInt(); //gets number of columns from text file to create Y dimensions
maze = new char[rows][cols]; //creates maze array of base type char with specified dimnensions
//loop to process the array and read in values from the text file.
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
maze[i][j] = TextIO.getChar();
}
TextIO.getln();
} //end for loop
TextIO.readStandardInput(); //closes MazeGame.txt file and reads from
//standard input.
//loop to process the array values and display as unicode characters
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
if (i == plyrX && j == plyrY){
plyrX = i;
plyrY = j;
TextIO.put(PLAYER); //puts the player character at player co-ords
}
else{
if (maze[i][j] == '0') TextIO.putf("%c",WALL); //puts wall block
if (maze[i][j] == '1') TextIO.putf("%c",PATH); //puts path block
if (maze[i][j] == '2') {
entX = i;
entY = j;
TextIO.putf("%c",ENTRANCE); //puts entrance character
}
if (maze[i][j] == '3') {
exitX = i; //holds value of exit
exitY = j; //co-ordinates
TextIO.putf("%c",EXIT); //puts exit character
}
}
}
TextIO.putln();
} //end for loop
} //end redrawMaze method
/**replay game method
*
*/
public static void replayGame(){
TextIO.readFile("MazeGame.txt"); //now reads from the external MazeGame.txt file
rows = TextIO.getInt(); //gets the number of rows from text file to create X dimensions
cols = TextIO.getlnInt(); //gets number of columns from text file to create Y dimensions
mazeCopy = new char[rows][cols]; //creates maze array of base type char with specified dimnensions
//loop to process the array and read in values from the text file.
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
mazeCopy[i][j] = TextIO.getChar();
}
TextIO.getln();
} //end for loop
TextIO.readStandardInput(); //closes MazeGame.txt file and reads from
//standard input.
//loop to process the array values and display as unicode characters
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
if (i == c_plyrX && j == c_plyrY){
c_plyrX = i;
c_plyrY = j;
TextIO.put(PLAYER); //puts the player character at player co-ords
}
else{
if (mazeCopy[i][j] == '0') TextIO.putf("%c",WALL); //puts wall block
if (mazeCopy[i][j] == '1') TextIO.putf("%c",PATH); //puts path block
if (mazeCopy[i][j] == '2') {
entX = i;
entY = j;
TextIO.putf("%c",ENTRANCE); //puts entrance character
}
if (mazeCopy[i][j] == '3') {
exitX = i; //holds value of exit
exitY = j; //co-ordinates
TextIO.putf("%c",EXIT); //puts exit character
}
}
}
TextIO.putln();
} //end for loop
TextIO.putln("postion at " + mazeCopy[c_plyrX][c_plyrY]);
} //end replayGame method
/**plyrMoves method, method for moving the players character
* around the maze.
*/
public static void plyrMoves(){
int nplyrX = plyrX;
int nplyrY = plyrY;
int pMoves;
direction();
//UP
if (dir == 'U' || dir == 'u'){
nplyrX = plyrX;
nplyrY = plyrY;
c_plyrX = plyrX;
c_plyrY = plyrY;
for(pMoves = 0; pMoves <= spaces; pMoves++){
if (maze[nplyrX][nplyrY] == '0'){
TextIO.putln("Invalid move, try again.");
}
else if (pMoves != spaces){
nplyrX =plyrX + 1;
c_plyrX = nplyrX;
}
else {
plyrX = plyrX-spaces;
c_plyrX = plyrX;
movesTaken++;
}
}
}//end UP if
//DOWN
if (dir == 'D' || dir == 'd'){
nplyrX = plyrX;
nplyrY = plyrY;
c_plyrX = plyrX;
c_plyrY = plyrY;
for (pMoves = 0; pMoves <= spaces; pMoves ++){
if (maze[nplyrX][nplyrY] == '0'){
TextIO.putln("Invalid move, try again");
}
else if (pMoves != spaces){
nplyrX = plyrX+1;
c_plyrX = nplyrX;
}
else{
plyrX = plyrX+spaces;
c_plyrX = plyrX;
movesTaken++;
}
}
} //end DOWN if
//LEFT
if (dir == 'L' || dir =='l'){
nplyrX = plyrX;
nplyrY = plyrY;
c_plyrX = plyrX;
c_plyrY = plyrY;
for (pMoves = 0; pMoves <= spaces; pMoves++){
if (maze[nplyrX][nplyrY] == '0'){
TextIO.putln("Invalid move, try again");
}
else if (pMoves != spaces){
nplyrY = plyrY + 1;
c_plyrY = nplyrY;
}
else{
plyrY = plyrY-spaces;
c_plyrY = plyrY;
movesTaken++;
}
}
} //end LEFT if
//RIGHT
if (dir == 'R' || dir == 'r'){
nplyrX = plyrX;
nplyrY = plyrY;
c_plyrX = plyrX;
c_plyrY = plyrY;
for (pMoves = 0; pMoves <= spaces; pMoves++){
if (maze[nplyrX][nplyrY] == '0'){
TextIO.putln("Invalid move, try again.");
}
else if (pMoves != spaces){
nplyrY = plyrY + 1;
c_plyrY = nplyrY;
}
else{
plyrY = plyrY+spaces;
c_plyrY = plyrY;
movesTaken++;
}
}
} //end RIGHT if
//prints message if player escapes from the maze.
if (maze[plyrX][plyrY] == '3'){
TextIO.putln("****Congratulations****");
TextIO.putln();
TextIO.putln("You have escaped from the maze.");
TextIO.putln();
userMenu();
}
else{
movesTaken++;
redrawMaze();
plyrMoves();
}
} //end of plyrMoves method
/**direction, method
*
*/
public static char direction(){
TextIO.putln("Enter the direction you wish to move in and the distance");
TextIO.putln("i.e D3 = move down 3 spaces");
TextIO.putln("U - Up, D - Down, L - Left, R - Right: ");
dir = TextIO.getChar();
if (dir =='U' || dir == 'D' || dir == 'L' || dir == 'R'
|| dir == 'u' || dir == 'd' || dir == 'l' || dir == 'r'){
spacesMoved();
}
else{
loadMaze();
TextIO.putln("Invalid direction!");
TextIO.put("Direction must be one of U, D, L or R");
direction();
}
return dir; //returns the value of dir (direction)
} //end direction method
/**spaces method, gets the amount of spaces the user wants to move
*
*/
public static int spacesMoved(){
TextIO.putln(" ");
spaces = TextIO.getlnInt();
if (spaces <= 0){
redrawMaze();
TextIO.put("Invalid amount of spaces, type spaces again");
spacesMoved();
}
return spaces;
} //end spacesMoved method
} //end of MazeGame class
코드가 읽을 수 있도록 질문을 다시 포맷해야합니다 (stackoverflow [markdown help] (http://stackoverflow.com/editing-help) 참조). – ocodo
게시 된 코드의 384 줄은 TextIO 클래스가 없기 때문에 컴파일되지 않습니다. 좀 더 빨리 도움을 받으려면 SSCCE (http://pscode.org/sscce.html)를 게시하십시오. –