Skip to content

Commit ddae3bd

Browse files
dzianis-sudkoudzianis-sudkou
authored andcommitted
Final version
1 parent 2f36d07 commit ddae3bd

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

src/Game.effekt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,28 @@ import src/Generate
1111
def playGame(): Unit = {
1212

1313
// Returns the chosen Game Mode
14-
def chooseGameMode(): GameMode / {WrongInput} = {
15-
println("Choose the Game Mode:\n[1 - Local, 2 - vs Computer]:")
14+
def chooseGameMode(): GameMode / {WrongInput, ExitGame, PrintRules} = {
15+
println("Choose the Game Mode:\n[1 - Local; 2 - vs Computer; h - Read the rules; q - Exit the game]")
1616
val side = consoleInput()
1717
side match {
1818
case "1" => Local()
1919
case "2" => Computer()
20+
case "h" => do PrintRules(); chooseGameMode()
21+
case "q" => do ExitGame(); chooseGameMode()
2022
case _ =>
2123
do WrongInput("Invalid Game Mode. Please type '1' or '2'!")
2224
chooseGameMode()
2325
}
2426
}
2527

2628
// Returns the Cell type for the side
27-
def chooseSide(): Cell / {WrongInput} = {
29+
def chooseSide(): Cell / {WrongInput, ExitGame} = {
2830
println("Choose the side[X, O]:")
2931
val side = consoleInput()
3032
side match {
3133
case "X" => Cross()
3234
case "O" => Nought()
35+
case "q" => do ExitGame(); chooseSide()
3336
case _ =>
3437
do WrongInput("Invalid player type. Please type 'X' or 'O'!")
3538
chooseSide()
@@ -174,21 +177,15 @@ def playGame(): Unit = {
174177

175178
// MAIN SECTION
176179

177-
clearScreen()
178-
179180
// Greeting Message
180-
println("Welcome to the ULTIMATE TIC-TAC-TOE!")
181-
println("Press enter to start the game.")
182-
183-
consoleInput()
181+
printWelcomeScreen()
184182

185183
var play: Bool = true
186184

187-
var player: Cell = Nought()
185+
var player: Cell = Cross()
188186

189187
var gameMode: GameMode = Local()
190188
while (play) {
191-
192189
try{
193190
// Choosing a Game Mode
194191
gameMode = chooseGameMode()
@@ -202,9 +199,14 @@ def playGame(): Unit = {
202199
} with WrongInput { msg =>
203200
println(msg)
204201
resume(())
202+
} with ExitGame {
203+
play = false
204+
} with PrintRules {
205+
printRules()
206+
resume(())
205207
}
206208
}
207-
println("Goodbye!")
209+
printExitScreen()
208210
}
209211

210212

src/Render.effekt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,68 @@ def printGameScreen(gameBoard: GameBoard, player: Cell): Unit = {
8484

8585
renderBoard(gameBoard.bigBoard)
8686
newLine()
87+
}
88+
89+
def printWelcomeScreen(): Unit = {
90+
clearScreen()
91+
with Formatted::formatting
92+
val welcomeScreen: String = """
93+
__ ____ ____________ ______ ____________ ________________ _________ ______ __________ ______
94+
/ / / / / /_ __/ _/ |/ / |/_ __/ ____/ /_ __/ _/ ____/ /_ __/ | / ____/ /_ __/ __ \/ ____/
95+
/ / / / / / / / // /|_/ / /| | / / / __/ / / / // / ______/ / / /| |/ / ______/ / / / / / __/
96+
/ /_/ / /___/ / _/ // / / / ___ |/ / / /___ / / _/ // /__/_____/ / / ___ / /__/_____/ / / /_/ / /___
97+
\____/_____/_/ /___/_/ /_/_/ |_/_/ /_____/ /_/ /___/\____/ /_/ /_/ |_\____/ /_/ \____/_____/
98+
99+
[enter]
100+
""".green
101+
println("\u001b[10H") // Move the cursor to the middle
102+
println(welcomeScreen)
103+
consoleInput()
104+
()
105+
}
106+
107+
def printExitScreen(): Unit = {
108+
clearScreen()
109+
with Formatted::formatting
110+
val exitScreen: String = """
111+
______ ____ __
112+
/ ____/___ ____ ____/ / /_ __ _____ / /
113+
/ / __/ __ \/ __ \/ __ / __ \/ / / / _ \/ /
114+
/ /_/ / /_/ / /_/ / /_/ / /_/ / /_/ / __/_/
115+
\____/\____/\____/\__,_/_.___/\__, /\___(_)
116+
/____/
117+
118+
[enter]
119+
""".green
120+
println("\u001b[10H") // Move the cursor to the middle
121+
println(exitScreen)
122+
consoleInput()
123+
()
124+
}
125+
126+
def printRules(): Unit = {
127+
clearScreen()
128+
val rules: String = """
129+
Ultimate Tic-Tac-Toe Rules:
130+
131+
1. The Board: The game is played on a large Tic-Tac-Toe board
132+
composed of 9 smaller Tic-Tac-Toe boards.
133+
134+
2. Winning a Small Board: Within each small board, standard Tic-Tac-Toe rules apply.
135+
The first player to get three in a row (horizontally, vertically, or diagonally) wins that small board.
136+
137+
3. Winning the Big Board: To win the entire game, a player must win three small boards in a row on the large board.
138+
139+
4. Where to Play: Your move on the big board is determined by your opponent's previous move.
140+
The small board you must play in next corresponds to the cell number within a small board where your opponent just played.
141+
Example: If your opponent plays in cell '7' of a small board, your next move must be in the small board numbered '7' on the big board.
142+
143+
5. Sent to a Won Board: If your opponent's move sends you to a small board that has already been won (or is a draw),
144+
you can play in any of the other 8 small boards.
145+
146+
[Enter]
147+
"""
148+
println(rules)
149+
consoleInput()
150+
clearScreen()
87151
}

src/lib.effekt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module src/lib
33
import io/console
44

55
effect WrongInput(msg: String): Unit
6+
effect ExitGame(): Unit
7+
effect PrintRules(): Unit
68

79
type GameMode {
810
Local()
@@ -27,6 +29,7 @@ type WinBoard {
2729

2830
type SmallBoard = List[Cell]
2931
type BigBoard = List[SmallBoard]
32+
3033
record GameBoard(bigBoard: BigBoard, smallCopy: SmallBoard)
3134

3235
val Numbers: List[String] = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]

0 commit comments

Comments
 (0)