Skip to content

Instantly share code, notes, and snippets.

@0proto
Created June 17, 2014 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0proto/2e0b6e4b7203e4c616f8 to your computer and use it in GitHub Desktop.
Save 0proto/2e0b6e4b7203e4c616f8 to your computer and use it in GitHub Desktop.
package;
import openfl.display.Sprite;
enum Rotation {
North;
East;
South;
West;
}
typedef Robo = {
var x:Int;
var y:Int;
var r:Rotation;
}
class Main extends Sprite {
var robo:Robo;
var fieldWidth : Int;
var fieldHeight: Int;
public function new () {
super ();
robo = {x:0,y:0,r:North};
//trace(robo);
initGameField(4,4,2,2,North);
turn("RGLGLGGGGGR");
}
function turn(turnString:String) {
var length = turnString.length;
var currentRotation = robo.r;
for (i in 0...length) {
switch (turnString.charAt(i)) {
case "L":
robo.r = turnCounterClockwise();
case "R":
robo.r = turnClockwise();
case "G":
moveRobo();
}
}
printReport();
}
function turnCounterClockwise():Rotation {
var newRotation = Type.enumIndex(robo.r)-1;
if (newRotation<0) {
newRotation=3;
}
return Type.createEnumIndex(Rotation,newRotation);
}
function turnClockwise():Rotation {
var newRotation = Type.enumIndex(robo.r)+1;
if (newRotation>3) {
newRotation=0;
}
return Type.createEnumIndex(Rotation,newRotation);
}
function moveRobo() {
var newX = robo.x;
var newY = robo.y;
switch (robo.r) {
case North:
newY-=1;
case East:
newX+=1;
case South:
newY+=1;
case West:
newX-=1;
}
if (newX<=fieldWidth && newX>=0)
robo.x = newX;
if (newY<=fieldHeight && newY>=0)
robo.y = newY;
}
function printReport() {
trace("Report: "+robo.x+" "+robo.y+" "+robo.r);
}
function initGameField(width,height,initXPos,initYPos,initRotation) {
fieldWidth = width;
fieldHeight = height;
robo = {x:initXPos,y:initYPos,r:initRotation};
trace(robo);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment