def print_board(self): for row in self.state: for item in row: if item == 1: print_red(item, ' ') elif item == 2: print_blue(item, ' ') else: print(item, end=' ') print('\n')
def undo(): id = request.args.get('id') db, board = decode(id) result = board.undo() if result: board.print_board() else: print_red("No change\n", '') output = update(db, id, board, result) return output
def run_game(): signal.signal(signal.SIGINT, sigint_handler) player, board_size, reverse_status = game_setup() # Play Game with 1 or 2 players winner = play_game(board_size, player) # Results if (reverse_status and winner == 2) or (not reverse_status and winner == 1): print_red(player[0].name + ' has Won.\n', '') elif (reverse_status and winner == 1) or (not reverse_status and winner == 2): print_blue(player[1].name + ' has Won.\n', '') else: print('Game ends in Tie.\n')
def ai_move(db, id, board): player = board.turn + 1 if board.mode != 2: print_red("AI: choosing move\n", '') col = choose_column(board, board.rows, board.difficulty) else: print_red("AI: Choosing worst column\n", '') col = choose_worst_column(board, board.rows, board.difficulty) result = board.place_token(col) game_status = validate_state(db, player, board) output = update(db, id, board, result) output['game_status'] = game_status board.print_board() return output
def place_token(): id = request.args.get('id') col = int(request.args.get('col')) db, board = decode(id) player = board.turn + 1 #Color of current player result = board.place_token(col) #If the move was successful or not if result: board.print_board() else: print_red("No change\n", '') game_status = validate_state(db, player, board) if game_status != 0 or board.players[1] != 'AI': output = update(db, id, board, result) output['game_status'] = game_status output['prev'] = player return output else: return ai_move(db, id, board)
def players_turn(self): statement = "It is " + self.name + "'s turn\n" if self.color == 1: print_red(statement, '') else: print_blue(statement, '')
import sys color_path = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'color') print(os.path.dirname(color_path)) sys.path.append(os.path.dirname(color_path)) import color if __name__ == '__main__': import logging import logging.handlers import io color.print_dark_red('dark red') color.print_red('red') color.print_dark_green('dark green') color.print_green('green') color.print_dark_skyblue('dark skyblue') color.print_skyblue('skyblue') color.print_yellow('yellow') color.print_purple('purple') color.print_blue('blue') color.print_white('White Char') color.print_dark_gray('dark gray') color.print_normal('normal for normal words') color.print_italic('italic for comment and other like italic font usage') color.print_bold('bold use like bold font') color.print_ok('ok means that exec action succeed') color.print_warning('warning')
def game_setup(): # Decide if you want to play against an AI or another human print('Select Number of Players\nChoose 1 to play an AI, and choose 2 to play a friend.') while True: try: num_players = int(input('Number of Players (1 or 2): ')) except ValueError: print('Invalid Input') continue if num_players is 1 or num_players is 2: break else: print('Invalid Number of Players') # Select board size while True: try: board_size = int(input('Select a board length from 6 to 8: ')) except ValueError: print('Invalid Input') continue if 5 < board_size < 9: break else: print('Invalid Board Size') # Decide which mode of Connect4 you wish to play reverse_mode = False mayhem_mode = False while True: try: mode_selection = input('Type C to play normal Connect4, D to play "Dont Connect 4!", and M to play Mayhem! ') except ValueError: print('Invalid Input') continue if mode_selection == 'C' or mode_selection == 'c': break elif mode_selection == 'D' or mode_selection == 'd': reverse_mode = True break elif mode_selection == 'M' or mode_selection == 'm': mayhem_mode = True break else: print('Invalid Input') if num_players == 1: while True: try: difficulty = int(input('Select difficulty level from 1 (Easy) to 9 (Hard): ')) except ValueError: print('Invalid Input') continue if difficulty < 1 or difficulty > 9: print('Invalid Difficulty Level') continue else: difficulty += 2 break player_name = input("Please enter player1's name: ") player1 = Player(player_name, 1, reverse=reverse_mode, mayhem=mayhem_mode) print_red('You are Player 1.\n', '') print_blue('The AI is Player 2.\n', '') player2 = Player("AI", 2, difficulty, reverse_mode, mayhem_mode) else: player_name = input("Please enter player1's name: ") player1 = Player(player_name, 1, reverse=reverse_mode, mayhem=mayhem_mode) player_name = input("Please enter player2's name: ") player2 = Player(player_name, 2, reverse=reverse_mode, mayhem=mayhem_mode) return [player1, player2], board_size, reverse_mode