Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- public class LatinSquaresGame {
- protected Scanner configScanner;
- protected ArrayList<Character> parsedData = new ArrayList<Character>();
- /**
- *
- * @param configPath
- * - this is the path to the file
- * @throws FileNotFoundException
- * - if the path is invalid
- *
- * Initializes the LatinSquaresGame Scanner
- */
- public LatinSquaresGame(String configPath) throws FileNotFoundException {
- File configFile = new File(configPath);
- configScanner = new Scanner(configFile);
- }
- /**
- *
- * @returns a char double array - the game board
- * @throws Exception
- * - the error specifies
- *
- * Verifies input, then calls getBoard method
- */
- public char[][] play() {
- while (configScanner.hasNext())
- parsedData.add(configScanner.next().toCharArray()[0]);
- return getBoard();
- }
- public boolean isValid() {
- return !(parsedData.size() <= 4 || ((parsedData.size() - 4) % 3 != 0)
- || parsedData.get(6) != getNEntries().get(0) || (parsedData.get(9) != getNEntries().get(1)
- && parsedData.get(6) != getNEntries().get(1) && parsedData.get(9) != getNEntries().get(0))
- || parsedData.get(0) != parsedData.get(3));
- }
- /**
- *
- * @param n
- * -- the n size
- * @param input
- * - ArrayList of characters -- from the parsedData, everything past
- * the 4th index
- * @return a char double array -- the game board
- *
- * Creates the board Prints out during each step
- */
- public char[][] createBoard(int n, ArrayList<Character> input) { // 0 0 a 1 0 b
- char[][] board = new char[n][n];
- for (int u = 0; u < n; u++)
- for (int w = 0; w < n; w++)
- board[u][w] = '-';
- int[] coords = new int[n];
- for (int x = 0; x < input.size(); x++) {
- if (x % 3 == 0) {
- coords[0] = Integer.parseInt(input.get(x).toString());
- } else if (x % 3 == 1) {
- coords[1] = Integer.parseInt(input.get(x).toString());
- } else if (x % 3 == 2) {
- if (coords[0] != coords[1]) {
- board[coords[0]][coords[1]] = input.get(x);
- print(this, board);
- board[coords[1]][coords[0]] = input.get(x);
- print(this, board);
- } else {
- board[coords[0]][coords[1]] = input.get(x);
- print(this, board);
- if (coords[0] == 1) {
- board[0][0] = input.get(x);
- print(this, board);
- } else if (coords[0] == 0) {
- board[1][1] = input.get(x);
- print(this, board);
- }
- }
- coords = new int[n];
- }
- }
- return board;
- }
- /**
- *
- * @param game
- * -- a LatinInputGame
- * @param gameBoard
- * -- the game board Prints the top 0 and 1, and calls printGame
- * method
- */
- public void print(LatinSquaresGame game, char[][] gameBoard) {
- System.out.println(" 0 1");
- printGame(game, gameBoard);
- }
- /**
- *
- * @param game
- * -- a LatinInputGame
- * @param gameBoard
- * -- the game board
- *
- * Prints out the game board
- */
- public void printGame(LatinSquaresGame game, char[][] gameBoard) {
- for (int rows = 0; rows < game.getN(); rows++) {
- System.out.print(rows + " ");
- for (int cols = 0; cols < game.getN(); cols++) {
- System.out.print("[" + gameBoard[rows][cols] + "]");
- }
- System.out.println(" ");
- }
- System.out.println(" ");
- }
- /**
- *
- * @returns the game board -- a char double array
- *
- * Calls createBoard method with n and the data read from the file
- */
- public char[][] getBoard() {
- System.out.println(
- "n = " + this.getN() + " { " + this.getNEntries().get(0) + ", " + this.getNEntries().get(1) + " }");
- System.out.println("k = " + this.getParsedData().get(3));
- return createBoard(this.getN(), this.getActualData());
- }
- /**
- *
- * @returns the parsed data from the file
- */
- public ArrayList<Character> getParsedData() {
- return parsedData;
- }
- /**
- *
- * @returns all the data past the fourth index of the parsed Data
- * @throws ArrayIndexOutOfBoundsException
- * if the parsedData list is too short
- *
- * Returns a new list
- */
- public ArrayList<Character> getActualData() throws ArrayIndexOutOfBoundsException {
- ArrayList<Character> data = new ArrayList<Character>();
- for (int x = 4; x < parsedData.size(); x++) {
- data.add(parsedData.get(x));
- }
- return data;
- }
- /**
- *
- * @returns the 2nd and 3rd entries in the parsedData
- * @throws NullPointerException
- * if the parsedData is null
- */
- public ArrayList<Character> getNEntries() throws NullPointerException {
- ArrayList<Character> n = new ArrayList<Character>();
- n.add(parsedData.get(1));
- n.add(parsedData.get(2));
- return n;
- }
- /**
- *
- * @returns the N from the parsed Data
- */
- public int getN() {
- return Integer.parseInt(parsedData.get(0).toString());
- }
- public void printWelcome() {
- System.out.println(" _ _ _ _____\r\n" + " | | | | (_) / ____|\r\n"
- + " | | __ _| |_ _ _ __ | (___ __ _ _ _ __ _ _ __ ___ ___\r\n"
- + " | | / _` | __| | '_ \\ \\___ \\ / _` | | | |/ _` | '__/ _ \\/ __|\r\n"
- + " | |___| (_| | |_| | | | |____) | (_| | |_| | (_| | | | __/\\__ \\\r\n"
- + " |______\\__,_|\\__|_|_| |_|_____/ \\__, |\\__,_|\\__,_|_| \\___||___/\r\n"
- + " CSCI 1302 | | v2018.fa\r\n" + " |_|");
- }
- public void printWin() {
- System.out.println(
- " .''.\r\n" + " .''. *''* :_\\/_: . \r\n"
- + " :_\\/_: . .:.*_\\/_* : /\\ : .'.:.'.\r\n"
- + " .''.: /\\ : _\\(/_ ':'* /\\ * : '..'. -=:o:=-\r\n"
- + " :_\\/_:'.:::. /)\\*''* .|.* '.\\'/.'_\\(/_'.':'.'\r\n"
- + " : /\\ : ::::: '*_\\/_* | | -= o =- /)\\ ' *\r\n"
- + " '..' ':::' * /\\ * |'| .'/.\\'. '._____\r\n"
- + " * __*..* | | : |. |' .---\"|\r\n"
- + " _* .-' '-. | | .--'| || | _| |\r\n"
- + " .-'| _.| | || '-__ | | | || |\r\n"
- + " |' | |. | || | | | | || |\r\n"
- + " ___| '-' ' \"\" '-' '-.' '` |____\r\n"
- + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n"
- + " CONGRATULATIONS! YOU COMPLETED THE LATIN SQUARE!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement