Commit 3f7ae9f0 authored by mva021's avatar mva021
Browse files

Tested and fixed some bugs.

parent 46995823
package inf101.v20.sem2.GUI; package inf101.v20.sem2.GUI;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import inf101.v20.sem2.games.Game; import inf101.v20.sem2.games.Game;
import inf101.v20.sem2.games.Player;
import inf101.v20.sem2.games.TicTacToe; import inf101.v20.sem2.games.TicTacToe;
......
...@@ -38,9 +38,10 @@ public class GameGUI implements ActionListener{ ...@@ -38,9 +38,10 @@ public class GameGUI implements ActionListener{
private void drawGameBoard() { private void drawGameBoard() {
//add the clickable grid panel //add the clickable grid panel
frame.add("Center", gameBoard); frame.add("Center", gameBoard);
frame.pack(); frame.pack();
frame.setVisible(true); frame.setVisible(true);
//update to reflect the state of the game
gameBoard.updateUI();
} }
/** /**
...@@ -72,6 +73,7 @@ public class GameGUI implements ActionListener{ ...@@ -72,6 +73,7 @@ public class GameGUI implements ActionListener{
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
Iterable<Player> players = getPlayers(); Iterable<Player> players = getPlayers();
frame.remove(gameBoard);
if(e.getSource() == playConnectFourButton) { if(e.getSource() == playConnectFourButton) {
this.gameBoard = new MNKGameGUI(new FourinRow(players)); this.gameBoard = new MNKGameGUI(new FourinRow(players));
} }
...@@ -92,7 +94,7 @@ public class GameGUI implements ActionListener{ ...@@ -92,7 +94,7 @@ public class GameGUI implements ActionListener{
}else { }else {
//make AI //make AI
//TODO: prompt for level of intelligence in AI player //TODO: prompt for level of intelligence in AI player
players.add(new MiniMaxPlayer(Game.piece2, 3)); players.add(new MiniMaxPlayer(Game.piece2, 5));
} }
......
...@@ -7,7 +7,6 @@ import java.util.Arrays; ...@@ -7,7 +7,6 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import inf101.v20.sem2.games.Game; import inf101.v20.sem2.games.Game;
......
...@@ -24,7 +24,7 @@ public class FourinRow extends Game { ...@@ -24,7 +24,7 @@ public class FourinRow extends Game {
@Override @Override
public boolean isWinner(Player player) { public boolean isWinner(Player player) {
return board.countNumInRow(player)==4; return board.countNumInRow(player)>=4;
} }
@Override @Override
......
...@@ -77,6 +77,7 @@ public abstract class Game implements IGame { ...@@ -77,6 +77,7 @@ public abstract class Game implements IGame {
* This method starts the game. * This method starts the game.
* and keeps running until the game is over * and keeps running until the game is over
*/ */
@Override
public void start() { public void start() {
while(!gameOver()) { while(!gameOver()) {
if(currentIsGuiPlayer()) { if(currentIsGuiPlayer()) {
...@@ -94,8 +95,9 @@ public abstract class Game implements IGame { ...@@ -94,8 +95,9 @@ public abstract class Game implements IGame {
protected void stop() { protected void stop() {
//nothing needed to do to stop the game in normal mode. //nothing needed to do to stop the game in normal mode.
//Only in GUI mode this is needed. //Only in GUI mode this is needed.
if(gui != null) if(gui != null) {
gui.updateUI(); gui.updateUI();
}
} }
private boolean currentIsGuiPlayer(){ private boolean currentIsGuiPlayer(){
...@@ -146,6 +148,8 @@ public abstract class Game implements IGame { ...@@ -146,6 +148,8 @@ public abstract class Game implements IGame {
* @return * @return
*/ */
public boolean makeMove(Location loc) { public boolean makeMove(Location loc) {
if(gameOver())
return false;
try { try {
makeMove(loc,players.currentPlayer()); makeMove(loc,players.currentPlayer());
}catch(IllegalArgumentException e) { }catch(IllegalArgumentException e) {
...@@ -222,10 +226,12 @@ public abstract class Game implements IGame { ...@@ -222,10 +226,12 @@ public abstract class Game implements IGame {
return !board.isFull(); return !board.isFull();
} }
@Override
public List<Location> possibleMoves(){ public List<Location> possibleMoves(){
return possibleMoves(players.currentPlayer()); return possibleMoves(players.currentPlayer());
} }
@Override
public List<Location> possibleMoves(Player p){ public List<Location> possibleMoves(Player p){
ArrayList<Location> moves = new ArrayList<Location>(); ArrayList<Location> moves = new ArrayList<Location>();
for(Location loc : board.locations()) { for(Location loc : board.locations()) {
...@@ -258,6 +264,8 @@ public abstract class Game implements IGame { ...@@ -258,6 +264,8 @@ public abstract class Game implements IGame {
public void makeGuiMove(Location loc) { public void makeGuiMove(Location loc) {
if(!currentIsGuiPlayer()) if(!currentIsGuiPlayer())
return; //Only GuiPlayer may call this method return; //Only GuiPlayer may call this method
if(gameOver()) //can not make more moves after there is a winner
return;
makeMove(loc); makeMove(loc);
start(); start();
} }
......
...@@ -84,6 +84,21 @@ class FourinRowTest { ...@@ -84,6 +84,21 @@ class FourinRowTest {
assertFalse(game.isWinner(player1)); assertFalse(game.isWinner(player1));
} }
@Test
void testIsWinnerLongHorisontal() {
game.makeMove(game.drop(1));
game.makeMove(game.drop(1));
game.makeMove(game.drop(2));
game.makeMove(game.drop(2));
game.makeMove(game.drop(4));
game.makeMove(game.drop(4));
game.makeMove(game.drop(5));
game.makeMove(game.drop(5));
game.makeMove(game.drop(3));
assertTrue(game.isWinner(player1));
assertFalse(game.isWinner(player2));
}
@Test @Test
void testDrop() { void testDrop() {
for(Location loc : game.getGameBoard().locations()) { for(Location loc : game.getGameBoard().locations()) {
......
...@@ -22,22 +22,47 @@ public class GameTest { ...@@ -22,22 +22,47 @@ public class GameTest {
} }
@Test @Test
void AICanBeatRandom() { void AICanBeatRandomTicTacToe() {
int aiwins =0; int aiwins =0;
int randomwins=0; int randomwins=0;
for(int i=0; i<100; i++) { for(int i=0; i<100; i++) {
Game game = new TicTacToe(getTwoDifferentAI()); Game game = new TicTacToe(getTwoDifferentAI());
playGame(game); int result = AICanBeatRandom(game);
for(Player player : game.players()) { if(result == 1)
if(game.isWinner(player)) { aiwins++;
if(player instanceof MiniMaxPlayer) if(result == -1)
aiwins++; randomwins++;
if(player instanceof RandomPlayer) }
randomwins++; assertTrue(aiwins>5*randomwins,"random won "+randomwins+", ai won "+aiwins);
} }
@Test
void AICanBeatRandomFourInRow() {
int aiwins =0;
int randomwins=0;
for(int i=0; i<100; i++) {
Game game = new FourinRow(getTwoDifferentAI());
int result = AICanBeatRandom(game);
if(result == 1)
aiwins++;
if(result == -1)
randomwins++;
}
assertTrue(aiwins>5*randomwins,"random won "+randomwins+", ai won "+aiwins);
}
int AICanBeatRandom(Game game) {
playGame(game);
for(Player player : game.players()) {
if(game.isWinner(player)) {
if(player instanceof MiniMaxPlayer)
return 1;
if(player instanceof RandomPlayer)
return -1;
} }
} }
assertTrue(aiwins>10*randomwins,"random won "+randomwins+", ai won "+aiwins); return 0;
} }
private ArrayList<Player> getMiniMaxPlayers() { private ArrayList<Player> getMiniMaxPlayers() {
......
...@@ -83,11 +83,13 @@ class TicTacToeTest { ...@@ -83,11 +83,13 @@ class TicTacToeTest {
@Test @Test
void testCanPlace() { void testCanPlace() {
for(Location loc : tictactoe.board.locations()) { for(Location loc : tictactoe.board.locations()) {
assertTrue(tictactoe.canPlace(loc, player1)); if(!tictactoe.gameOver()) {
assertTrue(tictactoe.canPlace(loc, player2)); assertTrue(tictactoe.canPlace(loc, player1));
tictactoe.makeMove(loc); assertTrue(tictactoe.canPlace(loc, player2));
assertFalse(tictactoe.canPlace(loc, player1)); tictactoe.makeMove(loc);
assertFalse(tictactoe.canPlace(loc, player2)); assertFalse(tictactoe.canPlace(loc, player1));
assertFalse(tictactoe.canPlace(loc, player2));
}
} }
assertFalse(tictactoe.canPlace(new Location(1, 3),player1)); assertFalse(tictactoe.canPlace(new Location(1, 3),player1));
......
Supports Markdown
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