Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6d9c8ca

Browse files
committed
Immutable Classes ,Sets & hashsets
1 parent 3fa32eb commit 6d9c8ca

37 files changed

+947
-0
lines changed

‎CODE/074_Immutable-classes/.idea/misc.xml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎CODE/074_Immutable-classes/.idea/modules.xml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎CODE/074_Immutable-classes/.idea/workspace.xml‎

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
1.35 KB
Binary file not shown.
3.82 KB
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class Location {
5+
private final int locationID;
6+
private final String description;
7+
private final Map<String, Integer> exits;
8+
9+
public Location(int locationID, String description, Map<String, Integer> exits) {
10+
this.locationID = locationID;
11+
this.description = description;
12+
if(exits != null) {
13+
this.exits = new HashMap<String, Integer>(exits);
14+
} else {
15+
this.exits = new HashMap<String, Integer>();
16+
}
17+
this.exits.put("Q", 0);
18+
}
19+
20+
// public void addExit(String direction, int location) {
21+
// exits.put(direction, location);
22+
// }
23+
24+
public int getLocationID() {
25+
return locationID;
26+
}
27+
28+
public String getDescription() {
29+
return description;
30+
}
31+
32+
public Map<String, Integer> getExits() {
33+
return new HashMap<String, Integer>(exits);
34+
}
35+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
import java.util.Scanner;
5+
6+
public class Main {
7+
private static Map<Integer, Location> locations = new HashMap<Integer, Location>();
8+
9+
public static void main(String[] args) {
10+
// Change the program to allow players to type full words, or phrases, then move to the
11+
// correct location based upon their input.
12+
// The player should be able to type commands such as "Go West", "run South", or just "East"
13+
// and the program will move to the appropriate location if there is one. As at present, an
14+
// attempt to move in an invalid direction should print a message and remain in the same place.
15+
//
16+
// Single letter commands (N, W, S, E, Q) should still be available.
17+
18+
Scanner scanner = new Scanner(System.in);
19+
20+
Map<String, Integer> tempExit = new HashMap<String, Integer>();
21+
locations.put(0, new Location(0, "You are sitting in front of a computer learning Java",tempExit));
22+
23+
tempExit = new HashMap<String, Integer>();
24+
tempExit.put("W", 2);
25+
tempExit.put("E", 3);
26+
tempExit.put("S", 4);
27+
tempExit.put("N", 5);
28+
locations.put(1, new Location(1, "You are standing at the end of a road before a small brick building",tempExit));
29+
30+
tempExit = new HashMap<String, Integer>();
31+
tempExit.put("N", 5);
32+
locations.put(2, new Location(2, "You are at the top of a hill",tempExit));
33+
34+
tempExit = new HashMap<String, Integer>();
35+
tempExit.put("W", 1);
36+
locations.put(3, new Location(3, "You are inside a building, a well house for a small spring",tempExit));
37+
38+
tempExit = new HashMap<String, Integer>();
39+
tempExit.put("N", 1);
40+
tempExit.put("W", 2);
41+
locations.put(4, new Location(4, "You are in a valley beside a stream",tempExit));
42+
43+
tempExit = new HashMap<String, Integer>();
44+
tempExit.put("S", 1);
45+
tempExit.put("W", 2);
46+
locations.put(5, new Location(5, "You are in the forest",tempExit));
47+
48+
Map<String, String> vocabulary = new HashMap<String, String>();
49+
vocabulary.put("QUIT", "Q");
50+
vocabulary.put("NORTH", "N");
51+
vocabulary.put("SOUTH", "S");
52+
vocabulary.put("WEST", "W");
53+
vocabulary.put("EAST", "E");
54+
55+
56+
int loc = 1;
57+
while(true) {
58+
System.out.println(locations.get(loc).getDescription());
59+
tempExit.remove("S");
60+
61+
if(loc == 0) {
62+
break;
63+
}
64+
65+
Map<String, Integer> exits = locations.get(loc).getExits();
66+
System.out.print("Available exits are ");
67+
for(String exit: exits.keySet()) {
68+
System.out.print(exit + ", ");
69+
}
70+
System.out.println();
71+
72+
String direction = scanner.nextLine().toUpperCase();
73+
if(direction.length() > 1) {
74+
String[] words = direction.split(" ");
75+
for(String word: words) {
76+
if(vocabulary.containsKey(word)) {
77+
direction = vocabulary.get(word);
78+
break;
79+
}
80+
}
81+
}
82+
83+
if(exits.containsKey(direction)) {
84+
loc = exits.get(direction);
85+
86+
} else {
87+
System.out.println("You cannot go in that direction");
88+
}
89+
}
90+
91+
}
92+
}

‎CODE/075_Sets-&-Hashsets/.idea/misc.xml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎CODE/075_Sets-&-Hashsets/.idea/modules.xml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /