-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameBoardArray.java
130 lines (105 loc) · 2.2 KB
/
GameBoardArray.java
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
package hangman;
/**
* The Array implementation of the GameBoard interface.
*/
public class GameBoardArray implements GameBoard, HangManConstants {
/** The number of characters (lower/upper). */
private static int ALPHABET_COUNT = 26*2;
private int numGuess=0;
/** hung state */
private int state;
private char[] prevGuess = new char[ALPHABET_COUNT];
private char[] guessWord;
private char[] gameBoard;
/**
* Creates a new GameBoardArray object.
*
* guessWord the word to guess
*/
public GameBoardArray(String guessWord) {
this.guessWord = guessWord.toCharArray();
gameBoard = new char[this.guessWord.length];
for(char c: gameBoard)
c = 0;
for(char c: prevGuess)
c = 0;
state = STARTING_STATE;
}
public boolean isPriorGuess(char guess) {
for(char c: prevGuess)
if(c == guess){
return true;
}
return false;
}
public int numberOfGuesses() {
return numGuess;
}
public boolean isCorrectGuess(char guess) {
numGuess++;
for(char c: guessWord)
if(c == guess)
return true;
state++;
return false;
}
public boolean doMove(char guess) {
if(!isPriorGuess(guess))
{
prevGuess[numberOfGuesses()] = guess;
if(isCorrectGuess(guess))
{
for(int i=0; i<guessWord.length;i++)
{
if(guessWord[i] == guess)
gameBoard[i] = guess;
}
return true;
}
}
return false;
}
public boolean inWinningState() {
for(char c: gameBoard)
if(c == 0)
return false;
return true;
}
public boolean inLosingState() {
for(char c: gameBoard)
if(c==0 && currentHungState()==10)
return true;
return false;
}
public String toString() {
String s = "";
for(int i=0; i<gameBoard.length; i++)
{
if(gameBoard[i]!=0)
s+= gameBoard[i];
else if(gameBoard[i]==0)
s+="_";
if(i < gameBoard.length-1)
s+= " ";
}
return s;
}
public String previousGuessString() {
String s = "";
s+= "[";
for(int i=0; i<prevGuess.length;i++)
if(prevGuess[i] != 0)
{
if(prevGuess[i+1] == 0)
s += prevGuess[i] +"]";
else
s += prevGuess[i]+ ", ";
}
return s;
}
public int currentHungState() {
if(state <= 10)
return state;
return 10;
}
}