Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.io.BufferedWriter;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import org.apache.commons.lang3.time.StopWatch;
- /* latest version as of 24/04/2015
- *
- *
- * recent changes:
- *-----------------
- * - added in the ability to retract last move
- * - made text output look nicer
- *
- *
- * to do:
- *-----------------
- * - bigger board (maybe)
- * - ability to load save file and continue with last saved progress
- *
- *
- * done:
- *-----------------
- * - core program - essential
- *
- * - save file - extra feature
- * - custom starting position - extra feature
- * - timer - extra feature
- * - move retraction - extra feature
- *
- */
- public class Index {
- public static void main(String[] args) {
- introMessage();
- beginTour();
- } // end of main
- private static void beginTour(){
- // most of the movement logic is carried out using the knight class
- int[][] boardSizeArray = new int[3][4]; // maybe be better off as global, to save the need to pass it so many times?
- boolean gameOver = false;
- Knight knight = new Knight(); // new object called "knight" of data type Knight
- StopWatch timer = new StopWatch(); // creating a new timer using apache commons library
- timer.start();
- TextIO.putln("Enter your starting position (Anywhere in A or D columns):");
- String choice = TextIO.getln();
- String knightStart = choice.toUpperCase(); // converting input to upper case incase user entered in lower
- // switch statement to place the knight where the player wants to start
- switch (knightStart)
- {
- case "A1": knight.setStartingPos(knightStart); // setting the start location
- boardSizeArray[0][0] = 1; // starting square
- break;
- case "A2": knight.setStartingPos(knightStart);
- boardSizeArray[1][0] = 1;
- break;
- case "A3": knight.setStartingPos(knightStart);
- boardSizeArray[2][0] = 1;
- break;
- case "D1": knight.setStartingPos(knightStart);
- boardSizeArray[0][3] = 1;
- break;
- case "D2": knight.setStartingPos(knightStart);
- boardSizeArray[1][3] = 1;
- break;
- case "D3": knight.setStartingPos(knightStart);
- boardSizeArray[2][3] = 1; // D3
- break;
- default: TextIO.putln("Invalid choice. Choose again!");
- beginTour();
- break;
- } // end switch
- while (gameOver == false)
- {
- displayBoard(boardSizeArray);
- knight.moveKnight(boardSizeArray); // passing the board array to place the knight on new coordinate
- if (knight.getKnightPos() == null) // sometimes knight becomes null, this will detect it
- TextIO.putln("The knight cannot be on an invalid coordinate!");
- else
- {
- TextIO.putln();
- TextIO.putln(" ================== ");
- TextIO.putln(" YOU ARE ON MOVE #" + knight.getMoves() + " ");
- TextIO.putln(" ================== ");
- TextIO.putln("Knight's position: ~" + knight.getKnightPos() + "~");
- TextIO.putln("Knight's previous position: ~" + knight.getPreviousPos() + "~");
- TextIO.putln();
- }
- if (knight.getMoves() == 12) // if player has finished the tour, game is over
- gameOver = true;
- } // end of while loop
- timer.stop(); // once tour is over, timer is stopped
- victoryMessage(boardSizeArray, timer);
- } // end of playGame
- public static void savePath(int[][] boardArray){ // public method to allow the knight class to use it
- // save file code
- Date dNow = new Date( );
- SimpleDateFormat ft =
- new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); //inserts the date and time of the most recent save file
- TextIO.writeFile("Open Knights Tour.txt"); // name of the save file
- TextIO.putln("Open Knights Tour Text File");
- TextIO.putln("***************************************************************************");
- TextIO.putln("");
- TextIO.putln("Your Most Recent End Game Was At: " + ft.format(dNow));
- TextIO.putln("");
- TextIO.putln();
- displayBoard(boardArray); // writing the array to file using the displayBoard method
- TextIO.putln("***************************************************************************");
- TextIO.writeStandardOutput();
- // end of saving file, displaying messages
- TextIO.putln();
- TextIO.putln();
- TextIO.putln("Your progress was saved to a text file. It will be located in the same place you ran the program from.");
- TextIO.putln();
- TextIO.putln("Thank you for playing The Knight's Tour! Press enter to close the program.");
- char enter = TextIO.getAnyChar(); // gives user time to read messages, then closes
- System.exit(0);
- } // end of savePath
- private static void displayBoard(int[][] board){ // prints the 2d array to display the positions visited & current position
- TextIO.putln();
- TextIO.putln(" A B C D");
- for (int i = 0; i < board.length; i++){ // displaying the board as a table - rows
- TextIO.put((i + 1) + ": ");
- for (int j = 0; j < board[i].length; j++){ // columns
- TextIO.put(board[i][j] + " ");
- }// end of INNER for loop
- TextIO.putln();
- } // end of OUTER for loop
- TextIO.putln();
- } // end of displayBoard
- //open file prints a textio version of the save file to the console
- public static void openFile(){
- BufferedReader reader = null;
- try {
- File file = new File("Open Knights Tour.txt");
- reader = new BufferedReader(new FileReader(file));
- String line;
- while ((line = reader.readLine()) != null) {
- TextIO.putln(line);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }//end of open file
- /*
- *
- * MESSAGES BELOW
- *
- */
- private static void introMessage(){ // provide instructions about the program
- TextIO.putln(" |******| ");
- TextIO.putln("THE KNIGHT'S TOUR");
- TextIO.putln(" |******| ");
- TextIO.putln();
- TextIO.putln();
- TextIO.putln("By Cameron Dempster & Grant Hair");
- TextIO.putln();
- TextIO.putln("You will take control of the Knight piece, which can only move in an L shape, \ne.g. 1 across, 2 down, or 2 across, 1 down, etc, basically the same as chess.");
- TextIO.putln("You can choose where your knight will start, either A1 or D3.");
- TextIO.putln("You must visit every square on the board, without visiting the same square more \nthan once.");
- TextIO.putln();
- TextIO.putln("You do not have to enter coordiantes in upper case, you can enter them in lower \ncase such as a1, d3 etc and the program will convert to upper case.");
- TextIO.putln("You may also retract 2 moves per game.");
- TextIO.putln();
- TextIO.putln("=================================================================================");
- TextIO.putln("Press enter to begin or press O to open your most recent save game.");
- char choice = TextIO.getAnyChar();
- switch(choice){
- case 'o' : openFile();
- break;
- case 'O' : openFile();
- break;
- }
- } // end of introMessage
- private static void victoryMessage(int[][] boardArray, StopWatch timer){ // receiving the timer and array to display to user
- TextIO.putln();
- TextIO.putln();
- TextIO.putln("Congratulations! You have completed the tour, successfully visiting every square!");
- TextIO.putln("Time taken to complete: " + timer.toString()); // displaying timer as a string
- TextIO.putln();
- TextIO.putln("Here is the full route you took to complete the game:");
- displayBoard(boardArray);
- TextIO.putln();
- TextIO.putln("You can now save the path you took to a file, for future viewing.");
- TextIO.putln("Do you want to save the path you took? (Y/N)");
- char choice = TextIO.getChar();
- char saveFile = Character.toUpperCase(choice);
- if (saveFile == 'Y')
- savePath(boardArray);
- //else if
- } // end of victoryMessage
- private static void exitMessage(){
- } // end of exitMessage
- } // end of Index class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement