Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring test code and pulling console redirection out into a trait
- Loading branch information
Showing
25 changed files
with
502 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,82 @@ | ||
package trptcolin.tictactoescala.basegame | ||
|
||
object Board{ | ||
object Board | ||
{ | ||
val winSets = | ||
List( | ||
List(0, 1, 2), | ||
List(3, 4, 5), | ||
List(6, 7, 8), | ||
List(0, 3, 6), | ||
List(1, 4, 7), | ||
List(2, 5, 8), | ||
List(0, 4, 8), | ||
List(2, 4, 6)) | ||
List( | ||
List(0, 1, 2), | ||
List(3, 4, 5), | ||
List(6, 7, 8), | ||
List(0, 3, 6), | ||
List(1, 4, 7), | ||
List(2, 5, 8), | ||
List(0, 4, 8), | ||
List(2, 4, 6)) | ||
|
||
} | ||
|
||
trait Board{ | ||
def full(): Boolean ={ | ||
trait Board | ||
{ | ||
def positions: List[String] | ||
def clear(): Board | ||
def move(mark: String, position: Int): Board | ||
|
||
def full(): Boolean = | ||
{ | ||
return !positions.exists(_ == null) | ||
} | ||
|
||
def openPositions(): List[Int] ={ | ||
def openPositions(): List[Int] = | ||
{ | ||
if (full()) | ||
{ | ||
return List() | ||
} | ||
else{ | ||
val positionsList = | ||
for { | ||
for | ||
{ | ||
position <- (0 to 8).toList | ||
if positions(position) == null | ||
} yield position | ||
} | ||
yield position | ||
|
||
return positionsList | ||
} | ||
} | ||
|
||
def won(): Boolean ={ | ||
def won(): Boolean = | ||
{ | ||
return wonBy("X") || wonBy("O") | ||
} | ||
|
||
def wonBy(mark: String): Boolean ={ | ||
def wonBy(mark: String): Boolean = | ||
{ | ||
return Board.winSets.exists(winSet => winSet.forall(positions(_) == mark)) | ||
} | ||
|
||
def over(): Boolean = { | ||
def over(): Boolean = | ||
{ | ||
return full() || won() | ||
} | ||
|
||
def positions: List[String] | ||
def clear(): Board | ||
|
||
def move(mark: String, position: Int): Board | ||
|
||
override def toString: String ={ | ||
override def toString: String = | ||
{ | ||
return positions.toString | ||
} | ||
|
||
def newPositions(mark: String, position: Int): List[String] ={ | ||
def newPositions(mark: String, position: Int): List[String] = | ||
{ | ||
val newPositions = new Array[String](9) | ||
positions.copyToArray(newPositions, 0) | ||
|
||
newPositions(position) = mark | ||
return newPositions.toList | ||
} | ||
|
||
def invalidMove(position: Int): Boolean ={ | ||
def invalidMove(position: Int): Boolean = | ||
{ | ||
return position < 0 || position > 8 || positions(position) != null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
package trptcolin.tictactoescala.basegame | ||
|
||
class BoardImpl(val positions: List[String]) extends Board { | ||
def this() ={ | ||
class BoardImpl(val positions: List[String]) extends Board | ||
{ | ||
def this() = | ||
{ | ||
this (List[String]( | ||
null, null, null, | ||
null, null, null, | ||
null, null, null)) | ||
} | ||
|
||
def move(mark: String, position: Int): Board ={ | ||
def move(mark: String, position: Int): Board = | ||
{ | ||
return new BoardImpl(newPositions(mark, position)) | ||
} | ||
|
||
def clear(): Board = { | ||
def clear(): Board = | ||
{ | ||
new BoardImpl() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.