Skip to content

Instantly share code, notes, and snippets.

@joennlae
Created May 11, 2017 16:03
Show Gist options
  • Save joennlae/ee4ac239bb890dbac4074dba7a32808e to your computer and use it in GitHub Desktop.
Save joennlae/ee4ac239bb890dbac4074dba7a32808e to your computer and use it in GitHub Desktop.
package u9a2;
import java.util.ArrayList;
import reversi.*;
public class Alpha implements ReversiPlayer
{
/**
* Die Farbe des Spielers.
*/
private int color = 0;
private long timeLimit = 0;
public Alpha()
{
}
/**
* Speichert die Farbe und den Timeout-Wert in Instanzvariablen ab. Diese
* Methode wird vor Beginn des Spiels von {@link Arena} aufgerufen.
*
* @see reversi.ReversiPlayer
*/
public void initialize(int color, long timeout)
{
//this.timeLimit = timeout;
this.timeLimit = timeout;
this.color = color;
if (color == GameBoard.RED)
{
System.out.println("Alpha ist Spieler RED.");
}
else if (color == GameBoard.GREEN)
{
System.out.println("Aplha ist Spieler GREEN.");
}
}
public Coordinates nextMove(GameBoard gb)
{
long timeout = System.currentTimeMillis() + timeLimit - 10;
Coordinates coord = null;
int tiefe = 1;
System.out.print("Alpha");
if (color == GameBoard.RED)
{
System.out.print("(RED)");
}
else if (color == GameBoard.GREEN)
{
System.out.print("(GREEN)");
}
if(gb.isMoveAvailable(color)){ //damit gepasst wird
MoveSave MoveSave = null;
try{
while(true){
MoveSave = max(tiefe, timeout, gb,0,-100,100);
tiefe++;
}
}
catch( Timeout e){
System.out.println("Tiefe " + tiefe + "ging noch");
}
coord = MoveSave.coord;
}
return coord;
}
public ArrayList<Coordinates> getPossibleMoves(GameBoard gb, int player){
ArrayList<Coordinates> possibleMoves = new ArrayList<Coordinates>();
for(int i = 1; i<9; i++){
for(int j= 1; j<9; j++){
if(gb.checkMove(player, new Coordinates(i,j))){
possibleMoves.add(new Coordinates(i,j));
}
}
}
return possibleMoves;
}
private MoveSave max(int maxDepth,long timeout, GameBoard gb, int depth, int alpha, int beta) throws Timeout{
if(System.currentTimeMillis() >= timeout) throw new Timeout();
if(depth== maxDepth) return new MoveSave(null,eval(gb));
ArrayList<Coordinates> possibleMoves = getPossibleMoves(gb, color);
if( possibleMoves.isEmpty()){
if(gb.isMoveAvailable(Utils.other(color))){ //schauen, was andere Spieler zieht nach passen des eigenen Spielers
int result = min(maxDepth,timeout, gb, depth + 1, alpha,beta);
return new MoveSave(null, result);
}
else { //spiel fertig da kein Spieler mehr ziehen kann
return new MoveSave(null, finalRes(gb));
}
}
MoveSave MoveSave = new MoveSave(null, -1); //
for( Coordinates coord : possibleMoves){
GameBoard gbc = gb.clone();
gbc.checkMove(color, coord);
gbc.makeMove(color, coord);
int result = min(maxDepth,timeout,gbc,depth + 1,alpha,beta);
if(result> MoveSave.value){
MoveSave.coord = coord;
MoveSave.value = result;
}
}
return MoveSave;
}
private int maxTwo(int maxDepth,long timeout, GameBoard gb, int depth, int alpha, int beta) throws Timeout{
if(System.currentTimeMillis() >= timeout) throw new Timeout();
if(depth== maxDepth) return eval(gb);
ArrayList<Coordinates> possibleMoves = getPossibleMoves(gb, color);
if( possibleMoves.isEmpty()){
if(gb.isMoveAvailable(Utils.other(color))){ //schauen, was andere Spieler zieht nach passen des eigenen Spielers
return min(maxDepth,timeout, gb, depth + 1, alpha,beta);
}
else { //spiel fertig da kein Spieler mehr ziehen kann
return finalRes(gb);
}
}
for( Coordinates coord : possibleMoves){
GameBoard gbc = gb.clone();
gbc.checkMove(color, coord);
gbc.makeMove(color, coord);
alpha = Math.max(alpha, min(maxDepth,timeout,gbc,depth + 1,alpha,beta));
if(alpha >= beta){
break; //beta schnitt
}
}
return alpha;
}
private int min(int maxDepth,long timeout, GameBoard gb, int depth, int alpha, int beta) throws Timeout {
if(System.currentTimeMillis() >= timeout) throw new Timeout();
if(depth== maxDepth) return eval(gb);
ArrayList<Coordinates> possibleMoves = getPossibleMoves(gb, Utils.other(color));
if( possibleMoves.isEmpty()){
if(gb.isMoveAvailable(color)){ //schauen, was andere Spieler zieht nach passen des eigenen Spielers
return maxTwo(maxDepth, timeout,gb, depth + 1,alpha,beta);
}
else { //spiel fertig da kein Spieler mehr ziehen kann
return finalRes(gb);
}
}
for( Coordinates coord : possibleMoves){
GameBoard gbc = gb.clone();
gbc.checkMove(Utils.other(color), coord);
gbc.makeMove(Utils.other(color), coord);
beta = Math.min(beta,maxTwo(maxDepth,timeout,gbc,depth + 1,alpha,beta));
if(beta <= alpha){
break; // alpha schniit
}
}
return beta;
}
private int eval(GameBoard gb){
return gb.countStones(color);
}
private int finalRes(GameBoard gb){
return gb.countStones(color);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment