@@ -2,9 +2,186 @@ module src/test
22
33import test
44import src/lib
5+ import src/Generate
6+
7+ def main() = mainSuite("main") {
8+ with on[OutOfBounds].panic
9+
10+ def createWinBoard(combination: List[Int], player: Cell): SmallBoard = {
11+ var board: SmallBoard = fill(9, Empty())
12+ combination.foreach { index => board = board.replace(index, player) }
13+ board
14+ }
515
6- def main () = mainSuite(" lib" ) {
716 test("Hello world") {
817 assertEqual(helloWorld(), "Hello, world!")
918 }
19+ test("Player String for Cross") {
20+ assertEqual(playerString(Cross()), "X")
21+ }
22+ test("Player String for Nought") {
23+ assertEqual(playerString(Nought()), "O")
24+ }
25+ test("generateNewBigBoard creates BigBoard with 9 SmallBoards") {
26+ val newGame: GameBoard = generateNewBigBoard()
27+ assertEqual(newGame.bigBoard.size(), 9)
28+ }
29+ test("generateNewBigBoard initializes SmallBoards with Empty cells") {
30+ val newGame: GameBoard = generateNewBigBoard()
31+ newGame.bigBoard.foreach { smallBoard =>
32+ smallBoard.foreach { cell =>
33+ assertEqual(cell, Empty())
34+ }
35+ assertEqual(smallBoard.size(), 9)
36+ }
37+ }
38+ test("generateNewBigBoard initializes smallCopy with Empty cells") {
39+ val newGame = generateNewBigBoard()
40+ newGame.smallCopy.foreach { cell =>
41+ assertEqual(cell, Empty())
42+ }
43+ assertEqual(newGame.smallCopy.size(), 9)
44+ }
45+
46+ test("newActiveSmallBoard activates small board 0 and updates smallCopy") {
47+ var game = generateNewBigBoard()
48+ game = newActiveSmallBoard(game, 0)
49+
50+ assertEqual(game.smallCopy.get(0), Active()) // Check smallCopy update
51+
52+ val smallBoard0 = game.bigBoard.get(0)
53+ smallBoard0.foreach { cell =>
54+ assertEqual(cell, Active()) // Check if all cells in smallBoard0 are Active
55+ }
56+ }
57+
58+ test("newActiveSmallBoard activates small board 5 and updates smallCopy") {
59+ var game = generateNewBigBoard()
60+ game = newActiveSmallBoard(game, 5)
61+
62+ assertEqual(game.smallCopy.get(5), Active()) // Check smallCopy update
63+
64+ val smallBoard5 = game.bigBoard.get(5)
65+ smallBoard5.foreach { cell =>
66+ assertEqual(cell, Active()) // Check if all cells in smallBoard5 are Active
67+ }
68+ }
69+
70+ test("deactivateSmallBoard deactivates small board 0"){
71+ var game = generateNewBigBoard()
72+ game = newActiveSmallBoard(game, 0) // First activate it
73+ game = deactivateSmallBoard(game, 0) // Then deactivate
74+
75+ val smallBoard0 = game.bigBoard.get(0)
76+ smallBoard0.foreach { cell =>
77+ assertEqual(cell, Empty()) // Check if all cells in smallBoard0 are Empty again
78+ }
79+ // You might want to assert something about smallCopy here depending on desired behavior
80+ // For example, if smallCopy should also be reset:
81+ assertEqual(game.smallCopy.get(0), Empty())
82+ }
83+
84+ test("checkNewCell places Cross in cell 0 of small board 0") {
85+ var game = generateNewBigBoard()
86+ game = checkNewCell(game, 0, 0, Cross())
87+ assertEqual(game.bigBoard.get(0).get(0), Cross())
88+ }
89+
90+ test("checkNewCell places Nought in cell 5 of small board 3") {
91+ var game = generateNewBigBoard()
92+ game = generateNewBigBoard()
93+ game = checkNewCell(game, 3, 5, Nought())
94+ assertEqual(game.bigBoard.get(3).get(5), Nought())
95+ }
96+
97+ test("checkAvailableCell returns true for Empty cell") {
98+ val smallBoard: SmallBoard = fill(9, Empty())
99+ assertEqual(checkAvailableCell(smallBoard, 2), true)
100+ }
101+
102+ test("checkAvailableCell returns true for Active cell") {
103+ val smallBoard: SmallBoard = fill(9, Empty()).replace(4, Active())
104+ assertEqual(checkAvailableCell(smallBoard, 4), true)
105+ }
106+
107+ test("checkAvailableCell returns false for Cross cell") {
108+ val smallBoard: SmallBoard = fill(9, Empty()).replace(7, Cross())
109+ assertEqual(checkAvailableCell(smallBoard, 7), false)
110+ }
111+
112+ test("checkAvailableCell returns false for Nought cell") {
113+ val smallBoard: SmallBoard = fill(9, Empty()).replace(1, Nought())
114+ assertEqual(checkAvailableCell(smallBoard, 1), false)
115+ }
116+ test("Winning Combinations"){
117+ winCombinations.take(3).foreach { combination => // First 3 are rows
118+ val winBoard = createWinBoard(combination, Cross())
119+ assertEqual(checkWinSituation(winBoard, Cross()), Win())
120+ }
121+ }
122+ test("Draw (Loose)") {
123+ var drawBoard: SmallBoard = empty()
124+ [Cross(), Nought(), Cross(), Nought(), Cross(), Cross(), Nought(), Cross(), Nought()].foreach { player =>
125+ drawBoard = drawBoard.insert(drawBoard.size(), player)
126+ }
127+ assertEqual(checkWinSituation(drawBoard, Cross()), Loose())
128+ }
129+
130+ test("No Win Yet") {
131+ val noWinBoard: SmallBoard = [Cross(), Empty(), Nought(), Empty(), Cross(), Empty(), Empty(), Empty(), Nought()]
132+ assertEqual(checkWinSituation(noWinBoard, Cross()), Nope())
133+ }
134+
135+ test("fillWinningGameBoard fills with Cross") {
136+ var game = generateNewBigBoard()
137+ game = fillWinningGameBoard(game, 2, Cross())
138+ val smallBoard2 = game.bigBoard.get(2)
139+ smallBoard2.foreach { cell => assertEqual(cell, Cross()) }
140+ assertEqual(game.smallCopy.get(2), Cross())
141+ }
142+
143+ test("fillWinningGameBoard fills with Nought") {
144+ var game = generateNewBigBoard()
145+ game = fillWinningGameBoard(game, 7, Nought())
146+ val smallBoard7 = game.bigBoard.get(7)
147+ smallBoard7.foreach { cell => assertEqual(cell, Nought()) }
148+ assertEqual(game.smallCopy.get(7), Nought())
149+ }
150+
151+ test("fillWinningGameBoard fills with Draw") {
152+ var game = generateNewBigBoard()
153+ game = fillWinningGameBoard(game, 4, Draw())
154+ val smallBoard4 = game.bigBoard.get(4)
155+ smallBoard4.foreach { cell => assertEqual(cell, Draw()) }
156+ assertEqual(game.smallCopy.get(4), Draw())
157+ }
158+
159+ test("Computer Cross, Player Cross - Computer's turn") {
160+ assertEqual(isComputerTurn(Cross(), Cross()), true)
161+ }
162+
163+ test("Computer Cross, Player Nought - Not computer's turn") {
164+ assertEqual(isComputerTurn(Nought(), Cross()), false)
165+ }
166+ test("Computer Nought, Player Nought - Computer's turn") {
167+ assertEqual(isComputerTurn(Nought(), Nought()), true)
168+ }
169+ test("Computer Nought, Player Cross - Not computer's turn") {
170+ assertEqual(isComputerTurn(Cross(), Nought()), false)
171+ }
172+
173+ test("computerPlay returns valid cell on empty board") {
174+ val emptyBoard: SmallBoard = fill(9, Empty())
175+ val resultCell = computerPlay(emptyBoard, Cross())
176+ assertEqual(resultCell >= 0, resultCell <= 8)
177+ }
178+
179+ test("computerPlay returns valid cell on almost full board") {
180+ var almostFullBoard: SmallBoard = empty()
181+ [Cross(), Nought(), Cross(), Nought(), Cross(), Nought(), Nought(), Cross(), Empty()].foreach { player =>
182+ almostFullBoard = almostFullBoard.insert(almostFullBoard.size(), player)
183+ }
184+ val resultCell = computerPlay(almostFullBoard, Nought())
185+ assertEqual(resultCell >= 0, resultCell <= 8)
186+ }
10187}
0 commit comments