-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNimBoard.java
More file actions
47 lines (43 loc) · 929 Bytes
/
NimBoard.java
File metadata and controls
47 lines (43 loc) · 929 Bytes
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
/**
* NimBoard.java
*
* This class handles the rule and board of Nim game.
* Takes in two ints that reperesent a move in the game and return the result of that move.
*
*/
public class NimBoard{
private int[] board;
NimBoard(){
board=new int[3];
board[0]=3;
board[1]=4;
board[2]=5;
}
public int makeChange(int row, int count)//Takes in two ints
//return 0 if the games continues, 1 if the games end, and 2 if there is an error
{
if (row>3 || row==0)
return 2;
if (board[row-1]<count || count==0)
return 2;
board[row-1]-=count;
//checking if the board is empty
count=0;
for(int x=0; x<3;++x)
count+=board[x];
if(count==0)
return 1;
return 0;
}
public String toString()//this method can be used to print the board on the screen
{
String gameBoard="";
for(int x=0;x<3;++x){
for(int c=board[x];c>0;--c){
gameBoard+="O";
}
gameBoard+="\n";
}
return gameBoard;
}
}