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

Tested and fixed some bugs.

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