Commit ddadbd89 authored by Matt Tower's avatar Matt Tower

AI Testing, complete

parent e60d8e20
package model.player;
import static org.junit.Assert.assertTrue;
import model.Board;
import model.Game;
import model.Mark;
import org.junit.Test;
/**
*
* @author mjt023
*
* This is the AIPlayerTest that tests the AIPlayer class.
*/
public class AIPlayerTest {
/** a game instance */
private Game theGame;
/**
* tests the decideMove() method
*/
@Test
public void decideMoveTest() {
int[] statsForX = new int[3];
theGame = new Game(3);
theGame.setPlayerX(new AIPlayer(theGame.getBoard(), Mark.X));
theGame.setPlayerO(new RandomPlayer(theGame.getBoard(), Mark.O));
// array for wins, draws, losses
int[] stats = new int[3];
for (int i = 0; i < 10000; i++) {
while (theGame.isPlayable()) {
if (theGame.getCurPlayer().getMark() == Mark.X) {
((AIPlayer) theGame.getCurPlayer()).decideMove();
} else {
((RandomPlayer) theGame.getCurPlayer()).decideMove();
}
if (theGame.isWinFor(theGame.getCurPlayer())
&& theGame.getCurPlayer().getMark() == Mark.X) {
statsForX[0] += 1;
} else if (theGame.isWinFor(theGame.getCurPlayer())
&& !(theGame.getCurPlayer().getMark() == Mark.X)) {
statsForX[2] += 1;
} else if (theGame.getState() == Board.State.DRAW) {
statsForX[1] += 1;
} else {
theGame.switchTurn();
}
}
theGame.newGame();
}
assertTrue(statsForX[0] > 6000);
}
}
......@@ -50,10 +50,27 @@ public class BlockerPlayerTest {
theGame.getBoard().clear();
// test for diagonal win
theGame.getBoard().setMark(2, 0, Mark.X);
theGame.getBoard().setMark(0, 2, Mark.X);
blockerPlayer.decideMove();
assertTrue(theGame.getBoard().getMark(1, 1) == Mark.O);
theGame.getBoard().clear();
// test for vertical win
theGame.getBoard().setMark(0, 0, Mark.X);
theGame.getBoard().setMark(1, 0, Mark.X);
theGame.getBoard().setMark(2, 0, Mark.X);
blockerPlayer.decideMove();
assertTrue(theGame.getBoard().getMark(1, 0) == Mark.O);
assertTrue(theGame.getBoard().getMark(0, 0) == Mark.O);
// middle priority
theGame.getBoard().clear();
blockerPlayer.decideMove();
assertTrue(theGame.getBoard().getMark(1, 1) == Mark.O);
theGame.getBoard().clear();
// corner priority
theGame.getBoard().setMark(1, 1, Mark.X);
blockerPlayer.decideMove();
assertTrue(theGame.getBoard().getMark(0, 0) == Mark.O);
}
}
/**
* CSCI 205 - Software Design and Engineering
* Name(s): Matt Tower
*
* Work: hw02
* Created: Nov 11, 2014, 8:18:37 PM
*/
package model.player;
import static org.junit.Assert.assertTrue;
import java.util.Hashtable;
import model.Board;
import model.Game;
import model.Mark;
import org.junit.Test;
/**
* @author mjt023
*
* This is the RandomPlayerTest class that tests the RandomPlayer class.
*/
public class RandomPlayerTest {
/** a game instance */
private Game theGame;
/** a board instance */
private Board theBoard;
/** a RandomPlayer instance */
private RandomPlayer randPlayer;
/**
* tests the decideMove() method
*/
@Test
public void decideMoveTest() {
int[] statsForX = new int[3];
theGame = new Game(3);
theBoard = theGame.getBoard();
theGame.setPlayerX(new RandomPlayer(theBoard, Mark.X));
theGame.setPlayerO(new RandomPlayer(theBoard, Mark.O));
for (int trial = 0; trial < 500; trial++) {
while (theGame.isPlayable()) {
((RandomPlayer) theGame.getCurPlayer()).decideMove();
if (theGame.isWinFor(theGame.getCurPlayer())
&& theGame.getCurPlayer().getMark() == Mark.X) {
statsForX[0] += 1;
} else if (theGame.isWinFor(theGame.getCurPlayer())
&& !(theGame.getCurPlayer().getMark() == Mark.X)) {
statsForX[2] += 1;
} else if (theGame.getState() == Board.State.DRAW) {
statsForX[1] += 1;
} else {
theGame.switchTurn();
}
}
theGame.getBoard().clear();
}
assertTrue(statsForX[0] < 250 && statsForX[0] > 190);
}
/**
* tests randomness of RandomPlayer
*/
@Test
public void RandomnessTest() {
theGame = new Game(3);
theBoard = theGame.getBoard();
randPlayer = new RandomPlayer(theGame.getBoard(), Mark.O);
Hashtable<String, Integer> freeCoordsCounts = new Hashtable<String, Integer>();
String s = "";
String key;
int value;
String[] spaces = new String[9];
spaces[0] = "00";
spaces[1] = "01";
spaces[2] = "02";
spaces[3] = "10";
spaces[4] = "11";
spaces[5] = "12";
spaces[6] = "20";
spaces[7] = "21";
spaces[8] = "22";
for (int trials = 0; trials < 10; trials++) {
freeCoordsCounts.put("00", 0);
freeCoordsCounts.put("01", 0);
freeCoordsCounts.put("02", 0);
freeCoordsCounts.put("10", 0);
freeCoordsCounts.put("11", 0);
freeCoordsCounts.put("12", 0);
freeCoordsCounts.put("20", 0);
freeCoordsCounts.put("21", 0);
freeCoordsCounts.put("22", 0);
randPlayer.decideMove();
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (theGame.getBoard().getMark(row, col) == Mark.O) {
key = Integer.toString(row) + Integer.toString(col);
value = freeCoordsCounts.get(Integer.toString(row)
+ Integer.toString(col));
freeCoordsCounts.put(key, value + 1);
}
}
}
theGame.getBoard().clear();
int spaceCount;
for (String space : spaces) {
// space counter for each space on the board
spaceCount = freeCoordsCounts.get(space);
assertTrue((spaceCount >= 80) || (spaceCount <= 120));
}
}
}
}
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