Created
December 6, 2012 02:16
-
-
Save kevinbarbour/4221312 to your computer and use it in GitHub Desktop.
CMSC 256 Project 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Kevin Barbour | |
* December 6, 2012 | |
* Project 6 | |
* | |
* Purpose: Find a path out of a field without running into any hedges or ogres | |
* and collect gold along the way | |
* Input: Filename in args[0], file contains dimensions of a field, contents | |
* of the field and starting coordinates | |
* Output: List of moves taken to find civilization, amount of gold found in | |
* field, and number of minutes taken to find civilization | |
*/ | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.util.Scanner; | |
import java.util.Stack; | |
/* class Tile | |
* Tile class for each coordinate on the map. Contains what is at the tile | |
* and whether or not the tile has been visited | |
*/ | |
class Tile { | |
private char value; | |
private boolean visited; | |
public Tile(char value) { | |
this.value = value; | |
visited = false; | |
} | |
public char getValue() { return value; } | |
public boolean isVisited() { return visited; } | |
public void setVisit(boolean visited) { this.visited = visited; } | |
} | |
/* class Map | |
* Contains a 2D array of tiles, the starting coordinates, and the dimensions | |
* of the field | |
*/ | |
class Map { | |
private int rows, cols, start_row, start_col; | |
private Tile[][] tiles; | |
public Map(int rows, int cols, int start_row, int start_col, Tile[][] tiles) { | |
this.rows = rows; | |
this.cols = cols; | |
this.start_row = start_row; | |
this.start_col = start_col; | |
this.tiles = tiles; | |
} | |
/* public void reset | |
* Method used to reset all tiles to unvisited so that map can be used | |
* again with second move method | |
*/ | |
public void reset() { | |
for(int i = 0; i < rows; i++) | |
for(int j = 0; j < cols; j++) | |
tiles[i][j].setVisit(false); | |
} | |
public Tile tileAt(int row, int col) { return tiles[row - 1][col - 1]; } | |
public int get_rows() { return rows; } | |
public int get_cols() { return cols; } | |
public int get_start_row() { return start_row; } | |
public int get_start_col() { return start_col; } | |
} | |
class Player { | |
private int row, col, gold, minutes, start_row, start_col; | |
private Stack<Character> moves; | |
public Player(int start_row, int start_col) { | |
this.start_row = start_row; | |
this.start_col = start_col; | |
row = start_row; | |
col = start_col; | |
gold = 0; | |
minutes = 0; | |
moves = new Stack<Character>(); | |
} | |
/* public void reset | |
* Method to reset player to start row and column, return gold and minutes | |
* to zero, and make sure the moves stack is empty, so player can be used | |
* again with second move method | |
*/ | |
public void reset() { | |
row = start_row; | |
col = start_col; | |
gold = 0; | |
minutes = 0; | |
moves = new Stack<Character>(); | |
} | |
/* public void print_info | |
* Method to print information about the player | |
* Moves taken, gold found, and minutes taken to find civilization | |
*/ | |
public void print_info() { | |
if(!moves.empty()) { | |
while(true) { | |
System.out.printf("%c", moves.pop()); | |
if(moves.empty()) break; | |
else System.out.printf(","); | |
} | |
} | |
System.out.println(); | |
System.out.printf("%d %d\n", gold, minutes); | |
} | |
public boolean moveNWES(Map map, int row_dir, int col_dir) { | |
int new_row = row + row_dir; | |
int new_col = col + col_dir; | |
// Check if location exists | |
if(new_row < 1 || new_row > map.get_rows()) return false; | |
if(new_col < 1 || new_col > map.get_cols()) return false; | |
Tile cur_tile = map.tileAt(new_row, new_col); | |
char value = cur_tile.getValue(); | |
// Make sure location can be moved to | |
if(cur_tile.isVisited() == true || value == 'O' || value == 'H') return false; | |
if(value == 'C') return true; | |
if(value == 'G') gold++; | |
// Mark the current tile as visited | |
cur_tile.setVisit(true); | |
row = new_row; | |
col = new_col; | |
minutes++; | |
// Move north | |
if(moveNWES(map, -1, 0)) { | |
moves.push('N'); | |
return true; | |
} | |
// Move west | |
if(moveNWES(map, 0, -1)) { | |
moves.push('W'); | |
return true; | |
} | |
// Move east | |
if(moveNWES(map, 0, 1)) { | |
moves.push('E'); | |
return true; | |
} | |
// Move south | |
if(moveNWES(map, 1, 0)) { | |
moves.push('S'); | |
return true; | |
} | |
// Backtracking | |
minutes++; | |
cur_tile.setVisit(false); | |
if(value == 'G') gold--; | |
row -= row_dir; | |
col -= col_dir; | |
return false; | |
} | |
public boolean moveSEWN(Map map, int row_dir, int col_dir) { | |
int new_row = row + row_dir; | |
int new_col = col + col_dir; | |
// Check if new location exists | |
if(new_row < 1 || new_row > map.get_rows()) return false; | |
if(new_col < 1 || new_col > map.get_cols()) return false; | |
Tile cur_tile = map.tileAt(new_row, new_col); | |
char value = cur_tile.getValue(); | |
// Check if new location can be moved to | |
if(cur_tile.isVisited() == true || value == 'O' || value == 'H') return false; | |
if(value == 'C') return true; | |
if(value == 'G') gold++; | |
// Mark the current tile as visited | |
cur_tile.setVisit(true); | |
row = new_row; | |
col = new_col; | |
minutes++; | |
// Move south | |
if(moveSEWN(map, 1, 0)) { | |
moves.push('S'); | |
return true; | |
} | |
// Move east | |
if(moveSEWN(map, 0, 1)) { | |
moves.push('E'); | |
return true; | |
} | |
// Move west | |
if(moveSEWN(map, 0, -1)) { | |
moves.push('W'); | |
return true; | |
} | |
// Move north | |
if(moveSEWN(map, -1, 0)) { | |
moves.push('N'); | |
return true; | |
} | |
// Backtracking | |
minutes++; | |
cur_tile.setVisit(false); | |
if(value == 'G') { gold--; } | |
row -= row_dir; | |
col -= col_dir; | |
return false; | |
} | |
} | |
public class GoldKDB { | |
public static void main(String[] args) throws FileNotFoundException, IOException { | |
Map map = parseFile(args[0]); | |
Player player = new Player(map.get_start_row(), map.get_start_col()); | |
player.moveNWES(map, 0, 0); | |
player.print_info(); | |
map.reset(); | |
player.reset(); | |
player.moveSEWN(map, 0, 0); | |
player.print_info(); | |
} | |
static Map parseFile(String filename) throws FileNotFoundException, IOException { | |
File input = new File(filename); | |
Scanner file_scanner = new Scanner(input); | |
int[] dimensions = {file_scanner.nextInt(), file_scanner.nextInt()}; | |
Tile[][] tiles = new Tile[dimensions[0]][dimensions[1]]; | |
String line; | |
file_scanner.nextLine(); | |
for(int i = 0; i < dimensions[0]; i++) { | |
line = file_scanner.nextLine(); | |
for(int j = 0; j < dimensions[1]; j++) { | |
tiles[i][j] = new Tile(line.charAt(j)); | |
} | |
} | |
return new Map(dimensions[0], dimensions[1], file_scanner.nextInt(), file_scanner.nextInt(), tiles); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment