Пример #1
0
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
Пример #2
0
 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])])
Пример #3
0
 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)
Пример #4
0
 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
Пример #5
0
 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
Пример #6
0
 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])])
Пример #7
0
 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
Пример #8
0
def gsb(request):
    return HttpResponse(json.dumps(Cb.getstartingboard()),
                        content_type="application/json")