def duelit(aione, aitwo, turnlimit=100): """ Duels two ais against each other. Returns REDWON, BLACKWON, or NOWIN (draw). :param aione: first ai (RED) :param aitwo: second ai (BLACK) :param turnlimit: if this turn limit is exceeded, the game is announced a draw (NOWIN) :return: REDWON, BLACKWON, or NOWIN (draw). """ state = NOWIN turn = 1 board = Cb.getstartingboard() if aione not in Sb.SWITCHABLE_AIS or aitwo not in Sb.SWITCHABLE_AIS: return NOWIN # error - should I report this somehow to the caller? while (turn < turnlimit): # first ai fmove = Sb.getaimove(aione, board, RED) board = Cb.makemove(board, fmove) state = Cb.iswon(board) if state != NOWIN: break # second ai smove = Sb.getaimove(aitwo, board, BLACK) board = Cb.makemove(board, smove) state = Cb.iswon(board) if state != NOWIN: break return state
def test_getupjumpmoves(self): sboard = Cb.getstartingboard() sboard[17] = BLACK self.assertEqual(Cb.getupjumpmoves(sboard, 22), [(22, 13, [17])]) sboard[6] = EMPTY self.assertEqual(Cb.getupjumpmoves(sboard, 22), [(22, 6, [17, 9])]) sboard[18] = BLACK self.assertEqual(Cb.getupjumpmoves(sboard, 22), [(22, 6, [17, 9]), (22, 6, [18, 10])])
def test_getkingjumpmoves(self): sboard = Cb.getstartingboard() sboard[22] = REDKING sboard[17] = BLACK self.assertEqual(Cb.getkingjumpmoves(sboard, 22), [(22, 13, [17])]) sboard[6] = EMPTY self.assertEqual(Cb.getkingjumpmoves(sboard, 22), [(22, 15, [17, 9, 10])]) sboard[18] = BLACK self.assertEqual(Cb.getkingjumpmoves(sboard, 22), [(22, 15, [17, 9, 10]), (22, 13, [18, 10, 9])]) sboard[22] = EMPTY sboard[6] = REDKING self.assertEqual(Cb.getkingjumpmoves(sboard, 6), [(6, 15, [9, 17, 18]), (6, 13, [10, 18, 17])])
def test_getdownmoves(self): # this does not test filtertype sboard = Cb.getstartingboard() self.assertEqual(Cb.getdownmoves(sboard, 4), []) # left side with no right move self.assertEqual(Cb.getdownmoves(sboard, 8), [(8, 12, []), (8, 13, [])]) # multiple self.assertEqual(Cb.getdownmoves(sboard, 6), []) # nothing self.assertEqual(Cb.getdownmoves(sboard, 26), []) # nothing again self.assertEqual(Cb.getdownmoves(sboard, 30), []) # bottom self.assertEqual(Cb.getdownmoves(sboard, 11), [(11, 15, [])]) # right side
def test_iswon(self): sboard = Cb.getstartingboard() # no win rboard = Cb.getemptyboard() bboard = Cb.getemptyboard() rboard[0] = RED # red win bboard[0] = BLACK # black win self.assertEqual(Cb.iswon(sboard), NOWIN) # checks self.assertEqual(Cb.iswon(rboard), REDWON) self.assertEqual(Cb.iswon(bboard), BLACKWON)
def test_getupmoves(self): # this does not test filtertype sboard = Cb.getstartingboard() self.assertEqual(Cb.getupmoves(sboard, 20), [(20, 16, [])]) # left side self.assertEqual(Cb.getupmoves(sboard, 22), [(22, 17, []), (22, 18, [])]) # multiple self.assertEqual(Cb.getupmoves(sboard, 27), []) # nothing self.assertEqual(Cb.getupmoves(sboard, 3), []) # top self.assertEqual(Cb.getupmoves(sboard, 19), [(19, 15, [])]) # right side
def getrandomaimove(board, color=BLACK): """ Gets a random move. :param board: 32-element list :param color: what color is the AI getting a move for? :return: list: [starting tile, ending tile, (tiles jumped)] """ moves = Cb.getallpossiblemoves(board, color) if (len(moves) != 0): movenum = random.randint(0, len(moves) - 1) move = moves[movenum] return move else: return []
def test_getrownumber(self): self.assertEqual(Cb.getrownumber(0), 0) self.assertEqual(Cb.getrownumber(3), 0) self.assertEqual(Cb.getrownumber(5), 1) self.assertEqual(Cb.getrownumber(21), 5) self.assertEqual(Cb.getrownumber(31), 7)
def test_getstartingboard(self): board = Cb.getstartingboard() self.assertEqual(len(board), 32) # size of board self.assertEqual(board.count(BLACK), 12) # number of black tiles self.assertEqual(board.count(EMPTY), 8) # number of empty tiles self.assertEqual(board.count(RED), 12) # number of red tiles
def test_getemptyboard(self): board = Cb.getemptyboard() self.assertEqual(len(board), 32) # size of board self.assertEqual(board.count(EMPTY), 32) # number of empty tiles
def domakemove(request): board = json.loads(request.GET['board']) move = json.loads(request.GET['move']) return HttpResponse(json.dumps(Cb.makemove(board, move)), content_type="application/json")
def gapmoves(request): board = json.loads(request.GET['board']) color = json.loads(request.GET['color']) return HttpResponse(json.dumps(Cb.getallpossiblemoves(board, color)), content_type="application/json")
def gpmoves(request): board = json.loads(request.GET['board']) tile = json.loads(request.GET['tile']) return HttpResponse(json.dumps(Cb.getpossiblemoves(board, tile)), content_type="application/json")
def iswon(request): board = json.loads(request.GET['board']) return HttpResponse(json.dumps(Cb.iswon(board)), content_type="application/json")
def gsb(request): return HttpResponse(json.dumps(Cb.getstartingboard()), content_type="application/json")