def process_turn(game: Board, players: list, player_turn: int, timers: TimerManager, underlining): valid_move = False while not valid_move: if game.check_capacity() == 0: raise BoardFullError print("Current board:") game.print_board(underlining=underlining) timers.start_timer(players[player_turn]) try: move = input(f"Player {players[player_turn]}, make your move:\n") move = move.split() if len(move) != 2: raise InvalidInputError game.set_tile(int(move[0]), int(move[1]), players[player_turn]) valid_move = True except OccupiedSpaceError: print("Invalid move. That space is already occupied). Try again.") except InvalidInputError: print( "Invalid input. Please enter two coordinates (row & column) separated by a space." ) else: timers.end_timer(players[player_turn]) player_turn = player_turn + 1 if player_turn + 1 != len( players) else 0 return player_turn
def main(): running = True game = Board() player_turn = 0 player_timers = TimerManager() while running: in_game = True # Initialize player list, board, and if the user's output supports underlining players = init_players() init_board(game) underlining = check_underlining() while in_game: try: # Processes the turn AND advances who's turn it is. player_turn = process_turn(game, players, player_turn, player_timers, underlining) except BoardFullError: print("All spaces have become occupied!") in_game = False else: if game.winner is not None: in_game = False game.print_endscreen(player_timers) play_again = input( "Would you like to start a new game? (Enter 'yes' or 'y' if so)\n") if play_again.lower() not in ("yes", 'y'): running = False print("Terminating program. Thank you for playing!")
def init_board(board: Board): valid_board_size = False board_size = 3 while not valid_board_size: board_size = int( input( "How big would you like the board to be? (Enter an integer)\n") ) if board_size >= 3: valid_board_size = True board.reset(board_size) else: print("The board size must be 3 or greater.") return board
def connect_four(player1, player2): """ Plays a game of Connect Four between the two specified players, and returns the Board object as it looks at the end of the game. inputs: player1 and player2 are objects representing Connect Four players (objects of the Player class or a subclass of Player). One player should use 'X' checkers and the other should use 'O' checkers. """ # Make sure one player is 'X' and one player is 'O'. if player1.checker not in 'XO' or player2.checker not in 'XO' \ or player1.checker == player2.checker: print('need one X player and one O player.') return None print('Welcome to Connect Four!') print() board = Board(6, 7) print(board) while True: if process_move(player1, board) == True: return board if process_move(player2, board) == True: return board
def next_move(self, board): list_available_col = [] for i in range(board.width): if Board.can_add_to(board, i) == True: list_available_col += [i] chosen_col = random.choice(list_available_col) self.num_moves += 1 return chosen_col
def game(clock, size, difficulty): run = True sudoku_board, solved_board = sudoku_solver.generateBoard(size, difficulty) board = Board(sudoku_board, WINDOW_WIDTH, WINDOW_HEIGHT) while run: clock.tick(FPS) SCREEN.fill((255, 255, 255)) for event in pygame.event.get(): #if the user pressed X or esc they return back to main menu if event.type == pygame.QUIT: run = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: run = False gameControls(event, board, solved_board) board.draw(SCREEN) pygame.display.update()
def process_move(player, board): """performs all of the steps involved in processing a single move by the specified player on the specified board""" print() s = player.__repr__() + "'s turn" print(s) chosen_col = player.next_move(board) Board.add_checker(board, player.checker, chosen_col) print() print(board) print() if Board.is_win_for(board, player.checker) == True: print(player.__repr__(), 'wins in', player.num_moves, 'moves.') print('Congratulations!') return True elif Board.is_full(board) == True and Board.is_win_for( board, player.checker) == False: print("It's a tie!") return True else: return False
def main(): # get setup parameters limb, distance = get_distance_data() # read colour data from file colours = get_object_data() # create board class theBoard = Board() # create stack class instance baxter = robot(limb, distance, colours, theBoard) print "limb = ", limb print "distance = ", distance # open the gripper baxter.gripper.open() # move to start position baxter.start_position() # wait for camera feedback rospy.sleep(1) baxter.send_image('sleep') baxter.locate_board() baxter.send_image('default') game = True while game: theBoard.printBoard() baxter.send_image('sleep') baxter.detect_pieces() theBoard.printBoard() if theBoard.checkWinner(baxter.playerPiece): baxter.send_image('anger') theBoard.printBoard() print('Hooray! You have won the game!') break elif theBoard.isFull(): baxter.send_image('default') theBoard.printBoard() print('The game is a tie!') break baxter.send_image('default') baxter.take_turn() if theBoard.checkWinner('R'): baxter.send_image('happy') theBoard.printBoard() print('The computer has beaten you! You lose.') game = False elif theBoard.isFull(): baxter.send_image('default') theBoard.printBoard() print('The game is a tie!') break sys.exit() rospy.spin()
import pygame import time from drawBoard import * from BoardClass import Board from OpponentClass import Opponent pygame.init() board = Board() boardDrawer = BoardDrawer() opponent = Opponent() #draw initial board boardDrawer.init_board_draw() pygame.display.update() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONUP: mousePos = pygame.mouse.get_pos() x = mousePos[0] // 75 y = mousePos[1] // 75 if board.moveKnight(x, y): boardDrawer.draw_knights(board) opponent.makeMove(board) pygame.display.update() time.sleep(0.03) boardDrawer.draw_knights(board)
def __init__(self): self.board = Board() self.move_input = GetMoves()
display_size = list(gameDisplay.get_size()) # print(display_size) gameSurface = pygame.Surface([display_size[0], display_size[1]]) gameSurface.convert() gameSurface.fill(bg_color.value) return (gameDisplay, gameSurface) pygame.init() screens = startGame() newBoard = Board(screens[1], screens[1].get_size(), amount_boards=(5, 5), line_color=Colors.WHITISH, hasBorder=False) clock = pygame.time.Clock() running = True mouse_pos = () while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() newBoard.check_Squares(mouse_pos)
#!/usr/bin/env python import rospy from BoardClass import Board from turn_msg.srv import Turn, TurnResponse board = Board() player = 'x' def hook(player): print player + " won!!" def update_board(request): global player if (player == request.player): if (board.put_mark(player, request.x, request.y)): if (board.check_winner()): rospy.loginfo(player + " won!") return board.board if (player == 'x'): player = 'o' else: player = 'x' return board.board else: rospy.logerr("This cell is already filled!") else:
def __init__(self, arm, distance, colours): # piece data self.playerPiece = 'X' self.playerPieceIndex = 2 # ( 1 or 2, 3 = board) self.robotPiece = 'Y' self.robotPieceIndex = 1 # space detection self.board = Board() self.boardHeight = 0.289 self.boardWidth = 0.410 self.storePointPix = True self.pixelPoints = [] # Background subtraction self.bgsubImg = None self.img = None # arm ("left" or "right") self.limb = arm self.limb_interface = baxter_interface.Limb(self.limb) if arm == "left": self.other_limb = "right" else: self.other_limb = "left" self.other_limb_interface = baxter_interface.Limb(self.other_limb) # gripper ("left" or "right") self.gripper = baxter_interface.Gripper(arm) # object color dictionary data self.objects = colours # zeroed lists for pixel coordinates of objects self.x = np.zeros(shape=(len(colours)), dtype=int) self.y = np.zeros(shape=(len(colours)), dtype=int) # speeds self.normal = 0.3 self.slow = 0.1 # start positions self.start_pos_x = 0.50 # x = front back self.start_pos_y = 0.30 # y = left right self.start_pos_z = 0.15 # z = up down self.roll = -1.0 * math.pi # roll = horizontal self.pitch = 0.0 * math.pi # pitch = vertical self.yaw = 0.0 * math.pi # yaw = rotation self.pose = [self.start_pos_x, self.start_pos_y, self.start_pos_z, \ self.roll, self.pitch, self.yaw] # distances self.distance = distance self.block_height = 0.05 self.block_pickup_height = self.distance - self.block_height self.block_stack_height = self.distance - (self.block_height * 2) self.block_grip_height = 0.092 self.block_tolerance = 0.005 # camera parameters (NB. other parameters in open_camera) self.cam_calib = 0.0025 # meters per pixel at 1 meter #self.cam_x_offset = 0.045 # original camera gripper offset #self.cam_y_offset = -0.01 self.cam_x_offset = 0.035 # camera gripper offset self.cam_y_offset = -0.02 self.width = 960 # Camera resolution self.height = 600 # Enable the actuators baxter_interface.RobotEnable().enable() # set speed as a ratio of maximum speed self.limb_interface.set_joint_position_speed(0.8) self.other_limb_interface.set_joint_position_speed(0.8) # calibrate the gripper self.gripper.calibrate() # set camera resolution self.config_camera(self.limb, self.width, self.height) # subscribe to required camera self.subscribe_to_camera(self.limb) # move other arm out of harms way if arm == "left": self.baxter_ik_move("right", (0.25, -0.50, 0.2, math.pi, 0.0, 0.0)) else: self.baxter_ik_move("left", (0.25, 0.50, 0.2, math.pi, 0.0, 0.0))