-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPUPlayer.java
More file actions
58 lines (52 loc) · 1.54 KB
/
CPUPlayer.java
File metadata and controls
58 lines (52 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* CPU player class. Implements Player interface.
* Does not find best move. Chooses random move.
*
*
* @author jkwong
*/
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Random;
public class CPUPlayer implements Player{
String name;
char marker;
int playerType;
CPUPlayer(String name, char marker){
this.name = name;
this.marker = marker;
this.playerType = 1;
}
public void SetMarker(char marker){
// sets marker; not user settable because always either x or o
this.marker = marker;
}
public char GetMarker(){
return(marker);
}
public void SetName(){
Scanner userInput = new Scanner(System.in);
this.name = userInput.next();
}
public int GetMove(Board b){
// get all possible moves
boolean [] possibleMovesMask = b.FindMoves();
// possibleMovesMask = b.FindMoves();
ArrayList<Integer> possibleMoves = new ArrayList<Integer>();
// get an ArrayList of possible moves;
for (int i = 0; i< 8; i++){
if (possibleMovesMask[i]){
possibleMoves.add(i);
}
}
// For now the cpu just chooses a random move
Random randGenerator = new Random();
System.out.println(possibleMoves.size());
int index = randGenerator.nextInt(possibleMoves.size());
return(possibleMoves.get(index));
}
}