Skip to content

Commit b18de54

Browse files
dzianis-sudkoudzianis-sudkou
authored andcommitted
Finally everything works fine. I forgot to mention that I hate this language.
1 parent f1f69c4 commit b18de54

File tree

3 files changed

+67
-22
lines changed

3 files changed

+67
-22
lines changed

src/Generate.effekt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,12 @@ def checkAvailableCell(bigBoards: BigBoards, smallBoardNumber: Int, cellNumber:
116116
* Input : SmallBoardNumber, player
117117
* Output : Boolean
118118
*/
119-
def checkWinSituation(smallBoard: SmallBoard, player: Cell): Bool = {
119+
def checkWinSituation(smallBoard: SmallBoard, player: Cell): WinBoard = {
120120
var cr: Bool = true
121121
if (player is Cross()) cr = true else cr = false
122-
inspect(player)
123-
inspect(cr)
124-
consoleInput()
125122
var checkedCells: List[Int] = empty()
126123
var counter: Int = 0
127-
var win: Bool = false
124+
var win: WinBoard = Nope()
128125

129126
smallBoard.foreach {cell =>
130127
cell match {
@@ -135,17 +132,30 @@ def checkWinSituation(smallBoard: SmallBoard, player: Cell): Bool = {
135132
counter = counter + 1
136133
}
137134
counter = 0
138-
inspect(checkedCells)
139-
consoleInput()
140135
// Here we are checking if all three elements from the win combination are in the
141136
// Checked cells list
142137
winCombinations.foreach { combination =>
143138
combination.foreach { el =>
144139
if(any(checkedCells) {cell => cell == el}) counter = counter + 1
145140
}
146-
if (counter == 3) win = true
141+
if (counter == 3) win = Win()
147142
else counter = 0
148143
}
144+
145+
// Check if there's a draw in board
146+
counter = 0
147+
if (win is Nope()){
148+
smallBoard.foreach {cell =>
149+
cell match {
150+
case Cross() => counter = counter + 1
151+
case Nought() => counter = counter + 1
152+
case _ => ()
153+
}
154+
if (counter == 9) {
155+
win = Loose()
156+
}
157+
}
158+
}
149159
win
150160
}
151161

src/Stupid.effekt

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,31 @@ def playGame(): Unit = {
9898

9999
// Check the winning situation
100100
with on[OutOfBounds].panic
101-
if (checkWinSituation(gameBoard.bigBoard.get(currentSmallBoard - 1), currentPlayer)){
102-
consoleInput()
103-
println("\nYou won the Small Board![Enter]")
104-
consoleInput()
105-
gameBoard = fillWinningGameBoard(gameBoard, currentSmallBoard, currentPlayer)
101+
checkWinSituation(gameBoard.bigBoard.get(currentSmallBoard - 1), currentPlayer) match {
102+
case Win() => {
103+
println("\nYou won the Small Board!")
104+
consoleInput()
105+
gameBoard = fillWinningGameBoard(gameBoard, currentSmallBoard, currentPlayer)
106+
}
107+
case Loose() => {
108+
println("\n It's a draw in the Small Board!")
109+
consoleInput()
110+
gameBoard = fillWinningGameBoard(gameBoard, currentSmallBoard, Draw())
111+
}
112+
case _ => ()
106113
}
114+
// if (checkWinSituation(gameBoard.bigBoard.get(currentSmallBoard - 1), currentPlayer) is Win()){
115+
// println("\nYou won the Small Board!")
116+
// consoleInput()
117+
// gameBoard = fillWinningGameBoard(gameBoard, currentSmallBoard, currentPlayer)
118+
// }
107119

108120
// After The Player's Move
109121
printGameScreen(gameBoard, currentPlayer)
110122
println("Excellent Move! Now Press the Enter to finish your turn.")
111123

112124
consoleInput()
113125

114-
// Change The Current Player
115-
if(currentPlayer is Cross()) currentPlayer = Nought()
116-
else currentPlayer = Cross()
117-
118126
// Change the Current Small Board
119127
if (checkAvailableSmallBoard(gameBoard, currentCell)){
120128
gameBoard = deactivateSmallBoard(gameBoard, currentSmallBoard)
@@ -125,6 +133,30 @@ def playGame(): Unit = {
125133
gameBoard = deactivateSmallBoard(gameBoard, currentSmallBoard)
126134
currentSmallBoard = 10
127135
}
136+
137+
// Verify if the game has been won
138+
if (checkWinSituation(gameBoard.smallCopy, currentPlayer) is Win()){
139+
println(playerString(currentPlayer) ++ ", You won the game!")
140+
consoleInput()
141+
endGame = true
142+
}
143+
144+
checkWinSituation(gameBoard.smallCopy, currentPlayer) match {
145+
case Win() => {
146+
println(playerString(currentPlayer) ++ ", You won the game!")
147+
consoleInput()
148+
endGame = true
149+
}
150+
case Loose() => {
151+
println(playerString(currentPlayer) ++ ", You lost the game!")
152+
consoleInput()
153+
endGame = true
154+
}
155+
case _ => ()
156+
}
157+
// Change The Current Player
158+
if(currentPlayer is Cross()) currentPlayer = Nought()
159+
else currentPlayer = Cross()
128160
()
129161
}
130162
}

src/lib.effekt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ import io/console
55
effect WrongInput(msg: String): Unit
66

77

8-
// type Player {
9-
// Nought()
10-
// Cross()
11-
// }
12-
138
type Cell {
149
Empty()
1510
Active()
11+
1612
Nought()
1713
Cross()
14+
1815
Draw()
1916

2017
NewLine()
2118
Separator()
2219
}
2320

21+
type WinBoard {
22+
Win()
23+
Loose()
24+
Nope()
25+
}
26+
2427
type SmallBoard = List[Cell]
2528
type BigBoard = List[SmallBoard]
2629

0 commit comments

Comments
 (0)