Skip to content

Instantly share code, notes, and snippets.

@mugityaopen
Last active January 8, 2016 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mugityaopen/c4cc614ded8cf1b442bd to your computer and use it in GitHub Desktop.
Save mugityaopen/c4cc614ded8cf1b442bd to your computer and use it in GitHub Desktop.
reversi_for_swift
var workArray:[[String]] = [
[" "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "],
[" ","○","●","●","○","●","●"," "," "," "],
[" "," "," "," ","●","○","●"," "," "," "],
[" "," "," "," "," "," ","●"," "," "," "],
[" "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "],
[" "," "," "," "," "," "," "," "," "," "],
]
enum OBJTYPE:Int {
case WHITE
case BLACK
}
let object:[String] = ["○","●"]
// 配列状態を表示
func printArray() {
print("printArray")
print("____0____1____2____3____4____5____6____7____8____9__")
for var idx0 = 0; idx0 < workArray.count; idx0++ {
print(idx0,workArray[idx0])
for var idx1 = 0; idx1 < workArray[idx0].count; idx1++ {
}
}
}
// 周辺検索用のテーブル
let searchTbl:[[(x:Int,y:Int)]] = [
[(x:-1,y:-1),(x: 0,y:-1),(x: 1,y:-1)],
[(x:-1,y: 0),(x: 0,y: 0),(x: 1,y: 0)],
[(x:-1,y: 1),(x: 0,y: 1),(x: 1,y: 1)],
]
// 周辺8マスを検索して置ける条件に当てはまれば置く
func searchObject(x:Int,y:Int,type:Int) -> Bool {
let revType = type == OBJTYPE.WHITE.rawValue ? OBJTYPE.BLACK.rawValue : OBJTYPE.WHITE.rawValue
var ret = false // 書き換えが行われたかどうか
var pinch = false // はさんでいるか
let oldArray = workArray
print(x,y,"に",object[type],"を置こうとしています")
// 縦
for var idx0 = 0; idx0 < searchTbl.count; idx0++ {
// 横
for var idx1 = 0; idx1 < searchTbl[idx0].count; idx1++ {
// 現在の座標
let current:(x:Int,y:Int) = (x:x + searchTbl[idx0][idx1].x,y:y + searchTbl[idx0][idx1].y)
let loop = 10//後で直す
// 範囲外の場合はコンテニュー
if(loop <= current.y || loop <= current.x || current.y < 0 || current.x < 0) {
print("current continue")
continue
}
print("current",current)
// 指定したオブジェクト種別と違うものが存在する場合
if(workArray[current.y][current.x] == object[revType]) {
print(current.x,current.y,"に",object[revType],"があります")
// 挟めるかチェック
for var cnt = loop; 0 <= cnt; cnt-- {
// 移動後の座標
let next:(x:Int,y:Int) = (x: current.x + (searchTbl[idx0][idx1].x * cnt),y: (current.y + (searchTbl[idx0][idx1].y * cnt)))
// 範囲外の場合はコンテニュー
if(loop <= next.y || loop <= next.x || next.y < 0 || next.x < 0) {
print("next continue")
continue
}
print("next",next)
// 同じオブジェクト種別が存在するかチェック
if(workArray[next.y][next.x] == object[type]) {
print("!!true!!")
ret = true
pinch = true
workArray = oldArray // 同じオブジェクトを二つ以上発見したらテーブルを元に戻す
}
// はさめる場合はひっくり返す
if(pinch == true) {
workArray[next.y][next.x] = object[type]
}
}
}
pinch = false
}
}
return ret
}
// オブジェクトを表示(x:X座標,y:Y座標,type:オブジェクトタイプ(0 ■,!0 ●))
func setObject(x:Int,y:Int,type:Int) {
// 範囲外の場合はエラー
if(workArray.count <= y || workArray[y].count <= x || y < 0 || x < 0) {
print("over position")
return
}
// 既に何かが置かれている場合置けない
if(workArray[y][x] != " ") {
print("not put1")
return
}
// オブジェクトが置けるか検索
if searchObject(x,y:y,type:type) {
// オブジェクトを配置
workArray[y][x] = object[type]
}
else {
print("not put2")
}
print("end")
}
// XY座標とオブジェクトの種別を引数に入れるとリバーシっぽくなる
setObject(7,y:4,type:0)
printArray()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment