patternMinor
Scala Tic Tac Toe Game
Viewed 0 times
toetictacscalagame
Problem
This is my first Scala game. I would love some feedback on my coding style, or your brief input on how you would do it.
```
object tictactoe extends App {
def tttFormat(board: Array[Char]): String =
"|" + board(0) + "|" + board(1) + "|" + board(2) + "|\n" +
"|" + board(3) + "|" + board(4) + "|" + board(5) + "|\n" +
"|" + board(6) + "|" + board(7) + "|" + board(8) + "|\n"
println("Enter the number of the square you want to occupy!\n"+
tttFormat(GameObj.board))
while(GameObj.atPlay) {
println(tttFormat(GameObj.updatedStateArray))
}
GameObj.nextTurn
println("game over! " + GameObj.nextTurn + "'s win!")
}
object GameObj {
val board: Array[Char] = Array('1','2','3',
'6','5','4',
'7','8','9')
var whosTurn = false
def nextTurn: Char = {
whosTurn = !whosTurn;
if (whosTurn) 'X' else 'O'
}
def atPlay: Boolean =
!(board(0) == 'X' && board(1) == 'X' && board(2) == 'X') &&
!(board(3) == 'X' && board(4) == 'X' && board(5) == 'X') &&
!(board(6) == 'X' && board(7) == 'X' && board(8) == 'X') &&
!(board(0) == 'X' && board(4) == 'X' && board(8) == 'X') &&
!(board(6) == 'X' && board(4) == 'X' && board(2) == 'X') &&
!(board(0) == 'X' && board(3) == 'X' && board(6) == 'X') &&
!(board(1) == 'X' && board(4) == 'X' && board(7) == 'X') &&
!(board(2) == 'X' && board(5) == 'X' && board(8) == 'X') &&
!(board(0) == 'O' && board(1) == 'O' && board(2) == 'O') &&
!(board(3) == 'O' && board(4) == 'O' && board(5) == 'O') &&
!(board(6) == 'O' && board(7) == 'O' && board(8) == 'O') &&
!(board(0) == 'O' && board(4) == 'O' && board(8) == 'O') &&
!(board(6) == 'O' && board(4) == 'O' && board(2) == 'O') &&
!(board(0) == 'O' && board(3) == 'O' && board(6) == 'O') &&
!(board(1) == 'O' && board(4) == 'O' && board(7) == 'O') &&
!(board(2) == 'O' && board(5) == 'O' && board(8) == 'O')
def updatedStateArray: Array[Char] = {
```
object tictactoe extends App {
def tttFormat(board: Array[Char]): String =
"|" + board(0) + "|" + board(1) + "|" + board(2) + "|\n" +
"|" + board(3) + "|" + board(4) + "|" + board(5) + "|\n" +
"|" + board(6) + "|" + board(7) + "|" + board(8) + "|\n"
println("Enter the number of the square you want to occupy!\n"+
tttFormat(GameObj.board))
while(GameObj.atPlay) {
println(tttFormat(GameObj.updatedStateArray))
}
GameObj.nextTurn
println("game over! " + GameObj.nextTurn + "'s win!")
}
object GameObj {
val board: Array[Char] = Array('1','2','3',
'6','5','4',
'7','8','9')
var whosTurn = false
def nextTurn: Char = {
whosTurn = !whosTurn;
if (whosTurn) 'X' else 'O'
}
def atPlay: Boolean =
!(board(0) == 'X' && board(1) == 'X' && board(2) == 'X') &&
!(board(3) == 'X' && board(4) == 'X' && board(5) == 'X') &&
!(board(6) == 'X' && board(7) == 'X' && board(8) == 'X') &&
!(board(0) == 'X' && board(4) == 'X' && board(8) == 'X') &&
!(board(6) == 'X' && board(4) == 'X' && board(2) == 'X') &&
!(board(0) == 'X' && board(3) == 'X' && board(6) == 'X') &&
!(board(1) == 'X' && board(4) == 'X' && board(7) == 'X') &&
!(board(2) == 'X' && board(5) == 'X' && board(8) == 'X') &&
!(board(0) == 'O' && board(1) == 'O' && board(2) == 'O') &&
!(board(3) == 'O' && board(4) == 'O' && board(5) == 'O') &&
!(board(6) == 'O' && board(7) == 'O' && board(8) == 'O') &&
!(board(0) == 'O' && board(4) == 'O' && board(8) == 'O') &&
!(board(6) == 'O' && board(4) == 'O' && board(2) == 'O') &&
!(board(0) == 'O' && board(3) == 'O' && board(6) == 'O') &&
!(board(1) == 'O' && board(4) == 'O' && board(7) == 'O') &&
!(board(2) == 'O' && board(5) == 'O' && board(8) == 'O')
def updatedStateArray: Array[Char] = {
Solution
-
Whether Scala or any other language, it's not a good sign when you have big blocks made of pasted/copied lines (tttFormat, board and atPlay). There is some logical structure to those repeated lines and you should code that instead of copying lines.
-
There is a bug: the middle row is inverted (654 instead of 456).
-
The line asking for the player move should probably be in the while-loop. Just my opinion.
-
To keep good "separation of concerns", the player input should be read in the main loop and passed to the method that updates the board.
-
The nextTurn followed by nextTurn at the end to get the winner is "hacky".
Whether Scala or any other language, it's not a good sign when you have big blocks made of pasted/copied lines (tttFormat, board and atPlay). There is some logical structure to those repeated lines and you should code that instead of copying lines.
-
There is a bug: the middle row is inverted (654 instead of 456).
-
The line asking for the player move should probably be in the while-loop. Just my opinion.
-
To keep good "separation of concerns", the player input should be read in the main loop and passed to the method that updates the board.
-
The nextTurn followed by nextTurn at the end to get the winner is "hacky".
Context
StackExchange Code Review Q#54034, answer score: 2
Revisions (0)
No revisions yet.