-
Notifications
You must be signed in to change notification settings - Fork 2
/
RockPaperScissors.java
147 lines (125 loc) · 2.72 KB
/
RockPaperScissors.java
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package shea.ev3.rps;
import lejos.hardware.Brick;
import lejos.hardware.BrickFinder;
import lejos.hardware.Button;
import lejos.hardware.LED;
import lejos.hardware.motor.EV3MediumRegulatedMotor;
import lejos.hardware.port.Port;
import lejos.hardware.sensor.AnalogSensor;
import lejos.hardware.sensor.NXTTouchSensor;
import lejos.robotics.RegulatedMotor;
import lejos.robotics.TouchAdapter;
/**
* @author shea
*
*/
public class RockPaperScissors {
/**
* The EV3 brick
*/
private final Brick ev3 = BrickFinder.getDefault();
/**
* Touch sensor objects
*/
private AnalogSensor[] touchSensors = new AnalogSensor[3];
/**
* Abstracts the touch sensor class and makes it easier to use
*/
private TouchAdapter[] buttons = new TouchAdapter[3];
/**
* Motors
*/
private RegulatedMotor[] lamps = new RegulatedMotor[3];
/**
* LED lights
*/
public final LED led = ev3.getLED();
/**
* Run the program
*/
public RockPaperScissors() {
RPS rps = new RPS(this);
Thread game = new Thread(rps);
game.setDaemon(true);
openSenors();
game.start();
Button.ESCAPE.waitForPressAndRelease();
game.interrupt();
closeSensors();
}
/**
* Wait until a touch sensor button is pressed
* @return the number of the pressed button (0, 1, or 2)
*/
public int waitForButtonPress() {
while (! (buttons[0].isPressed() || buttons[1].isPressed() || buttons[2].isPressed()) ) {}
if (buttons[0].isPressed()) {
return 0;
} else if (buttons[1].isPressed()) {
return 1;
} else {
return 2;
}
}
/**
* Switch on a lamp
* @param lamp the lamp number
*/
public void switchOnLamp(int lamp) {
lamps[lamp].forward();
}
/**
* Switch off a lamp
* @param lamp the lamp number
*/
public void switchOffLamp(int lamp) {
lamps[lamp].flt();
}
/**
* Retrieve the corresponding output port letter
* @param port a number from 0-3
* @return "A", B", "C", or "D"
*/
private static String getPortLetter(int port) {
switch (port) {
case 0:
return "A";
case 1:
return "B";
case 2:
return "C";
case 3:
default:
return "D";
}
}
/**
* Initialize the touch sensors and lamps
*/
private void openSenors() {
Port lampPort, touchPort;
for (int i=0; i < 3; i++) {
lampPort = ev3.getPort(getPortLetter(i));
lamps[i] = new EV3MediumRegulatedMotor(lampPort);
touchPort = ev3.getPort("S" + (i+1));
touchSensors[i] = new NXTTouchSensor(touchPort);
buttons[i] = new TouchAdapter(touchSensors[i]);
}
}
/**
* Close the touch sensors and lamps
*/
private void closeSensors() {
for (int i=0; i < 3; i++) {
lamps[i].close();
touchSensors[i].close();
}
}
/**
* Main method
* @param args
*/
public static void main(String[] args) {
new RockPaperScissors();
}
}