Created
April 27, 2017 14:36
-
-
Save joennlae/5e7d1e4fea6fb37ad04c93cf4f772cb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package u7a4; | |
import java.util.ArrayList; | |
import reversi.*; | |
public class GreedyPlayer implements ReversiPlayer | |
{ | |
/** | |
* Die Farbe des Spielers. | |
*/ | |
private int color = 0; | |
public GreedyPlayer() | |
{ | |
} | |
/** | |
* 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.color = color; | |
if (color == GameBoard.RED) | |
{ | |
System.out.println("MyPlayer ist Spieler RED."); | |
} | |
else if (color == GameBoard.GREEN) | |
{ | |
System.out.println("MyPlayer ist Spieler GREEN."); | |
} | |
} | |
/** | |
* Macht einen Zug für den HumanPlayer, indem der Benutzer zur Eingabe eines | |
* Zuges aufgefordert wird. Diese Methode wird von {@link reversi.Arena} | |
* abwechselnd aufgerufen. | |
* | |
* @see reversi.ReversiPlayer | |
* @return Der Zug des HumanPlayers. | |
*/ | |
public Coordinates nextMove(GameBoard gb) | |
{ | |
Coordinates coord = null; | |
System.out.print("GreedyPlayer "); | |
if (color == GameBoard.RED) | |
{ | |
System.out.print("(RED)"); | |
} | |
else if (color == GameBoard.GREEN) | |
{ | |
System.out.print("(GREEN)"); | |
} | |
if(gb.isMoveAvailable(color)){ | |
ArrayList<Coordinates> possibleMoves = new ArrayList<Coordinates>(); | |
for(int i = 1; i<9; i++){ | |
for(int j= 1; j<9; j++){ | |
if(gb.checkMove(color, new Coordinates(i,j))){ | |
possibleMoves.add(new Coordinates(i,j)); | |
} | |
} | |
} | |
int bestValue = 0; | |
Coordinates bestMove = null; | |
for(int i = 0; i < possibleMoves.size(); i++){ | |
GameBoard gbc = gb.clone(); | |
gbc.checkMove(color, possibleMoves.get(i)); | |
gbc.makeMove(color, possibleMoves.get(i)); | |
if(gbc.countStones(color)>bestValue){ | |
bestValue = gbc.countStones(color); | |
bestMove = possibleMoves.get(i); | |
} | |
} | |
coord = bestMove; | |
} | |
return coord; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment