Created
December 9, 2016 21:21
-
-
Save ilkinulas/1f2cfc30d0cfe045b4f20976e9bf636a to your computer and use it in GitHub Desktop.
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
package peakrobots.ilkin; | |
import robocode.AdvancedRobot; | |
import java.awt.event.KeyEvent; | |
public class TankTrainer extends AdvancedRobot { | |
enum State { | |
STEADY, AHEAD, LEFT, RIGHT | |
} | |
private State state = State.STEADY; | |
private int numKeyPressed = 0; | |
@Override | |
public void run() { | |
while (true) { | |
switch (state) { | |
case STEADY: | |
setStop(); | |
break; | |
case AHEAD: | |
setAhead(10000); | |
break; | |
case LEFT: | |
setTurnLeft(10000); | |
break; | |
case RIGHT: | |
setTurnRight(10000); | |
break; | |
} | |
execute(); | |
} | |
} | |
@Override | |
public void onKeyPressed(KeyEvent e) { | |
numKeyPressed ++; | |
switch (e.getKeyCode()) { | |
case KeyEvent.VK_LEFT : state = State.LEFT; break; | |
case KeyEvent.VK_RIGHT : state = State.RIGHT; break; | |
case KeyEvent.VK_UP : state = State.AHEAD; break; | |
case KeyEvent.VK_SPACE : setFire(1); break; | |
} | |
} | |
@Override | |
public void onKeyReleased(KeyEvent e) { | |
numKeyPressed --; | |
if (numKeyPressed == 0) { | |
state = State.STEADY; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment