@@ -10,6 +10,20 @@ import Testing
10
10
11
11
@Suite
12
12
final class GameLogicTests {
13
+
14
+ private func makeMoves( _ logic: GameLogic , moves: [ ( Int , Int ) ] ) {
15
+ moves. forEach { ( row, col) in
16
+ _ = logic. makeMove ( row: row, col: col)
17
+ }
18
+ }
19
+
20
+ private func expectWin( for player: GameLogic . Player , logic: GameLogic , message: Comment ) {
21
+ #expect( logic. gameState == . won( player) , message)
22
+ }
23
+
24
+ private func expectDraw( logic: GameLogic , message: Comment ) {
25
+ #expect( logic. gameState == . draw, message)
26
+ }
13
27
14
28
@Test ( " Initial Game State Test " )
15
29
func testInitialGameState( ) {
@@ -18,42 +32,85 @@ final class GameLogicTests {
18
32
#expect( logic. currentPlayer == . x, " Expected the first player to be X " )
19
33
}
20
34
21
- @Test ( " Move Updates State " , arguments : [ ( 0 , 0 ) , ( 1 , 1 ) ] )
22
- func testMakeMoveUpdatesState ( row : Int , col : Int ) {
35
+ @Test ( " Turn Switching Test " )
36
+ func testTurnSwitching ( ) {
23
37
let logic = GameLogic ( boardSize: 3 )
24
- let moveResult = logic. makeMove ( row: row, col: col)
25
- #expect( moveResult == true , " Expected move to be successful " )
26
- #expect( logic. currentPlayer == . o, " Expected current player to switch to O after X's move " )
38
+
39
+ _ = logic. makeMove ( row: 0 , col: 0 )
40
+ #expect( logic. currentPlayer == . o, " Expected turn to switch to O after X's move " )
41
+
42
+ _ = logic. makeMove ( row: 1 , col: 1 )
43
+ #expect( logic. currentPlayer == . x, " Expected turn to switch back to X after O's move " )
27
44
}
28
45
29
- @Test ( " Win Condition Test " , arguments: [
30
- [ ( 0 , 0 ) , ( 1 , 0 ) , ( 0 , 1 ) , ( 1 , 1 ) , ( 0 , 2 ) ] ,
31
- [ ( 2 , 0 ) , ( 1 , 1 ) , ( 2 , 1 ) , ( 1 , 2 ) , ( 2 , 2 ) ]
32
- ] )
33
- func testWinCondition( moves: [ ( Int , Int ) ] ) {
46
+ @Test ( " Invalid Move on Taken Position " )
47
+ func testInvalidMoveOnTakenPosition( ) {
34
48
let logic = GameLogic ( boardSize: 3 )
35
49
36
- moves. enumerated ( ) . forEach { index, move in
37
- let ( row, col) = move
38
- _ = logic. makeMove ( row: row, col: col)
39
- if index == moves. count - 1 { // Last move for the win
40
- #expect( logic. gameState == . won( . x) , " Expected X to win on final move " )
41
- }
42
- }
50
+ _ = logic. makeMove ( row: 0 , col: 0 )
51
+ let invalidMove = logic. makeMove ( row: 0 , col: 0 )
52
+ #expect( invalidMove == false , " Expected move on taken position to be invalid " )
43
53
}
44
54
45
- @Test ( " Draw Condition Test " )
46
- func testDrawCondition( ) {
55
+ @Test ( " Win Condition Test for Player X " , arguments: [
56
+ ( [ ( 0 , 0 ) , ( 1 , 0 ) , ( 0 , 1 ) , ( 1 , 1 ) , ( 0 , 2 ) ] , GameLogic . GameState. won ( . x) ) ,
57
+ ( [ ( 2 , 0 ) , ( 1 , 1 ) , ( 2 , 1 ) , ( 1 , 2 ) , ( 2 , 2 ) ] , GameLogic . GameState. won ( . x) )
58
+ ] )
59
+ func testWinCondition( moves: [ ( Int , Int ) ] , expectedState: GameLogic . GameState ) {
47
60
let logic = GameLogic ( boardSize: 3 )
61
+ makeMoves ( logic, moves: moves)
62
+ #expect( logic. gameState == expectedState, " Expected \( expectedState) for the given move sequence " )
63
+ }
64
+
65
+ @Test ( " Draw Condition Test on 3x3 Board " , arguments: [
66
+ ( [ ( 0 , 0 ) , ( 0 , 1 ) , ( 0 , 2 ) , ( 1 , 1 ) , ( 1 , 0 ) , ( 2 , 0 ) , ( 1 , 2 ) , ( 2 , 2 ) , ( 2 , 1 ) ] , GameLogic . GameState. draw)
67
+ ] )
68
+ func testDrawConditionOn3x3Board( moves: [ ( Int , Int ) ] , expectedState: GameLogic . GameState ) {
69
+ let logic = GameLogic ( boardSize: 3 )
70
+ makeMoves ( logic, moves: moves)
71
+ #expect( logic. gameState == expectedState, " Expected \( expectedState) on a 3x3 board filled without a win " )
72
+ }
73
+
74
+ @Test ( " No Move After Game Ends " , arguments: [
75
+ ( [ ( 0 , 0 ) , ( 1 , 0 ) , ( 0 , 1 ) , ( 1 , 1 ) , ( 0 , 2 ) ] , false )
76
+ ] )
77
+ func testNoMoveAfterGameEnds( moves: [ ( Int , Int ) ] , moveAfterEndExpected: Bool ) {
78
+ let logic = GameLogic ( boardSize: 3 )
79
+ makeMoves ( logic, moves: moves)
48
80
49
- let moves = [ ( 0 , 0 ) , ( 0 , 1 ) , ( 0 , 2 ) ,
50
- ( 1 , 1 ) , ( 1 , 0 ) , ( 2 , 0 ) ,
51
- ( 1 , 2 ) , ( 2 , 2 ) , ( 2 , 1 ) ]
52
-
53
- moves. forEach { ( row, col) in
54
- _ = logic. makeMove ( row: row, col: col)
55
- }
56
-
57
- #expect( logic. gameState == . draw, " Expected game to end in a draw " )
81
+ let moveAfterWin = logic. makeMove ( row: 2 , col: 2 )
82
+ #expect( moveAfterWin == moveAfterEndExpected, " Expected no moves allowed after game ends " )
83
+ }
84
+
85
+ @Test ( " Win Condition on 4x4 Board " , arguments: [
86
+ ( [ ( 0 , 0 ) , ( 1 , 1 ) , ( 0 , 1 ) , ( 1 , 2 ) , ( 0 , 2 ) , ( 1 , 0 ) , ( 0 , 3 ) ] , GameLogic . GameState. won ( . x) )
87
+ ] )
88
+ func testWinConditionOnFourByFourBoard( moves: [ ( Int , Int ) ] , expectedState: GameLogic . GameState ) {
89
+ let logic = GameLogic ( boardSize: 4 )
90
+ makeMoves ( logic, moves: moves)
91
+ #expect( logic. gameState == expectedState, " Expected \( expectedState) with a winning line on a 4x4 board " )
92
+ }
93
+
94
+ @Test ( " Draw Condition on 4x4 Board " , arguments: [
95
+ ( [
96
+ ( 0 , 0 ) , ( 0 , 1 ) , ( 0 , 2 ) , ( 0 , 3 ) ,
97
+ ( 1 , 1 ) , ( 1 , 0 ) , ( 1 , 3 ) , ( 1 , 2 ) ,
98
+ ( 2 , 2 ) , ( 2 , 3 ) , ( 2 , 1 ) , ( 2 , 0 ) ,
99
+ ( 3 , 3 ) , ( 3 , 2 ) , ( 3 , 0 ) , ( 3 , 1 )
100
+ ] , GameLogic . GameState. draw)
101
+ ] )
102
+ func testDrawConditionOnFourByFourBoard( moves: [ ( Int , Int ) ] , expectedState: GameLogic . GameState ) {
103
+ let logic = GameLogic ( boardSize: 4 )
104
+ makeMoves ( logic, moves: moves)
105
+ #expect( logic. gameState == expectedState, " Expected \( expectedState) on a 4x4 board filled without a win " )
106
+ }
107
+
108
+ @Test ( " Smallest Possible Board - 1x1 " , arguments: [
109
+ ( [ ( 0 , 0 ) ] , GameLogic . GameState. won ( . x) )
110
+ ] )
111
+ func testSmallestPossibleBoard( moves: [ ( Int , Int ) ] , expectedState: GameLogic . GameState ) {
112
+ let logic = GameLogic ( boardSize: 1 )
113
+ makeMoves ( logic, moves: moves)
114
+ #expect( logic. gameState == expectedState, " Expected \( expectedState) immediately on a 1x1 board " )
58
115
}
59
116
}
0 commit comments