commit f326de9fccdc8e4f6c92e466c62b062ccea92724 Author: Thom Dickson Date: Tue Sep 6 19:48:13 2022 -0400 adsfasdfasdf:wq diff --git a/.syntastic_javac_config b/.syntastic_javac_config new file mode 100644 index 0000000..3538b05 --- /dev/null +++ b/.syntastic_javac_config @@ -0,0 +1 @@ +let g:syntastic_java_javac_classpath = '/home/thom/CS2440/lab02/src:../lib' diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..2f09e5e --- /dev/null +++ b/build.xml @@ -0,0 +1,87 @@ + + + simple example build file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/solution/MazeGame.java b/src/solution/MazeGame.java new file mode 100644 index 0000000..5c3c504 --- /dev/null +++ b/src/solution/MazeGame.java @@ -0,0 +1,380 @@ +package solution; + +import java.util.Scanner; +import java.io.File; +import java.io.FileNotFoundException; + +/** + * A maze game. + * + * @author Thom Dickson + * @version 2021-01-27 + * + */ +public class MazeGame +{ + /** + * The size of each side of the game map. + */ + private final static int HEIGHT = 19; + private final static int WIDTH = 39; + + /** + * The game map, as a 2D array of ints. + */ + private boolean[][] blocked; + + /** + * The map of bread crumbs. + */ + private boolean[][] bc; + + /** + * The current location of the player vertically. + */ + private int userRow; + + /** + * The current location of the player horizontally. + */ + private int userCol; + + /** + * The scanner from which each move is read. + */ + private Scanner moveScanner; + + /** + * The row and column of the goal. + */ + // TODO: add fields here. + private int goalCol; + private int goalRow; + + /** + * The row and column of the start. + */ + // TODO: add fields here. + private int[] start = {0, 0}; + + /** + * Constructor initializes the maze with the data in 'mazeFile'. + * @param mazeFile the input file for the maze + */ + public MazeGame(String mazeFile) + { + this.loadMaze(mazeFile); + this.moveScanner = new Scanner(System.in); + } + + /** + * Constructor initializes the maze with the 'mazeFile' and the move + * scanner with 'moveScanner'. + * @param mazeFile the input file for the maze + * @param moveScanner the scanner object from which to read user moves + */ + public MazeGame(String mazeFile, Scanner moveScanner) + { + // TODO + this.loadMaze(mazeFile); + this.moveScanner = moveScanner; + } + + /** + * getMaze returns a copy of the current maze for testing purposes. + * + * @return the grid + */ + public boolean[][] getMaze() + { + if (blocked == null) + { + return null; + } + boolean[][] copy = new boolean[HEIGHT][WIDTH]; + for (int i = 0; i < HEIGHT; i++) + { + for (int j = 0; j < WIDTH; j++) + { + copy[i][j] = blocked[i][j]; + } + } + return copy; + } + + /** + * setMaze sets the current map for testing purposes. + * + * @param maze + * another maze. + */ + public void setMaze(boolean[][] maze) + { + this.blocked = maze; + } + + /** + * Function loads the data from the maze file and creates the 'blocked' + * 2D array. + * + * @param mazeFile the input maze file. + */ + private void loadMaze(String mazeFile) + { + // Set field width and height + final int WIDTH = 39; + final int HEIGHT = 19; + + // Create empty field to load data into + // TODO Remove magic numbers + this.blocked = new boolean[HEIGHT][WIDTH]; + this.bc = new boolean[HEIGHT][WIDTH]; + this.bc[0][0] = true; + + // Load file + try + { + File inputData = new File(mazeFile); + Scanner dataReader = new Scanner(inputData); + + for (int i = 0; i < this.blocked.length; i++) + { + for (int j = 0; j < this.blocked[i].length; j++) + { + String iData = dataReader.next(); + switch (iData) + { + case "1": this.blocked[i][j] = true; + break; + case "S": this.userRow = i; + this.userRow = j; + this.start[0] = i; + this.start[1] = j; + break; + case "G": this.goalRow = i; + this.goalCol = j; + break; + default: this.blocked[i][j] = false; + } + } + } + } + catch (FileNotFoundException e) + { + System.out.println(e); + } + } + + /** + * Actually plays the game. + */ + public void playGame() + { + String input = ""; + boolean won = false; + do + { + input = this.moveScanner.next(); + this.makeMove(input); + this.printMaze(); + won = this.playerAtGoal(); + } while (!input.equals("quit") && !won); + } + + /** + * Checks to see if the player has won the game. + * @return true if the player has won. + */ + public boolean playerAtGoal() + { + return this.userCol == this.goalCol && this.userRow == this.goalRow; + } + + /** + * Makes a move based on the String. + * + * @param move + * the direction to make a move in. + * @return whether the move was valid. + */ + public boolean makeMove(String move) + { + int[] curLoc = {this.userCol, this.userRow}; + int[] newLoc = curLoc; + switch (move) + { + case "up": newLoc[1] = this.userRow - 1; break; + case "down": newLoc[1] = this.userRow + 1; break; + case "left": newLoc[0] = this.userCol - 1; break; + case "right": newLoc[0] = this.userCol + 1; break; + default: break; + } + + if (newLoc[0] < 0 || + newLoc[0] > this.blocked[0].length - 1 || + newLoc[1] < 0 || + newLoc[1] > this.blocked.length - 1 || + this.blocked[newLoc[1]][newLoc[0]]) + { + return false; + } + else + { + this.userRow = newLoc[1]; + this.userRow = newLoc[1]; + this.userCol = newLoc[0]; + this.userCol = newLoc[0]; + this.bc[newLoc[1]][newLoc[0]] = true; + return true; + } + + } + + /** + * Prints the map of the maze. + */ + public void printMaze() + { + System.out.print("*"); + for (int i = 0; i < this.blocked[0].length; i++) + { + System.out.print("-"); + } + System.out.print("*"); + System.out.println(); + for (int i = 0; i < this.blocked.length; i++) + { + System.out.print("|"); + for (int j = 0; j < this.blocked[i].length; j++) + { + if (this.blocked[i][j]) + { + System.out.print("X"); + } + else if (this.userRow == i && this.userCol == j) + { + System.out.print("@"); + } + else if (this.start[0] == i && this.start[1] == j) + { + System.out.print("S"); + } + else if (this.goalRow == i && this.goalCol == j) + { + System.out.print("G"); + } + else if (this.bc[i][j]) + { + System.out.print("."); + } + else + { + System.out.print(" "); + } + } + System.out.print("|\n"); + } + System.out.print("*"); + for (int i = 0; i < this.blocked[0].length; i++) + { + System.out.print("-"); + } + System.out.print("*\n"); + } + + /** + * Creates a new game, using a command line argument file name, if one is + * provided. + * + * @param args the command line arguments + */ + + public static void main(String[] args) + { + String mapFile = "data/easy.txt"; + Scanner scan = new Scanner(System.in); + MazeGame game = new MazeGame(mapFile, scan); + game.printMaze(); + game.playGame(); + } + + /** + * returns the userCol field + * + * @return userCol + */ + public int getUserCol() + { + return this.userCol; + } + + /** + * returns the userRow field + * + * @return userRow + */ + public int getUserRow() + { + return this.userRow; + } + + /** + * returns the blocked field + * + * @return blocked + */ + public boolean[][] getBlocked() + { + return this.blocked; + } + + /** + * returns the moveScanner field + * + * @return moveScanner + */ + public Scanner getMoveScanner() + { + return this.moveScanner; + } + + /** + * set the userCol field + * + * @param col new userCol field + */ + public void setUserCol(int col) + { + this.userCol = col; + } + + /** + * set the userRow field + * + * @param row new userRow field + */ + public void setUserRow(int row) + { + this.userRow = row; + } + + /** + * set the blocked field + * + * @param blocked new blocked field + */ + public void setBlocked(boolean[][] blocked) + { + this.blocked = blocked; + } + + /** + * set the moveScanner field + * + * @param scn new moveScanner field + */ + public void setMoveScanner(Scanner scn) + { + this.moveScanner = scn; + } +} diff --git a/syntastic_load_lib.sh b/syntastic_load_lib.sh new file mode 100644 index 0000000..3d9a37d --- /dev/null +++ b/syntastic_load_lib.sh @@ -0,0 +1,6 @@ +#!/bin/bash +file=".syntastic_javac_config" +rm $file +echo -n "let g:syntastic_java_javac_classpath = '" >> $file +ant -q path | grep echo | cut -f2- -d] | tr -d '\n' | tr -d ' ' >> $file +echo "'" >> $file diff --git a/tags b/tags new file mode 100644 index 0000000..7ba3065 --- /dev/null +++ b/tags @@ -0,0 +1,103 @@ +!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ +!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ +!_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/ +!_TAG_OUTPUT_FILESEP slash /slash or backslash/ +!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/ +!_TAG_PATTERN_LENGTH_LIMIT 96 /0 for no limit/ +!_TAG_PROC_CWD /home/thom/CS2440/lab02/ // +!_TAG_PROGRAM_AUTHOR Universal Ctags Team // +!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/ +!_TAG_PROGRAM_URL https://ctags.io/ /official site/ +!_TAG_PROGRAM_VERSION 5.9.0 /p5.9.20210110.0/ +EASY_LOSE_CRUMBS src/tests/MazeGameTest.java /^ final static private String EASY_LOSE_CRUMBS = "data\/easy_lose_crumbs.txt";$/;" f class:MazeGameTest file: +EASY_MAP src/tests/MazeGameTest.java /^ final static private String EASY_MAP = "data\/easy.txt";$/;" f class:MazeGameTest file: +EASY_MAP_DATA src/tests/MazeGameTest.java /^ final static private boolean[][] EASY_MAP_DATA = {$/;" f class:MazeGameTest file: +EASY_MAP_OUTPUT src/tests/MazeGameTest.java /^ final static private String EASY_MAP_OUTPUT = "data\/easy_output.txt";$/;" f class:MazeGameTest file: +EASY_WIN_CRUMBS src/tests/MazeGameTest.java /^ final static private String EASY_WIN_CRUMBS = "data\/easy_win_crumbs.txt";$/;" f class:MazeGameTest file: +HEIGHT src/solution/MazeGame.java /^ private final static int HEIGHT = 19;$/;" f class:MazeGame file: +HEIGHT src/tests/MazeGameTest.java /^ final static private int HEIGHT = 19;$/;" f class:MazeGameTest file: +LOSE_INPUT src/tests/MazeGameTest.java /^ private static final String LOSE_INPUT = "down\\ndown\\nright\\nright\\ndown\\n"$/;" f class:MazeGameTest file: +MAP_1 src/tests/MazeGameTest.java /^ final static private String MAP_1 = "data\/hard.txt";$/;" f class:MazeGameTest file: +MAP_1_OUTPUT src/tests/MazeGameTest.java /^ final static private String MAP_1_OUTPUT = "data\/hard_output.txt";$/;" f class:MazeGameTest file: +MOVE_INPUT src/tests/MazeGameTest.java /^ private static final String MOVE_INPUT = "down\\ndown\\nright\\nright\\ndown\\n"$/;" f class:MazeGameTest file: +MazeGame src/solution/MazeGame.java /^ public MazeGame(String mazeFile) $/;" m class:MazeGame +MazeGame src/solution/MazeGame.java /^ public MazeGame(String mazeFile, Scanner moveScanner) $/;" m class:MazeGame +MazeGame src/solution/MazeGame.java /^public class MazeGame $/;" c +MazeGameTest src/tests/MazeGameTest.java /^public class MazeGameTest$/;" c +MyProject build.xml /^$/;" p +WIDTH src/solution/MazeGame.java /^ private final static int WIDTH = 39;$/;" f class:MazeGame file: +WIDTH src/tests/MazeGameTest.java /^ final static private int WIDTH = 39;$/;" f class:MazeGameTest file: +bc src/solution/MazeGame.java /^ private boolean[][] bc;$/;" f class:MazeGame file: +blocked src/solution/MazeGame.java /^ private boolean[][] blocked;$/;" f class:MazeGame file: +breadCrumbHelper src/tests/MazeGameTest.java /^ private void breadCrumbHelper(String moves, String mazeFile, $/;" m class:MazeGameTest file: +build build.xml /^ $/;" P project:MyProject +checkMaze src/tests/MazeGameTest.java /^ private void checkMaze(boolean[][] correct, boolean[][] blocked)$/;" m class:MazeGameTest file: +classpath.base build.xml /^ $/;" i +classpath.test build.xml /^ $/;" i +clean build.xml /^ description="clean up">$/;" t project:MyProject +compareLines src/tests/MazeGameTest.java /^ private void compareLines(String correct, String read)$/;" m class:MazeGameTest file: +compareOutputToSource src/tests/MazeGameTest.java /^ private void compareOutputToSource(Scanner outputScanner, Scanner file)$/;" m class:MazeGameTest file: +compile build.xml /^ description="compile the source">$/;" t project:MyProject +dist build.xml /^ description="generate the distribution">$/;" t project:MyProject +dist build.xml /^ $/;" P project:MyProject +game src/tests/MazeGameTest.java /^ private static MazeGame game;$/;" f class:MazeGameTest file: +getBlocked src/solution/MazeGame.java /^ public boolean[][] getBlocked() {$/;" m class:MazeGame +getMaze src/solution/MazeGame.java /^ public boolean[][] getMaze()$/;" m class:MazeGame +getMoveScanner src/solution/MazeGame.java /^ public Scanner getMoveScanner() {$/;" m class:MazeGame +getUserCol src/solution/MazeGame.java /^ public int getUserCol() {$/;" m class:MazeGame +getUserRow src/solution/MazeGame.java /^ public int getUserRow() {$/;" m class:MazeGame +goalCol src/solution/MazeGame.java /^ private int goalCol;$/;" f class:MazeGame file: +goalRow src/solution/MazeGame.java /^ private int goalRow;$/;" f class:MazeGame file: +init build.xml /^ $/;" t project:MyProject +lib build.xml /^ $/;" P project:MyProject +loadMaze src/solution/MazeGame.java /^ private void loadMaze(String mazeFile) {$/;" m class:MazeGame file: +main src/solution/MazeGame.java /^ public static void main(String[] args) $/;" m class:MazeGame +makeMove src/solution/MazeGame.java /^ public boolean makeMove(String move)$/;" m class:MazeGame +moveScanner src/solution/MazeGame.java /^ private Scanner moveScanner;$/;" f class:MazeGame file: +path build.xml /^ $/;" t project:MyProject +playGame src/solution/MazeGame.java /^ public void playGame()$/;" m class:MazeGame +playerAtGoal src/solution/MazeGame.java /^ public boolean playerAtGoal() {$/;" m class:MazeGame +printMaze src/solution/MazeGame.java /^ public void printMaze()$/;" m class:MazeGame +run build.xml /^ $/;" t project:MyProject +setBlocked src/solution/MazeGame.java /^ public void setBlocked(boolean[][] blocked) {$/;" m class:MazeGame +setMaze src/solution/MazeGame.java /^ public void setMaze(boolean[][] maze)$/;" m class:MazeGame +setMoveScanner src/solution/MazeGame.java /^ public void setMoveScanner(Scanner scn) {$/;" m class:MazeGame +setUp src/tests/MazeGameTest.java /^ public void setUp() throws Exception$/;" m class:MazeGameTest +setUserCol src/solution/MazeGame.java /^ public void setUserCol(int col) {$/;" m class:MazeGame +setUserRow src/solution/MazeGame.java /^ public void setUserRow(int row) {$/;" m class:MazeGame +solution src/solution/MazeGame.java /^package solution;$/;" p +src build.xml /^ $/;" P project:MyProject +test build.xml /^ $/;" t project:MyProject +testBreadCrumbs src/tests/MazeGameTest.java /^ public void testBreadCrumbs()$/;" m class:MazeGameTest +testBreadCrumbs2 src/tests/MazeGameTest.java /^ public void testBreadCrumbs2()$/;" m class:MazeGameTest +testCol src/tests/MazeGameTest.java /^ private void testCol(String method, int col, int correctCol,$/;" m class:MazeGameTest file: +testConstructor src/tests/MazeGameTest.java /^ public void testConstructor() $/;" m class:MazeGameTest +testLosePlayGame src/tests/MazeGameTest.java /^ public void testLosePlayGame()$/;" m class:MazeGameTest +testMakeMoveDown src/tests/MazeGameTest.java /^ public void testMakeMoveDown()$/;" m class:MazeGameTest +testMakeMoveDown2 src/tests/MazeGameTest.java /^ public void testMakeMoveDown2()$/;" m class:MazeGameTest +testMakeMoveDown3 src/tests/MazeGameTest.java /^ public void testMakeMoveDown3()$/;" m class:MazeGameTest +testMakeMoveInvalid src/tests/MazeGameTest.java /^ public void testMakeMoveInvalid()$/;" m class:MazeGameTest +testMakeMoveInvalid2 src/tests/MazeGameTest.java /^ public void testMakeMoveInvalid2() throws FileNotFoundException$/;" m class:MazeGameTest +testMakeMoveLeft src/tests/MazeGameTest.java /^ public void testMakeMoveLeft()$/;" m class:MazeGameTest +testMakeMoveLeft2 src/tests/MazeGameTest.java /^ public void testMakeMoveLeft2()$/;" m class:MazeGameTest +testMakeMoveLeft3 src/tests/MazeGameTest.java /^ public void testMakeMoveLeft3()$/;" m class:MazeGameTest +testMakeMoveRight src/tests/MazeGameTest.java /^ public void testMakeMoveRight()$/;" m class:MazeGameTest +testMakeMoveRight2 src/tests/MazeGameTest.java /^ public void testMakeMoveRight2()$/;" m class:MazeGameTest +testMakeMoveRight3 src/tests/MazeGameTest.java /^ public void testMakeMoveRight3()$/;" m class:MazeGameTest +testMakeMoveUp src/tests/MazeGameTest.java /^ public void testMakeMoveUp()$/;" m class:MazeGameTest +testMakeMoveUp2 src/tests/MazeGameTest.java /^ public void testMakeMoveUp2()$/;" m class:MazeGameTest +testMakeMoveUp3 src/tests/MazeGameTest.java /^ public void testMakeMoveUp3()$/;" m class:MazeGameTest +testOneArgConstructor src/tests/MazeGameTest.java /^ public void testOneArgConstructor() throws FileNotFoundException$/;" m class:MazeGameTest +testPlayerAtGoal src/tests/MazeGameTest.java /^ public void testPlayerAtGoal()$/;" m class:MazeGameTest +testPrintMap src/tests/MazeGameTest.java /^ public void testPrintMap() throws FileNotFoundException$/;" m class:MazeGameTest +testPrintMap2 src/tests/MazeGameTest.java /^ public void testPrintMap2() throws FileNotFoundException$/;" m class:MazeGameTest +testRow src/tests/MazeGameTest.java /^ private void testRow(String method, int row, int correctRow, int newRow)$/;" m class:MazeGameTest file: +testSetGetInput src/tests/MazeGameTest.java /^ public void testSetGetInput() $/;" m class:MazeGameTest +testSetGetMap src/tests/MazeGameTest.java /^ public void testSetGetMap() throws FileNotFoundException$/;" m class:MazeGameTest +testSetGetUserCol src/tests/MazeGameTest.java /^ public void testSetGetUserCol() throws FileNotFoundException$/;" m class:MazeGameTest +testSetGetUserRow src/tests/MazeGameTest.java /^ public void testSetGetUserRow() throws FileNotFoundException$/;" m class:MazeGameTest +testWinPlayGame src/tests/MazeGameTest.java /^ public void testWinPlayGame() $/;" m class:MazeGameTest +testdir build.xml /^ $/;" P project:MyProject +tests src/tests/MazeGameTest.java /^package tests;$/;" p +userCol src/solution/MazeGame.java /^ private int userCol;$/;" f class:MazeGame file: +userRow src/solution/MazeGame.java /^ private int userRow;$/;" f class:MazeGame file: