-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUISource.java
More file actions
90 lines (79 loc) · 2.68 KB
/
GUISource.java
File metadata and controls
90 lines (79 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package game2048;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
/** A type of InputSource that receives commands from a GUI.
* @author P. N. Hilfinger
*/
class GUISource implements InputSource {
/** Provides input from SOURCE, logging it iff LOG. Use RANDOMSOURCE to
* select random tiles having value 2 with probability PROBOF2. */
GUISource(GUI source, Random randomSource, double probOf2,
String logFileName) {
_source = source;
_randomSource = randomSource;
_probOf2 = probOf2;
if (logFileName != null) {
File logFile = new File(logFileName);
try {
_logFileWriter = new FileWriter(logFile);
} catch (IOException e) {
System.err.println("Error: no such file " + logFileName);
System.exit(1);
}
}
}
@Override
/** Return and log which direction arrow was pressed. */
public String getKey() {
String command = _source.readKey();
switch (command) {
case "↑" :
command = "Up";
break;
case "→" :
command = "Right";
break;
case "↓" :
command = "Down";
break;
case "←" :
command = "Left";
break;
default :
break;
}
String logLine = String.format("K %s%n", command);
if (_logFileWriter != null) {
System.out.print(logLine);
try {
_logFileWriter.write(logLine);
} catch (IOException e) {
System.err.print("Error: cannot write to log file");
System.exit(1);
}
}
return command;
}
@Override
/** Return a randomly positioned tile with either value of 2 with
* probability _probOf2 or a value of 4 with probability 1 - _probOf2 in a
* board with size SIZE. */
public Tile getNewTile(int size) {
int c = _randomSource.nextInt(size), r = _randomSource.nextInt(size);
int v = _randomSource.nextDouble() <= _probOf2 ? 2 : 4;
if (_logFileWriter != null) {
System.out.printf("T %d %d %d%n", v, c, r);
}
return Tile.create(v, c, r);
}
/** Input source. */
private GUI _source;
/** Random source for Tiles. */
private Random _randomSource;
/** Probabilty that value of new Tile is 2 rather than 4. */
private double _probOf2;
/** The FileWriter to log inputs to (null if no logging required). */
private FileWriter _logFileWriter;
}