Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.12 KB | None | 0 0
  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. import java.io.BufferedWriter;
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.File;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9.  
  10.  
  11. import org.apache.commons.lang3.time.StopWatch;
  12.  
  13. /* latest version as of 24/04/2015
  14. *
  15. *
  16. *       recent changes:
  17. *-----------------
  18. * - added in the ability to retract last move
  19. * - made text output look nicer
  20. *
  21. *
  22. *       to do:
  23. *-----------------
  24. * - bigger board (maybe)
  25. * - ability to load save file and continue with last saved progress
  26. *
  27. *
  28. *       done:
  29. *-----------------
  30. * - core program - essential
  31. *
  32. * - save file - extra feature
  33. * - custom starting position - extra feature
  34. * - timer - extra feature
  35. * - move retraction - extra feature
  36. *
  37. */
  38.  
  39.  
  40. public class Index {
  41.  
  42.  
  43.        
  44.         public static void main(String[] args) {          
  45.                 introMessage();
  46.                
  47.                 beginTour();
  48.  
  49.         } // end of main
  50.  
  51.        
  52.        
  53.        
  54.        
  55.        
  56.         private static void beginTour(){
  57.                 // most of the movement logic is carried out using the knight class
  58.                
  59.                 int[][] boardSizeArray = new int[3][4]; // maybe be better off as global, to save the need to pass it so many times?
  60.                
  61.                 boolean gameOver = false;
  62.                
  63.  
  64.                 Knight knight = new Knight(); // new object called "knight" of data type Knight
  65.                 StopWatch timer = new StopWatch(); // creating a new timer using apache commons library
  66.                
  67.                
  68.  
  69.                 timer.start();
  70.                
  71.                 TextIO.putln("Enter your starting position (Anywhere in A or D columns):");
  72.                         String choice = TextIO.getln();
  73.                        
  74.                 String knightStart = choice.toUpperCase(); // converting input to upper case incase user entered in lower
  75.                                
  76.                
  77.                 // switch statement to place the knight where the player wants to start
  78.                 switch (knightStart)
  79.                 {
  80.                         case "A1": knight.setStartingPos(knightStart); // setting the start location
  81.                                    boardSizeArray[0][0] = 1; // starting square
  82.                         break;
  83.                                
  84.                         case "A2": knight.setStartingPos(knightStart);
  85.                                            boardSizeArray[1][0] = 1;
  86.                         break;
  87.                                            
  88.                         case "A3": knight.setStartingPos(knightStart);
  89.                                            boardSizeArray[2][0] = 1;  
  90.                         break;
  91.                                            
  92.                         case "D1": knight.setStartingPos(knightStart);
  93.                                            boardSizeArray[0][3] = 1;  
  94.                         break;
  95.                                            
  96.                         case "D2": knight.setStartingPos(knightStart);
  97.                                            boardSizeArray[1][3] = 1;          
  98.                         break;
  99.                                            
  100.                         case "D3": knight.setStartingPos(knightStart);
  101.                                            boardSizeArray[2][3] = 1; // D3    
  102.                         break;
  103.                                            
  104.                                            
  105.                         default: TextIO.putln("Invalid choice. Choose again!");
  106.                                          beginTour();
  107.                         break;
  108.                 } // end switch
  109.                
  110.                
  111.                
  112.        
  113.                
  114.                
  115.                
  116.                 while (gameOver == false)
  117.                 {
  118.  
  119.                         displayBoard(boardSizeArray);
  120.                        
  121.                         knight.moveKnight(boardSizeArray); // passing the board array to place the knight on new coordinate
  122.                        
  123.                        
  124.                        
  125.                         if (knight.getKnightPos() == null) // sometimes knight becomes null, this will detect it
  126.                                 TextIO.putln("The knight cannot be on an invalid coordinate!");
  127.                                
  128.                        
  129.                        
  130.                         else
  131.                         {
  132.                                 TextIO.putln();
  133.                                 TextIO.putln("                           ==================                          ");
  134.                                 TextIO.putln("                           YOU ARE ON MOVE #" + knight.getMoves() + "                           ");
  135.                                 TextIO.putln("                           ==================                          ");
  136.                                
  137.                                
  138.                                
  139.                                 TextIO.putln("Knight's position: ~" + knight.getKnightPos() + "~");
  140.                                 TextIO.putln("Knight's previous position: ~" + knight.getPreviousPos() + "~");
  141.                                 TextIO.putln();
  142.                         }
  143.                        
  144.                        
  145.                        
  146.                        
  147.                         if (knight.getMoves() == 12) // if player has finished the tour, game is over
  148.                                 gameOver = true;
  149.                                
  150.                        
  151.                        
  152.                 } // end of while loop
  153.                
  154.                
  155.                 timer.stop(); // once tour is over, timer is stopped
  156.                 victoryMessage(boardSizeArray, timer);
  157.                
  158.         } // end of playGame
  159.        
  160.        
  161.        
  162.        
  163.        
  164.  
  165.  
  166.  
  167.         public static void savePath(int[][] boardArray){ // public method to allow the knight class to use it
  168.                 // save file code
  169.                
  170.                 Date dNow = new Date( );
  171.         SimpleDateFormat ft =
  172.         new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); //inserts the date and time of the most recent save file
  173.  
  174.    
  175.      
  176.            TextIO.writeFile("Open Knights Tour.txt"); // name of the save file
  177.          
  178.           TextIO.putln("Open Knights Tour Text File");
  179.           TextIO.putln("***************************************************************************");
  180.           TextIO.putln("");
  181.           TextIO.putln("Your Most Recent End Game Was At: " + ft.format(dNow));
  182.           TextIO.putln("");
  183.          
  184.          
  185.          
  186.           TextIO.putln();
  187.          
  188.           displayBoard(boardArray); // writing the array to file using the displayBoard method            
  189.           TextIO.putln("***************************************************************************");
  190.          
  191.          
  192.           TextIO.writeStandardOutput();
  193.            
  194.      
  195.          
  196.           // end of saving file, displaying messages
  197.          
  198.           TextIO.putln();
  199.           TextIO.putln();
  200.           TextIO.putln("Your progress was saved to a text file. It will be located in the same place you ran the program from.");
  201.           TextIO.putln();
  202.           TextIO.putln("Thank you for playing The Knight's Tour! Press enter to close the program.");
  203.          
  204.          
  205.           char enter = TextIO.getAnyChar(); // gives user time to read messages, then closes
  206.          
  207.           System.exit(0);
  208.          
  209.          
  210.         } // end of savePath
  211.        
  212.        
  213.        
  214.        
  215.        
  216.        
  217.        
  218.        
  219.         private static void displayBoard(int[][] board){ // prints the 2d array to display the positions visited & current position
  220.                 TextIO.putln();
  221.                 TextIO.putln("   A  B  C  D");
  222.                
  223.  
  224.                 for (int i = 0; i < board.length; i++){ // displaying the board as a table - rows
  225.                        
  226.                         TextIO.put((i + 1) + ": ");
  227.                        
  228.                         for (int j = 0; j < board[i].length; j++){ // columns
  229.                                
  230.                                
  231.                                 TextIO.put(board[i][j] + "  ");
  232.                                
  233.                         }// end of INNER for loop
  234.                                                
  235.                         TextIO.putln();
  236.  
  237.                 } // end of OUTER for loop
  238.                 TextIO.putln();
  239.                
  240.         } // end of displayBoard
  241.        
  242.        
  243.      //open file prints a textio version of the save file to the console
  244.         public static void openFile(){
  245.            
  246.            BufferedReader reader = null;
  247.  
  248.            try {
  249.                File file = new File("Open Knights Tour.txt");
  250.                reader = new BufferedReader(new FileReader(file));
  251.  
  252.                String line;
  253.                while ((line = reader.readLine()) != null) {
  254.                   TextIO.putln(line);
  255.                  
  256.                }
  257.  
  258.            } catch (IOException e) {
  259.                e.printStackTrace();
  260.            } finally {
  261.                try {
  262.                    reader.close();
  263.                } catch (IOException e) {
  264.                    e.printStackTrace();
  265.                }
  266.            }
  267.            
  268.            
  269.          
  270.            
  271.            
  272.            
  273.        }//end of open file
  274.        
  275.        
  276.        
  277.        
  278.        
  279.         /*
  280.          *
  281.          * MESSAGES BELOW
  282.          *
  283.          */
  284.        
  285.        
  286.        
  287.        
  288.        
  289.        
  290.        
  291.        
  292.        
  293.        
  294.        
  295.        
  296.        
  297.        
  298.         private static void introMessage(){ // provide instructions about the program
  299.                
  300.                 TextIO.putln("   |******|   ");
  301.                 TextIO.putln("THE KNIGHT'S TOUR");
  302.                 TextIO.putln("   |******|   ");
  303.                 TextIO.putln();
  304.                 TextIO.putln();
  305.                
  306.                 TextIO.putln("By Cameron Dempster & Grant Hair");
  307.                 TextIO.putln();
  308.                
  309.                
  310.                 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.");
  311.                 TextIO.putln("You can choose where your knight will start, either A1 or D3.");
  312.                 TextIO.putln("You must visit every square on the board, without visiting the same square more \nthan once.");
  313.                 TextIO.putln();
  314.                 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.");
  315.                 TextIO.putln("You may also retract 2 moves per game.");
  316.                 TextIO.putln();
  317.                
  318.                 TextIO.putln("=================================================================================");
  319.                
  320.                 TextIO.putln("Press enter to begin or press O to open your most recent save game.");
  321.                         char choice = TextIO.getAnyChar();
  322.                        
  323.                 switch(choice){
  324.                 case 'o' : openFile();
  325.                 break;
  326.                 case 'O' : openFile();
  327.                
  328.                 break;
  329.              
  330.                 }
  331.        
  332.        
  333.        
  334.        
  335.        
  336.        
  337.        
  338.        
  339.         } // end of introMessage
  340.        
  341.        
  342.        
  343.        
  344.        
  345.        
  346.         private static void victoryMessage(int[][] boardArray, StopWatch timer){ // receiving the timer and array to display to user
  347.                
  348.                 TextIO.putln();
  349.                 TextIO.putln();
  350.                
  351.                 TextIO.putln("Congratulations! You have completed the tour, successfully visiting every square!");
  352.                 TextIO.putln("Time taken to complete: " + timer.toString()); // displaying timer as a string
  353.                 TextIO.putln();
  354.                
  355.                 TextIO.putln("Here is the full route you took to complete the game:");
  356.                 displayBoard(boardArray);
  357.                
  358.                
  359.                 TextIO.putln();
  360.                 TextIO.putln("You can now save the path you took to a file, for future viewing.");
  361.                
  362.                 TextIO.putln("Do you want to save the path you took? (Y/N)");
  363.                         char choice = TextIO.getChar();
  364.                        
  365.                 char saveFile = Character.toUpperCase(choice);
  366.                        
  367.                
  368.                
  369.                 if (saveFile == 'Y')
  370.                         savePath(boardArray);
  371.                
  372.                 //else if
  373.                
  374.                
  375.         } // end of victoryMessage
  376.        
  377.        
  378.        
  379.        
  380.        
  381.        
  382.         private static void exitMessage(){
  383.                
  384.                
  385.                
  386.                
  387.         } // end of exitMessage
  388.        
  389.        
  390.        
  391.        
  392.        
  393.        
  394. } // end of Index class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement