Skip to content

Commit

Permalink
Pieces return to game playground on fail to get into the board
Browse files Browse the repository at this point in the history
  • Loading branch information
Lisergishnu committed Oct 10, 2016
1 parent 7d41f7c commit fcc8dda
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions TetraVex/TVGameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class TVGameViewController: NSViewController {
if boardModel!.addPieceToBoard(pv.pieceModel!, x: i, y: j) {
pv.frame.origin.x = CGFloat(i)*pv.frame.width + boardAreaBox.frame.origin.x
pv.frame.origin.y = CGFloat(j)*pv.frame.height + boardAreaBox.frame.origin.y
} else {
pv.frame.origin.x = templatePieceView.frame.origin.x + CGFloat(Int.random(0...100))
pv.frame.origin.y = templatePieceView.frame.origin.y - CGFloat(Int.random(0...100))
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion TetraVexKit/TetraVexBoardModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ open class TetraVexBoardModel {

// MARK: - Properties
open var board : [[PieceModel?]]
open var boardWidth : Int = 0
open var boardHeight : Int = 0
open var startedPlaying : Bool = false

// MARK: - Initializer
public init(width: Int, height: Int) {
board = Array(repeating: Array(repeating: nil, count: height), count: width)
boardWidth = width
boardHeight = height
}

// MARK: - Board manipulation
Expand Down Expand Up @@ -69,7 +73,19 @@ open class TetraVexBoardModel {
return true
}

func isCompleted() -> Bool {
open func removePieceFromBoard(_ piece: PieceModel) -> Bool {
for i in 0..<boardWidth {
for j in 0..<boardHeight {
if board[i][j] != nil && board[i][j]! == piece {
board[i][j] = nil
return true
}
}
}
return false
}

open func isCompleted() -> Bool {
for a in board {
for p in a {
if p == nil {
Expand Down
9 changes: 8 additions & 1 deletion TetraVexKitTests/TetraVexKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ class TetraVexKitTests: XCTestCase {
let c = PieceModel(top: 3, left: 1, bottom: 4, right: 2)
let d = PieceModel(top: 2, left: 2, bottom: 1, right: 1)


let board = TetraVexBoardModel(width: 2, height: 2)
var r = board.addPieceToBoard(a, x: 0, y: 1)
var r = board.removePieceFromBoard(a)
XCTAssert(r == false)
r = board.addPieceToBoard(a, x: 1, y: 1)
XCTAssert(r == true)
r = board.removePieceFromBoard(a)
XCTAssert(r == true)
r = board.addPieceToBoard(a, x: 0, y: 1)
XCTAssert(r == true)
r = board.addPieceToBoard(b, x: 0, y: 1)
XCTAssert(r == false)
Expand Down

0 comments on commit fcc8dda

Please sign in to comment.