problem 3 finish

This commit is contained in:
Xevion
2019-09-19 15:47:40 -05:00
parent ff1db0a8ab
commit bd4d504bd3
6 changed files with 102 additions and 57 deletions

View File

@@ -2,15 +2,8 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="250f6713-4389-436f-88a0-d6dffc72af7e" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../../12/bin/CheckerBoard.class" beforeDir="false" afterPath="$PROJECT_DIR$/../../12/bin/CheckerBoard.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../../12/bin/Point.class" beforeDir="false" afterPath="$PROJECT_DIR$/../../12/bin/Point.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../../12/bin/problem12.class" beforeDir="false" afterPath="$PROJECT_DIR$/../../12/bin/problem12.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../../12/src/problem12.java" beforeDir="false" afterPath="$PROJECT_DIR$/../../12/src/problem12.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/input.dat" beforeDir="false" afterPath="$PROJECT_DIR$/input.dat" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/problem3.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/problem3.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../../9/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../../9/.idea/workspace.xml" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/out/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@@ -57,26 +50,26 @@
</usages-collector>
<usages-collector id="statistics.file.extensions.edit">
<counts>
<entry key="dat" value="23" />
<entry key="java" value="2538" />
<entry key="dat" value="105" />
<entry key="java" value="5234" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.types.edit">
<counts>
<entry key="JAVA" value="2538" />
<entry key="PLAIN_TEXT" value="23" />
<entry key="JAVA" value="5234" />
<entry key="PLAIN_TEXT" value="105" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.extensions.open">
<counts>
<entry key="dat" value="7" />
<entry key="dat" value="8" />
<entry key="java" value="2" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.types.open">
<counts>
<entry key="JAVA" value="2" />
<entry key="PLAIN_TEXT" value="7" />
<entry key="PLAIN_TEXT" value="8" />
</counts>
</usages-collector>
</session>
@@ -86,12 +79,16 @@
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/problem3.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="216">
<caret line="68" column="32" selection-start-line="68" selection-start-column="32" selection-end-line="68" selection-end-column="32" />
<state relative-caret-position="289">
<caret line="88" column="21" lean-forward="true" selection-start-line="88" selection-start-column="21" selection-end-line="88" selection-end-column="21" />
<folding>
<element signature="imports" expanded="true" />
<element signature="e#242#243#0" expanded="true" />
<element signature="e#261#262#0" expanded="true" />
<element signature="e#456#457#0" expanded="true" />
<element signature="e#517#518#0" expanded="true" />
<element signature="e#597#598#0" expanded="true" />
<element signature="e#658#659#0" expanded="true" />
<element signature="e#641#648#0" expanded="true" />
<element signature="e#692#699#0" expanded="true" />
</folding>
@@ -111,6 +108,9 @@
<component name="FindInProjectRecents">
<findStrings>
<find>out.println</find>
<find>out.prin</find>
<find>listContains</find>
<find>listContai</find>
</findStrings>
</component>
<component name="Git.Settings">
@@ -128,8 +128,8 @@
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/input.dat" />
<option value="$PROJECT_DIR$/src/problem3.java" />
<option value="$PROJECT_DIR$/input.dat" />
</list>
</option>
</component>
@@ -161,7 +161,7 @@
</panes>
</component>
<component name="PropertiesComponent">
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1568902382723" />
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1568926039217" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="settings.editor.selected.configurable" value="preferences.pluginManager" />
</component>
@@ -204,6 +204,15 @@
</task>
<servers />
</component>
<component name="TodoView">
<todo-panel id="selected-file">
<is-autoscroll-to-source value="true" />
</todo-panel>
<todo-panel id="all">
<are-packages-shown value="true" />
<is-autoscroll-to-source value="true" />
</todo-panel>
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="1936" height="1066" extended-state="6" />
<editor active="true" />
@@ -218,12 +227,12 @@
<window_info anchor="bottom" id="Messages" weight="0.32939914" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.35193133" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.29077253" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.39914164" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Terminal" order="7" />
<window_info anchor="bottom" id="TODO" order="6" weight="0.32939914" />
<window_info anchor="bottom" id="Terminal" order="7" weight="0.32939914" />
<window_info anchor="bottom" id="Event Log" order="8" side_tool="true" />
<window_info anchor="bottom" id="Version Control" order="9" show_stripe_button="false" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
@@ -267,12 +276,16 @@
</entry>
<entry file="file://$PROJECT_DIR$/src/problem3.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="216">
<caret line="68" column="32" selection-start-line="68" selection-start-column="32" selection-end-line="68" selection-end-column="32" />
<state relative-caret-position="289">
<caret line="88" column="21" lean-forward="true" selection-start-line="88" selection-start-column="21" selection-end-line="88" selection-end-column="21" />
<folding>
<element signature="imports" expanded="true" />
<element signature="e#242#243#0" expanded="true" />
<element signature="e#261#262#0" expanded="true" />
<element signature="e#456#457#0" expanded="true" />
<element signature="e#517#518#0" expanded="true" />
<element signature="e#597#598#0" expanded="true" />
<element signature="e#658#659#0" expanded="true" />
<element signature="e#641#648#0" expanded="true" />
<element signature="e#692#699#0" expanded="true" />
</folding>

View File

@@ -14,7 +14,7 @@
F
F |
4
UUUUULL
UULULUL
UUUUUURUULL
DRDRDD
UOOOULOOLUOORD
UULULULUL
UOOOUORUOLOOOODOLDR
ODDRDDROD

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

@@ -1,34 +1,55 @@
import static java.lang.System.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Set;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.HashSet;
import java.util.*;
// Point representing and (X, Y) coordinate
class Point {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
void merge(Point other) {
this.x += other.x;
this.y += other.y;
// Returns a point which is the translated end-point based on the other point (offset).
Point merge(Point other) {
return new Point(this.x + other.x, this.y + other.y);
}
public String toString() {
return String.format("Point(%d, %d)", this.x, this.y);
}
public boolean equals(Point other) {
return this.x == other.x && this.y == other.y;
}
public static boolean collectionContains(Collection<Point> collection, Point point) {
for(Point other : collection) {
if(point.x == other.x && point.y == other.y) {
return true;
}
}
return false;
}
// Class method that searches a List
public static int listContains(List<Point> c, Point p) {
for(Point o : c) {
if(p.x == o.x && p.y == o.y)
return c.indexOf(o);
}
return -1;
}
}
class Maze {
// Offset Constants
List<Point> positionOffsets = Arrays.asList(new Point(0, 1), new Point(1, 0), new Point(0, -1), new Point(-1, 0));
List<String> nameOffsets = Arrays.asList("U R D L".split(" "));
public List<Point> positionOffsets = Arrays.asList(new Point(1, 0), new Point(0, 1), new Point(-1, 0), new Point(0, -1));
private List<String> nameOffsets = Arrays.asList("D R U L".split(" "));
// Maze Data
private List<Point> snake = new ArrayList<Point>();;
List<Point> snake = new ArrayList<Point>();;
private List<Point> pellets = new ArrayList<Point>();
private char[][] rawMatrix;
@@ -38,14 +59,18 @@ class Maze {
for(int x = 0; x < 15; x++) {
for(int y = 0; y < 15; y++) {
switch(rawMatrix[x][y]) {
// Empty Space
case ' ':
break;
// Point Found
case 'X':
this.snake.add(new Point(x, y));
break;
// Pellet Found
case 'F':
this.pellets.add(new Point(x, y));
break;
// Should not be found based on input data. Something has gone wrong/input data is invalid.
default:
out.println("Possibly faulty Maze Input with item \"" + rawMatrix[x][y] + "\" found.");
break;
@@ -56,27 +81,33 @@ class Maze {
// Simulate the snake game using instructions
String simulate(String input) {
char curDirection = 'R';
String curDirection = "R";
int score = 0;
for(int i = 0; i < input.length(); i++) {
// Calculate the offset based on the current instruction
Point offset;
char curOffset = input.charAt(i);
if(curOffset == 'O')
String curOffset = input.substring(i, i + 1);
if(!curDirection.equals(curOffset) && !curOffset.equals("O"))
curDirection = curOffset;
if(curOffset.equals("O"))
offset = positionOffsets.get(nameOffsets.indexOf(curDirection));
else
offset = positionOffsets.get(nameOffsets.indexOf(input.substring(i, i+1)));
out.println(offset);
offset = positionOffsets.get(nameOffsets.indexOf(curOffset));
// Calculate the new point, ensure it's real
Point newPoint = snake.get(snake.size() -1);
newPoint.merge(offset);
Point newPoint = snake.get(snake.size() - 1);
newPoint = newPoint.merge(offset);
// New Point was out of bounds, cannot continue
if(!this.inBounds(newPoint))
return "GAME OVER @ " + newPoint;
return "GAME OVER";
// Add new point, discard end of snake to simulate it's movement
snake.add(newPoint);
snake.remove(0);
// Check if we're at a pellet
score += pellets.contains(newPoint) ? 1 : 0;
snake.add(newPoint);
// Check if we're at a pellet, add score.
int pelletIndex = Point.listContains(pellets, newPoint);
if(pelletIndex != -1) {
score += 1;
this.pellets.remove(pelletIndex);
}
}
return Integer.toString(score);
}
@@ -91,7 +122,9 @@ class Maze {
for(int x = 0; x < 15; x++) {
String[] temp = new String[15];
for(int y = 0; y < 15; y++) {
temp[y] = Character.toString(rawMatrix[x][y]);
Point point = new Point(x, y);
// Priority: Empty Space < Pellet < Snake
temp[y] = Point.collectionContains(this.snake, point) ? "X" : (Point.collectionContains(this.pellets, point) ? "F" : " ");
}
lines[x] = String.join(" - ", temp);
}
@@ -113,8 +146,7 @@ public class problem3 {
rawMatrix[x][y] = line.charAt(y);
}
}
Maze originalMaze = new Maze(rawMatrix);
// Read each of the inputs
int lines = Integer.parseInt(read.nextLine());
String[] inputs = new String[lines];
@@ -123,10 +155,10 @@ public class problem3 {
// Simulate inputs
for(String input : inputs) {
Maze temp = originalMaze;
out.println(temp.simulate(input));
out.println(temp);
}
Maze maze = new Maze(rawMatrix);
String pellets = maze.simulate(input);
// Uncomment to display the final position of the maze
// out.println(maze);
out.println(pellets); }
}
}