コード例 #1
0
def main():
    print ("***************************************************")
    print ("*                    Checkers                     *")
    print ("*                                                 *")
    print ("*                 Andrew Edwards                  *")
    print ("*            www.almostimplemented.com            *")
    print ("***************************************************")
    print ("\n")
    print ("\n")

    n = -1
    while not n in [0, 1, 2]:
        n = raw_input("How many human players? (0, 1, 2): ")
        try:
            n = int(n)
        except ValueError:
            print "Please input 0, 1, or 2."

    if n == 2:
        B = checkers.CheckerBoard()
        print "Black moves first."
        turn = 1
        current_player = B.active
        while not game_over(B):
            print B

            legal_moves = B.get_moves()

            if B.jump:
                print "Make jump."
                print ""
            else:
                print "Turn %i" % turn
                print ""

            for (i, move) in enumerate(get_move_strings(B)):
                print "Move " + str(i) + ": " + move

            while True:
                move_idx = raw_input("Enter your move number: ")
                try:
                    move_idx = int(move_idx)
                except ValueError:
                    print "Please input a valid move number."
                    continue

                if move_idx in range(len(legal_moves)):
                    break
                else:
                    print "Please input a valid move number."
                    continue

            B.make_move(legal_moves[move_idx])

            # If jumps remain, then the board will not update current player
            if B.active == current_player:
                print "Jumps must be taken."
                continue
            else:
                current_player = B.active
                turn += 1

        print B
        if B.active == WHITE:
            print "Congrats Black, you win!"
        else:
            print "Congrats White, you win!"

        return 0


    elif n == 1:
        agent_module = raw_input("Enter name of agent module: ");
        __import__(agent_module)
        agent_module = sys.modules[agent_module]
        cpu = agent.CheckersAgent(agent_module.move_function, agent_module.return_moves)
        while True:
            choice = raw_input("Enter 0 to go first and 1 to go second: ")
            try:
                choice = int(choice)
                break
            except ValueError:
                print "Please input 0 or 1."
                continue
        turn = 0
        B = checkers.CheckerBoard()
        current_player = B.active
        print "Black moves first."
        while not B.is_over():
            print B
            if turn % 2 == choice:
                legal_moves = B.get_moves()
                if B.jump:
                    print "Make jump."
                    print ""
                else:
                    print "Turn %i" % (turn + 1)
                    print ""
                move_strings = get_move_strings(B)
                for (i, move) in enumerate(move_strings):
                    print "Move " + str(i) + ": " + move
                while True:
                    move_idx = raw_input("Enter your move number: ")
                    try:
                        move_idx = int(move_idx)
                    except ValueError:
                        print "Please input a valid move number."
                        continue
                    if move_idx in range(len(legal_moves)):
                        break
                    else:
                        print "Please input a valid move number."
                        continue
                B.make_move(legal_moves[move_idx])
                humanmove = move_strings[move_idx]
                tgt1 = int(cpumove(:2))
                tgt2 = int(cpumove(-2:))
                
                # If jumps remain, then the board will not update current player
                if B.active == current_player:
                    print "Jumps must be taken."
                    continue
                else:
                    current_player = B.active
                    turn += 1
            else:
                print "!!!!Printing CPU Move!!!!\n"
                cpumove = cpu.return_moves(B)
                tgt1 = int(cpumove(:2))
                tgt2 = int(cpumove(-2:))
                print "Done printing"
                B.make_move(cpu.make_move(B))
                if B.active == current_player:
                    print "Jumps must be taken."
                    continue
                else:
                    current_player = B.active
                    turn += 1
        print B
        if B.active == WHITE:
            print "Congrats Black, you win!"
        else:
            print "Congrats White, you win!"
        return 0
    else:
        agent_module = raw_input("Enter name of first agent module: ");
        __import__(agent_module)
        agent_module = sys.modules[agent_module]
        cpu_1 = agent.CheckersAgent(agent_module.move_function)
        agent_module = raw_input("Enter name of second agent module: ");
        __import__(agent_module)
        agent_module = sys.modules[agent_module]
        cpu_2 = agent.CheckersAgent(agent_module.move_function)
        debug = raw_input("Would you like to step through game play? [Y/N]: ")
        debug = 1 if debug.lower()[0] == 'y' else 0
        B = checkers.CheckerBoard()
        current_player = B.active
        if debug:
            print "sorry not ready"
            return 0
        else:
            while not B.is_over():
                B.make_move(cpu_1.make_move(B))
                if B.active == current_player:
                    continue
                current_player = B.active
                while B.active == current_player and not B.is_over():
                    B.make_move(cpu_2.make_move(B))
                current_player = B.active
            if B.active == WHITE:
                print "Congrats Black, you win!"
            else:
                print "Congrats White, you win!"
            return 0
コード例 #2
0
def main():
    n = -1
    while not n in [0, 1, 2]:
        n = input("How many human players? (0, 1, 2): ")
        try:
            n = int(n)
        except ValueError:
            print("Please input 0, 1, or 2.")

    if n == 2:
        B = checkers.CheckerBoard()
        print("Black moves first.")
        turn = 1
        current_player = B.active
        while not game_over(B):
            print(B)

            legal_moves = B.get_moves()

            if B.jump:
                print("Make jump.\n")
            else:
                print("Turn %i" % turn)

            for (i, move) in enumerate(get_move_strings(B)):
                print("Move " + str(i) + ": " + move)

            while True:
                move_idx = input("Enter your move number: ")
                try:
                    move_idx = int(move_idx)
                except ValueError:
                    print("Please input a valid move number.")
                    continue

                if move_idx in range(len(legal_moves)):
                    break
                else:
                    print("Please input a valid move number.")
                    continue

            B.make_move(legal_moves[move_idx])

            # If jumps remain, then the board will not update current player
            if B.active == current_player:
                print("Jumps must be taken.")
                continue
            else:
                current_player = B.active
                turn += 1

        print(B)
        if B.active == WHITE:
            print("Congrats Black, you win!")
        else:
            print("Congrats White, you win!")

        return 0


    elif n == 1:
        agent_module = input("Enter name of agent module: ")
        __import__(agent_module)
        agent_module = sys.modules[agent_module]
        cpu = agent.CheckersAgent(agent_module.move_function)
        while True:
            choice = input("Enter 0 to go first and 1 to go second: ")
            try:
                choice = int(choice)
                break
            except ValueError:
                print("Please input 0 or 1.")
                continue
        turn = 0
        B = checkers.CheckerBoard()
        current_player = B.active
        print("Black moves first.")
        while not B.is_over():
            print(B)
            if turn % 2 == choice:
                legal_moves = B.get_moves()
                if B.jump:
                    print("Make jump.\n")
                else:
                    print("Turn %i \n" % (turn + 1))
                for (i, move) in enumerate(get_move_strings(B)):
                    print("Move " + str(i) + ": " + move)
                while True:
                    move_idx = input("Enter your move number: ")
                    try:
                        move_idx = int(move_idx)
                    except ValueError:
                        print("Please input a valid move number.")
                        continue
                    if move_idx in range(len(legal_moves)):
                        break
                    else:
                        print("Please input a valid move number.")
                        continue
                B.make_move(legal_moves[move_idx])
                # If jumps remain, then the board will not update current player
                if B.active == current_player:
                    print("Jumps must be taken.")
                    continue
                else:
                    current_player = B.active
                    turn += 1
            else:
                B.make_move(cpu.make_move(B))
                if B.active == current_player:
                    print("Jumps must be taken.")
                    continue
                else:
                    current_player = B.active
                    turn += 1
        print(B)
        if B.active == WHITE:
            print("Congrats Black, you win!")
        else:
            print("Congrats White, you win!")
        return 0
    else:
        agent_module = input("Enter name of first agent module: ")
        __import__(agent_module)
        agent_module = sys.modules[agent_module]
        cpu_1 = agent.CheckersAgent(agent_module.move_function)
        agent_module = input("Enter name of second agent module: ")
        __import__(agent_module)
        agent_module = sys.modules[agent_module]
        cpu_2 = agent.CheckersAgent(agent_module.move_function)
        debug = input("Would you like to step through game play? [Y/N]: ")
        debug = 1 if debug.lower()[0] == 'y' else 0
        B = checkers.CheckerBoard()
        current_player = B.active
        if debug:
            print("sorry not ready")
            return 0
        else:
            while not B.is_over():
                B.make_move(cpu_1.make_move(B))
                if B.active == current_player:
                    continue
                current_player = B.active
                while B.active == current_player and not B.is_over():
                    B.make_move(cpu_2.make_move(B))
                current_player = B.active
            if B.active == WHITE:
                print("Congrats Black, you win!")
            else:
                print("Congrats White, you win!")
            return 0
コード例 #3
0
def main():
    print "***************************************************"
    print "*                    Checkers                     *"
    print "*                                                 *"
    print "*                 Andrew Edwards                  *"
    print "*            www.almostimplemented.com            *"
    print "***************************************************"
    print "\n"
    print "\n"

    n = -1
    while not n in [0, 1, 2, 3]:
        n = raw_input("How many human players? (0, 1, 2): ")
        try:
            n = int(n)
        except ValueError:
            print "Please input 0, 1, or 2."
    if n == 2:
        B = checkers.CheckerBoard()
        print "Black moves first."
        turn = 1
        current_player = B.active
        while not game_over(B):
            print B

            legal_moves = B.get_moves()

            if B.jump:
                print "Make jump."
                print ""
            else:
                print "Turn %i" % turn
                print ""

            for (i, move) in enumerate(get_move_strings(B)):
                print "Move " + str(i) + ": " + move

            while True:
                move_idx = raw_input("Enter your move number: ")
                try:
                    move_idx = int(move_idx)
                except ValueError:
                    print "Please input a valid move number."
                    continue

                if move_idx in range(len(legal_moves)):
                    break
                else:
                    print "Please input a valid move number."
                    continue

            B.make_move(legal_moves[move_idx])

            # If jumps remain, then the board will not update current player
            if B.active == current_player:
                print "Jumps must be taken."
                continue
            else:
                current_player = B.active
                turn += 1

        print B
        if B.active == WHITE:
            print "Congrats Black, you win!"
        else:
            print "Congrats White, you win!"

        return 0

    elif n == 1:
        agent_module = raw_input("Enter name of agent module: ");
        __import__(agent_module)
        agent_module = sys.modules[agent_module]
        cpu = agent.CheckersAgent(agent_module.move_function)
        while True:
            choice = raw_input("Enter 0 to go first and 1 to go second: ")
            try:
                choice = int(choice)
                break
            except ValueError:
                print "Please input 0 or 1."
                continue
        turn = 0
        B = checkers.CheckerBoard()
        current_player = B.active
        print "Black moves first."
        while not B.is_over():
            print B
            if turn % 2 == choice:
                legal_moves = B.get_moves()
                if B.jump:
                    print "Make jump."
                    print ""
                else:
                    print "Turn %i" % (turn + 1)
                    print ""
                for (i, move) in enumerate(get_move_strings(B)):
                    print "Move " + str(i) + ": " + move
                while True:
                    move_idx = raw_input("Enter your move number: ")
                    try:
                        move_idx = int(move_idx)
                    except ValueError:
                        print "Please input a valid move number."
                        continue
                    if move_idx in range(len(legal_moves)):
                        break
                    else:
                        print "Please input a valid move number."
                        continue
                B.make_move(legal_moves[move_idx])
                # If jumps remain, then the board will not update current player
                if B.active == current_player:
                    print "Jumps must be taken."
                    continue
                else:
                    current_player = B.active
                    turn += 1
            else:
                B.make_move(cpu.make_move(B))
                if B.active == current_player:
                    print "Jumps must be taken."
                    continue
                else:
                    current_player = B.active
                    turn += 1
        print B
        if B.active == WHITE:
            print "Congrats Black, you win!"
        else:
            print "Congrats White, you win!"
        return 0
    else:
        #stats=[0,0,0,0] #total, black win, white win, draw
        agent_module = raw_input("Enter name of first agent module: ");
        __import__(agent_module)
        agent_module = sys.modules[agent_module]
        cpu_1 = agent.CheckersAgent(agent_module.move_function, agent_module.end_function)
        agent_module = raw_input("Enter name of second agent module: ");
        __import__(agent_module)
        agent_module = sys.modules[agent_module]
        cpu_2 = agent.CheckersAgent(agent_module.move_function, agent_module.end_function)
        debug = raw_input("Would you like to step through game play? [Y/N]: ")
        debug = 1 if debug.lower()[0] == 'y' else 0
        #while stats[0] < TRAIN_GAMES:
        while True:
            #print(stats[0])
            #stats[0]=stats[0]+1
            B = checkers.CheckerBoard()
            current_player = B.active
            if debug:
                print "sorry not ready"
                return 0
            else:
                counter=0
                while not B.is_over() and counter<MOVE_LIMIT:
                    B.make_move(cpu_1.make_move(B))
                    if B.active == current_player:
                        continue
                    current_player = B.active
                    while B.active == current_player and not B.is_over():
                        B.make_move(cpu_2.make_move(B))
                    current_player = B.active
                    counter=counter+1
                if counter==MOVE_LIMIT: #game gets stuck, both lose, terminate
                    cpu_1.inform_endgame(B, True)
                    cpu_2.inform_endgame(B, True)
                else:
                    cpu_1.inform_endgame(B, False)
                    cpu_2.inform_endgame(B, False)
コード例 #4
0
ファイル: test.py プロジェクト: HryMar/DS-courses
import checkers
import agent
import arthur
import random_agent

BLACK, WHITE = 0, 1

f = open('logfile', 'w')

for i in range(100):
    print "game: " + str(i)
    B = checkers.CheckerBoard()
    cpu_1 = agent.CheckersAgent(lambda board: arthur.move_function(board, 4))
    cpu_2 = agent.CheckersAgent(lambda board: arthur.move_function(board, 6))
    current_player = B.active
    turn = 1
    while not B.is_over():
        f.write(str(B))
        if turn % 100 == 0:
            print "# of turns: " + str(turn)
        B.make_move(cpu_1.make_move(B))
        if B.active == current_player:
            continue
        current_player = B.active
        turn += 1
        while not B.is_over() and B.active == current_player:
            B.make_move(cpu_2.make_move(B))
        current_player = B.active
    if B.active == WHITE:
        print "Congrats Black, you win!"
    else:
コード例 #5
0
ファイル: game.py プロジェクト: rpandya922/sawyer_checkers
def main():
    # Initiate node, listener, and gripper
    rospy.init_node("game")
    listener = tf.TransformListener()
    right_gripper = robot_gripper.Gripper('right')

    # Publishers for human and robot moves
    human_pub = rospy.Publisher('human_moves', CheckersMove, queue_size=10)
    robot_pub = rospy.Publisher('robot_moves', CheckersMove, queue_size=10)
    calibration_pub = rospy.Publisher('board_calibration',
                                      BoardCalibration,
                                      queue_size=10)
    cpu = agent.CheckersAgent(arthur.move_function)

    # Initialize game
    choice = 1
    turn = 0
    B = checkers.CheckerBoard()
    current_player = B.active
    print "Black moves first."

    while not rospy.is_shutdown():
        right_gripper.open()

        try:
            # Create and initialize the MoveGroup
            group = MoveGroupCommander("right_arm")
            init_move_group(group)
            obs_group = MoveGroupCommander("left_arm")
            init_move_group(obs_group)

            move_group_to(obs_group, UL_POS[0], UL_POS[1], UL_POS[2],
                          UL_ORIENTATION[0], UL_ORIENTATION[1],
                          UL_ORIENTATION[2], UL_ORIENTATION[3])
            raw_input("wait")
            upper_left = get_artag_location(listener, "ar_marker_23")

            move_group_to(obs_group, LR_POS[0], LR_POS[1], LR_POS[2],
                          LR_ORIENTATION[0], LR_ORIENTATION[1],
                          LR_ORIENTATION[2], LR_ORIENTATION[3])
            raw_input("wait")
            lower_right = get_artag_location(listener, "ar_marker_22")

            raw_input("wait")
            move_group_to(obs_group, OBS_POS[0], OBS_POS[1], OBS_POS[2],
                          OBS_ORIENTATION[0], OBS_ORIENTATION[1],
                          OBS_ORIENTATION[2], OBS_ORIENTATION[3])

            cg = RobotCheckers(upper_left, lower_right)

            calibration_pub.publish(upper_left[0], upper_left[1], upper_left[2], \
                                    lower_right[0], lower_right[1], lower_right[2])

            # Game loop
            while not B.is_over():
                print B

                # Human's turn
                if turn % 2 == choice:
                    moves = zip(B.get_moves(), get_move_strings(B))
                    human_move = cg.detect_opponent_move(listener, obs_group)
                    human_move = [
                        move for (move, move_tuple) in moves
                        if move_tuple == human_move
                    ]
                    if human_move != -1:
                        human_pub.publish(human_move[0], human_move[1])
                    # print human_move
                    if len(human_move) <= 0:
                        legal_moves = B.get_moves()
                        if B.jump:
                            print "Make jump."
                            print ""
                        else:
                            print "Turn %i" % (turn + 1)
                            print ""
                        for (i, move) in enumerate(get_move_strings(B)):
                            print "Move " + str(i) + ": " + str(move)
                        while True:
                            move_idx = raw_input("Enter your move number: ")
                            try:
                                move_idx = int(move_idx)
                            except ValueError:
                                print "Please input a valid move number."
                                continue
                            if move_idx in range(len(legal_moves)):
                                break
                            else:
                                print "Please input a valid move number."
                                continue
                        B.make_move(legal_moves[move_idx])
                    else:
                        B.make_move(human_move[0])

                    # If jumps remain, then the board will not update current player
                    if B.active == current_player:
                        print "Jumps must be taken."
                        continue
                    else:
                        current_player = B.active
                        turn += 1
                else:
                    # Baxter's turn
                    move, move_tuple = cpu.make_move(B)
                    taken_pieces = B.make_move(move)
                    start, end = move_tuple
                    print start, end
                    robot_pub.publish(start, end)
                    cg.set_taken_pieces(B.state)
                    # cg.robot_make_move(group, right_gripper, listener, start, end, taken_pieces)

                    if B.active == current_player:
                        print "Jumps must be taken."
                        continue
                    else:
                        current_player = B.active
                        turn += 1

        except rospy.ServiceException, e:
            print "Service call failed: %s" % e