def loop(self): ai = chessai(1, self.chess.board) ai2 = HardAi() test = chessboard() test.board = self.chess.board clock = pygame.time.Clock() self.chess.draw() color = 2 while self.stat != 'quit': if (self.chess.color != color): self.chess.step = ai.get_best_step() self.draw() # 画下棋的位置 color = self.chess.color temp = copy.deepcopy(self.chess.board) self.chess.step = ai2.next(2, temp) self.draw() # 画下棋的位置 # print "&****"+str(color) # while True: # pos = self.run() # if(pos!= None): # break # self.draw()#画下棋的位置 clock.tick(60) test.display() pygame.quit()
def test_chessboard(): obj = chessboard() obj.reset() #position out of range check t1 = obj.changevalue(-1, -1, 1) assert t1 == 0 #input value out of range check t2 = obj.changevalue(-1, -1, 5) assert t2 == 0 #occupy conflict t3 = obj.changevalue(2, 3, 2) assert t3 == 1 t4 = obj.changevalue(2, 3, 1) assert t4 == 0 obj.reset() #black win 1 (five in a row) for n in range(5): b_valid1 = obj.changevalue(0, n, 1) assert b_valid1 == 1 w_valid1 = obj.changevalue(2, 2 * n, 2) assert w_valid1 == 1 winner1 = obj.checkwinner() assert winner1 == 1 obj.reset() #black win 2 (five in a column) for n in range(5): b_valid2 = obj.changevalue(n, 0, 1) assert b_valid2 == 1 w_valid2 = obj.changevalue(2 * n, 3, 2) assert w_valid2 == 1 winner2 = obj.checkwinner() assert winner2 == 1 obj.reset() #black win 3 (five in a polyline) for n in range(5): b_valid3 = obj.changevalue(n, n, 1) assert b_valid3 == 1 w_valid3 = obj.changevalue(5, 2 * n, 2) assert w_valid3 == 1 winner3 = obj.checkwinner() assert winner3 == 1 obj.reset() #white win 1 for n in range(5): b_valid4 = obj.changevalue(5, 2 * n, 1) assert b_valid4 == 1 w_valid4 = obj.changevalue(3, n, 2) assert w_valid4 == 1 winner4 = obj.checkwinner() assert winner4 == 2 obj.reset()
from threading import Thread from Queue import Queue, Empty ON_POSIX = 'posix' in sys.builtin_module_names def enqueue_output(out, queue): for line in iter(out.readline, ''): queue.put(line) out.close() pygame.init() DISPLAY_REFRESH = pygame.USEREVENT fps = 60 screen = pygame.display.set_mode(size, 0, 32) chessboard = chessboard() #ai_options = chessai.get_ai_engine_options('harmless') ai_options = chessai.get_ai_engine_options('eleeye') if len(sys.argv) == 2 and sys.argv[1][:2] == '-n': chessboard.net = chessnet() if sys.argv[1][2:] == 'r': pygame.display.set_caption("red") chessboard.side = RED elif sys.argv[1][2:] == 'b': pygame.display.set_caption("black") chessboard.side = BLACK else: print '>> quit game' sys.exit()
def test_chessboard(): obj = chessboard(15) obj.reset() #position out of range check t1 = obj.changevalue(-1, -1, 1) assert t1 == 0 #input value out of range check t2 = obj.changevalue(-1, -1, 5) assert t2 == 0 #occupy conflict t3 = obj.changevalue(2, 3, 2) assert t3 == 1 t4 = obj.changevalue(2, 3, 1) assert t4 == 0 obj.reset() #black win 1 (five in a row) for n in range(5): b_valid1 = obj.changevalue(0, n, 1) assert b_valid1 == 1 w_valid1 = obj.changevalue(2, 2 * n, 2) assert w_valid1 == 1 winner1 = obj.checkwinner() assert winner1 == 1 obj.reset() #black win 2 (five in a column) for n in range(5): b_valid2 = obj.changevalue(n, 0, 1) assert b_valid2 == 1 w_valid2 = obj.changevalue(2 * n, 3, 2) assert w_valid2 == 1 winner2 = obj.checkwinner() assert winner2 == 1 obj.reset() #black win 3 (five in a polyline) for n in range(5): b_valid3 = obj.changevalue(n, n, 1) assert b_valid3 == 1 w_valid3 = obj.changevalue(5, 2 * n, 2) assert w_valid3 == 1 winner3 = obj.checkwinner() assert winner3 == 1 obj.reset() #white win 1 for n in range(5): b_valid4 = obj.changevalue(5, 2 * n, 1) assert b_valid4 == 1 w_valid4 = obj.changevalue(3, n, 2) assert w_valid4 == 1 winner4 = obj.checkwinner() assert winner4 == 2 obj.reset() #test the loop #test inside loop first # black five in a polyline for n in range(5): b_valid5 = obj.changevalue(n, n, 1) assert b_valid5 == 1 w_valid5 = obj.changevalue(5, 2 * n, 2) assert w_valid5 == 1 b_valid5 = obj.changevalue(0, 1, 1) assert b_valid5 == 1 b_valid5 = obj.changevalue(0, 2, 1) assert b_valid5 == 1 b_valid5 = obj.changevalue(0, 3, 1) assert b_valid5 == 1 b_valid5 = obj.changevalue(1, 0, 1) assert b_valid5 == 1 b_valid5 = obj.changevalue(2, 0, 1) assert b_valid5 == 1 looptest = obj.five_in_line(2, 0, 0, (1, 1)) assert looptest == False looptest = obj.five_in_line(1, 0, 0, (0, 1)) assert looptest == False looptest = obj.five_in_line(1, 0, 0, (1, 0)) assert looptest == False looptest = obj.five_in_line(1, 0, 0, (1, 1)) assert looptest == True winnertest = obj.checkwinner() assert winnertest == 1
import sys from subprocess import PIPE, Popen from threading import Thread from Queue import Queue, Empty ON_POSIX = 'posix' in sys.builtin_module_names def enqueue_output(out, queue): for line in iter(out.readline, ''): queue.put(line) out.close() pygame.init() screen = pygame.display.set_mode(size, 0, 32) chessboard = chessboard() if len(sys.argv) == 2 and sys.argv[1][:2] == '-n': chessboard.net = chessnet() if sys.argv[1][2:] == 'r': pygame.display.set_caption("red") chessboard.side = RED elif sys.argv[1][2:] == 'b': pygame.display.set_caption("black") chessboard.side = BLACK else: print '>> quit game' sys.exit() chessboard.net.NET_HOST = sys.argv[2]
itemw = ((chessboard.GOAL // 3) + 1) << 1 winw = (itemw + 3) * chessboard.LENGTH + 3 winh = chessboard.LENGTH * 3 + 2 win = stdscr.subwin(winh, winw, 0, 0) win.box() form = [] for x in range(chessboard.LENGTH): form.append([]) for y in range(chessboard.LENGTH): form[x].append(stdscr.subwin(3, itemw + 2, 3 * y + 1, (itemw + 3) * x + 2)) form[x][y].box() stdscr.refresh() curses.start_color() curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE) cb = chessboard() cb.randput() # Loop while True: cb.randput() numpad = cb.getchessboard() for x in range(chessboard.LENGTH): for y in range(chessboard.LENGTH): form[x][y].addstr(1, 1, "".join(map(lambda c: ' ' + c, "%4d" % ((int(numpad[y][x] != 0)) * (0x1 << numpad[y][x]))))) form[x][y].refresh() stdscr.refresh() if cb.checkwin(): winwin = stdscr.subwin(9, 3, winh >> 1, winw >> 1) winwin.box()