Skip to content

Commit

Permalink
refs #35 2012/02/09 nightly.
Browse files Browse the repository at this point in the history
  • Loading branch information
alphaneet committed Feb 9, 2012
1 parent 6ad1be6 commit 611fece
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 45 deletions.
42 changes: 21 additions & 21 deletions models/src/main/scala/Board.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.github.alphaneet.suparobo

case class BoardValue(id: Int, symbol: Symbol, rangeCost: Int) {
val isMove: Boolean = (rangeCost > 0)
}

object Board {
val FLAT = BoardValue(1, 'ft, 1)
val WOOD = BoardValue(3, 'wd, 2)
val HILL = BoardValue(4, 'hl, 3)
val MOUNT = BoardValue(5, 'mt, 0)

val statuses: List[BoardValue] =
sealed case class Status(id: Int, symbol: Symbol, rangeCost: Int) {
val isMove: Boolean = (rangeCost > 0)
}

object FLAT extends Status(1, 'ft, 1)
object WOOD extends Status(3, 'wd, 2)
object HILL extends Status(4, 'hl, 3)
object MOUNT extends Status(5, 'mt, 0)

val statuses: List[Status] =
FLAT ::
WOOD ::
HILL ::
Expand All @@ -37,10 +37,10 @@ class Board(
val width: Int,
val height: Int,
val name: String = "",
defaultData: Seq[BoardValue] = Nil
defaultData: Seq[Board.Status] = Nil
) extends NotNull {
val size = width * height
val data: Array[BoardValue] = if (defaultData.isEmpty || defaultData.size != size) {
val data: Array[Board.Status] = if (defaultData.size != size) {
Array.fill(size)(Board.FLAT)
} else {
defaultData.toArray
Expand All @@ -50,25 +50,25 @@ class Board(
def index(pos: Position): Int = index(pos.x, pos.y)
def index(x: Int, y: Int): Int = x + y * width

def apply(pos: Position): BoardValue = apply(pos.x, pos.y)
def apply(x: Int, y: Int): BoardValue = {
def apply(pos: Position): Board.Status = apply(pos.x, pos.y)
def apply(x: Int, y: Int): Board.Status = {
checkRectangle(x, y)
apply(index(x, y))
}
def apply(index: Int): BoardValue = try {
def apply(index: Int): Board.Status = try {
data(index)
} catch {
case _: ArrayIndexOutOfBoundsException =>
throw new OutsideBoardException("index: " + index)
}

def update(pos: Position, value: BoardValue) { update(pos.x, pos.y, value) }
def update(x: Int, y: Int, value: BoardValue) {
def update(pos: Position, status: Board.Status) { update(pos.x, pos.y, status) }
def update(x: Int, y: Int, status: Board.Status) {
checkRectangle(x, y)
update(index(x, y), value)
update(index(x, y), status)
}
def update(index: Int, value: BoardValue): Unit = try {
data(index) = value
def update(index: Int, status: Board.Status): Unit = try {
data(index) = status
} catch {
case _: ArrayIndexOutOfBoundsException =>
throw new OutsideBoardException("index: " + index)
Expand All @@ -83,7 +83,7 @@ class Board(
def checkRectangle(x: Int, y: Int) { checkWidth(x); checkHeight(y) }
def checkRectangle(pos: Position) { checkRectangle(pos.x, pos.y) }

def foreach(f: Pair[Position, BoardValue] => Unit) {
def foreach(f: Pair[Position, Board.Status] => Unit) {
data.iterator.zipWithIndex foreach {
case (status, index) =>
f(pos(index), status)
Expand Down
55 changes: 49 additions & 6 deletions models/src/main/scala/Game.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
package com.github.alphaneet.suparobo

case class GameMaker(
inside: Option[Player] = None,
outside: Option[Player] = None,
board: Option[Board] = None
) {
def createGame() = Game(
inside getOrElse (throw new NoSuchInsidePlayerException),
outside getOrElse (throw new NoSuchOutsidePlayerException),
board getOrElse (throw new NoSuchBoardException)
)
}

object Game {
sealed case class Status(id: Int) extends NotNull
object SETUP extends Status(1)
object PLAY extends Status(2)
object END extends Status(3)
}

// TK: GameReady とかの Option 用のクラスを用意すべきかも
// GameDeliver という名前を思いついた
case class Game(
self: Option[Player] = None,
other: Option[Player] = None,
board: Option[Board] = None
) extends NotNull
inside: Player,
outside: Player,
board: Board
) extends NotNull {
import Game._
private var _status: Status = SETUP
private def status_=(status: Status) { _status = status }
def status = _status

def isSetup = status == SETUP
def isPlay = status == PLAY
def isEnd = status == END

def gotoSetup() {}
def gotoPlay() {
if (isComplateSetup) {
status = PLAY
} else {
throw new IllegalGotoGameStatusException(status + " -> " + PLAY)
}
}
def gotoEnd() {}

def isComplateSetup: Boolean = {
if(!isSetup) return false


true
}
}

12 changes: 12 additions & 0 deletions models/src/main/scala/exceptions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ class NoSuchChampionException(msg: String = "") extends Exception(msg)

// コストオーバーの場合
class OverCostException(msg: String = "") extends Exception(msg)

// 手前のプレイヤーが選択されていない場合
class NoSuchInsidePlayerException(msg: String = "") extends Exception(msg)

// 奥のプレイヤーが選択されていない場合
class NoSuchOutsidePlayerException(msg: String = "") extends Exception(msg)

// ステージが選択されていない場合
class NoSuchBoardException(msg: String = "") extends Exception(msg)

//
class IllegalGotoGameStatusException(msg: String = "") extends Exception(msg)
4 changes: 2 additions & 2 deletions models/src/test/scala/BoardSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class BoardSuite extends FunSuite with ShouldMatchers {
class Fixture(
val width: Int,
val height: Int,
val data: Seq[BoardValue] = Nil
val data: Seq[Board.Status] = Nil
) extends BoardFixture {
implicit val board = new Board(width, height, defaultData = data)
}
Expand All @@ -22,7 +22,7 @@ class BoardSuite extends FunSuite with ShouldMatchers {

test("defaultData.size と #size が違う場合は FLAT で初期化される") {
new BoardFixture {
val data: Array[BoardValue] = Array(
val data: Array[Board.Status] = Array(
WOOD, HILL,
MOUNT, WOOD,
HILL, FLAT
Expand Down
4 changes: 4 additions & 0 deletions models/src/test/scala/GameSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.alphaneet.suparobo

class GameSuite extends FunSuite with ShouldMatchers {
}
4 changes: 2 additions & 2 deletions processing2D/BoardSelectScene.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.alphaneet.suparobo

case class BoardSelectScene(
game: Game = Game()
gameMaker: GameMaker = GameMaker()
)(implicit
applet: SPApplet,
i18n: I18N
Expand Down Expand Up @@ -81,7 +81,7 @@ case class BoardSelectScene(
return
}

DeckSelectScene(game.copy(board = selectBoard.map(_.board)))
DeckSelectScene(gameMaker.copy(board = selectBoard.map(_.board)))
}

def back() {
Expand Down
22 changes: 16 additions & 6 deletions processing2D/DeckSelectScene.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.alphaneet.suparobo

case class DeckSelectScene(
game: Game = Game()
gameMaker: GameMaker = GameMaker()
)(implicit
applet: SPApplet,
i18n: I18N
Expand All @@ -18,12 +18,12 @@ case class DeckSelectScene(
)

val boardNameLabel = createLabel(
game.board.map(_.name).getOrElse(""),
gameMaker.board.map(_.name).getOrElse(""),
'boardName,
size = 20
)

val boardViewer: Option[Sprite] = game.board.map {
val boardViewer: Option[Sprite] = gameMaker.board.map {
createBoardSprite(
_,
layout.rect('viewer),
Expand All @@ -37,15 +37,25 @@ case class DeckSelectScene(

def back() {
dialog.confirm(t("BoardSelectScene.back")) {
BoardSelectScene(game)
BoardSelectScene(gameMaker)
}
}

def entry() {
dialog.confirm(nowDeckName + " " + t("DeckSelectScene.entryMessage")) {
save {
val self = Option(Player(nowDeck))
GameScene(game.copy(self = self))
try {
val self = Option(Player(nowDeck))
val game = gameMaker.copy(inside = self).createGame()
GameScene(game)
} catch {
case _: NoSuchInsidePlayerException =>
dialog.message(t("NoSuchSelfPlayerException"), DeckSelectScene(gameMaker))
case _: NoSuchOutsidePlayerException =>
dialog.message(t("NoSuchOtherPlayerException"), TitleScene())
case _: NoSuchBoardException =>
dialog.message(t("NoSuchBoardException"), BoardSelectScene(gameMaker))
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion processing2D/GameScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ case class GameScene(
// implicit val layout = new LayoutXML(LAYOUTS_PATH + "BoardSelectScene.xml")
implicit val gg = new GraphicsGenerator(applet)

val board = game.board.get // TK: ちょい適当 @ で直す
val board = game.board
object boardViewer {
val width = applet.width
val height = width * (board.height / board.width)
Expand Down
4 changes: 2 additions & 2 deletions processing2D/TitleScene.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ case class TitleScene(implicit applet: SPApplet, i18n: I18N) extends Scene(apple
)

def start() {
val other = Option(Player(createDeck.random(champions, minions)))
BoardSelectScene(Game(other = other))
val enemy = Option(Player(createDeck.random(champions, minions)))
BoardSelectScene(GameMaker(outside = enemy))
}

def network() { println("未実装") }
Expand Down
3 changes: 3 additions & 0 deletions processing2D/data/locales/ja.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@

<NoSuchChampionException>チャンピオンを選択してください</NoSuchChampionException>
<OverCostException>コストオーバーです</OverCostException>
<NoSuchSelfPlayerException>手前のプレイヤーが選択されません</NoSuchSelfPlayerException>
<NoSuchOtherPlayerException>奥のプレイヤーが選択されてません</NoSuchOtherPlayerException>
<NoSuchBoardException>ステージが選択されてません</NoSuchBoardException>

<TitleScene>
<start>ステージ選択</start>
Expand Down
14 changes: 9 additions & 5 deletions processing2D/main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ object Main extends SPApplet {
// TitleScene()(this, i18n)

// TK: test
def create = Option(new Player(createDeck.random(champions, minions)))
def create = new Player(createDeck.random(champions, minions))
GameScene(
Game(
self = create,
other = create,
inside = create,
outside = create,
// board = Option(new Board(10, 20))
board = Option(Board.loadXML(BOARDS_PATH + "stage3.xml"))
board = Board.loadXML(BOARDS_PATH + "stage3.xml")
)
)(this, i18n)
}
Expand Down Expand Up @@ -98,8 +98,12 @@ abstract class Dialog(val applet: SPApplet) {
mode = Option(Confirm)
}

def message(body: String) {
def message(body: String, action: => Unit = {}) {
isOpen = true
Message.ok.action {
isOpen = false
action
}
Message.body = body
mode = Option(Message)
}
Expand Down

0 comments on commit 611fece

Please sign in to comment.