-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove.java
More file actions
83 lines (74 loc) · 1.54 KB
/
Move.java
File metadata and controls
83 lines (74 loc) · 1.54 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
import java.util.*;
public class Move
{
private ArrayList<Cell> cells;
private ArrayList<Integer> lettersAdded;
public Move(ArrayList<Cell> cells)
{
lettersAdded = new ArrayList<Integer>();
this.cells = cells;
}
public int getLength()
{
return cells.size();
}
//pre: must be the same length
public boolean fits(String word)
{
for (int i = 0; i < word.length(); i++)
{
if (!cells.get(i).getValue().equals(word.substring(i,i+1)) && !cells.get(i).getValue().equals("-"))
{
return false;
}
}
return true;
}
public void write(String toWrite)
{
for (int i = 0; i < toWrite.length(); i++)
{
if (cells.get(i).getValue().equals("-"))
{
cells.get(i).setValue(toWrite.substring(i,i+1));
lettersAdded.add(i);
}
}
}
//pre: must have written something
public void undo()
{
for (int i = 0; i < lettersAdded.size(); i++)
{
cells.get(lettersAdded.get(i)).setValue("-");
}
}
public String getChars()
{
String s = "";
for (int i = 0; i < cells.size(); i++)
{
if (!cells.get(i).getValue().equals("-"))
s = s + cells.get(i).getValue();
else
s = s + "-";
}
return s;
}
public String getWord()
{
if (!getChars().contains("-"))
return getChars();
else
return "";
}
public boolean filled()
{
for (int i = 0; i < cells.size(); i++)
{
if (cells.get(i).getValue().equals("-"))
return false;
}
return true;
}
}