Commit ac674ab4 authored by mva021's avatar mva021
Browse files

Fixed bug in AI player

parent c0d437fc
......@@ -12,12 +12,8 @@ import inf101.v20.sem2.games.TicTacToe;
public class GUIMain {
public static void main(String[] args) {
ArrayList<Player> players = new ArrayList<Player>();
players.add(new GuiPlayer(TicTacToe.piece1));
players.add(new GuiPlayer(TicTacToe.piece2));
Game game = new TicTacToe(players);
Game game = new TicTacToe(GameGUI.getPlayers());
MNKGameGUI gameGUI = new MNKGameGUI(game);
game.setGui(gameGUI);
GameGUI gui = new GameGUI(gameGUI);
}
}
\ No newline at end of file
......@@ -11,6 +11,8 @@ import javax.swing.JOptionPane;
import javax.swing.JPanel;
import inf101.v20.sem2.games.FourinRow;
import inf101.v20.sem2.games.Game;
import inf101.v20.sem2.games.MiniMaxPlayer;
import inf101.v20.sem2.games.Player;
import inf101.v20.sem2.games.TicTacToe;
......@@ -71,24 +73,26 @@ public class GameGUI implements ActionListener{
public void actionPerformed(ActionEvent e) {
Iterable<Player> players = getPlayers();
if(e.getSource() == playConnectFourButton) {
gameBoard.reset(new FourinRow(players));
this.gameBoard = new MNKGameGUI(new FourinRow(players));
}
if(e.getSource() == playTicTacToeButton) {
gameBoard.reset(new TicTacToe(players));
this.gameBoard = new MNKGameGUI(new TicTacToe(players));
}
drawGameBoard();
}
private Iterable<Player> getPlayers() {
public static Iterable<Player> getPlayers() {
ArrayList<Player> players = new ArrayList<Player>();
//add player1
players.add(new GuiPlayer(TicTacToe.piece1));
players.add(new GuiPlayer(Game.piece1));
//add player2
if(promptMultiplayer()) {
//make AI
players.add(new GuiPlayer(Game.piece2));
}else {
players.add(new GuiPlayer(TicTacToe.piece2));
//make AI
//TODO: prompt for level of intelligence in AI player
players.add(new MiniMaxPlayer(Game.piece2, 3));
}
......@@ -99,7 +103,7 @@ public class GameGUI implements ActionListener{
* Helper method that prompts for multiplayer or not
* @return true if multiplayer is selected, false otherwise
*/
private boolean promptMultiplayer() {
private static boolean promptMultiplayer() {
Object[] possibilities = {"Multiplayer", "Single Player (against AI)"};
String s = (String)JOptionPane.showInputDialog(
null,
......@@ -115,7 +119,6 @@ public class GameGUI implements ActionListener{
if ((s != null) && (s.length() > 0)) {
System.out.println("Received " + s);
}
return s.charAt(0)=='M';
return s.equals(possibilities[0]);
}
}
......@@ -27,6 +27,7 @@ public class MNKGameGUI extends JPanel{
public MNKGameGUI(Game game) {
this.game = game;
game.setGui(this);
statusMessage = new JLabel();
statusMessage.setText("Welcome to this game! " + game.getCurrentPlayer().getName() + " begins.");
clickablePanels = new ClickableGrid(game, MNKGameGUI.getColors());
......@@ -46,7 +47,7 @@ public class MNKGameGUI extends JPanel{
public void reset(Game newGame) {
this.game = newGame;
this.game.setGui(this);
//updateMessage();
removeAll();
initialize();
......
......@@ -18,6 +18,9 @@ public abstract class Game implements IGame {
GameBoard board;
PlayerList players;
MNKGameGUI gui = null;
public static final String piece1 = "X";
public static final String piece2 = "O";
//********** CONSTRUCTORS **********//
/**
......
......@@ -32,7 +32,7 @@ public class MiniMaxPlayer extends AbstractPlayer {
Game newGame = game.copy();
newGame.makeMove(loc); //note that this changes the current player in the copy but not the real game
int score = 0;
if(newGame.isDraw() || depth==1) { //No more moves can be made
if(newGame.gameOver() || depth==1) { //No more moves can be made
score = score(newGame,game.getCurrentPlayer());
}else {
//call recursively such that the opponent makes the move that is best for him
......@@ -48,6 +48,8 @@ public class MiniMaxPlayer extends AbstractPlayer {
best = new Strategy(loc, score);
}
}
if(best == null)
System.err.println("This should not happen!");
return best;
}
......
......@@ -48,7 +48,7 @@ public interface Player {
* @return true if the name is valid, false otherwise.
*/
public static boolean isValidName(String name) {
return !name.isBlank();
return name !=null && !name.isBlank();
}
}
package inf101.v20.sem2.games;
public class TicTacToe extends Game {
public static final String piece1 = "X";
public static final String piece2 = "O";
/**
* Constructor for normal 3x3 TicTacToe
*/
......
......@@ -3,6 +3,7 @@ package inf101.v20.sem2.games;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Collections;
import org.junit.jupiter.api.Test;
......@@ -15,11 +16,30 @@ public class GameTest {
}
@Test
void TwoRandomPlay() {
void TwoRandomCanPlay() {
playGame(new TicTacToe(getRandomPlayers()));
playGame(new FourinRow(getRandomPlayers()));
}
@Test
void AICanBeatRandom() {
int aiwins =0;
int randomwins=0;
for(int i=0; i<100; i++) {
Game game = new TicTacToe(getTwoDifferentAI());
playGame(game);
for(Player player : game.players()) {
if(game.isWinner(player)) {
if(player instanceof MiniMaxPlayer)
aiwins++;
if(player instanceof RandomPlayer)
randomwins++;
}
}
}
assertTrue(aiwins>10*randomwins,"random won "+randomwins+", ai won "+aiwins);
}
private ArrayList<Player> getMiniMaxPlayers() {
ArrayList<Player> players = new ArrayList<Player>();
players.add(new MiniMaxPlayer("X", 3));
......@@ -34,6 +54,14 @@ public class GameTest {
return players;
}
private ArrayList<Player> getTwoDifferentAI() {
ArrayList<Player> players = new ArrayList<Player>();
players.add(new MiniMaxPlayer("X", 3));
players.add(new RandomPlayer("O"));
Collections.shuffle(players);
return players;
}
void playGame(Game game) {
game.start();
assertTrue(game.gameOver());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment