I am new to Scala and functional programming. I'm creating a tic-tac-toe game (Day 1 of 'Seven Languages in Seven Weeks' (book)) and I'd like to know how to do 'check if won' method in a functional way.
I want to make the 'checkrow' part (first part) like the 'checkcolumn' (second part) part, but what I'm trying is not working.
Here my (working) code:
def checkGame() {
for (y <- board.indices) {
// checks a row
checkRow(y)
}
for (x <- board(0).indices) {
// checks a column
if(!board.exists(y => y(x) != 'x')){
println("You have won mate! (column: " + x + ")")
}
}
}
def checkRow(y: Integer) {
var isWon = true
for (x <- board(y).indices) {
if (board(y)(x) != 'x') {
isWon = false
}
}
if (isWon) println("You have won mate! (row: " + y + ")")
}
Note: board is a 2 dimensional array.
What I got so far (doesn't work):
if(!board.exists(x => x(y) != 'x')){
println("You have won mate! (row: " + x + ")")
}
forlopps and variable mutations.