def flip(self, color, x, y): self.push(); result = False; # if the BMI2 instruction set is used by the function board::flip() # we do not need to compare self.brd.get(x + (y << 3)) with rv.blank if not check((x,y)) or self.brd.get(x + (y << 3)) != rv.blank: result = False; else: brd_save = rv.board(self.brd); self.brd.flip(color, x + (y << 3)); result = (self.brd.bget(True) != brd_save.bget(True)); if result: self.print_log( "place a " + ("black" if color else "white") + " stone at " + chr(x + ord("A")) + chr(y + ord("1")) + "\n" ); if not (self.brd.get_status(not color) & rv.sts_again): self.color = not color; self.pos = (x,y); self.paint(); else: if self.flag_auto_save: self.pop(); self.print_log( "cannot place a " + ("black" if color else "white") +" stone here\n" ); return result;
def generate_label(sample): assert (os.path.exists(data_path + sample)) with open(data_path + sample, "rb") as fobj: sample = np.load(fobj) if not os.path.exists(data_path + "label.dat"): label = np.zeros(sample.shape[0]) with open(data_path + "label.dat", "wb") as fobj: pickle.dump([0, label], fobj) with open(data_path + "label.dat", "rb") as fobj: [num, label] = pickle.load(fobj) brd = rv.board() handler_save = signal.signal(signal.SIGINT, on_interrupt) for i in range(num, sample.shape[0]): brd.assign(int(sample[i, 0]), int(sample[i, 1])) label[i] = brd.search(True, 4) if i % 1000 == 0: print("finish", i) if flag_int: break with open(data_path + "label.dat", "wb") as fobj: pickle.dump([i, label], fobj) signal.signal(signal.SIGINT, handler_save)
def pop(self): if len(self.record) == 0: return False; else: self.storage.append((rv.board(self.brd), self.color, self.pos)); (self.brd, self.color, self.pos) = self.record[-1]; self.record.pop(); return True;
def redo(self): if len(self.storage) == 0: return False; else: self.record.append((rv.board(self.brd), self.color, self.pos)); (self.brd, self.color, self.pos) = self.storage[-1]; self.storage.pop(); self.print_log("redo\n"); self.flag_lock = False; self.paint(); return True;
def load_node(fobj): ptr = empty() brd_white = int.from_bytes(fobj.read(8), "little") brd_black = int.from_bytes(fobj.read(8), "little") ptr.brd = rv.board(brd_black, brd_white) ptr.color = int.from_bytes(fobj.read(1), "little") fobj.read(1) ptr.height = int.from_bytes(fobj.read(2), "little") ptr.depth = int.from_bytes(fobj.read(2), "little") fobj.read(2) ptr.alpha = struct.unpack('f', fobj.read(4))[0] ptr.beta = struct.unpack('f', fobj.read(4))[0] ptr.result = struct.unpack('f', fobj.read(4))[0] fobj.read(4) ptr.child = [] return ptr
def __init__(self): self.brd = rv.board(0, 0); self.color = True; self.pos = (-1, -1); self.mthd = rv.mthd_ab | rv.mthd_kill | rv.mthd_pvs | rv.mthd_trans | rv.mthd_mtdf | rv.mthd_ptn; self.depth = -1; self.flag_auto_save = True; self.flag_lock = True; self.ply = (player(), player()); self.record = []; self.storage = []; self.choices = None; self.pv = None; self.ply[False].type = rv.ply_ai; self.ply[False].path = default_path self.ply[True].type = rv.ply_human; self.ply[True].path = default_path
def train(mthd, depth, size, num): brd = rv.board() brd.config() grp = rv.group() mthd_full = "mthd_" + mthd mthd_id = getattr(rv, mthd_full) grp.assign(size) grp.initial() for i in range(num): grp.train(mthd_id, depth) name = "ptn_%s_d%ds%dn%d" % (mthd, depth, size, num) grp.save(name + ".dat") print(name)
def do_show(self, dc): dc.Clear(); #draw valid moves dc.SetBrush(wx.Brush(wx.Colour(30,100,0))); dc.SetPen(wx.Pen(wx.Colour(30,100,0), thick)); brd_move = self.brd.get_move(self.color); for i in range(reversi.board.size2): if brd_move & (1 << i): dc.DrawRectangle(bias + cell * (i & 7), bias + cell * (i >> 3), cell, cell); #draw a board dc.SetPen(wx.Pen(wx.BLACK, thick)); for i in range(num + 1): dc.DrawLine(bias, bias + cell * i, bias + width, bias + cell * i); for i in range(num + 1): dc.DrawLine(bias + cell * i, bias, bias + cell * i, bias + width); #draw the outline of the board dc.SetBrush(wx.BLACK_BRUSH); dc.SetPen(wx.Pen(wx.BLACK,thick)); dc.DrawRectangle(bias - margin, bias - margin, margin, width + margin * 2); dc.DrawRectangle(bias - margin, bias - margin, width + margin * 2, margin); dc.DrawRectangle(bias + width, bias - margin, margin, width + margin * 2); dc.DrawRectangle(bias - margin, bias + width, width + margin * 2, margin); #draw coordinate labels dc.SetTextForeground(wx.Colour(190,190,190)); dc.SetFont( wx.Font( 12, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD,False, "Consolas" ,wx.FONTENCODING_DEFAULT ) ); for i in range(num): dc.DrawText(chr(ord('A') + i), bias + cell / 2 + cell * i - 4, bias - margin / 2 - 10); dc.DrawText(chr(ord('A') + i), bias + cell / 2 + cell * i - 4, bias + width + margin / 2 - 12); dc.DrawText(chr(ord('1') + i), bias - margin / 2 - 4,bias + cell / 2 + cell * i - 10); dc.DrawText(chr(ord('1') + i), bias + width + margin / 2 - 5, bias + cell / 2 + cell * i - 10); #draw stones for i in range(num): for j in range(num): chssmn = self.brd.get(i + (j << 3)); if chssmn == rv.black: dc.SetBrush(wx.Brush(wx.Colour(40,40,40))); dc.SetPen(wx.Pen(wx.Colour(20,20,20), thick)); dc.DrawCircle(wx.Point(cbias + cell * i, cbias + cell * j),radius); elif chssmn == reversi.white: dc.SetBrush(wx.Brush(wx.Colour(210,210,210))); dc.SetPen(wx.Pen(wx.Colour(230,230,230), thick)); dc.DrawCircle(wx.Point(cbias + cell * i, cbias + cell * j),radius); #show where is the last move if check(self.pos): if self.get(self.pos[0], self.pos[1]) == rv.black: dc.SetBrush(wx.Brush(wx.Colour(50,50,30))); dc.SetPen(wx.Pen(wx.Colour(90,90,0), thick)); else: dc.SetBrush(wx.Brush(wx.Colour(210,210,170))); dc.SetPen(wx.Pen(wx.Colour(200,200,30), thick)); dc.DrawCircle(wx.Point(cbias + cell * self.pos[0], cbias + cell * self.pos[1]),radius); if not (self.choices is None): dc.SetTextForeground(wx.Colour(255,30,30)); dc.SetFont( wx.Font( 9, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False, "Consolas", wx.FONTENCODING_DEFAULT ) ); for c in self.choices: x = c.pos & 7; y = c.pos >> 3; s = "%.3f" % c.val; dc.DrawText( s, bias + cell * x + cell / 2 - 3.5 * len(s), bias + cell * y + cell / 2 - 8 ); if not (self.pv is None): brd = rv.board(self.brd); color = self.color; for i in range(len(self.pv)): x = self.pv[i] & 0x7; y = self.pv[i] >> 3; if color: if brd_move & (1 << self.pv[i]): dc.SetBrush(wx.Brush(wx.Colour(30,100,0))); else: dc.SetBrush(wx.Brush(wx.Colour(43,155,0))); dc.SetPen(wx.Pen(wx.Colour(20,20,20), thick)); dc.DrawCircle(wx.Point(cbias + cell * x, cbias + cell * y), radius); dc.SetTextForeground(wx.Colour(20,20,20)); s = "%d" % (i + 1); dc.DrawText( s, bias + cell * x + cell / 2 - 3.5 * len(s), bias + cell * y + cell / 2 - 8 ); else: if brd_move & (1 << self.pv[i]): dc.SetBrush(wx.Brush(wx.Colour(30,100,0))); else: dc.SetBrush(wx.Brush(wx.Colour(43,155,0))); dc.SetPen(wx.Pen(wx.Colour(230,230,230), thick)); dc.DrawCircle(wx.Point(cbias + cell * x, cbias + cell * y), radius); dc.SetTextForeground(wx.Colour(230,230,230)); s = "%d" % (i + 1); dc.DrawText( s, bias + cell * x + cell / 2 - 3.5 * len(s), bias + cell * y + cell / 2 - 8 ); brd.flip(color, self.pv[i]); status = brd.get_status(not color); if status & rv.sts_black: color = True; else: color = False;
def push(self): if self.flag_auto_save: self.record.append((rv.board(self.brd), self.color, self.pos)); self.storage = [];
def extract_sample(filename): result_pack = [] package = zipfile.ZipFile(data_path + filename) color = None for name in package.namelist(): record = {} package.extract(name, data_path) with open(data_path + name, "r") as fobj: lines = fobj.readlines() os.remove(data_path + name) for line in lines: data = json.loads(line) assert ("log" in data) log = data["log"] brd = rv.board() brd.initial() for turn_id in range(len(log)): try: turn = log[turn_id] if "0" in turn: color = True elif "1" in turn: color = False else: continue assert (turn_id + 1 < len(log)) turn_next = log[turn_id + 1] if "output" in turn_next and "display" in turn_next[ "output"] and "err" in turn_next["output"][ "display"]: break turn = turn["0" if color else "1"].copy() assert ("verdict" in turn) if turn["verdict"] != "OK": break assert ("response" in turn) turn = turn["response"] assert ("x" in turn and "y" in turn) if turn["x"] < 0 or turn["y"] < 0: continue pos = turn["x"] + (turn["y"] << 3) brd.flip(color, pos) element = (brd.bget(color), brd.bget(not color)) if element in record: record[element] += 1 else: record[element] = 1 except: print(turn) print(log) print(name) assert (False) result = np.empty((len(record), 4), dtype=np.uint64) count = 0 for i, j in record.items(): result[count, :2] = i result[count, 2] = j count += 1 result_pack.append(result) print("finish " + name) with open(data_path + filename[:-4] + ".dat", "wb") as fobj: pickle.dump(result_pack, fobj)