Skip to content

Commit 554d9d3

Browse files
dzianis-sudkoudzianis-sudkou
authored andcommitted
Finished the logic for the Computer Play. Now it's crashing again.
1 parent a7b7a97 commit 554d9d3

File tree

3 files changed

+17
-81
lines changed

3 files changed

+17
-81
lines changed

src/Generate.effekt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ def fillWinningGameBoard(gameBoard: BigBoards, smallBoardNumber: Int, player: Ce
152152
}
153153

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

157159
var newSmallBoard: SmallBoard = smallBoard
@@ -214,7 +216,7 @@ def isComputerTurn(currentPlayer: Cell, computer: Cell): Bool = {
214216
var pcCross: Bool = false
215217
var returnValue: Bool = false
216218
if (computer is Cross()) pcCross = true
217-
if (currentPlayer is Cross()) if (pcCross) returnValue = true
218-
if (currentPlayer is Nought()) if (not(pcCross)) returnValue = true
219-
false
219+
if (currentPlayer is Cross() and pcCross) returnValue = true
220+
if (currentPlayer is Nought() and not(pcCross)) returnValue = true
221+
returnValue
220222
}

src/Stupid.effekt

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def playGame(): Unit = {
1313

1414
// Returns the chosen Game Mode
1515
def chooseGameMode(): GameMode / {WrongInput} = {
16-
println("Choose the Game Mode:\n[1 - 2 Players, 2 - vs Computer]:")
16+
println("Choose the Game Mode:\n[1 - Local, 2 - vs Computer]:")
1717
val side = consoleInput()
1818
side match {
1919
case "1" => Local()
@@ -100,7 +100,7 @@ def playGame(): Unit = {
100100
currentSmallBoard = chooseSmallBoard(gameBoard)
101101
gameBoard = newActiveSmallBoard(gameBoard, currentSmallBoard)
102102
} else {
103-
println("Opponent is starting the game!\nChoose the starting Small Board.")
103+
println("Opponent is starting the game!\n")
104104
if (gameMode is Computer()){
105105
currentSmallBoard = computerPlay(gameBoard.smallCopy, computer)
106106
gameBoard = newActiveSmallBoard(gameBoard, currentSmallBoard)
@@ -112,23 +112,21 @@ def playGame(): Unit = {
112112

113113
// INNER GAME LOOP OF ONE GAME
114114
while (not(endGame)){
115+
with on[OutOfBounds].panic
116+
117+
// Choose the new Small Board
115118
if (currentSmallBoard == 10) {
116-
if (isComputerTurn(currentPlayer, computer)){
117-
currentSmallBoard = computerPlay(gameBoard.smallCopy, computer)
118-
gameBoard = newActiveSmallBoard(gameBoard, currentSmallBoard)
119-
}
120-
printGameScreen(gameBoard, currentPlayer)
121-
currentSmallBoard = chooseSmallBoard(gameBoard)
119+
if (gameMode is Computer()) if (isComputerTurn(currentPlayer, computer)) currentSmallBoard = computerPlay(gameBoard.smallCopy, computer)
120+
else currentSmallBoard = chooseSmallBoard(gameBoard)
122121
gameBoard = newActiveSmallBoard(gameBoard, currentSmallBoard)
123122
}
124123

125-
// Before The Player's Move
124+
// The Move Phase
126125
printGameScreen(gameBoard, currentPlayer)
127-
currentCell = chooseCell(gameBoard, currentSmallBoard)
128-
gameBoard = checkNewCell(gameBoard, currentSmallBoard, currentCell, currentPlayer)
126+
if(gameMode is Computer()) if (isComputerTurn(currentPlayer, computer)) currentCell = computerPlay(gameBoard.bigBoard.get(currentSmallBoard), computer)
127+
else currentCell = chooseCell(gameBoard, currentSmallBoard)
129128

130-
// Check the winning situation
131-
with on[OutOfBounds].panic
129+
gameBoard = checkNewCell(gameBoard, currentSmallBoard, currentCell, currentPlayer)
132130
checkWinSituation(gameBoard.bigBoard.get(currentSmallBoard - 1), currentPlayer) match {
133131
case Win() => {
134132
println("\nYou won the Small Board!")
@@ -146,19 +144,18 @@ def playGame(): Unit = {
146144
// After The Player's Move
147145
printGameScreen(gameBoard, currentPlayer)
148146
println("Excellent Move! Now Press the Enter to finish your turn.")
149-
150147
consoleInput()
151148

152149
// Change the Current Small Board
153150
if (checkAvailableCell(gameBoard.smallCopy, currentCell)){
154151
gameBoard = deactivateSmallBoard(gameBoard, currentSmallBoard)
155152
currentSmallBoard = currentCell
156153
gameBoard = newActiveSmallBoard(gameBoard, currentSmallBoard)
157-
currentCell = 10
158154
} else {
159155
gameBoard = deactivateSmallBoard(gameBoard, currentSmallBoard)
160156
currentSmallBoard = 10
161157
}
158+
currentCell = 10
162159

163160
// Verify if the game has been won
164161
if (checkWinSituation(gameBoard.smallCopy, currentPlayer) is Win()){
@@ -218,7 +215,6 @@ def playGame(): Unit = {
218215
println(msg)
219216
resume(())
220217
}
221-
()
222218
}
223219
println("Goodbye!")
224220
}

src/game.effekt

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)