Skip to content

Commit 766c657

Browse files
dzianis-sudkoudzianis-sudkou
authored andcommitted
New Tests
1 parent 16db6f7 commit 766c657

File tree

6 files changed

+200
-22
lines changed

6 files changed

+200
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ out
44
# Nix
55
result
66
result-*
7+
.DS_STORE

src/Generate.effekt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import src/lib
88
/**
99
* Generates a new BigBoard
1010
* Input : None
11-
* Output : BigBoards
11+
* Output : GameBoard
1212
*/
13-
def generateNewBigBoard(): BigBoards = {
13+
def generateNewBigBoard(): GameBoard = {
1414
val smallBoard: SmallBoard = fill(9, Empty())
1515
val bigBoard: BigBoard = fill(9, smallBoard)
16-
BigBoards(bigBoard, smallBoard)
16+
GameBoard(bigBoard, smallBoard)
1717
}
1818

1919

2020
// Makes a new Small Board Active()
21-
def newActiveSmallBoard(gameBoard: BigBoards, smallBoardNumber: Int): BigBoards = {
21+
def newActiveSmallBoard(gameBoard: GameBoard, smallBoardNumber: Int): GameBoard = {
2222
with on[OutOfBounds].panic
2323

2424
var newBigBoard: BigBoard = gameBoard.bigBoard
@@ -41,18 +41,18 @@ def newActiveSmallBoard(gameBoard: BigBoards, smallBoardNumber: Int): BigBoards
4141
}
4242

4343
newBigBoard = newBigBoard.replace(smallBoardNumber, newSmallBoard)
44-
BigBoards(newBigBoard, newGameBoardSmallCopy)
44+
GameBoard(newBigBoard, newGameBoardSmallCopy)
4545
}
4646

47-
def deactivateSmallBoard(gameBoard: BigBoards, smallBoardNumber: Int): BigBoards = {
47+
def deactivateSmallBoard(gameBoard: GameBoard, smallBoardNumber: Int): GameBoard = {
4848
with on[OutOfBounds].panic
4949

5050
var newBigBoard: BigBoard = gameBoard.bigBoard
5151
var newGameBoardSmallCopy: SmallBoard = gameBoard.smallCopy
5252

5353
var newSmallBoard: SmallBoard = newBigBoard.get(smallBoardNumber)
5454
var counter: Int = 0
55-
55+
newGameBoardSmallCopy = newGameBoardSmallCopy.replace(smallBoardNumber, Empty())
5656

5757
newSmallBoard.foreach {cell =>
5858
cell match {
@@ -63,23 +63,23 @@ def deactivateSmallBoard(gameBoard: BigBoards, smallBoardNumber: Int): BigBoards
6363
()
6464
}
6565
newBigBoard = newBigBoard.replace(smallBoardNumber, newSmallBoard)
66-
BigBoards(newBigBoard, newGameBoardSmallCopy)
66+
GameBoard(newBigBoard, newGameBoardSmallCopy)
6767
}
6868

69-
def checkNewCell(gameBoard: BigBoards, smallBoardNumber: Int, cellNumber: Int, player: Cell): BigBoards = {
69+
def checkNewCell(gameBoard: GameBoard, smallBoardNumber: Int, cellNumber: Int, player: Cell): GameBoard = {
7070
with on[OutOfBounds].panic
7171

7272
var newBigBoard: BigBoard = gameBoard.bigBoard
7373
var newSmallBoard: SmallBoard = newBigBoard.get(smallBoardNumber)
7474
newSmallBoard = newSmallBoard.replace(cellNumber, player)
7575
newBigBoard = newBigBoard.replace(smallBoardNumber, newSmallBoard)
76-
BigBoards(newBigBoard, gameBoard.smallCopy)
76+
GameBoard(newBigBoard, gameBoard.smallCopy)
7777
}
7878

7979
/*
8080
* Returns true if the cell is free.
8181
* False Otherwise
82-
* Input : BigBoards, SmallBoardNumber, Cell Number
82+
* Input : GameBoard, SmallBoardNumber, Cell Number
8383
* Output : Boolean
8484
*/
8585
def checkAvailableCell(smallBoard: SmallBoard, cellNumber: Int): Bool = {
@@ -142,19 +142,18 @@ def checkWinSituation(smallBoard: SmallBoard, player: Cell): WinBoard = {
142142
win
143143
}
144144

145-
def fillWinningGameBoard(gameBoard: BigBoards, smallBoardNumber: Int, player: Cell): BigBoards = {
145+
def fillWinningGameBoard(gameBoard: GameBoard, smallBoardNumber: Int, player: Cell): GameBoard = {
146146
var newBigBoard: BigBoard = gameBoard.bigBoard
147147
var newGameBoardSmallCopy: SmallBoard = gameBoard.smallCopy
148148

149149
newBigBoard = newBigBoard.replace(smallBoardNumber, fill(9, player))
150150
newGameBoardSmallCopy = newGameBoardSmallCopy.replace(smallBoardNumber, player)
151-
BigBoards(newBigBoard, newGameBoardSmallCopy)
151+
GameBoard(newBigBoard, newGameBoardSmallCopy)
152152
}
153153

154154
def computerPlay(smallBoard: SmallBoard, computer: Cell): Int = {
155-
println("It's computer playing. Please press Enter")
156-
consoleInput()
157155
with on[OutOfBounds].panic
156+
println("It's computer playing.")
158157

159158
var newSmallBoard: SmallBoard = smallBoard
160159
var player: Cell = Cross()

src/Render.effekt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ def createUnifiedRow(boardRow: List[SmallBoard]): List[Cell] = {
7777
resultRow
7878
}
7979

80-
def printGameScreen(bigBoards: BigBoards, player: Cell): Unit = {
80+
def printGameScreen(gameBoard: GameBoard, player: Cell): Unit = {
8181
clearScreen()
8282
println("You're playing as " ++ playerString(player))
8383
newLine()
8484

85-
renderBoard(bigBoards.bigBoard)
85+
renderBoard(gameBoard.bigBoard)
8686
newLine()
8787
}

src/Stupid.effekt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def playGame(): Unit = {
3838

3939

4040
// Returns the Small Board Number [0 - 8]
41-
def chooseSmallBoard(gameBoard: BigBoards): Int / {WrongInput} = {
41+
def chooseSmallBoard(gameBoard: GameBoard): Int / {WrongInput} = {
4242
println("Choose the Small Board[1 - 9]:")
4343
val smallBoardNumber: String = consoleInput()
4444

@@ -59,7 +59,7 @@ def playGame(): Unit = {
5959
}
6060

6161
// Returns the chosen Cell inside the board [0 - 8]
62-
def chooseCell(gameBoard: BigBoards, smallBoardNumber: Int): Int / {WrongInput} = {
62+
def chooseCell(gameBoard: GameBoard, smallBoardNumber: Int): Int / {WrongInput} = {
6363
with on[OutOfBounds].panic
6464

6565
println("Choose the cell inside your Small Board")
@@ -84,7 +84,7 @@ def playGame(): Unit = {
8484
def gameLoop(player: Cell, gameMode: GameMode): Unit / {WrongInput} = {
8585
var endGame: Bool = false
8686

87-
var gameBoard: BigBoards = generateNewBigBoard()
87+
var gameBoard: GameBoard = generateNewBigBoard()
8888
var currentSmallBoard: Int = 10
8989
var currentCell: Int = 10
9090

src/lib.effekt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ type WinBoard {
2727

2828
type SmallBoard = List[Cell]
2929
type BigBoard = List[SmallBoard]
30+
record GameBoard(bigBoard: BigBoard, smallCopy: SmallBoard)
3031

3132
val Numbers: List[String] = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
3233
val winCombinations: List[List[Int]] = [[0,1,2], [3,4,5], [6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
3334

34-
record BigBoards(bigBoard: BigBoard, smallCopy: SmallBoard)
35+
3536

3637
def helloWorld(): String = "Hello, world!"
3738

src/test.effekt

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,186 @@ module src/test
22

33
import test
44
import 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

Comments
 (0)