-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputer.java
More file actions
46 lines (38 loc) · 993 Bytes
/
Copy pathComputer.java
File metadata and controls
46 lines (38 loc) · 993 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
/*****************************************
* A template for a computer Nim player
****************************************/
import java.util.Random;
public class Computer{
private int mode;
private int choice;
private Random rn;
public Computer(int m)
{
mode = m;
choice = -1;
rn = new Random();
}
public void move(int marblesLeft){
if(marblesLeft==1)
choice = 1;
else if(mode == 1)
{
choice = rn.nextInt(marblesLeft/2)+1;
}
else
{
int n=1,power=0;
while(n<=marblesLeft)
{
n=2*n;
}
if(marblesLeft+1==n)choice = rn.nextInt(marblesLeft/2)+1;
else choice=marblesLeft-n/2+1;
}
System.out.println("Computer Choice ::" + choice +"\n");
// your code here
}
public int getChoice(){
return choice;
}
}