def test_equality(self): b1 = ttt.Board() b2 = ttt.Board() s = set([b1]) self.assertEqual(b1, b2) self.assertIn(b1, s) self.assertIn(b2, s)
def test_le(self): b1 = ttt.Board() b2 = ttt.Board() self.assertTrue(b1 <= b2) self.assertTrue(b2 <= b1) b2 = b2.play(1, 1) self.assertFalse(b2 <= b1) b1 = b1.play(1, 1) self.assertTrue(b1 <= b2) self.assertTrue(b2 <= b1)
def _test_eval(self): board0 = ttt.Board([ '.', '.', '.' '.', 'x', '.', '.', '.', '.', ]) board1 = ttt.Board([ 'x', '.', '.', '.', '.', '.', '.', '.', '.', ]) self.assertTrue( ttt.AI.evaluate(board0, 'x') > ttt.AI.evaluate(board1, 'x') )
def main(): # parse args args = get_args() key = args.key.read() if args.key else None addr = socket.gethostbyname(socket.gethostname()) port = args.port print("Server listing...\ \nAddress:", addr, "\ \nPort :", port) if (args.ttt): print("tik-tak-toe mode") board = ttt.Board() # use with to ensure socket is "cleaned up" with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: # make socket reusable s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((addr, port)) s.listen(5) conn, c_addr = s.accept() print('Connected by', c_addr) with conn: while True: if(args.ttt): recv_move(conn, board) send_move(conn, board) else: recv_txt(conn, key) send_txt(conn, key)
def main(): # parse args args = get_args() key = args.key.read() if args.key else None port = args.port addr = args.ipaddr or socket.gethostbyname(socket.gethostname()) print("Client connecting...\ \nAddress :", addr, "\ \nPort :", port) if (args.ttt): print("tik-tak-toe mode") board = ttt.Board() # use with to ensure socket is "cleaned up" with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: # make socket reusable s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.connect((addr, port)) print("connected to", addr) while True: while True: if (args.ttt): send_move(s, board) recv_move(s, board) else: send_txt(s, key) recv_txt(s, key)
def test_get_empty_idxs(self): b = ttt.Board() self.assertEqual(b.get_empty_idxs(), [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]) b = b.play(0, 0) self.assertEqual(b.get_empty_idxs(), [(0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)])
def ttt_challenge(request): try: board = ttt.Board(request.GET['board']) ai = ttt.TTTAI() board.make_move(ttt.O, ai.next_move(board)) except: return HttpResponseBadRequest() return HttpResponse(str(board))
def test_is_over(self): # Cases of "over" because someone won b = ttt.Board() self.assertFalse(b.is_over()) b = b.play(1, 1) b = b.play(1, 0) b = b.play(2, 2) self.assertFalse(b.is_over()) b = b.play(0, 0) b = b.play(0, 2) b = b.play(2, 0) self.assertTrue(b.is_over()) # Cases of "over" because the board is full b = ttt.Board() for r in [0, 2, 1]: for c in range(0, 3): b = b.play(r, c) self.assertTrue(b.is_over())
def test_play(self): ori_b = ttt.Board() b = ori_b.play(0, 0) # Make sure old board has not changed self.assertEqual(ori_b, ttt.Board()) try: b = b.play(0, 0) self.assertTrue(False) # Execution must not reach here except RuntimeWarning: pass b = b.play(0, 1) b = b.play(1, 0) b = b.play(1, 1) b = b.play(2, 0) try: b = b.play(2, 1) self.assertTrue(False) # Execution must not reach here except RuntimeWarning: pass
def test_eval_we_win(self): board = ttt.Board([ 'x', '.', 'o', '.', 'x', 'o', '.', '.', 'x', ]) self.assertEqual( sys.maxint, ttt.AI.evaluate(board, 'x') )
def test_who_won(self): b = ttt.Board() self.assertEqual(b.who_won(), None) b = b.play(1, 1) b = b.play(1, 0) b = b.play(2, 2) self.assertEqual(b.who_won(), None) b = b.play(0, 0) b = b.play(0, 2) b = b.play(2, 0) self.assertEqual(b.who_won(), 'o')
def test_next_move(self): board = ttt.Board([ 'x', 'o', 'o', 'o', 'x', '.', 'x', 'o', '.', ]) moves = ttt.all_moves(board, 'x') self.assertEqual( [ (2, 1), (2, 2), ], list(moves), )
def test_next_player(self): b = ttt.Board() self.assertEqual(b.next_player(), b.p1) b = b.play(1, 1) self.assertEqual(b.next_player(), b.p2) b = b.play(1, 0) self.assertEqual(b.next_player(), b.p1) b = b.play(2, 2) self.assertEqual(b.next_player(), b.p2) b = b.play(0, 0) b = b.play(2, 0) self.assertEqual(b.next_player(), b.p2) b = b.play(0, 1) self.assertEqual(b.next_player(), b.p1) b = b.play(0, 2) self.assertEqual(b.next_player(), None)
def test_get_children(self): b = ttt.Board() bs = b.get_children() answer = [ ((b.p1, b.e, b.e), (b.e, b.e, b.e), (b.e, b.e, b.e)), ((b.e, b.p1, b.e), (b.e, b.e, b.e), (b.e, b.e, b.e)), ((b.e, b.e, b.p1), (b.e, b.e, b.e), (b.e, b.e, b.e)), ((b.e, b.e, b.e), (b.p1, b.e, b.e), (b.e, b.e, b.e)), ((b.e, b.e, b.e), (b.e, b.p1, b.e), (b.e, b.e, b.e)), ((b.e, b.e, b.e), (b.e, b.e, b.p1), (b.e, b.e, b.e)), ((b.e, b.e, b.e), (b.e, b.e, b.e), (b.p1, b.e, b.e)), ((b.e, b.e, b.e), (b.e, b.e, b.e), (b.e, b.p1, b.e)), ((b.e, b.e, b.e), (b.e, b.e, b.e), (b.e, b.e, b.p1)), ] for idx, b in enumerate(bs): self.assertIn(b.b, answer)
def test_get_descendants(self): bs = ttt.Board().get_descendants() #for b in bs: # print(b) # print() #print(len(bs)) self.assertEqual(len(bs), 5478) p1 = [b for b in bs if b.who_won() == b.p1] #for b in p1: print(b); print() #print(len(p1)) self.assertEqual(len(p1), 626) p2 = [b for b in bs if b.who_won() == b.p2] #for b in p2: print(b); print() #print(len(p2)) self.assertEqual(len(p2), 316) cats = [b for b in bs if b.who_won() == None and b.is_over()] #for b in cats: print(b); print() #print(len(cats)) self.assertEqual(len(cats), 16)
def test_is_empty(self): ori_b = ttt.Board() self.assertTrue(ori_b.is_empty()) b = ori_b.play(0, 1) self.assertFalse(b.is_empty()) self.assertTrue(ori_b.is_empty())
def play(self, b): best_b = None best_reward = -float("inf") cs = b.get_children() for c in cs: if c not in self.rewards: if c.who_won() == self.p: self.rewards[c] = 1.0 elif c.who_won() != None: # We lost self.rewards[c] = 0.0 else: self.rewards[c] = 0.5 # coin-toss tmp = self.rewards[c] if best_reward < tmp: best_reward = tmp best_b = c assert (best_b != None) if self.prev_b != None: self.rewards[self.prev_b] += self.params['lr'] * ( self.rewards[best_b] - self.rewards[self.prev_b]) self.prev_b = best_b return get_play_from_parent_and_child(b, best_b) if __name__ == "__main__": #ap = APlayer(ttt.Board.default_p1) #ap = DPlayer(ttt.Board.default_p1) #ap = ADPlayer(ttt.Board.default_p1) ap = DRPlayer(ttt.Board.default_p1) ap.play(ttt.Board())
import ttt b = ttt.Board(3, 4, 3) b.make_move(1, 1, 'X') b.make_move(1, 2, 'X') b.make_move(1, 3, 'X') print(b) print(b.winner())