Merge branch 'malte-ai' into 'master'

Malte ai

See merge request !8
This commit is contained in:
Malte Tammena 2017-10-18 13:57:51 +02:00
commit cabe330bd5
3 changed files with 50 additions and 26 deletions

View file

@ -1,26 +0,0 @@
package player;
import java.util.Random;
public class MalteAI implements Player{
private String name;
private Random ran;
public MalteAI(String name){
this.name = name;
this.ran = new Random();
}
public int move(int[][] board){
int choice = ran.nextInt(7);
while (board[choice][0] != 0) {
choice = ran.nextInt(7);
}
return choice;
}
public String getName(){
return this.name;
}
}

View file

@ -0,0 +1,50 @@
package player;
import java.util.Random;
import java.util.Set;
import java.util.HashSet;
import java.util.Arrays;
public class MalteAI implements Player{
private String name;
private Random ran;
public MalteAI(String name){
this.name = name;
this.ran = new Random();
}
public int move(int[][] board){
Set<Integer> options = new HashSet<>(Arrays.asList(0,1,2,3,4,5,6));
for (Integer i: copySet(options)) {
if (board[i][0] != 0) {
options.remove(i);
}
}
// Set<Integer> preventions = getPreventionOptions(options, board);
return takeRandom(options).intValue();
}
private Set<Integer> copySet(Set<Integer> s) {
return new HashSet<Integer>(s);
}
private Integer takeRandom(Set<Integer> s) {
int item = ran.nextInt(s.size());
int i = 0;
for (Object obj: s) {
if (i == item) {
return (Integer) obj;
}
i++;
}
// TODO: Change this
return 0;
}
public String getName(){
return this.name;
}
}