Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5.  
  6. public class LatinSquaresGame {
  7.  
  8. protected Scanner configScanner;
  9. protected ArrayList<Character> parsedData = new ArrayList<Character>();
  10.  
  11. /**
  12. *
  13. * @param configPath
  14. * - this is the path to the file
  15. * @throws FileNotFoundException
  16. * - if the path is invalid
  17. *
  18. * Initializes the LatinSquaresGame Scanner
  19. */
  20. public LatinSquaresGame(String configPath) throws FileNotFoundException {
  21. File configFile = new File(configPath);
  22. configScanner = new Scanner(configFile);
  23. }
  24.  
  25. /**
  26. *
  27. * @returns a char double array - the game board
  28. * @throws Exception
  29. * - the error specifies
  30. *
  31. * Verifies input, then calls getBoard method
  32. */
  33. public char[][] play() {
  34. while (configScanner.hasNext())
  35. parsedData.add(configScanner.next().toCharArray()[0]);
  36.  
  37. return getBoard();
  38. }
  39.  
  40. public boolean isValid() {
  41. return !(parsedData.size() <= 4 || ((parsedData.size() - 4) % 3 != 0)
  42. || parsedData.get(6) != getNEntries().get(0) || (parsedData.get(9) != getNEntries().get(1)
  43. && parsedData.get(6) != getNEntries().get(1) && parsedData.get(9) != getNEntries().get(0))
  44. || parsedData.get(0) != parsedData.get(3));
  45. }
  46.  
  47. /**
  48. *
  49. * @param n
  50. * -- the n size
  51. * @param input
  52. * - ArrayList of characters -- from the parsedData, everything past
  53. * the 4th index
  54. * @return a char double array -- the game board
  55. *
  56. * Creates the board Prints out during each step
  57. */
  58. public char[][] createBoard(int n, ArrayList<Character> input) { // 0 0 a 1 0 b
  59. char[][] board = new char[n][n];
  60. for (int u = 0; u < n; u++)
  61. for (int w = 0; w < n; w++)
  62. board[u][w] = '-';
  63. int[] coords = new int[n];
  64. for (int x = 0; x < input.size(); x++) {
  65. if (x % 3 == 0) {
  66. coords[0] = Integer.parseInt(input.get(x).toString());
  67. } else if (x % 3 == 1) {
  68. coords[1] = Integer.parseInt(input.get(x).toString());
  69. } else if (x % 3 == 2) {
  70. if (coords[0] != coords[1]) {
  71. board[coords[0]][coords[1]] = input.get(x);
  72. print(this, board);
  73. board[coords[1]][coords[0]] = input.get(x);
  74. print(this, board);
  75. } else {
  76. board[coords[0]][coords[1]] = input.get(x);
  77. print(this, board);
  78. if (coords[0] == 1) {
  79. board[0][0] = input.get(x);
  80. print(this, board);
  81. } else if (coords[0] == 0) {
  82. board[1][1] = input.get(x);
  83. print(this, board);
  84. }
  85. }
  86. coords = new int[n];
  87. }
  88. }
  89. return board;
  90. }
  91.  
  92. /**
  93. *
  94. * @param game
  95. * -- a LatinInputGame
  96. * @param gameBoard
  97. * -- the game board Prints the top 0 and 1, and calls printGame
  98. * method
  99. */
  100. public void print(LatinSquaresGame game, char[][] gameBoard) {
  101. System.out.println(" 0 1");
  102. printGame(game, gameBoard);
  103. }
  104.  
  105. /**
  106. *
  107. * @param game
  108. * -- a LatinInputGame
  109. * @param gameBoard
  110. * -- the game board
  111. *
  112. * Prints out the game board
  113. */
  114. public void printGame(LatinSquaresGame game, char[][] gameBoard) {
  115. for (int rows = 0; rows < game.getN(); rows++) {
  116. System.out.print(rows + " ");
  117. for (int cols = 0; cols < game.getN(); cols++) {
  118. System.out.print("[" + gameBoard[rows][cols] + "]");
  119. }
  120. System.out.println(" ");
  121. }
  122. System.out.println(" ");
  123. }
  124.  
  125. /**
  126. *
  127. * @returns the game board -- a char double array
  128. *
  129. * Calls createBoard method with n and the data read from the file
  130. */
  131. public char[][] getBoard() {
  132. System.out.println(
  133. "n = " + this.getN() + " { " + this.getNEntries().get(0) + ", " + this.getNEntries().get(1) + " }");
  134. System.out.println("k = " + this.getParsedData().get(3));
  135. return createBoard(this.getN(), this.getActualData());
  136. }
  137.  
  138. /**
  139. *
  140. * @returns the parsed data from the file
  141. */
  142. public ArrayList<Character> getParsedData() {
  143. return parsedData;
  144. }
  145.  
  146. /**
  147. *
  148. * @returns all the data past the fourth index of the parsed Data
  149. * @throws ArrayIndexOutOfBoundsException
  150. * if the parsedData list is too short
  151. *
  152. * Returns a new list
  153. */
  154. public ArrayList<Character> getActualData() throws ArrayIndexOutOfBoundsException {
  155. ArrayList<Character> data = new ArrayList<Character>();
  156. for (int x = 4; x < parsedData.size(); x++) {
  157. data.add(parsedData.get(x));
  158. }
  159. return data;
  160. }
  161.  
  162. /**
  163. *
  164. * @returns the 2nd and 3rd entries in the parsedData
  165. * @throws NullPointerException
  166. * if the parsedData is null
  167. */
  168. public ArrayList<Character> getNEntries() throws NullPointerException {
  169. ArrayList<Character> n = new ArrayList<Character>();
  170. n.add(parsedData.get(1));
  171. n.add(parsedData.get(2));
  172. return n;
  173. }
  174.  
  175. /**
  176. *
  177. * @returns the N from the parsed Data
  178. */
  179. public int getN() {
  180. return Integer.parseInt(parsedData.get(0).toString());
  181. }
  182.  
  183. public void printWelcome() {
  184. System.out.println(" _ _ _ _____\r\n" + " | | | | (_) / ____|\r\n"
  185. + " | | __ _| |_ _ _ __ | (___ __ _ _ _ __ _ _ __ ___ ___\r\n"
  186. + " | | / _` | __| | '_ \\ \\___ \\ / _` | | | |/ _` | '__/ _ \\/ __|\r\n"
  187. + " | |___| (_| | |_| | | | |____) | (_| | |_| | (_| | | | __/\\__ \\\r\n"
  188. + " |______\\__,_|\\__|_|_| |_|_____/ \\__, |\\__,_|\\__,_|_| \\___||___/\r\n"
  189. + " CSCI 1302 | | v2018.fa\r\n" + " |_|");
  190. }
  191.  
  192. public void printWin() {
  193. System.out.println(
  194. " .''.\r\n" + " .''. *''* :_\\/_: . \r\n"
  195. + " :_\\/_: . .:.*_\\/_* : /\\ : .'.:.'.\r\n"
  196. + " .''.: /\\ : _\\(/_ ':'* /\\ * : '..'. -=:o:=-\r\n"
  197. + " :_\\/_:'.:::. /)\\*''* .|.* '.\\'/.'_\\(/_'.':'.'\r\n"
  198. + " : /\\ : ::::: '*_\\/_* | | -= o =- /)\\ ' *\r\n"
  199. + " '..' ':::' * /\\ * |'| .'/.\\'. '._____\r\n"
  200. + " * __*..* | | : |. |' .---\"|\r\n"
  201. + " _* .-' '-. | | .--'| || | _| |\r\n"
  202. + " .-'| _.| | || '-__ | | | || |\r\n"
  203. + " |' | |. | || | | | | || |\r\n"
  204. + " ___| '-' ' \"\" '-' '-.' '` |____\r\n"
  205. + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n"
  206. + " CONGRATULATIONS! YOU COMPLETED THE LATIN SQUARE!");
  207.  
  208. }
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement