|
| 1 | +// TTTMinimaxTests.java |
| 2 | +// From Classic Computer Science Problems in Java Chapter 8 |
| 3 | +// Copyright 2020 David Kopec |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package chapter8; |
| 18 | + |
| 19 | +import java.lang.annotation.Retention; |
| 20 | +import java.lang.annotation.RetentionPolicy; |
| 21 | +import java.lang.reflect.Method; |
| 22 | + |
| 23 | +// Annotation for unit tests |
| 24 | +@Retention(RetentionPolicy.RUNTIME) |
| 25 | +@interface UnitTest { |
| 26 | + String name() default ""; |
| 27 | +} |
| 28 | + |
| 29 | +public class TTTMinimaxTests { |
| 30 | + |
| 31 | + // Check if two values are equal and report back |
| 32 | + public static <T> void assertEquality(T actual, T expected) { |
| 33 | + if (actual.equals(expected)) { |
| 34 | + System.out.println("Passed!"); |
| 35 | + } else { |
| 36 | + System.out.println("Failed!"); |
| 37 | + System.out.println("Actual: " + actual.toString()); |
| 38 | + System.out.println("Expected: " + expected.toString()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @UnitTest(name = "Easy Position") |
| 43 | + public void easyPosition() { |
| 44 | + TTTPiece[] toWinEasyPosition = new TTTPiece[] { |
| 45 | + TTTPiece.X, TTTPiece.O, TTTPiece.X, |
| 46 | + TTTPiece.X, TTTPiece.E, TTTPiece.O, |
| 47 | + TTTPiece.E, TTTPiece.E, TTTPiece.O }; |
| 48 | + TTTBoard testBoard1 = new TTTBoard(toWinEasyPosition, TTTPiece.X); |
| 49 | + Integer answer1 = Minimax.findBestMove(testBoard1, 8); |
| 50 | + assertEquality(answer1, 6); |
| 51 | + } |
| 52 | + |
| 53 | + @UnitTest(name = "Block Position") |
| 54 | + public void blockPosition() { |
| 55 | + TTTPiece[] toBlockPosition = new TTTPiece[] { |
| 56 | + TTTPiece.X, TTTPiece.E, TTTPiece.E, |
| 57 | + TTTPiece.E, TTTPiece.E, TTTPiece.O, |
| 58 | + TTTPiece.E, TTTPiece.X, TTTPiece.O }; |
| 59 | + TTTBoard testBoard2 = new TTTBoard(toBlockPosition, TTTPiece.X); |
| 60 | + Integer answer2 = Minimax.findBestMove(testBoard2, 8); |
| 61 | + assertEquality(answer2, 2); |
| 62 | + } |
| 63 | + |
| 64 | + @UnitTest(name = "Hard Position") |
| 65 | + public void hardPosition() { |
| 66 | + TTTPiece[] toWinHardPosition = new TTTPiece[] { |
| 67 | + TTTPiece.X, TTTPiece.E, TTTPiece.E, |
| 68 | + TTTPiece.E, TTTPiece.E, TTTPiece.O, |
| 69 | + TTTPiece.O, TTTPiece.X, TTTPiece.E }; |
| 70 | + TTTBoard testBoard3 = new TTTBoard(toWinHardPosition, TTTPiece.X); |
| 71 | + Integer answer3 = Minimax.findBestMove(testBoard3, 8); |
| 72 | + assertEquality(answer3, 1); |
| 73 | + } |
| 74 | + |
| 75 | + // Run all methods marked with the UnitTest annotation |
| 76 | + public void runAllTests() { |
| 77 | + for (Method method : this.getClass().getMethods()) { |
| 78 | + for (UnitTest annotation : method.getAnnotationsByType(UnitTest.class)) { |
| 79 | + System.out.println("Running Test " + annotation.name()); |
| 80 | + try { |
| 81 | + method.invoke(this); |
| 82 | + } catch (Exception e) { |
| 83 | + e.printStackTrace(); |
| 84 | + } |
| 85 | + System.out.println("____________________"); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static void main(String[] args) { |
| 91 | + new TTTMinimaxTests().runAllTests(); |
| 92 | + } |
| 93 | +} |
0 commit comments