Advertisement
Guest User

5:53 23/04 G Maze code

a guest
Apr 23rd, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.98 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.lang.*;
  4.  
  5. /*
  6.  *
  7.  * Maze Game
  8.  *
  9.  * INFO1103 Assignment 2
  10.  * 2017 Semester 1
  11.  *
  12.  * The Maze Game.
  13.  * In this assignment you will be designing a maze game.
  14.  * You will have a maze board and a player moving around the board.
  15.  * The player can step left, right, up or down.
  16.  * However, you need to complete the maze within a given number of steps.
  17.  *
  18.  * As in any maze, there are walls that you cannot move through. If you try to
  19.  * move through a wall, you lose a life. You have a limited number of lives.
  20.  * There is also gold on the board that you can collect if you move ontop of it.
  21.  *
  22.  * Please implement the methods provided, as some of the marks are allocated to
  23.  * testing these methods directly.
  24.  *
  25.  * @author YOU :)
  26.  * @date April, 2017
  27.  *
  28.  */
  29. public class MazeGame {
  30.     /* You can put variables that you need throughout the class up here.
  31.      * You MUST INITIALISE ALL of these class variables in your initialiseGame
  32.      * method.
  33.      */
  34.  
  35.     // A sample variable to show you can put variables here.
  36.     // You would initialise it in initialiseGame method.
  37.     // e.g. Have the following line in the initialiseGame method.
  38.     // sampleVariable = 1;
  39.  
  40.     static int lives;
  41.     static int steps;
  42.     static int gold;
  43.     static int rows;
  44.     static String[] board;
  45.     static int getX;
  46.     static int getY;
  47.     static int x;
  48.     static int y;
  49.     static String argument;
  50.     static String command;
  51.     static StringBuilder newBoard;
  52.     static StringBuilder newBoard2;
  53.  
  54.  
  55.     /**
  56.      * Initialises the game from the given configuration file.
  57.      * This includes the number of lives, the number of steps, the starting gold
  58.      * and the board.
  59.      *
  60.      * If the configuration file name is "DEFAULT", load the default
  61.      * game configuration.
  62.      *
  63.      * NOTE: Please also initialise all of your class variables.
  64.      *
  65.      * @args configFileName The name of the game configuration file to read from.
  66.      * @throws IOException If there was an error reading in the game.
  67.      *         For example, if the input file could not be found.
  68.      */
  69.  
  70.     public static void initialiseGame(String configFileName) throws IOException {
  71.         // TODO: Implement this method.
  72.         File maze = new File(argument);
  73.  
  74.         try{
  75.             Scanner scan = new Scanner(maze);
  76.             lives = scan.nextInt();
  77.             //System.out.println(lives);
  78.             steps = scan.nextInt();
  79.             //System.out.println(steps);
  80.             gold = scan.nextInt();
  81.             //System.out.println(gold);
  82.             rows = scan.nextInt();
  83.             //System.out.println(rows);
  84.            
  85.             String line = scan.nextLine(); //removes the space after the ints
  86.             board = new String[rows];
  87.            
  88.             for (int i = 0; i < rows; i++){
  89.                 board[i] = scan.nextLine();
  90.             }
  91.             scan.close();
  92.         } catch(FileNotFoundException e){
  93.             System.out.println("Error: Could not load the game configuration from '" + maze + "'.");
  94.             return;
  95.         }
  96.     }
  97.  
  98.    
  99.     /**
  100.      * Save the current board to the given file name.
  101.      * Note: save it in the same format as you read it in.
  102.      * That is:
  103.      *
  104.      * <number of lives> <number of steps> <amount of gold> <number of rows on the board>
  105.      * <BOARD>
  106.      *
  107.      * @args toFileName The name of the file to save the game configuration to.
  108.      * @throws IOException If there was an error writing the game to the file.
  109.      */
  110.  
  111.  
  112.  
  113.     public static void saveGame(String toFileName) throws IOException {
  114.         // TODO: Implement this method.
  115.     }
  116.  
  117.     /**
  118.      * Gets the current x position of the player.
  119.      *
  120.      * @return The players current x position.
  121.      */
  122.     public static int getCurrentXPosition() {
  123.         // TODO: Implement this method.
  124.         getX = -1;
  125.         for (int i = 0; i <= rows; i++) {
  126.             if (board[i].contains("&")) {
  127.                 getX = board[i].indexOf('&');
  128.                 //System.out.println(getX);
  129.                 break;
  130.             } else{
  131.                 continue;
  132.             }
  133.         }
  134.         return getX;
  135.     }
  136.  
  137.     /**
  138.      * Gets the current y position of the player.
  139.      *
  140.      * @return The players current y position.
  141.      */
  142.     public static int getCurrentYPosition() {
  143.         // TODO: Implement this method.
  144.         getY = -1;
  145.         for (int i = 0; i< rows; i++){
  146.             if (board[i].contains("&")) {
  147.                 getY = i;
  148.                 break;
  149.             } else{
  150.                 continue;
  151.             }
  152.         }
  153.         //System.out.println(getY);
  154.         return getY;
  155.     }
  156.  
  157.     /**
  158.      * Gets the number of lives the player currently has.
  159.      *
  160.      * @return The number of lives the player currently has.
  161.      */
  162.     public static int numberOfLives() {
  163.         // TODO: Implement this method.
  164.         lives--;
  165.         return lives;
  166.     }
  167.  
  168.     /**
  169.      * Gets the number of remaining steps that the player can use.
  170.      *
  171.      * @return The number of steps remaining in the game.
  172.      */
  173.     public static int numberOfStepsRemaining() {
  174.         // TODO: Implement this method.
  175.         steps--;
  176.         return steps;
  177.     }
  178.  
  179.     /**
  180.      * Gets the amount of gold that the player has collected so far.
  181.      *
  182.      * @return The amount of gold the player has collected so far.
  183.      */
  184.     public static int amountOfGold() {
  185.         // TODO: Implement this method.
  186.        
  187.         return gold;
  188.     }
  189.  
  190.  
  191.     /**
  192.      * Checks to see if the player has completed the maze.
  193.      * The player has completed the maze if they have reached the destination.
  194.      *
  195.      * @return True if the player has completed the maze.
  196.      */
  197.     public static boolean isMazeCompleted() {
  198.         // TODO: Implement this method.
  199.         char destination = board[y].charAt(x);
  200.         // System.out.println(x);
  201.         // System.out.println(y);
  202.         boolean finish;
  203.         if(destination == '@'){
  204.             finish = true;
  205.         } else{
  206.             finish = false;
  207.         }
  208.         return finish;
  209.         //return false;
  210.     }
  211.  
  212.     /**
  213.      * Checks to see if it is the end of the game.
  214.      * It is the end of the game if one of the following conditions is true:
  215.      *  - There are no remaining steps.
  216.      *  - The player has no lives.
  217.      *  - The player has completed the maze.
  218.      *
  219.      * @return True if any one of the conditions that end the game is true.
  220.      */
  221.     public static boolean isGameEnd() {
  222.         // TODO: Implement this method.
  223.         if (steps == 0 || lives == 0 || isMazeCompleted() == true){
  224.             return true;
  225.         } else {
  226.             return false;
  227.         }
  228.     }
  229.        
  230.  
  231.     /**
  232.      * Checks if the coordinates (x, y) are valid.
  233.      * That is, if they are on the board.
  234.      *
  235.      * @args x The x coordinate.
  236.      * @args y The y coordinate.
  237.      * @return True if the given coordinates are valid (on the board),
  238.      *         otherwise, false (the coordinates are out of range).
  239.      */
  240.     public static boolean isValidCoordinates(int x, int y) {
  241.         // TODO: Implement this method.
  242.         // System.out.println(x);
  243.         // System.out.println(y);
  244.         if (x < 0 || y < 0){
  245.             return false;
  246.         } else if(x > board[1].length() - 1 || y > rows - 1){
  247.             return false;
  248.         } else{
  249.             return true;
  250.         }
  251.     }
  252.  
  253.     /**
  254.      * Checks if a move to the given coordinates is valid.
  255.      * A move is invalid if:
  256.      *  - It is move to a coordinate off the board.
  257.      *  - There is a wall at that coordinate.
  258.      *  - The game is ended.
  259.      *
  260.      * @args x The x coordinate to move to.
  261.      * @args y The y coordinate to move to.
  262.      * @return True if the move is valid, otherwise false.
  263.      */
  264.     public static boolean canMoveTo(int x, int y) {
  265.         // TODO: Implement this method.
  266.         newBoard = new StringBuilder(board[y]);
  267.         // System.out.println(x);
  268.         // System.out.println(y);
  269.         char wall = newBoard.charAt(x);
  270.         boolean nani;
  271.         if(wall == '#'){
  272.             nani = false;
  273.         } else{
  274.             nani = true;
  275.         }
  276.         board[y] = newBoard.toString();
  277.         return nani;
  278.     }
  279.  
  280.     /**
  281.      * Move the player to the given coordinates on the board.
  282.      * After a successful move, it prints "Moved to (x, y)."
  283.      * where (x, y) were the coordinates given.
  284.      *
  285.      * If there was gold at the position the player moved to,
  286.      * the gold should be collected and the message "Plus n gold."
  287.      * should also be printed, where n is the amount of gold collected.
  288.      *
  289.      * If it is an invalid move, a life is lost.
  290.      * The method prints: "Invalid move. One life lost."
  291.      *
  292.      * @args x The x coordinate to move to.
  293.      * @args y The y coordinate to move to.
  294.      */
  295.     public static void moveTo(int x, int y) {
  296.         // TODO: Implement this method.
  297.         if (getY != y){
  298.             newBoard = new StringBuilder(board[y]);
  299.             newBoard2 = new StringBuilder(board[getY]);
  300.             newBoard.setCharAt(x, '&');
  301.             newBoard2.setCharAt(getX, '.');
  302.             board[y] = newBoard.toString();
  303.             board[getY] = newBoard2.toString();
  304.             getX = x;
  305.             getY = y;
  306.         } else{
  307.             newBoard = new StringBuilder(board[y]);
  308.             newBoard.setCharAt(x, '&');
  309.             newBoard.setCharAt(getX, '.');
  310.             board[y] = newBoard.toString();
  311.             getX = x;
  312.             getY = y;
  313.         }
  314.        
  315.                
  316.     }
  317.  
  318.     /**
  319.      * Prints out the help message.
  320.      */
  321.     public static void printHelp() {
  322.         System.out.println("Usage: You can type one of the following commands.");
  323.         System.out.println("help        Print this help message.");
  324.         System.out.println("board       Print the current board.");
  325.         System.out.println("status      Print the current status.");
  326.         System.out.println("left        Move the player 1 square to the left.");
  327.         System.out.println("right       Move the player 1 square to the right.");
  328.         System.out.println("up          Move the player 1 square up.");
  329.         System.out.println("down        Move the player 1 square down.");
  330.         System.out.println("save <file> Save the current game configuration to the given file.");
  331.         // TODO: Implement this method.
  332.     }
  333.  
  334.     /**
  335.      * Prints out the status message.
  336.      */
  337.     public static void printStatus() {
  338.         // TODO: Implement this method.
  339.         System.out.printf("Number of lives(s): %d\n", lives);
  340.         System.out.printf("Number of step(s)remaining: %d\n", steps);
  341.         System.out.printf("Amount of gold: %d\n", gold);
  342.     }
  343.  
  344.     /**
  345.      * Prints out the board.
  346.      */
  347.     public static void printBoard() {
  348.         for (int i = 0; i< rows; i++){
  349.             System.out.println(board[i]);
  350.         }
  351.         // TODO: Implement this method.
  352.     }
  353.  
  354.     /**
  355.      * Performs the given action by calling the appropriate helper methods.
  356.      * [For example, calling the printHelp() method if the action is "help".]
  357.      *
  358.      * The valid actions are "help", "board", "status", "left", "right",
  359.      * "up", "down", and "save".
  360.      * [Note: The actions are case insensitive.]
  361.      * If it is not a valid action, an IllegalArgumentException should be thrown.
  362.      *
  363.      * @args action The action we are performing.
  364.      * @throws IllegalArgumentException If the action given isn't one of the
  365.      *         allowed actions.
  366.      */
  367.     public static void performAction(String action) throws IllegalArgumentException {
  368.         // TODO: Implement this method.
  369.  
  370.          if (action.equalsIgnoreCase("help")){
  371.                 printHelp();
  372.             } else if (action.equalsIgnoreCase("board")){
  373.                 printBoard();
  374.             } else if (action.equalsIgnoreCase("status")){
  375.                 printStatus();
  376.             } else if (action.equalsIgnoreCase("getX")){
  377.                 System.out.println(getCurrentXPosition());
  378.             } else if (action.equalsIgnoreCase("getY")){
  379.                 System.out.println(getCurrentYPosition());
  380.             } else if (action.equalsIgnoreCase("left")){
  381.                     if (isValidCoordinates(getCurrentXPosition()-1, getCurrentYPosition()) == true){
  382.                         System.out.printf("Moved to (%d, %d)\n", getCurrentXPosition()-1, getCurrentYPosition());
  383.                         // System.out.println("isValidCoordinates: " + isValidCoordinates(x, y));
  384.                         // System.out.println("canMoveTo: " + canMoveTo(getX-1,getY));
  385.                         if (canMoveTo(getX-1, getY) == true){
  386.                             x = getX-1;
  387.                             y = getY;
  388.                             // System.out.println("isMazeCompleted: " + isMazeCompleted());
  389.                             if (isMazeCompleted() == true){
  390.                                 System.out.println("Congratulations! You completed the maze!");
  391.                                 numberOfStepsRemaining(); //means you moved onto the last destination
  392.                                 System.out.println("Your final status is:");
  393.                                 printStatus();
  394.                                 return;
  395.                             } else if (isGameEnd() == true){
  396.                                 if(steps ==0){
  397.                                     System.out.println("Oh no! You have no steps left");
  398.                                     System.out.println("Better luck next time!");
  399.                                     return;
  400.                                 }
  401.                             }else{
  402.                                 moveTo(getX-1, getY);
  403.                                 numberOfStepsRemaining();
  404.                                 // printBoard();
  405.                             }
  406.                         } else{ //if it is a wall
  407.                             System.out.println("Invalid move. One life lost.");
  408.                             numberOfStepsRemaining();
  409.                             numberOfLives();
  410.                             if (isGameEnd() == true){
  411.                                 if(steps ==0 && lives ==0){
  412.                                     System.out.println("Oh no! You have no lives and no steps left");
  413.                                     System.out.println("Better luck next time!");
  414.                                     return;
  415.                                 } else if (lives == 0){
  416.                                     System.out.println("Oh no! You have no lives left");
  417.                                     System.out.println("Better luck next time!");
  418.                                     return;
  419.                                 } else{
  420.                                     System.out.println("Oh no! You have no steps left");
  421.                                     System.out.println("Better luck next time!");
  422.                                     return;
  423.                                 }
  424.                             }
  425.                         // printBoard();
  426.                         }
  427.                     } else{ //if it is off the board
  428.                         System.out.println("Invalid move. One life lost.");
  429.                         numberOfStepsRemaining();
  430.                         numberOfLives();
  431.                             if (isGameEnd() == true){
  432.                                 if(steps ==0 && lives ==0){
  433.                                     System.out.println("Oh no! You have no lives and no steps left");
  434.                                     System.out.println("Better luck next time!");
  435.                                     return;
  436.                                 } else if (lives == 0){
  437.                                     System.out.println("Oh no! You have no lives left");
  438.                                     System.out.println("Better luck next time!");
  439.                                     return;
  440.                                 } else{
  441.                                     System.out.println("Oh no! You have no steps left");
  442.                                     System.out.println("Better luck next time!");
  443.                                     return;
  444.                                 }
  445.                             }
  446.                         // printBoard();
  447.                     }
  448.             } else if (action.equalsIgnoreCase("right")){
  449.                     if (isValidCoordinates(getCurrentXPosition()+1, getCurrentYPosition()) == true){
  450.                         System.out.printf("Moved to (%d, %d)\n", getCurrentXPosition()+1, getCurrentYPosition());
  451.                         // System.out.println("isValidCoordinates: " + isValidCoordinates(x, y));
  452.                         // System.out.println("canMoveTo: " + canMoveTo(getX+1,getY));
  453.                         if (canMoveTo(getX+1, getY) == true){
  454.                             x = getX+1;
  455.                             y = getY;
  456.                             // System.out.println("isMazeCompleted: " + isMazeCompleted());
  457.                             if (isMazeCompleted() == true){
  458.                                 System.out.println("Congratulations! You completed the maze!");
  459.                                 numberOfStepsRemaining();
  460.                                 System.out.println("Your final status is:");
  461.                                 printStatus();
  462.                                 return;
  463.                             }  else if (isGameEnd() == true){
  464.                                 if(steps ==0){
  465.                                     System.out.println("Oh no! You have no steps left");
  466.                                     System.out.println("Better luck next time!");
  467.                                     return;
  468.                                 }
  469.                             }else{
  470.                                 moveTo(getX+1, getY);
  471.                                 numberOfStepsRemaining();
  472.                                 // printBoard();
  473.                             }
  474.                         } else{ //if its a wall
  475.                             System.out.println("Invalid move. One life lost.");
  476.                             numberOfStepsRemaining();
  477.                             numberOfLives();
  478.                             if (isGameEnd() == true){
  479.                                 if(steps ==0 && lives ==0){
  480.                                     System.out.println("Oh no! You have no lives and no steps left");
  481.                                     System.out.println("Better luck next time!");
  482.                                     return;
  483.                                 } else if (lives == 0){
  484.                                     System.out.println("Oh no! You have no lives left");
  485.                                     System.out.println("Better luck next time!");
  486.                                     return;
  487.                                 } else{
  488.                                     System.out.println("Oh no! You have no steps left");
  489.                                     System.out.println("Better luck next time!");
  490.                                     return;
  491.                                 }
  492.                             }
  493.                             // printBoard();
  494.                         }
  495.                     } else{ //if its off the board
  496.                         System.out.println("Invalid move. One life lost.");
  497.                         numberOfStepsRemaining();
  498.                         numberOfLives();
  499.                             if (isGameEnd() == true){
  500.                                 if(steps ==0 && lives ==0){
  501.                                     System.out.println("Oh no! You have no lives and no steps left");
  502.                                     System.out.println("Better luck next time!");
  503.                                     return;
  504.                                 } else if (lives == 0){
  505.                                     System.out.println("Oh no! You have no lives left");
  506.                                     System.out.println("Better luck next time!");
  507.                                     return;
  508.                                 } else{
  509.                                     System.out.println("Oh no! You have no steps left");
  510.                                     System.out.println("Better luck next time!");
  511.                                     return;
  512.                                 }
  513.                             }
  514.                         // printBoard();
  515.                     }
  516.             } else if (action.equalsIgnoreCase("up")){
  517.                     if (isValidCoordinates(getCurrentXPosition(), getCurrentYPosition()-1) == true){
  518.                         System.out.printf("Moved to (%d, %d)\n", getCurrentXPosition(), getCurrentYPosition()-1);
  519.                         // System.out.println("isValidCoordinates: " + isValidCoordinates(x, y));
  520.                         // System.out.println("canMoveTo: " + canMoveTo(getX,getY-1));
  521.                         if (canMoveTo(getX, getY-1) == true){
  522.                             x = getX;
  523.                             y = getY-1;
  524.                             // System.out.println("isMazeCompleted: " + isMazeCompleted());
  525.                             if (isMazeCompleted() == true){
  526.                                 System.out.println("Congratulations! You completed the maze!");
  527.                                 numberOfStepsRemaining();
  528.                                 System.out.println("Your final status is:");
  529.                                 printStatus();
  530.                                 return;
  531.                             }else if (isGameEnd() == true){
  532.                                 if(steps ==0){
  533.                                     System.out.println("Oh no! You have no steps left");
  534.                                     System.out.println("Better luck next time!");
  535.                                     return;
  536.                                 }
  537.                             } else{
  538.                                 moveTo(getX, getY-1);
  539.                                 numberOfStepsRemaining();
  540.                                 // printBoard();
  541.                             }
  542.                         } else{ //if its a wall
  543.                             System.out.println("Invalid move. One life lost.");
  544.                             numberOfStepsRemaining();
  545.                             numberOfLives();
  546.                             if (isGameEnd() == true){
  547.                                 if(steps ==0 && lives ==0){
  548.                                     System.out.println("Oh no! You have no lives and no steps left");
  549.                                     System.out.println("Better luck next time!");
  550.                                     return;
  551.                                 } else if (lives == 0){
  552.                                     System.out.println("Oh no! You have no lives left");
  553.                                     System.out.println("Better luck next time!");
  554.                                     return;
  555.                                 } else{
  556.                                     System.out.println("Oh no! You have no steps left");
  557.                                     System.out.println("Better luck next time!");
  558.                                     return;
  559.                                 }
  560.                             }
  561.                             // printBoard();
  562.                         }
  563.                     } else{ //if its off the board
  564.                         System.out.println("Invalid move. One life lost.");
  565.                         numberOfStepsRemaining();
  566.                         numberOfLives();
  567.                             if (isGameEnd() == true){
  568.                                 if(steps ==0 && lives ==0){
  569.                                     System.out.println("Oh no! You have no lives and no steps left");
  570.                                     System.out.println("Better luck next time!");
  571.                                     return;
  572.                                 } else if (lives == 0){
  573.                                     System.out.println("Oh no! You have no lives left");
  574.                                     System.out.println("Better luck next time!");
  575.                                     return;
  576.                                 } else{
  577.                                     System.out.println("Oh no! You have no steps left");
  578.                                     System.out.println("Better luck next time!");
  579.                                     return;
  580.                                 }
  581.                             }
  582.                         // printBoard();
  583.                     }
  584.             } else if (action.equalsIgnoreCase("down")){
  585.                     if (isValidCoordinates(getCurrentXPosition(), getCurrentYPosition()+1) == true){
  586.                         System.out.printf("Moved to (%d, %d)\n", getCurrentXPosition(), getCurrentYPosition()+1);
  587.                         // System.out.println("isValidCoordinates: " + isValidCoordinates(x, y));
  588.                         // System.out.println("canMoveTo: " + canMoveTo(getX,getY+1));
  589.                         if (canMoveTo(getX, getY+1) == true){
  590.                             x = getX;
  591.                             y = getY+1;
  592.                             // System.out.println("isMazeCompleted: " + isMazeCompleted());
  593.                             if (isMazeCompleted() == true){
  594.                                 System.out.println("Congratulations! You completed the maze!");
  595.                                 numberOfStepsRemaining();
  596.                                 System.out.println("Your final status is:");
  597.                                 printStatus();
  598.                                 return;
  599.                             } else if (isGameEnd() == true){
  600.                                 if(steps ==0){
  601.                                     System.out.println("Oh no! You have no steps left");
  602.                                     System.out.println("Better luck next time!");
  603.                                     return;
  604.                                 }
  605.                             } else{
  606.                                 moveTo(getX, getY+1);
  607.                                 numberOfStepsRemaining();
  608.                                 // printBoard();
  609.                             }
  610.                         } else{ //if it is a wall
  611.                             System.out.println("Invalid move. One life lost.");
  612.                             numberOfStepsRemaining();
  613.                             numberOfLives();
  614.                             if (isGameEnd() == true){
  615.                                 if(steps ==0 && lives ==0){
  616.                                     System.out.println("Oh no! You have no lives and no steps left");
  617.                                     System.out.println("Better luck next time!");
  618.                                     return;
  619.                                 } else if (lives == 0){
  620.                                     System.out.println("Oh no! You have no lives left");
  621.                                     System.out.println("Better luck next time!");
  622.                                     return;
  623.                                 } else{
  624.                                     System.out.println("Oh no! You have no steps left");
  625.                                     System.out.println("Better luck next time!");
  626.                                     return;
  627.                                 }
  628.                             }
  629.                             // printBoard();
  630.                         }
  631.                     } else{ //if it is off the board
  632.                         System.out.println("Invalid move. One life lost.");
  633.                         numberOfStepsRemaining();
  634.                         numberOfLives();
  635.                         if (isGameEnd() == true){
  636.                                 if(steps ==0 && lives ==0){
  637.                                     System.out.println("Oh no! You have no lives and no steps left");
  638.                                     System.out.println("Better luck next time!");
  639.                                     return;
  640.                                 } else if (lives == 0){
  641.                                     System.out.println("Oh no! You have no lives left");
  642.                                     System.out.println("Better luck next time!");
  643.                                     return;
  644.                                 } else{
  645.                                     System.out.println("Oh no! You have no steps left");
  646.                                     System.out.println("Better luck next time!");
  647.                                     return;
  648.                                 }
  649.                         }
  650.                         // printBoard();
  651.                         }
  652.             } else{
  653.                 System.out.printf("Error: Could not find command '%s'.\n", command);
  654.                 System.out.println("To find the list of valid commands, please type 'help'.");
  655.             }      
  656.     }
  657.  
  658.     /**
  659.      * The main method of your program.
  660.      *
  661.      * @args args[0] The game configuration file from which to initialise the
  662.      *       maze game. If it is DEFAULT, load the default configuration.
  663.      */
  664.       public static void main(String[] args) {
  665.         if(args.length == 0){
  666.             System.out.println("Error: Too few arguments given. Expected 1 argument, found 0.");
  667.             System.out.println("Usage: MazeGame [<game configuration file>|DEFAULT]");
  668.             return;
  669.         } else if(args.length >= 2){
  670.             System.out.printf("Error: Too many arguments given. Expected 1 argument, found %s.\n", args.length);
  671.             System.out.println("Usage: MazeGame [<game configuration file>|DEFAULT]");
  672.             return;
  673.         }
  674.         Scanner scan = new Scanner(System.in);
  675.         try{
  676.             argument = args[0];
  677.             initialiseGame(argument);
  678.             while(scan.hasNextLine()){
  679.                 command = scan.nextLine();
  680.                 performAction(command);
  681.             }
  682.         } catch (IOException e){
  683.             System.out.println("Ok");
  684.         } catch (IllegalArgumentException e){
  685.             System.out.println("Ok");
  686.         }
  687.        
  688.     }
  689.    
  690.         // Run your program (reading in from args etc) from here.
  691.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement