/* * Main.java * * Created: February 26, 2005 * By: Stan Sclaroff * Description: Main class to help test methods developed for * CS111 Spring 05 Programming Assignment 5 * * */ package program5; public class Main { /** Creates a new instance of Main */ public Main() { } /** Set up TicTacToe interface, and run tests */ public static void main(String[] args) { // Create new instance of TicTacToe game TicTacToe t = new TicTacToe(); System.out.println("------ Testing findWinningMove ---------"); for(int i=0;i<6;++i) testFindWinningMove(i); System.out.println("------ Testing selectMove ---------"); for(int i=0;i<6;++i) testSelectMove(i); System.out.flush(); } // helper method that conducts some tests static private boolean testFindWinningMove(int testCase){ if(testCase > 5 || testCase < 0){ System.out.println("Test case number is outside of valid range"); return false; } // test board for rows int[][] board0 = {{TicTacToe.EMPTY, TicTacToe.HUMAN_PLAYER, TicTacToe.HUMAN_PLAYER}, {TicTacToe.EMPTY, TicTacToe.EMPTY, TicTacToe.EMPTY}, {TicTacToe.COMPUTER_PLAYER, TicTacToe.EMPTY, TicTacToe.COMPUTER_PLAYER}}; // test board for columns int[][] board1 = {{TicTacToe.EMPTY, TicTacToe.HUMAN_PLAYER, TicTacToe.COMPUTER_PLAYER}, {TicTacToe.EMPTY, TicTacToe.HUMAN_PLAYER, TicTacToe.EMPTY}, {TicTacToe.EMPTY, TicTacToe.EMPTY, TicTacToe.COMPUTER_PLAYER}}; // test board for diagonals int[][] board2 = {{TicTacToe.COMPUTER_PLAYER, TicTacToe.HUMAN_PLAYER, TicTacToe.HUMAN_PLAYER}, {TicTacToe.COMPUTER_PLAYER, TicTacToe.EMPTY, TicTacToe.EMPTY}, {TicTacToe.HUMAN_PLAYER, TicTacToe.EMPTY, TicTacToe.COMPUTER_PLAYER}}; int[][][] boards = {board0,board0,board1,board1,board2,board2}; int[] players = {TicTacToe.HUMAN_PLAYER, TicTacToe.COMPUTER_PLAYER, TicTacToe.HUMAN_PLAYER, TicTacToe.COMPUTER_PLAYER, TicTacToe.HUMAN_PLAYER, TicTacToe.COMPUTER_PLAYER}; int[][] mCorrect = {{0,0}, {2,1}, {2,1}, {1,2}, {1,1}, {1,1}}; System.out.println("\nTest case number " + testCase); printBoard(boards[testCase]); if(players[testCase]==TicTacToe.COMPUTER_PLAYER) System.out.println("Player O"); else System.out.println("Player X"); System.out.println("Correct winning move is " + mCorrect[testCase][0] + "," + mCorrect[testCase][1]); int[] m = MyPlayer.findWinningMove(boards[testCase],players[testCase]); System.out.println("Computed move is " + m[0] + "," + m[1]); if(m[0] == mCorrect[testCase][0] && m[1] == mCorrect[testCase][1]) return true; else return false; } static private boolean testSelectMove(int testCase){ if(testCase > 5 || testCase < 0){ System.out.println("Test case number is outside of valid range"); return false; } // test board for rows int[][] board0 = {{TicTacToe.EMPTY, TicTacToe.EMPTY, TicTacToe.HUMAN_PLAYER}, {TicTacToe.EMPTY, TicTacToe.EMPTY, TicTacToe.EMPTY}, {TicTacToe.COMPUTER_PLAYER, TicTacToe.EMPTY, TicTacToe.COMPUTER_PLAYER}}; // test board for columns int[][] board1 = {{TicTacToe.HUMAN_PLAYER, TicTacToe.EMPTY, TicTacToe.COMPUTER_PLAYER}, {TicTacToe.EMPTY, TicTacToe.HUMAN_PLAYER, TicTacToe.COMPUTER_PLAYER}, {TicTacToe.EMPTY, TicTacToe.EMPTY, TicTacToe.EMPTY}}; // test board for diagonals int[][] board2 = {{TicTacToe.COMPUTER_PLAYER, TicTacToe.EMPTY, TicTacToe.HUMAN_PLAYER}, {TicTacToe.COMPUTER_PLAYER, TicTacToe.EMPTY, TicTacToe.EMPTY}, {TicTacToe.EMPTY, TicTacToe.EMPTY, TicTacToe.HUMAN_PLAYER}}; int[][][] boards = {board0,board0,board1,board1,board2,board2}; int[] players = {TicTacToe.HUMAN_PLAYER, TicTacToe.COMPUTER_PLAYER, TicTacToe.HUMAN_PLAYER, TicTacToe.COMPUTER_PLAYER, TicTacToe.HUMAN_PLAYER, TicTacToe.COMPUTER_PLAYER}; int[][] mCorrect = {{2,1}, {2,1}, {2,2}, {2,2}, {1,2}, {2,0}}; System.out.println("\nTest case number " + testCase); printBoard(boards[testCase]); if(players[testCase]==TicTacToe.COMPUTER_PLAYER) System.out.println("Player O"); else System.out.println("Player X"); System.out.println("Correct move is " + mCorrect[testCase][0] + "," + mCorrect[testCase][1]); int[] m = MyPlayer.selectMove(boards[testCase],players[testCase]); System.out.println("Computed move is " + m[0] + "," + m[1]); if(m[0] == mCorrect[testCase][0] && m[1] == mCorrect[testCase][1]) return true; else return false; } static private void printBoard(int[][] board){ System.out.println("Test board:"); for(int i =0;i<3;++i){ for(int j=0;j<3;++j){ if(board[i][j] == TicTacToe.EMPTY) System.out.print("_"); else if(board[i][j]== TicTacToe.COMPUTER_PLAYER) System.out.print("O"); else System.out.print("X"); } System.out.println(""); } } }