-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserMove.java~
More file actions
135 lines (121 loc) · 3.72 KB
/
UserMove.java~
File metadata and controls
135 lines (121 loc) · 3.72 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
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
/**
* Private inner class that represents a move made by the user. This class
* will be used to check if a move is valid, to move the piece on the
* GUI grid, and also to store information to be used by the undo function.
*/
private class UserMove {
//private instance vars
private int direction;
private Color repColor;
private String repText;
private Button oldButton, newButton;
private int oldX, oldY, newX, newY;
/**
* Constructor that creates a UserMove by storing the direction given.
*
* @param d the direction of the user's mvoe
*/
public UserMove(int d) {
direction = d;
curX = x; curY = y;
newX = x; newY = y;
//determine new position
if (direction == LEFT) newY--;
else if (direction == RIGHT) newY++;
else if (direction == UP) newX--;
else if (direction == DOWN) newX++;
//save old and new position
oldButton = buttonGrid[oldX][oldY];
newButton = buttonGrid[newX][newY];
//save anything that was replaced by moving this piece
repColor = newButton.getBackground();
repNumber = newButton.getText();
}
/**
* Actually moves the player's piece on the screen.
*/
public void move() {
if (!isValidMove())
return; //don't do anything if the move is invalid
//leave path (color + number)
if (isTarget())
oldButton.setBackground(ColorScheme.target());
else
oldButton.setBackground(ColorScheme.path());
oldButton.setText("<html><font color='white'>" +
(counter++) + "</font></html>");
//move piece (color, push onto undo, update labels)
newButton.setBackground(ColorScheme.character());
newButton.setText("");
undoStack.push(this);
lPanel.updateStepsLabel();
lPanel.updateNextLabel();
}
/**
* Returns the color that was replaced by this move.
*
* @return the color that was replaced by this move.
*/
public Color getReplacedColor() {
return repColor;
}
/**
* Returns the text that was replaced by this move.
*
* @return the text that was replaced by this move.
*/
public String getReplacedText() {
return repText;
}
/**
* Private helper method that returns true if the position of this move
* contains a target.
*
* @return a boolean indicating whether the position contains a target
*/
private boolean isTarget() {
ArrayList<GridCoordinate> targets = gridGraph.getTargets();
for (GridCoordinate target: targets) {
if (target.getX()==newX && target.getY()==newY)
return true;
}
return false;
}
/**
* Private helper method that returns true if the move attempted is valid.
*
* @return a boolean that indicates whether this move is allowed
*/
private boolean isValidMove() {
if (counter >= limit)
return false;
return isValidPosition(newX, newY);
}
/**
* Private helper method that returns true if the position of this move
* is valid on grid.
*
* @return a boolean indicating whether the given position exists in the grid
*/
private boolean isValidPosition() {
boolean validX = newX>=0 && newX<height;
boolean validY = newY>=0 && newY<width;
if (validX && validY)
return !(newButton.getBackground().equals(ColorScheme.block()));
return false;
}
/**
* Undoes this move.
*/
public void undo() {
//move character back to "old" button (color, lower counter, update labels)
oldButton.setBackground(ColorScheme.character());
oldButton.setText("");
counter--;
lPanel.updateStepsLabel();
lPanel.updateNextLabel();
//"uncover" what this move replaced (restore replaced color + text)
newButton.setBackground(repColor);
newButton.setText(repText);
}
}