-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameBoard.java
More file actions
70 lines (62 loc) · 1.87 KB
/
GameBoard.java
File metadata and controls
70 lines (62 loc) · 1.87 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
import java.io.*;
class GameBoard
{
//instance variables
private int numGuesses;
private String[] board;
private int index;
//keeps track of what the row on the board that your'e changing as the user guesses
//constructor generates a board out of an array of strings
//intializes board and index and adds the first row displaying secret code of computer
//index++
public GameBoard(int n)
{
numGuesses = n;
board = new String[numGuesses+1];
index = 0;
board[index] = ".... Secret Code";
index++;
}
public void addPositions()
{
for(int i = 1; i <= numGuesses; i++)
{
board[i] = "....";
}
}
public void changeRow(Code userCode, Feedback feedback)
{
if(userCode == null || feedback == null)
{
throw new IllegalArgumentException("Violation of precondition:" +
"The user code and feedback cannot be null");
}
String str = "";
str += userCode.toString() + " "; //adds user code to string
str += "Feedback: " + feedback.toString(); //adds feedback to string
board[index] = str; //sets the board row to new string
index++;
}
public int getNumGuesses()
{
return numGuesses;
}
public String toString()
{
String str = "";
for(int i = 0; i <= numGuesses; i++)
{ //iterates through board and adds each row to the string
str += board[i] + "\n";
}
return str;
}
public String winnerToString()
{
String str = "";
for(int i = 1; i < index; i++)
{ //iterates through board until index that the user won in and adds those rows to the string
str += board[i] + "\n";
}
return str;
}
}