Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.lang.*;
- /*
- *
- * Maze Game
- *
- * INFO1103 Assignment 2
- * 2017 Semester 1
- *
- * The Maze Game.
- * In this assignment you will be designing a maze game.
- * You will have a maze board and a player moving around the board.
- * The player can step left, right, up or down.
- * However, you need to complete the maze within a given number of steps.
- *
- * As in any maze, there are walls that you cannot move through. If you try to
- * move through a wall, you lose a life. You have a limited number of lives.
- * There is also gold on the board that you can collect if you move ontop of it.
- *
- * Please implement the methods provided, as some of the marks are allocated to
- * testing these methods directly.
- *
- * @author YOU :)
- * @date April, 2017
- *
- */
- public class MazeGame {
- /* You can put variables that you need throughout the class up here.
- * You MUST INITIALISE ALL of these class variables in your initialiseGame
- * method.
- */
- // A sample variable to show you can put variables here.
- // You would initialise it in initialiseGame method.
- // e.g. Have the following line in the initialiseGame method.
- // sampleVariable = 1;
- static int lives;
- static int steps;
- static int gold;
- static int rows;
- static String[] board;
- static int getX;
- static int getY;
- static int x;
- static int y;
- static String argument;
- static String command;
- static StringBuilder newBoard;
- static StringBuilder newBoard2;
- /**
- * Initialises the game from the given configuration file.
- * This includes the number of lives, the number of steps, the starting gold
- * and the board.
- *
- * If the configuration file name is "DEFAULT", load the default
- * game configuration.
- *
- * NOTE: Please also initialise all of your class variables.
- *
- * @args configFileName The name of the game configuration file to read from.
- * @throws IOException If there was an error reading in the game.
- * For example, if the input file could not be found.
- */
- public static void initialiseGame(String configFileName) throws IOException {
- // TODO: Implement this method.
- File maze = new File(argument);
- try{
- Scanner scan = new Scanner(maze);
- lives = scan.nextInt();
- //System.out.println(lives);
- steps = scan.nextInt();
- //System.out.println(steps);
- gold = scan.nextInt();
- //System.out.println(gold);
- rows = scan.nextInt();
- //System.out.println(rows);
- String line = scan.nextLine(); //removes the space after the ints
- board = new String[rows];
- for (int i = 0; i < rows; i++){
- board[i] = scan.nextLine();
- }
- scan.close();
- } catch(FileNotFoundException e){
- System.out.println("Error: Could not load the game configuration from '" + maze + "'.");
- return;
- }
- }
- /**
- * Save the current board to the given file name.
- * Note: save it in the same format as you read it in.
- * That is:
- *
- * <number of lives> <number of steps> <amount of gold> <number of rows on the board>
- * <BOARD>
- *
- * @args toFileName The name of the file to save the game configuration to.
- * @throws IOException If there was an error writing the game to the file.
- */
- public static void saveGame(String toFileName) throws IOException {
- // TODO: Implement this method.
- }
- /**
- * Gets the current x position of the player.
- *
- * @return The players current x position.
- */
- public static int getCurrentXPosition() {
- // TODO: Implement this method.
- getX = -1;
- for (int i = 0; i <= rows; i++) {
- if (board[i].contains("&")) {
- getX = board[i].indexOf('&');
- //System.out.println(getX);
- break;
- } else{
- continue;
- }
- }
- return getX;
- }
- /**
- * Gets the current y position of the player.
- *
- * @return The players current y position.
- */
- public static int getCurrentYPosition() {
- // TODO: Implement this method.
- getY = -1;
- for (int i = 0; i< rows; i++){
- if (board[i].contains("&")) {
- getY = i;
- break;
- } else{
- continue;
- }
- }
- //System.out.println(getY);
- return getY;
- }
- /**
- * Gets the number of lives the player currently has.
- *
- * @return The number of lives the player currently has.
- */
- public static int numberOfLives() {
- // TODO: Implement this method.
- lives--;
- return lives;
- }
- /**
- * Gets the number of remaining steps that the player can use.
- *
- * @return The number of steps remaining in the game.
- */
- public static int numberOfStepsRemaining() {
- // TODO: Implement this method.
- steps--;
- return steps;
- }
- /**
- * Gets the amount of gold that the player has collected so far.
- *
- * @return The amount of gold the player has collected so far.
- */
- public static int amountOfGold() {
- // TODO: Implement this method.
- return gold;
- }
- /**
- * Checks to see if the player has completed the maze.
- * The player has completed the maze if they have reached the destination.
- *
- * @return True if the player has completed the maze.
- */
- public static boolean isMazeCompleted() {
- // TODO: Implement this method.
- char destination = board[y].charAt(x);
- // System.out.println(x);
- // System.out.println(y);
- boolean finish;
- if(destination == '@'){
- finish = true;
- } else{
- finish = false;
- }
- return finish;
- //return false;
- }
- /**
- * Checks to see if it is the end of the game.
- * It is the end of the game if one of the following conditions is true:
- * - There are no remaining steps.
- * - The player has no lives.
- * - The player has completed the maze.
- *
- * @return True if any one of the conditions that end the game is true.
- */
- public static boolean isGameEnd() {
- // TODO: Implement this method.
- if (steps == 0 || lives == 0 || isMazeCompleted() == true){
- return true;
- } else {
- return false;
- }
- }
- /**
- * Checks if the coordinates (x, y) are valid.
- * That is, if they are on the board.
- *
- * @args x The x coordinate.
- * @args y The y coordinate.
- * @return True if the given coordinates are valid (on the board),
- * otherwise, false (the coordinates are out of range).
- */
- public static boolean isValidCoordinates(int x, int y) {
- // TODO: Implement this method.
- // System.out.println(x);
- // System.out.println(y);
- if (x < 0 || y < 0){
- return false;
- } else if(x > board[1].length() - 1 || y > rows - 1){
- return false;
- } else{
- return true;
- }
- }
- /**
- * Checks if a move to the given coordinates is valid.
- * A move is invalid if:
- * - It is move to a coordinate off the board.
- * - There is a wall at that coordinate.
- * - The game is ended.
- *
- * @args x The x coordinate to move to.
- * @args y The y coordinate to move to.
- * @return True if the move is valid, otherwise false.
- */
- public static boolean canMoveTo(int x, int y) {
- // TODO: Implement this method.
- newBoard = new StringBuilder(board[y]);
- // System.out.println(x);
- // System.out.println(y);
- char wall = newBoard.charAt(x);
- boolean nani;
- if(wall == '#'){
- nani = false;
- } else{
- nani = true;
- }
- board[y] = newBoard.toString();
- return nani;
- }
- /**
- * Move the player to the given coordinates on the board.
- * After a successful move, it prints "Moved to (x, y)."
- * where (x, y) were the coordinates given.
- *
- * If there was gold at the position the player moved to,
- * the gold should be collected and the message "Plus n gold."
- * should also be printed, where n is the amount of gold collected.
- *
- * If it is an invalid move, a life is lost.
- * The method prints: "Invalid move. One life lost."
- *
- * @args x The x coordinate to move to.
- * @args y The y coordinate to move to.
- */
- public static void moveTo(int x, int y) {
- // TODO: Implement this method.
- if (getY != y){
- newBoard = new StringBuilder(board[y]);
- newBoard2 = new StringBuilder(board[getY]);
- newBoard.setCharAt(x, '&');
- newBoard2.setCharAt(getX, '.');
- board[y] = newBoard.toString();
- board[getY] = newBoard2.toString();
- getX = x;
- getY = y;
- } else{
- newBoard = new StringBuilder(board[y]);
- newBoard.setCharAt(x, '&');
- newBoard.setCharAt(getX, '.');
- board[y] = newBoard.toString();
- getX = x;
- getY = y;
- }
- }
- /**
- * Prints out the help message.
- */
- public static void printHelp() {
- System.out.println("Usage: You can type one of the following commands.");
- System.out.println("help Print this help message.");
- System.out.println("board Print the current board.");
- System.out.println("status Print the current status.");
- System.out.println("left Move the player 1 square to the left.");
- System.out.println("right Move the player 1 square to the right.");
- System.out.println("up Move the player 1 square up.");
- System.out.println("down Move the player 1 square down.");
- System.out.println("save <file> Save the current game configuration to the given file.");
- // TODO: Implement this method.
- }
- /**
- * Prints out the status message.
- */
- public static void printStatus() {
- // TODO: Implement this method.
- System.out.printf("Number of lives(s): %d\n", lives);
- System.out.printf("Number of step(s)remaining: %d\n", steps);
- System.out.printf("Amount of gold: %d\n", gold);
- }
- /**
- * Prints out the board.
- */
- public static void printBoard() {
- for (int i = 0; i< rows; i++){
- System.out.println(board[i]);
- }
- // TODO: Implement this method.
- }
- /**
- * Performs the given action by calling the appropriate helper methods.
- * [For example, calling the printHelp() method if the action is "help".]
- *
- * The valid actions are "help", "board", "status", "left", "right",
- * "up", "down", and "save".
- * [Note: The actions are case insensitive.]
- * If it is not a valid action, an IllegalArgumentException should be thrown.
- *
- * @args action The action we are performing.
- * @throws IllegalArgumentException If the action given isn't one of the
- * allowed actions.
- */
- public static void performAction(String action) throws IllegalArgumentException {
- // TODO: Implement this method.
- if (action.equalsIgnoreCase("help")){
- printHelp();
- } else if (action.equalsIgnoreCase("board")){
- printBoard();
- } else if (action.equalsIgnoreCase("status")){
- printStatus();
- } else if (action.equalsIgnoreCase("getX")){
- System.out.println(getCurrentXPosition());
- } else if (action.equalsIgnoreCase("getY")){
- System.out.println(getCurrentYPosition());
- } else if (action.equalsIgnoreCase("left")){
- if (isValidCoordinates(getCurrentXPosition()-1, getCurrentYPosition()) == true){
- System.out.printf("Moved to (%d, %d)\n", getCurrentXPosition()-1, getCurrentYPosition());
- // System.out.println("isValidCoordinates: " + isValidCoordinates(x, y));
- // System.out.println("canMoveTo: " + canMoveTo(getX-1,getY));
- if (canMoveTo(getX-1, getY) == true){
- x = getX-1;
- y = getY;
- // System.out.println("isMazeCompleted: " + isMazeCompleted());
- if (isMazeCompleted() == true){
- System.out.println("Congratulations! You completed the maze!");
- numberOfStepsRemaining(); //means you moved onto the last destination
- System.out.println("Your final status is:");
- printStatus();
- return;
- } else if (isGameEnd() == true){
- if(steps ==0){
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- }else{
- moveTo(getX-1, getY);
- numberOfStepsRemaining();
- // printBoard();
- }
- } else{ //if it is a wall
- System.out.println("Invalid move. One life lost.");
- numberOfStepsRemaining();
- numberOfLives();
- if (isGameEnd() == true){
- if(steps ==0 && lives ==0){
- System.out.println("Oh no! You have no lives and no steps left");
- System.out.println("Better luck next time!");
- return;
- } else if (lives == 0){
- System.out.println("Oh no! You have no lives left");
- System.out.println("Better luck next time!");
- return;
- } else{
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- }
- // printBoard();
- }
- } else{ //if it is off the board
- System.out.println("Invalid move. One life lost.");
- numberOfStepsRemaining();
- numberOfLives();
- if (isGameEnd() == true){
- if(steps ==0 && lives ==0){
- System.out.println("Oh no! You have no lives and no steps left");
- System.out.println("Better luck next time!");
- return;
- } else if (lives == 0){
- System.out.println("Oh no! You have no lives left");
- System.out.println("Better luck next time!");
- return;
- } else{
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- }
- // printBoard();
- }
- } else if (action.equalsIgnoreCase("right")){
- if (isValidCoordinates(getCurrentXPosition()+1, getCurrentYPosition()) == true){
- System.out.printf("Moved to (%d, %d)\n", getCurrentXPosition()+1, getCurrentYPosition());
- // System.out.println("isValidCoordinates: " + isValidCoordinates(x, y));
- // System.out.println("canMoveTo: " + canMoveTo(getX+1,getY));
- if (canMoveTo(getX+1, getY) == true){
- x = getX+1;
- y = getY;
- // System.out.println("isMazeCompleted: " + isMazeCompleted());
- if (isMazeCompleted() == true){
- System.out.println("Congratulations! You completed the maze!");
- numberOfStepsRemaining();
- System.out.println("Your final status is:");
- printStatus();
- return;
- } else if (isGameEnd() == true){
- if(steps ==0){
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- }else{
- moveTo(getX+1, getY);
- numberOfStepsRemaining();
- // printBoard();
- }
- } else{ //if its a wall
- System.out.println("Invalid move. One life lost.");
- numberOfStepsRemaining();
- numberOfLives();
- if (isGameEnd() == true){
- if(steps ==0 && lives ==0){
- System.out.println("Oh no! You have no lives and no steps left");
- System.out.println("Better luck next time!");
- return;
- } else if (lives == 0){
- System.out.println("Oh no! You have no lives left");
- System.out.println("Better luck next time!");
- return;
- } else{
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- }
- // printBoard();
- }
- } else{ //if its off the board
- System.out.println("Invalid move. One life lost.");
- numberOfStepsRemaining();
- numberOfLives();
- if (isGameEnd() == true){
- if(steps ==0 && lives ==0){
- System.out.println("Oh no! You have no lives and no steps left");
- System.out.println("Better luck next time!");
- return;
- } else if (lives == 0){
- System.out.println("Oh no! You have no lives left");
- System.out.println("Better luck next time!");
- return;
- } else{
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- }
- // printBoard();
- }
- } else if (action.equalsIgnoreCase("up")){
- if (isValidCoordinates(getCurrentXPosition(), getCurrentYPosition()-1) == true){
- System.out.printf("Moved to (%d, %d)\n", getCurrentXPosition(), getCurrentYPosition()-1);
- // System.out.println("isValidCoordinates: " + isValidCoordinates(x, y));
- // System.out.println("canMoveTo: " + canMoveTo(getX,getY-1));
- if (canMoveTo(getX, getY-1) == true){
- x = getX;
- y = getY-1;
- // System.out.println("isMazeCompleted: " + isMazeCompleted());
- if (isMazeCompleted() == true){
- System.out.println("Congratulations! You completed the maze!");
- numberOfStepsRemaining();
- System.out.println("Your final status is:");
- printStatus();
- return;
- }else if (isGameEnd() == true){
- if(steps ==0){
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- } else{
- moveTo(getX, getY-1);
- numberOfStepsRemaining();
- // printBoard();
- }
- } else{ //if its a wall
- System.out.println("Invalid move. One life lost.");
- numberOfStepsRemaining();
- numberOfLives();
- if (isGameEnd() == true){
- if(steps ==0 && lives ==0){
- System.out.println("Oh no! You have no lives and no steps left");
- System.out.println("Better luck next time!");
- return;
- } else if (lives == 0){
- System.out.println("Oh no! You have no lives left");
- System.out.println("Better luck next time!");
- return;
- } else{
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- }
- // printBoard();
- }
- } else{ //if its off the board
- System.out.println("Invalid move. One life lost.");
- numberOfStepsRemaining();
- numberOfLives();
- if (isGameEnd() == true){
- if(steps ==0 && lives ==0){
- System.out.println("Oh no! You have no lives and no steps left");
- System.out.println("Better luck next time!");
- return;
- } else if (lives == 0){
- System.out.println("Oh no! You have no lives left");
- System.out.println("Better luck next time!");
- return;
- } else{
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- }
- // printBoard();
- }
- } else if (action.equalsIgnoreCase("down")){
- if (isValidCoordinates(getCurrentXPosition(), getCurrentYPosition()+1) == true){
- System.out.printf("Moved to (%d, %d)\n", getCurrentXPosition(), getCurrentYPosition()+1);
- // System.out.println("isValidCoordinates: " + isValidCoordinates(x, y));
- // System.out.println("canMoveTo: " + canMoveTo(getX,getY+1));
- if (canMoveTo(getX, getY+1) == true){
- x = getX;
- y = getY+1;
- // System.out.println("isMazeCompleted: " + isMazeCompleted());
- if (isMazeCompleted() == true){
- System.out.println("Congratulations! You completed the maze!");
- numberOfStepsRemaining();
- System.out.println("Your final status is:");
- printStatus();
- return;
- } else if (isGameEnd() == true){
- if(steps ==0){
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- } else{
- moveTo(getX, getY+1);
- numberOfStepsRemaining();
- // printBoard();
- }
- } else{ //if it is a wall
- System.out.println("Invalid move. One life lost.");
- numberOfStepsRemaining();
- numberOfLives();
- if (isGameEnd() == true){
- if(steps ==0 && lives ==0){
- System.out.println("Oh no! You have no lives and no steps left");
- System.out.println("Better luck next time!");
- return;
- } else if (lives == 0){
- System.out.println("Oh no! You have no lives left");
- System.out.println("Better luck next time!");
- return;
- } else{
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- }
- // printBoard();
- }
- } else{ //if it is off the board
- System.out.println("Invalid move. One life lost.");
- numberOfStepsRemaining();
- numberOfLives();
- if (isGameEnd() == true){
- if(steps ==0 && lives ==0){
- System.out.println("Oh no! You have no lives and no steps left");
- System.out.println("Better luck next time!");
- return;
- } else if (lives == 0){
- System.out.println("Oh no! You have no lives left");
- System.out.println("Better luck next time!");
- return;
- } else{
- System.out.println("Oh no! You have no steps left");
- System.out.println("Better luck next time!");
- return;
- }
- }
- // printBoard();
- }
- } else{
- System.out.printf("Error: Could not find command '%s'.\n", command);
- System.out.println("To find the list of valid commands, please type 'help'.");
- }
- }
- /**
- * The main method of your program.
- *
- * @args args[0] The game configuration file from which to initialise the
- * maze game. If it is DEFAULT, load the default configuration.
- */
- public static void main(String[] args) {
- if(args.length == 0){
- System.out.println("Error: Too few arguments given. Expected 1 argument, found 0.");
- System.out.println("Usage: MazeGame [<game configuration file>|DEFAULT]");
- return;
- } else if(args.length >= 2){
- System.out.printf("Error: Too many arguments given. Expected 1 argument, found %s.\n", args.length);
- System.out.println("Usage: MazeGame [<game configuration file>|DEFAULT]");
- return;
- }
- Scanner scan = new Scanner(System.in);
- try{
- argument = args[0];
- initialiseGame(argument);
- while(scan.hasNextLine()){
- command = scan.nextLine();
- performAction(command);
- }
- } catch (IOException e){
- System.out.println("Ok");
- } catch (IllegalArgumentException e){
- System.out.println("Ok");
- }
- }
- // Run your program (reading in from args etc) from here.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement