Created
November 17, 2014 13:11
-
-
Save Pastez/e8456703bb0dc6c26fba to your computer and use it in GitHub Desktop.
Battleships - bad assumptions :)
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
import UIKit | |
//typealias GridSize = (w:Int, h:Int) | |
struct GridSize { | |
let w:Int | |
let h:Int | |
init(_ width:Int,_ height:Int) | |
{ | |
w = width | |
h = height | |
} | |
func area() -> Int | |
{ | |
return w * h | |
} | |
} | |
enum GameState | |
{ | |
case PlacingShips | |
case War | |
} | |
enum CellPlacingState | |
{ | |
case Empty | |
case Ship | |
case Lock(Int) | |
} | |
enum CellWarState | |
{ | |
case Empty, Ship, Miss, Hit | |
} | |
class Cell | |
{ | |
var placingState:CellPlacingState = CellPlacingState.Empty | |
{ | |
didSet | |
{ | |
switch placingState | |
{ | |
case .Lock(let x): | |
if x == 0 { | |
placingState = CellPlacingState.Empty | |
} | |
default: | |
break | |
} | |
} | |
} | |
var warState = CellWarState.Empty | |
unowned let grid:GameGrid | |
init(grid:GameGrid) | |
{ | |
self.grid = grid | |
} | |
} | |
class GameGrid { | |
var state = GameState.PlacingShips | |
var grid:[Cell]! | |
let size:GridSize | |
init(size:GridSize) | |
{ | |
self.size = size | |
grid = [Cell]() | |
for i in 0..<size.area() | |
{ | |
grid.append(Cell(grid: self)) | |
} | |
} | |
subscript(x:Int,y:Int) -> Cell? | |
{ | |
get { | |
if let idx = indexOfCellAt(x,y) { | |
return grid[idx] | |
} | |
return nil | |
} | |
set { | |
if let idx = indexOfCellAt(x,y) { | |
grid[idx] = newValue! | |
} | |
} | |
} | |
func indexOfCellAt(x:Int,_ y:Int) -> Int? | |
{ | |
if x >= 0 && x < size.w && y >= 0 && y < size.h | |
{ | |
return x + y * size.w | |
} | |
return nil | |
} | |
} | |
// GameGrid Placing | |
extension GameGrid | |
{ | |
func placeShipAt(x:Int,_ y:Int) -> Bool | |
{ | |
if let cell = canPlaceShipAt(x,y) | |
{ | |
cell.placingState = CellPlacingState.Ship | |
lockCellAt(x-1, y-1) | |
lockCellAt(x+1, y-1) | |
lockCellAt(x+1, y+1) | |
lockCellAt(x-1, y+1) | |
return true | |
} | |
return false | |
} | |
func removeShipAt(x:Int,_ y:Int) -> Bool | |
{ | |
if let cell = canRemoveShipAt(x,y) | |
{ | |
cell.placingState = CellPlacingState.Empty | |
unlockCellAt(x-1, y-1) | |
unlockCellAt(x+1, y-1) | |
unlockCellAt(x+1, y+1) | |
unlockCellAt(x-1, y+1) | |
} | |
return false | |
} | |
func canPlaceShipAt(x:Int,_ y:Int) -> Cell? | |
{ | |
if let cell = self[x,y] | |
{ | |
switch cell.placingState | |
{ | |
case .Empty: | |
return cell | |
default: | |
return nil | |
} | |
} | |
return nil | |
} | |
func canRemoveShipAt(x:Int,_ y:Int) -> Cell? | |
{ | |
if let cell = self[x,y] | |
{ | |
switch cell.placingState | |
{ | |
case .Ship: | |
return cell | |
default: | |
return nil | |
} | |
} | |
return nil | |
} | |
func lockCellAt(x:Int,_ y:Int) -> Bool | |
{ | |
if let cell = self[x,y] | |
{ | |
switch cell.placingState | |
{ | |
case .Lock(let retainCount): | |
cell.placingState = CellPlacingState.Lock(retainCount+1) | |
default: | |
cell.placingState = CellPlacingState.Lock(1) | |
} | |
return true | |
} | |
return false | |
} | |
func unlockCellAt(x:Int,_ y:Int) -> Bool | |
{ | |
if let cell = self[x,y] | |
{ | |
switch cell.placingState | |
{ | |
case .Lock(let retainCount): | |
if retainCount - 1 == 0 | |
{ | |
cell.placingState = CellPlacingState.Lock(retainCount-1) | |
} | |
else | |
{ | |
cell.placingState = CellPlacingState.Lock(retainCount-1) | |
} | |
default: | |
return true | |
} | |
} | |
return false | |
} | |
} | |
// GameGrid UTILS | |
extension GameGrid | |
{ | |
func gridDebugString() -> String! | |
{ | |
var str = " " | |
for x in 0..<self.size.w | |
{ | |
str += " \(x) " | |
} | |
str += "\n" | |
for y in 0..<self.size.h { | |
str += " \(y) " | |
for x in 0..<self.size.w | |
{ | |
if let cell = self[x,y] | |
{ | |
switch cell.placingState | |
{ | |
case .Empty: | |
str += "[ ]" | |
case .Ship: | |
str += "[S]" | |
case .Lock(let retainCount): | |
str += "[\(retainCount)]" | |
default: | |
str += "[X]" | |
} | |
} | |
} | |
str += "\n" | |
} | |
return str | |
} | |
} | |
// test | |
let g:GameGrid = GameGrid(size: GridSize(5,5)) | |
g.placeShipAt(1, 1) | |
g.placeShipAt(3, 1) | |
g.placeShipAt(2, 1) | |
//g.removeShipAt(3, 1) | |
println(g.gridDebugString()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment