def test_strategy(): b = mancala.Board() s = mancala.GreedyStrategy(0) pos, bp = s.best_play(b) assert pos == 2 assert bp.pits == [ [4, 4, 0, 0, 6, 6], [5, 5, 4, 4, 4, 4]] assert bp.score == [2, 0]
def main(): board = mancala.Board() board.position[0] = window_size[0]/2 - board.size[0]/2 board.position[1] = window_size[1]/2 - board.size[1]/2 while True: board.update() board.draw() pygame.Surface.blit(display, board.image, board.position) pygame.display.update()
def play(): board = mancala.Board() board.show() firstInput = input("Going first? (y/n): ").lower() if "y" in firstInput: playing = True else: playing = False while not board.noMoreMoves(): if playing: print("\nSearching Minimax Tree w/ Alpha-Beta Pruning...") t = time() score, sequence = minimax.minimaxAlphaBeta(board) print(f"Finished calculations (time {(time() - t) * 1000:.2f}ms).") print(f"\nYour best move this turn: pot(s) {', then '.join([str(move + 1) for move in sequence[0]])}. ") for move in sequence[0]: playing, takeOpposite = board.playerMove(move) else: opponentMove = input("Enter the move your opponent made: ") notPlaying, takeOpposite = board.opponentMove(int(opponentMove) - 1) playing = not notPlaying if notPlaying: print("[GO AGAIN]") if takeOpposite: print("[TAKE OPPOSITE STONES]") board.show() if board.heuristicScore() > 0: print(f"I win ({board.state[6]} to {board.state[13]}! ") elif board.heuristicScore() < 0: print(f"Opponent wins ({board.state[13]} to {board.state[6]}! ")
def play(): b = mancala.Board() s = b.start() ai = mcts.MonteCarlo(b) while not b.game_over(s): if b.current_player(s) == 1: print(s[1:8]) print(s[8:15]) p = int(input("Your play {}: ".format(b.legal_plays(s)))) while not p in b.legal_plays(s): p = int(input("Illegal. Pick again: ")) else: p = ai.get_play(s) print("Computer plays {}".format(p)) s = b.next_state(s, p) score = b.score(s) if score == 1: print("You win!") if score == -1: print("Computer wins!") if score == 0: print("Tie!")
def test_move_3(): b = mancala.Board() bp, again = mancala.move(b, 0, 3) assert again == False assert bp.pits == [[4, 4, 4, 0, 5, 5], [5, 4, 4, 4, 4, 4]] assert bp.score == [1, 0]
def test_board(): b = mancala.Board() assert b.score == [0, 0] assert len(b.pits) == 2 assert len(b.pits[0]) == 6 assert len(b.pits[1]) == 6
def test_move_2(): b = mancala.Board() bp, again = mancala.move(b, 0, 2) assert again == True assert bp.pits == [[4, 4, 0, 5, 5, 5], [4, 4, 4, 4, 4, 4]] assert bp.score == [1, 0]
def test_move_0(): b = mancala.Board() bp, again = mancala.move(b, 0, 0) assert again == False assert bp.pits == [[0, 5, 5, 5, 5, 4], [4, 4, 4, 4, 4, 4]] assert bp.score == [0, 0]
def board(): return mancala.Board()