Repository navigation
Expand file tree
/
Copy pathRobot.java
More file actions
103 lines (93 loc) · 2.25 KB
/
Copy pathRobot.java
File metadata and controls
103 lines (93 loc) · 2.25 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
91
92
93
94
95
96
97
98
99
100
101
102
103
public class Robot {
private String name;
private int positionX;
private int positionY;
public final static int NORTH = 1, EAST = 2, SOUTH = 3, WEST = 4;
private int facing;
public Robot(String n) {
this.name=n;
this.positionX=0;
this.positionY=0;
this.facing=NORTH;
}
public Robot(String name, int positionX, int positionY, int facing) {
this.name = name;
if(positionX<0){
this.positionX=0;
}
else {
this.positionX = positionX;
}
if(positionY<0){
this.positionY=0;
}
else {
this.positionY = positionY;
}
if(facing<1||facing>4){
this.facing=NORTH;
}
else {
this.facing = facing;
}
}
public String getName() {
return name;
}
public int getPositionX() {
return positionX;
}
public int getPositionY() {
return positionY;
}
public int getFacing() {
return facing;
}
public String getFacingName() {
switch (facing){
case 1:
return "NORTH";
case 2:
return "EAST";
case 3:
return "SOUTH";
case 4:
return "WEST";
}
return null;
}
@Override
public String toString() {
return '"'+name+','+" ("+positionX+','+positionY+"), "+getFacingName()+'"';
}
public void move(){
switch (this.facing){
case 1:
this.positionY++;
break;
case 2:
this.positionX++;
break;
case 3:
if(positionY>=1){
this.positionY--;
}
break;
case 4:
if (positionX>=1){
this.positionX--;
}
break;
}
}
public void turnLeft(){
switch (facing){
case 1:
facing=4;
break;
case 2, 3, 4:
facing--;
break;
}
}
}