Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions src/Generate.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ def deactivateSmallBoard(bigBoards: BigBoards, number: Int): BigBoards = {
with on[OutOfBounds].panic

var newBigBoard: BigBoard = bigBoards.bigBoard
var newBigBoardSmallCopy: SmallBoard = bigBoards.smallCopy

var newSmallBoard: SmallBoard = newBigBoard.get(number - 1)
// if(newSmallBoard.get(number - 1) is Filled(Player)){
// println("Oh, fuck!")
// do readLine()
// ()
// }else {
// newBigBoardSmallCopy = newBigBoardSmallCopy.replace(number - 1, Empty())
// }
var counter: Int = 0


Expand All @@ -59,7 +68,7 @@ def deactivateSmallBoard(bigBoards: BigBoards, number: Int): BigBoards = {
()
}
newBigBoard = newBigBoard.replace(number - 1, newSmallBoard)
BigBoards(newBigBoard, bigBoards.smallCopy)
BigBoards(newBigBoard, newBigBoardSmallCopy)
}

def checkNewCell(bigBoards: BigBoards, smallBoardNumber: Int, cellNumber: Int, player: Player): BigBoards = {
Expand All @@ -79,11 +88,16 @@ def checkNewCell(bigBoards: BigBoards, smallBoardNumber: Int, cellNumber: Int, p
* Output : Boolean
*/
def checkAvailableSmallBoard(bigBoards: BigBoards, cellNumber: Int): Bool = {
with console
with on[OutOfBounds].panic

val cellType: Cell = bigBoards.smallCopy.get(cellNumber - 1)
if (cellType is Empty()) true
else false

cellType match {
case Empty() => true
case Active() => true
case _ => false
}
}


Expand All @@ -106,24 +120,38 @@ def checkAvailableCell(bigBoards: BigBoards, smallBoardNumber: Int, cellNumber:
/*
* Returns true if Current Player has won in this Small Board.
* False Otherwise
* Input : BigBoards, SmallBoardNumber, Cell Number
* Input : SmallBoardNumber, player
* Output : Boolean
*/
def checkWinSituation(smallBoard: SmallBoard, player: Player): Bool = {
var checkedCells: List[Int] = empty()
var counter: Int = 0
var win: Bool = true
var win: Bool = false
smallBoard.foreach {cell =>
cell match {
case Filled(player) => checkedCells = checkedCells.insert(checkedCells.size(), counter)
case _ => ()
}
counter = counter + 1
}
counter = 0
// Here we are checking if all three elements from the win combination are in the
// Checked cells list
winCombinations.foreach { combination =>
if(combination.compare(checkedCells)){
win = true
combination.foreach { el =>
if(any(checkedCells) {cell => cell == el}) counter = counter + 1
}
if (counter == 3) win = true
else counter = 0
}
win
}

def fillWinningGameBoard(bigBoards: BigBoards, smallBoardNumber: Int, player: Player): BigBoards = {
var newBigBoard: BigBoard = bigBoards.bigBoard
var newBigBoardSmallCopy: SmallBoard = bigBoards.smallCopy

newBigBoard = newBigBoard.replace(smallBoardNumber - 1, fill(9, Filled(player)))
newBigBoardSmallCopy = newBigBoardSmallCopy.replace(smallBoardNumber - 1, Filled(player))
BigBoards(newBigBoard, newBigBoardSmallCopy)
}
31 changes: 17 additions & 14 deletions src/Stupid.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import src/Generate


def playGame(): Unit = {
with console

def chooseSide(): Player / {WrongInput} = {
println("Choose the side[X, O]:")
val side = do readLine()
val side = consoleInput()
side match {
case "X" => Cross()
case "O" => Nought()
Expand All @@ -26,16 +25,15 @@ def playGame(): Unit = {

def chooseSmallBoard(bigBoards: BigBoards): Int / {WrongInput} = {
println("Choose the Small Board[1 - 9]:")
val smallBoardNumber: String = do readLine()
val smallBoardNumber: String = consoleInput()

if (not(any(Numbers) {s => s == smallBoardNumber})) {
do WrongInput("Invalid input. Please type a number from 1 to 9.")
chooseSmallBoard(bigBoards)
}
else {
with on[WrongFormat].panic
val smallBoardNumber: Int = toInt(smallBoardNumber)

if (checkAvailableSmallBoard(bigBoards, smallBoardNumber)){
smallBoardNumber
}else {
Expand All @@ -47,7 +45,7 @@ def playGame(): Unit = {

def chooseCell(bigBoards: BigBoards, smallBoardNumber: Int): Int / {WrongInput} = {
println("Choose the cell inside your Small Board")
val cellNumber: String = do readLine()
val cellNumber: String = consoleInput()
if (not(any(Numbers) {s => s == cellNumber})) {
do WrongInput("Invalid input. Please type a number from 1 to 9.")
chooseCell(bigBoards, smallBoardNumber)
Expand All @@ -65,7 +63,6 @@ def playGame(): Unit = {
}

def gameLoop(player: Player): Unit / {WrongInput} = {

var endGame: Bool = false

var gameBoard: BigBoards = generateNewBigBoard()
Expand All @@ -80,20 +77,16 @@ def playGame(): Unit = {
println("You're starting the game!\nChoose the starting Small Board.")
currentSmallBoard = chooseSmallBoard(gameBoard)
gameBoard = newActiveSmallBoard(gameBoard, currentSmallBoard)
printGameScreen(gameBoard, player)
()
} else {
println("You're starting the game!\nChoose the starting Small Board.")
currentSmallBoard = chooseSmallBoard(gameBoard)
gameBoard = newActiveSmallBoard(gameBoard, currentSmallBoard)
printGameScreen(gameBoard, player)
}

// INNER GAME LOOP OF ONE GAME
while (not(endGame)){


if (currentSmallBoard == 10) {
printGameScreen(gameBoard, currentPlayer)
currentSmallBoard = chooseSmallBoard(gameBoard)
gameBoard = newActiveSmallBoard(gameBoard, currentSmallBoard)
}
Expand All @@ -103,10 +96,19 @@ def playGame(): Unit = {
currentCell = chooseCell(gameBoard, currentSmallBoard)
gameBoard = checkNewCell(gameBoard, currentSmallBoard, currentCell, currentPlayer)

// Check the winning situation
with on[OutOfBounds].panic
if (checkWinSituation(gameBoard.bigBoard.get(currentSmallBoard - 1), player)){
println("\nYou won the Small Board![Enter]")
consoleInput()
gameBoard = fillWinningGameBoard(gameBoard, currentSmallBoard, currentPlayer)
}

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

consoleInput()

// Change The Current Player
if(currentPlayer is Cross()) currentPlayer = Nought()
Expand All @@ -120,6 +122,7 @@ def playGame(): Unit = {
currentCell = 10
} else {
gameBoard = deactivateSmallBoard(gameBoard, currentCell)
// currentSmallBoard = 10
}
()
}
Expand All @@ -133,7 +136,7 @@ def playGame(): Unit = {
println("Welcome to the ULTIMATE TIC-TAC-TOE!")
println("Press enter to start the game.")

do readLine()
consoleInput()

var play: Bool = true

Expand Down
7 changes: 7 additions & 0 deletions src/lib.effekt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module src/lib

import io/console

effect WrongInput(msg: String): Unit


Expand Down Expand Up @@ -67,4 +69,9 @@ def compare(list1: List[Int], list2: List[Int]): Bool = {
}else{
false
}
}

def consoleInput(): String = {
with console
do readLine()
}
Loading