Skip to content

Commit

Permalink
Support command line Othello program
Browse files Browse the repository at this point in the history
  • Loading branch information
ysnrkdm committed Nov 9, 2016
1 parent 0ffa783 commit e7852b6
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 35 deletions.
16 changes: 4 additions & 12 deletions Graphene.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
8BB558231DCF052200D6202A /* Stack.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BB558211DCF052200D6202A /* Stack.swift */; };
8BB558241DCF052200D6202A /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BB558221DCF052200D6202A /* Utils.swift */; };
8BB5583A1DCF073D00D6202A /* Intrinsics.c in Sources */ = {isa = PBXBuildFile; fileRef = 8BB558271DCF056300D6202A /* Intrinsics.c */; };
8BB558471DD367C500D6202A /* Regexp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8BB558461DD367C500D6202A /* Regexp.swift */; };
_LinkFileRef_Graphene_via_GrapheneTestSuite /* Graphene.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = "_____Product_Graphene" /* Graphene.framework */; };
__src_cc_ref_Tests/Graphene/GrapheneTests.swift /* GrapheneTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = __PBXFileRef_Tests/Graphene/GrapheneTests.swift /* GrapheneTests.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -78,9 +79,8 @@
8BB558211DCF052200D6202A /* Stack.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Stack.swift; path = Sources/Graphene/Stack.swift; sourceTree = SOURCE_ROOT; };
8BB558221DCF052200D6202A /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Utils.swift; path = Sources/Graphene/Utils.swift; sourceTree = SOURCE_ROOT; };
8BB558271DCF056300D6202A /* Intrinsics.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = Intrinsics.c; sourceTree = "<group>"; };
8BB558311DCF06D700D6202A /* Intrinsics.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Intrinsics.h; sourceTree = "<group>"; };
8BB558321DCF06D700D6202A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8BB5583C1DCF0B1400D6202A /* Intrinsics.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Intrinsics.h; path = include/Intrinsics.h; sourceTree = "<group>"; };
8BB558461DD367C500D6202A /* Regexp.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Regexp.swift; path = Sources/Graphene/Regexp.swift; sourceTree = SOURCE_ROOT; };
__PBXFileRef_Graphene.xcodeproj/Configs/Project.xcconfig /* Project.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Project.xcconfig; path = Graphene.xcodeproj/Configs/Project.xcconfig; sourceTree = "<group>"; };
__PBXFileRef_GrapheneTestSuite_Info.plist /* GrapheneTestSuite_Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = GrapheneTestSuite_Info.plist; path = Graphene.xcodeproj/GrapheneTestSuite_Info.plist; sourceTree = SOURCE_ROOT; };
__PBXFileRef_Graphene_Info.plist /* Graphene_Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Graphene_Info.plist; path = Graphene.xcodeproj/Graphene_Info.plist; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -190,19 +190,11 @@
children = (
8BB558211DCF052200D6202A /* Stack.swift */,
8BB558221DCF052200D6202A /* Utils.swift */,
8BB558461DD367C500D6202A /* Regexp.swift */,
);
path = Utils;
sourceTree = "<group>";
};
8BB558301DCF06D700D6202A /* Intrinsics */ = {
isa = PBXGroup;
children = (
8BB558311DCF06D700D6202A /* Intrinsics.h */,
8BB558321DCF06D700D6202A /* Info.plist */,
);
path = Intrinsics;
sourceTree = "<group>";
};
TestProducts_ /* Tests */ = {
isa = PBXGroup;
children = (
Expand All @@ -218,7 +210,6 @@
"_____Configs_" /* Configs */,
"_____Sources_" /* Sources */,
"_______Tests_" /* Tests */,
8BB558301DCF06D700D6202A /* Intrinsics */,
"____Products_" /* Products */,
);
sourceTree = "<group>";
Expand Down Expand Up @@ -366,6 +357,7 @@
8BB558181DCF04F400D6202A /* Info.swift in Sources */,
8BB5581F1DCF04F400D6202A /* Think.swift in Sources */,
8BB557F71DCF048200D6202A /* BoardBuilder.swift in Sources */,
8BB558471DD367C500D6202A /* Regexp.swift in Sources */,
8BB558031DCF04AD00D6202A /* Evaluator.swift in Sources */,
8BB5580B1DCF04D200D6202A /* NegaAlphaSearch.swift in Sources */,
8BB5581E1DCF04F400D6202A /* SwingThink.swift in Sources */,
Expand Down
14 changes: 8 additions & 6 deletions Sources/Graphene/BoardBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ open class BoardBuilder {
}
i += 1
}

let bm = BoardMediator(board: board)

return build(board)
}

public class func build(_ fromBoard: Board) -> BoardRepresentation {
let bm = BoardMediator(board: fromBoard)
let ret = BoardRepresentation(boardMediator: bm)
return ret
}
Expand All @@ -51,9 +55,7 @@ open class BoardBuilder {
}
i += 1
}

let bm = BoardMediator(board: board)
let ret = BoardRepresentation(boardMediator: bm)
return ret

return build(board)
}
}
113 changes: 97 additions & 16 deletions Sources/Graphene/EdaxProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,94 @@

import Foundation

public func isMove(hand: String) -> Bool {
return false
public func handFromEdaxStr(edaxStr: String) -> Move {
let pattern = "^([a-hA-H][1-8]|PS)$"
if let matches = Regexp(pattern).matches(input: edaxStr) {
if matches.count == 2 {
let hand = matches[1]
if hand.uppercased() == "PS" {
return .Pass
} else {
return handFromStr(str: hand)
}
}
}
return .Invalid
}

public func handFromStr(str: String) -> Move {
let b = str.uppercased()
if let first = b.characters.first, let last = b.characters.last {
var row = -1;
var col = -1;
switch first {
case "A":
col = 0
case "B":
col = 1
case "C":
col = 2
case "D":
col = 3
case "E":
col = 4
case "F":
col = 5
case "G":
col = 6
case "H":
col = 7
default:
break
}
switch last {
case "1":
row = 0
case "2":
row = 1
case "3":
row = 2
case "4":
row = 3
case "5":
row = 4
case "6":
row = 5
case "7":
row = 6
case "8":
row = 7
default:
break
}
return .Move(col, row)
}
return .Invalid
}

public func isPass(hand: String) -> Bool {
return false
public enum Move {
case Move(Int, Int) // col, row
case Pass
case Invalid
}

public enum Result {
case Game(Board)
case Moved(Board)
case Quit
}

public func processSfen(sfen: String, think: Think, board: Board) -> Result {
public func processSfen(sfen: String, think: Think, board: Board, color: Pieces, info: Info) -> Result {
var commands = sfen.components(separatedBy: " ")
// print(commands)
switch commands[0] {
case "init":
var bb = SimpleBitBoard()
let bb = SimpleBitBoard()
bb.initialize(8, height: 8)
return Result.Game(bb)
return .Game(bb)
case "quit":
print("byebye!")
return Result.Quit
return .Quit
case "undo":
print("Not yet implemented")
case "redo":
Expand All @@ -43,20 +108,36 @@ public func processSfen(sfen: String, think: Think, board: Board) -> Result {
case "0":
break;
case "p":
// show fv
// TODO: show fv
break;
default:
print("")
}
case "go":
break
case let hand where isMove(hand: hand):
break
case let hand where isPass(hand: hand):
break
let boardRep = BoardBuilder.build(board)
let hand = think.think(color, board: boardRep, info: info)
if board.canPut(color, x: hand.col, y: hand.row) {
_ = board.put(color, x: hand.col, y: hand.row, guides: false, returnChanges: false)
return .Moved(board)
} else {
// Treat invalid put by think as pass
return .Moved(board)
}
default:
print("undefined command..")
switch handFromEdaxStr(edaxStr: sfen) {
case let .Move(col, row):
if board.canPut(color, x: col, y: row) {
_ = board.put(color, x: col, y: row, guides: false, returnChanges: false)
return .Moved(board)
} else {
return .Game(board)
}
case .Pass:
return .Moved(board)
case .Invalid:
print("undefined command..")
}
}

return Result.Game(board)
return .Game(board)
}
7 changes: 7 additions & 0 deletions Sources/Graphene/Info.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ open class SimpleLogInfo : Info {
LOG(body: message)
}
}

open class NullInfo : Info {
public init(){}
open func say(message: String) {
// Do nothing
}
}
38 changes: 38 additions & 0 deletions Sources/Graphene/Regexp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Regex.swift
// Graphite
//
// Created by KodamaYoshinori on 2016/11/06.
//
//

import Foundation

class Regexp {
let internalRegexp: NSRegularExpression
let pattern: String

init(_ pattern: String) {
self.pattern = pattern
self.internalRegexp = try! NSRegularExpression( pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
}

func isMatch(input: String) -> Bool {
let matches = self.internalRegexp.matches(in: input, options: [], range:NSMakeRange(0, input.characters.count))
return matches.count > 0
}

func matches(input: String) -> [String]? {
if self.isMatch(input: input) {
let nsString = input as NSString
if let matches = self.internalRegexp.firstMatch(in: input, options: [], range: NSMakeRange(0, nsString.length)) {
var results: [String] = []
for i in 0 ..< matches.numberOfRanges {
results.append((input as NSString).substring(with: matches.rangeAt(i)))
}
return results
}
}
return nil
}
}
2 changes: 1 addition & 1 deletion Sources/Graphene/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func max(_ a: Double, b: Double) -> Double {
return a > b ? a : b
}

func nextTurn(_ color: Pieces) -> Pieces {
public func nextTurn(_ color: Pieces) -> Pieces {
var s : Pieces = .black
switch color {
case .black:
Expand Down

0 comments on commit e7852b6

Please sign in to comment.