-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.swift
More file actions
208 lines (137 loc) · 5.38 KB
/
Copy pathBoard.swift
File metadata and controls
208 lines (137 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// Board.swift
// Flip
//
// Created by Thang Le Tan on 9/26/16.
// Copyright © 2016 Thang Le Tan. All rights reserved.
//
import UIKit
import GameplayKit
class Board: NSObject, GKGameModel {
var players: [GKGameModelPlayer]? {
return Player.allPlayers
}
var activePlayer: GKGameModelPlayer? {
return currentPlayer
}
static let size = 8
static let moves = [Move(row: 0, col: 1), Move(row: 1, col: 1), Move(row: 1, col: 0), Move(row: 1, col: -1)]
var currentPlayer = Player.allPlayers[0]
var rows = [[StoneColor]]()
var lastMove = Move(row: 0, col: 0) //dump init var
func canMoveIn(row: Int, col: Int) -> Bool {
//test 1: check if the move is sensible (not out of bound/range)
if !isInBound(row: row, col: col) { return false }
//test 2: check that the move hasnt been made already
let stone = rows[row][col]
if stone != .empty { return false }
return true
}
var rowsInt = [[Int]]()
func isInBound(row: Int, col: Int) -> Bool {
if row < 0 { return false }
if col < 0 { return false }
if row >= Board.size { return false }
if col >= Board.size { return false }
return true
}
func makeMove(player: Player, row: Int, col: Int) -> [Move] {
/*
let tests: [[Int]] = rows.map { $0.map { $0.hashValue } }
if let data = try? JSONSerialization.data(withJSONObject: tests, options: []) {
let text = String(data: data, encoding: String.Encoding.utf8)
print(text)
}
*/
//1: create an array to hold all the captured stones
var didCapture = [Move]()
//2: place the stone in the requested position
rows[row][col] = player.stoneColor
lastMove = Move(row: row, col: col)
didCapture.append(lastMove)
//10: send back the list of of captured stones
return didCapture
}
func getScores() -> (black: Int, white: Int) {
var black = 0
var white = 0
rows.forEach {
$0.forEach {
if $0 == .black {
black += 1
} else if $0 == .white {
white += 1
}
}
}
return (black, white)
}
func checkWin() -> Bool {
//print(currentPlayer.stoneColor)
return isWin(for: currentPlayer)
}
func isWin(for player: GKGameModelPlayer) -> Bool {
let row = lastMove.row
let col = lastMove.col
guard let player = player as? Player else { return false }
for move in Board.moves {
//3: look in this direction for captured stones
var count = 0
//move up and down
for way in [1,-1] {
var currentRow = row
var currentCol = col
for _ in 0 ..< Board.size {
currentRow += move.row * way
currentCol += move.col * way
//5: make sure this is a sensible position to move to
guard isInBound(row: currentRow, col: currentCol) else { break }
let stone = rows[currentRow][currentCol]
if stone == player.stoneColor {
count += 1
} else {
break
}
}
}
if count == 4 {
return true
}
}
return false
}
func gameModelUpdates(for player: GKGameModelPlayer) -> [GKGameModelUpdate]? {
//safely unwrap the player object
guard let playerObject = player as? Player else { return nil }
//if the game is over, exit now
if isWin(for: playerObject) || isWin(for: playerObject.opponent) {
return nil
}
//if we're still here prepare to send back a list of moves
var moves = [Move]()
//try every column in every row
for row in 0 ..< Board.size {
for col in 0 ..< Board.size {
if canMoveIn(row: row, col: col) {
moves.append(Move(row: row, col: col))
}
}
}
return moves
}
func apply(_ gameModelUpdate: GKGameModelUpdate) {
guard let move = gameModelUpdate as? Move else { return }
_ = makeMove(player: currentPlayer, row: move.row, col: move.col)
currentPlayer = currentPlayer.opponent
}
func setGameModel(_ gameModel: GKGameModel) {
guard let board = gameModel as? Board else { return }
currentPlayer = board.currentPlayer
rows = board.rows
}
func copy(with zone: NSZone? = nil) -> Any {
let copy = Board()
copy.setGameModel(self)
return copy
}
}