-
Notifications
You must be signed in to change notification settings - Fork 1
/
Greedy.java
75 lines (70 loc) · 2.84 KB
/
Greedy.java
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
71
72
73
74
75
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mancala;
import java.io.*;
/**
*
* @author Neelam
*/
//Get best move based on Greedy technique
public class Greedy {
Utility util=new Utility();
public void greedy(Game ga){
Game move=getAllMoves(ga);
try{
util.writeNextState(move);
}
catch(Exception ex){
System.out.println("Exception in Greedy : "+ex.getMessage());
}
}
//Get all next valid moves and select the best one based on highest value of eval
public Game getAllMoves(Game ga){
double maxEval=Double.NEGATIVE_INFINITY;
Game move=new Game(ga);
int pit=-1;
for(int i=0;i<ga.getBoardSize();i++){
Game temp=new Game(ga);
if(!util.GameOver(temp)){
if(temp.getPlayer()==1){
int[] localBoard1=new int[temp.getBoardSize()];
System.arraycopy(temp.getBoardPlayer1(), 0, localBoard1, 0, temp.getBoardSize());
if(localBoard1[i]>0){
temp=new Game(util.moveStones(temp, temp.getPlayer(), i));
if(temp.getGetAnotherTurn()){
temp=new Game(getAllMoves(temp));
}
if(maxEval<util.eval(temp.getPlayer(), temp.getMancalaPlayer1(),temp.getMancalaPlayer2())){
maxEval=util.eval(temp.getPlayer(), temp.getMancalaPlayer1(),temp.getMancalaPlayer2());
pit=i;
move=new Game(temp);
if(util.GameOver(move))
break;
}
}
}
else{
int[] localBoard2=new int[temp.getBoardSize()];
System.arraycopy(temp.getBoardPlayer2(), 0, localBoard2, 0, temp.getBoardSize());
if(localBoard2[i]>0){
temp=new Game(util.moveStones(temp, temp.getPlayer(), i));
if(temp.getGetAnotherTurn()){
temp=new Game(getAllMoves(temp));
}
if(maxEval<util.eval(temp.getPlayer(), temp.getMancalaPlayer1(),temp.getMancalaPlayer2())){
maxEval=util.eval(temp.getPlayer(), temp.getMancalaPlayer1(),temp.getMancalaPlayer2());
pit=i;
move=new Game(temp);
if(util.GameOver(move))
break;
}
}
}
}
}
return move;
}
}