Skip to content

Instantly share code, notes, and snippets.

@devnoo
Created January 11, 2012 20:38
Show Gist options
  • Select an option

  • Save devnoo/1596639 to your computer and use it in GitHub Desktop.

Select an option

Save devnoo/1596639 to your computer and use it in GitHub Desktop.
seven languages in seven weeks : scala day 1
package info.denoo.slisw.tictactoe
/**
* A representation of the gameboard.
*/
class Board() {
val cells = Array.fill(3, 3) {
new TicTacToeCell()
}
def printBoard() {
for (line <- cells) {
for (cell <- line){
print(cell.toString)
}
println();
}
}
def noMoreOptions() : Boolean = {
for (line <- cells) {
for (cell <- line){
if (cell.sign == Sign.Blank){
return false
}
}
}
true
}
def checkRows: Option[Status.Status] = {
for (line <- cells) {
println(line)
if (line(0).sign == line(1).sign && line(0).sign == line(2).sign) {
if (line(0).sign != Sign.Blank) {
return Some(winner(line(0).sign))
}
}
}
None
}
def checkColumns: Option[Status.Status] = {
for (i <- 0 to 2) {
if (cells(0)(i).sign == cells(1)(i).sign && cells(0)(i).sign == cells(2)(i).sign) {
if (cells(0)(i).sign != Sign.Blank) {
return Some(winner(cells(0)(i).sign))
}
}
}
None
}
def checkDiagonals: Option[Status.Status] = {
if (cells(0)(0).sign == cells(1)(1).sign && cells(0)(0).sign == cells(2)(2).sign) {
if (cells(0)(0).sign != Sign.Blank) {
return Some(winner(cells(0)(0).sign))
}
}
if (cells(0)(2).sign == cells(1)(1).sign && cells(0)(2).sign == cells(2)(0).sign) {
if (cells(0)(2).sign != Sign.Blank) {
return Some(winner(cells(0)(2).sign))
}
}
None
}
def status() : Status.Status = {
println("in status")
if (noMoreOptions()){
return Status.DRAW
}
checkRows match {
case Some(x) => return x
case None =>
}
checkColumns match {
case Some(x) => return x
case None =>
}
checkDiagonals match {
case Some(x) => return x
case None =>
}
Status.CONTINUE
}
def winner(sign : Sign.Sign) : Status.Status = {
if (sign == Sign.X){
Status.WINNER_X
}else {
Status.WINNER_O
}
}
def placeSign(x : Int, y : Int, sign : Sign.Sign) = {
if (cells(x)(y).sign == Sign.Blank){
cells(x)(y).sign = sign
}
}
}
package info.denoo.slisw.tictactoe
/**
* Representation of the players in the TicTacToe Game.
*/
case class Player(val name :String, val sign:Sign.Sign) {
}
package info.denoo.slisw.tictactoe
/**
* The sign, can be Blank for blank cells, and X or O for the others.
*/
object Sign extends Enumeration{
type Sign = Value
val Blank, X, O = Value
}
package info.denoo.slisw.tictactoe
/**
* The status of the game board.
*/
object Status extends Enumeration {
type Status = Value
val WINNER_X, WINNER_O, CONTINUE, DRAW = Value
}
package info.denoo.slisw.tictactoe
class TicTacToeCell {
var sign = Sign.Blank
override def toString(): String = sign match {
case Sign.Blank => "_"
case Sign.X => "X"
case Sign.O => "O"
}
}
package info.denoo.slisw.tictactoe
/**
* A Simple TicTacToe game for 2 players that ask for input.
*/
object TicTacToeGame {
val player1: Player = new Player("player1", Sign.X)
val player2: Player = new Player("player2", Sign.O)
val players = Array(player1, player2)
def createBoard(): Board = {
new Board()
}
def getInput: Int = {
Console.readInt()
}
def main(args: Array[String]) = {
val board = createBoard();
var playerNr = 0
while (board.status() == Status.CONTINUE) {
println(players(playerNr).name + " your move? (1-9 )")
val move = getInput
val x: Int = (move - 1) / 3
val y: Int = (move - 1) % 3
board.placeSign(x, y, players(playerNr).sign)
playerNr = (playerNr + 1) % 2
board.printBoard()
}
printResult(board)
}
def printResult(board: Board) {
if (board.status() == Status.DRAW) {
println("it is a draw")
} else {
println("The winner is " + winner(board.status()))
}
}
def winner(status: Status.Status): String = {
if (status == Status.WINNER_X) {
player1.name
}else{
player2.name
}
}
}
@erikvanoosten
Copy link
Copy Markdown

The following code

override def toString(): String = {
  if (sign == Sign.Blank) {
    return "_"
  } else if (sign == Sign.X) {
    return "X"
  } else {
    return "O"
  }
}

can be written as:

override def toString(): String = sign match {
  case Sign.Blank => "_"
  case Sign.X => "X"
  case Sign.O => "O"
}

@erikvanoosten
Copy link
Copy Markdown

You can write:

for (line <- cells) {
  for (cell <- line){
    if (cell.sign == Sign.Blank){
      return false
    }
  }
}
true

as:

! (cells exists (_ exists (_.sign == Sign.Blank)))

I can recommend the docs at http://www.scala-lang.org/docu/files/collections-api/collections.html for more tricks like this.

@devnoo
Copy link
Copy Markdown
Author

devnoo commented Feb 28, 2012

thanx erik, I finally updated the code with your tips.
I won't put it the last one, cause at this point in the book it is just the oo part of scala, collections and functional programming will be in the next chapters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment