首页 > > 详细

program讲解 、Java设计编程辅导

Main Menu
Game Start
To start the game, your program must do two things -
1. Read some command line args to set the maze length and width.
2. Display a welcome message.
Your program must take command line inputs in the below order.
maze length - Length of the maze
maze width - Width of the maze
food generator seed - some seed value to be passed to a Food Generator. The food Generator code has already been written for you. You need to invoke the constructor and methods appropriately.
More guidance for how to use these command-line params is provided for this here.
This means when you compile your program and run it, the commands on the terminal should look like this
javac GameEngine.java
java GameEngine 12 7 123
Warning: You must not hard code this in your program. We may provide all kind of integer outputs to test these.
In case there are insufficient inputs (missing input) or if any of the input is 0 or negative, then exit the program by printing the error message -
javac GameEngine.java
java GameEngine 12 7 123
Invalid Inputs to set layout. Exiting the program now.
Note: These command line parameters are set in EdStem platform and will be available to your program when you hit the submit/mark button. But if you are trying this in an IDE, you may have to set the command line parameters yourself using command line or IDE features.
If the inputs are valid, the program will show a welcome message. A pre-defined method is provided to you already in the files. You need to invoke the method appropriately.
____ __ ___ _ _ __ __ _
( _ \ / _\ / __) ( \/ ) / _\ ( ( \
) __/ / \ ( (__ / \/ \ / \ / /
(__) \_/\_/ \___) \_)(_/ \_/\_/ \_)__)

Let the fun begin
(`< ... ... ... .......... ...

Out of Scope: You can assume that the command line arguments are always integers and not String/Double or any other data types.
Main Menu
Once you have shown the display message, the next thing to show is the main menu for the user to select and perform some operations.
____ __ ___ _ _ __ __ _
( _ \ / _\ / __) ( \/ ) / _\ ( ( \
) __/ / \ ( (__ / \/ \ / \ / /
(__) \_/\_/ \___) \_)(_/ \_/\_/ \_)__)

Let the fun begin
(`< ... ... ... .......... ...

Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>
You should then take a user input and perform one of the actions out of the 5 given.
Exit Option
In case the user selects option 5, you must gracefully quit the program and print a goodbye message.
Warning: Do not use System.exit() as it is not a graceful exit.
Look at the complete output below.
Note: The text in bold represents user input here. You don't need to make the console inputs bold in your code.
____ __ ___ _ _ __ __ _
( _ \ / _\ / __) ( \/ ) / _\ ( ( \
) __/ / \ ( (__ / \/ \ / \ / /
(__) \_/\_/ \___) \_)(_/ \_/\_/ \_)__)

Let the fun begin
(`< ... ... ... .......... ...

Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 5
Pacman says - Bye Bye Player.
Invalid Command
In case the user provides an invalid input like 7 or 9, the program should be able to print “Invalid Input” and show the main menu. Sample output
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 7
Invalid Input.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>
Maze
Maze Types
There are three types of Maze we will explore in this assignment. A maze is rectangular in shape. The length and width are defined as follows.

Lower Triangular Maze
Here the upper triangle is blocked by the walls (-). The PACMAN can only navigate in the lower triangle area of the maze.
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Upper Triangular Maze
Here the lower triangle is blocked by the walls (-). The PACMAN can only navigate in the upper triangle area of the maze.
############
#P.........#
#-.........#
#--...*....#
#---.......#
#----......#
############
Horizontal Maze
In this maze, the PACMAN can navigate the maze using a horizontal path. Look at the dots as a path to move.
############
#P.........#
#.--------.#
#.....*....#
#.--------.#
#..........#
############
How to build a maze?
You would be using Control Flows like branching and looping to build the maze. You have to use conditional logic to determine where the boundary/wall/PACMAN/dots go. Here are some guidelines for building the maze.
Start by creating a maze as a rectangle.
The number of columns is determined by maze Length.
The number of rows is determined by maze width.
The first row and last row and first column and last column are always boundaries shown by #.
Next, you should consider creating the walls based on conditional logic and the maze type selected by the user.
PACMAN game starts with the initial position of PACMAN in row = 1 and column = 1. (Remember, counting in JAVA starts from 0)
For generating special food (*) you need to create a constructor of FoodGenerator and invoke appropriate methods. These methods would return you the row and column position of food.
Lastly, you should fill the rest of the maze with dots.
Food Generator:
For your program to work correctly you must generate the food only in the constructor of Maze. This piece of code is to be written at a specific place and is highlighted in the scaffold presented to you in the comments.
Note: How to create a triangular pattern? May be take a hint from these slides.
When the user selects option 1 from the main menu show them to choose a maze type. Once they have chosen a maze type, you must create a maze and show a message to the user.
Sample output for the above -
____ __ ___ _ _ __ __ _
( _ \ / _\ / __) ( \/ ) / _\ ( ( \
) __/ / \ ( (__ / \/ \ / \ / /
(__) \_/\_/ \___) \_)(_/ \_/\_/ \_)__)

Let the fun begin
(`< ... ... ... .......... ...

Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 1
Please select a maze type.
Press 1 to select lower triangle maze.
Press 2 to select upper triangle maze.
Press 3 to select horizontal maze.
> 2
Maze created. Proceed to play the game.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>
Note: The text in bold represents user input here. You don't need to make the console inputs bold in your code.
Invalid Input
If the user inputs an invalid integer while selecting the maze type then the program should display an "Invalid Input" as a message and show the maze selection menu again.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 1
Please select a maze type.
Press 1 to select lower triangle maze.
Press 2 to select upper triangle maze.
Press 3 to select horizontal maze.
> 6
Invalid Input.
Please select a maze type.
Press 1 to select lower triangle maze.
Press 2 to select upper triangle maze.
Press 3 to select horizontal maze.
> 3
Maze created. Proceed to play the game.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>
Play the Game
Option 2: Start the Game
When the user selects option 2 to start the game, there are three possibilities-
1.You haven't selected a maze type.
2.You have selected a maze type and are ready to play the game.
3.You have selected a maze type but you are in the middle of a game that is not over yet.
Scenario 1: Maze Type not selected
In this case, you should show an error message and repeat the main menu.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 2
Maze not created. Select option 1 from main menu.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>
Scenario 2: Maze Type selected new game started
If the maze is created you can start the game. At the start of the new game, you show some information about how you will gain points and show the user a menu to move the PACMAN. How to move the PACMAN will be shown in the move menu here.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 2
Move the Pacman towards the food pellet.
> You gain 20 points when Pacman get the food.
> You lose 0.5 point when you hit the wall/boundary.
> Score = 20 * Food - 0.5 * hits - 0.25 * moves.
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit
>
Scenario 3: Maze Type selected but previous game in progress
In this case, the program will show a message to the user that the previous game is in progress and ask if the user wants to keep the existing game.
If yes, the user presses N, then the program prompts the main menu again.
If not, the user presses any other key, then the program will discard the current game and start over a new game.
Sample output here -
Output for when the user selects N
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 2
Previous game hasn't ended yet. Do you want to discard previous game?
Press N to go back to main menu to resume the game or else press any key to discard
> N
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
Output when the user selects any other key
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 2
Previous game hasn't ended yet. Do you want to discard previous game?
Press N to go back to main menu to resume the game or else press any key to discard.
> S
Move the Pacman towards the food pellet.
> You gain 20 points when Pacman get the food.
> You lose 0.5 point when you hit the wall/boundary.
> Score = 20 * Food - 0.5 * hits - 0.25 * moves.
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit
>
Option 3: Resume the Game
A game is paused when the PACMAN hasn't reached/eaten the special food and you quit moving the PACMAN to go back to the main menu. More information here. You can only resume a game that is ongoing. If you do not have any ongoing game then you should show an error message. There are three scenarios here -
1.You haven't selected a maze type
2.You are resuming a paused game.
3.The previous game has ended and the new game hasn't started.
Scenario 1: Maze Type not selected.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 3
Maze not created. Select option 1 from main menu.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>
Scenario 2: Resuming a paused game
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 3
Restart your game from the last position you saved.
############
#.---------#
#.P--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit
>
Scenario 3: No paused game found
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 3
No paused game found. Select option 2 from main menu to start a new game.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>
Option 4: View Score
There are multiple possible scenarios.
1.No completed games found - Show an error message.
2.All games have been completed - Show scores of all games.
3.Some games have been completed and one game is ongoing - Only show scores of all completed games.
How do you generate # Game i.e., no of games played? Every time you start a new game, you should increment the game counter. Even if the game is discarded, the game counter increases with the next new game. In the below output, the score for game 3 is not shown as it was discarded.
Note: You should maintain a variable for game counter in such a way that every time game ends, the value of game counter is not lost.
Game Scores are to be shown in a formatted way.
# Game means game number
# Hits means the number of times Pacman hit the boundary and the wall
# Moves means the number of moves made by PACMAN to reach the special food. A hit is not included as a move.
# Score means score for the game calculated as 20 * Food - 0.5 * number of hits - 0.25 * number of moves.
Here is a sample output of game scores.
| # Game| # Hits| # Moves| # Score|
|========|========|========|========|
| 1| 0| 3| 19.25|
| 2| 1| 7| 17.75|
| 4| 1| 7| 17.75|
Tip: Use String.format method to format the strings. Use the String formatter "|%8s|%8s|%8s|%8s|%n" for the header and formatter "|%8d|%8d|%8d|%8.2f|%n" for printing the actual values.
Scenario 1: No completed games found
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 4
No completed games found.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>
Scenario 2 & 3: Show all completed games score
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 4
| # Game| # Hits| # Moves| # Score|
|========|========|========|========|
| 1| 0| 3| 19.25|
| 2| 1| 7| 17.75|
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>
Move Pacman
When the user selects option 2 or 3 and plays the game, you should show the move menu. However, the message for starting a game shows information on how to gain points (See slides here and section Start the Game> Scenario 2). If the game is resumed after pausing, the message will be different (See the slides here and section Resume the Game> scenario 2). For simplification, we are only showing the Move menu for a new game in the below outputs.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 2
Move the Pacman towards the food pellet.
> You gain 20 points when Pacman get the food.
> You lose 0.5 point when you hit the wall/boundary.
> Score = 20 * Food - 0.5 * hits - 0.25 * moves.
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
>
Warning: in this case you will expect a String input - w/a/s/d/q. This can be upper or lower case. You must handle the input accordingly.
Invalid Input
If the user enters any other string like “hi”, you must prompt "Invalid input". Sample output below -
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> hi
Invalid Input.
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
>
Movement - Right/Left/Top/Bottom
In case the user wants to move in any direction, the position of PACMAN should change. When the user selects option 2 for the first time, by default the top left corner is the starting position of PACMAN. If the user resumes a game, then you should start from the last saved position of PACMAN.
Every time PACMAN makes a valid move you should count the number of moves, and every time they hit a wall or a boundary you should count the no of hits. A hit is not counted as a move.
Move Right
############
#.---------#
#P.--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> D
############
#.---------#
#.P--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
>
Move Left
############
#.---------#
#.P--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> A
############
#.---------#
#P.--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
>
Move Down
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> S
############
#.---------#
#P.--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
>
Move up
############
#.---------#
#P.--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> W
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
>
Invalid Moves
If PACMAN is already at the leftmost position/rightmost position/ topmost position or bottommost position and the user wants to move left/right/up/down respectively, that would be an invalid move and PACMAN may hit a boundary or a wall. The program should print appropriate messages for the invalid move. Look at the sample outputs below -
Hit a wall
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> D
You have hit a wall.
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
>
Hit a boundary
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> A
You have hit the boundary.
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
>
Exiting from the PACMAN Movement Menu
If the user selects the option Q then the program should save the current position of PACMAN and go back to the main menu.
############
#P---------#
#..--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> S
############
#.---------#
#P.--------#
#...-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> S
############
#.---------#
#..--------#
#P..-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> D
############
#.---------#
#..--------#
#.P.-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> Q
Your game is paused and saved.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>
If the user resumes the game by selecting option 3 from the main menu, the game must restart at the same location.
############
#.---------#
#..--------#
#.P.-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> Q
Your game is paused and saved.
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 3
Restart your game from the last position you saved.
############
#.---------#
#..--------#
#.P.-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
>
Ending a Game
When the PACMAN has reached the special food, you must end the game by showing the score of the current game. This should also be visible if the user selects option 4 to see the scores of the completed games. See the output below -
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 3
Restart your game from the last position you saved.
############
#.---------#
#..--------#
#.P.-------#
#.*..------#
#.....-----#
############
Press W to move up.
Press A to move left.
Press S to move down.
Press D to move right.
Press Q to exit.
> S
############
#.---------#
#..--------#
#...-------#
#.P..------#
#.....-----#
############
Game has ended! Your score for this game is 14.25
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
> 4
| # Game| # Hits| # Moves| # Score|
|========|========|========|========|
| 1| 8| 7| 14.25|
Select an option to get started.
Press 1 to select a pacman maze type.
Press 2 to play the game.
Press 3 to resume the game.
Press 4 to view the scores.
Press 5 to exit.
>

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!