def main(): '''function that runs connectfour consol''' print('Welcome to ConnectFour! \n') print('Player 1, you are the R player. Player 2, you are the Y player') the_winner = connectfour.NONE player_one = connectfour.RED player_two = connectfour.YELLOW connectfour_function.display_board(connectfour.new_game_state().board) game_state = connectfour.new_game_state() #play the game until it ends while the_winner == connectfour.NONE: if game_state.turn == player_one: print('Player 1 this is your turn. please make a move') else: print('Player 2 this is your turn. please make a move') try: move = int(input('where will you drop your piece? type 1~7')) - 1 game_state = connectfour.drop_piece(game_state, move) connectfour_function.display_board(game_state.board) the_winner = connectfour.winning_player(game_state) except ValueError: print('Invalid input. Please type integer between 1 and 7') except connectfour.InvalidConnectFourMoveError: print('The column is full. Please select other column') #print the winner if the_winner == player_one: print('player 1, you won!') else: print('player 2, you won!')
def main(): '''function that runs connectfour consol''' print('Welcome to ConnectFour! \n') print('Player 1, you are the R player. Player 2, you are the Y player') the_winner = connectfour.NONE player_one = connectfour.RED player_two = connectfour.YELLOW connectfour_function.display_board(connectfour.new_game_state().board) game_state = connectfour.new_game_state() #play the game until it ends while the_winner == connectfour.NONE: if game_state.turn == player_one: print('Player 1 this is your turn. please make a move') else: print('Player 2 this is your turn. please make a move') try: move = int(input('where will you drop your piece? type 1~7'))-1 game_state = connectfour.drop_piece(game_state, move) connectfour_function.display_board(game_state.board) the_winner = connectfour.winning_player(game_state) except ValueError: print('Invalid input. Please type integer between 1 and 7') except connectfour.InvalidConnectFourMoveError: print('The column is full. Please select other column') #print the winner if the_winner == player_one: print('player 1, you won!') else: print('player 2, you won!')
def who_r_you(): state = connectfour.new_game_state() mode = input( 'Please enter what mode you want to play as (s: server, c: client, con: console): ' ) if mode == 's': return server(state) elif mode == 'c': return client(state) elif mode == 'con': return console() state = connectfour.new_game_state() winner = ' ' print_board(state.board) while winner == ' ': drop_or_pop = input('It\'s ' + state.turn + ' turn. Do you want to drop or pop? ') while drop_or_pop == 'drop': column = input('Which column (0-6) do you want to drop? ') newstate = _drop_piece(state, column) if state != newstate: state = newstate winner = connectfour.winning_player(state) break while drop_or_pop == 'pop': try: column = input('Which column (0-6) do you want to pop? ') newstate = _pop_piece(state, column) if state != newstate: state = newstate winner = connectfour.winning_player(state) break if state.board == connectfour.new_game_state().board: break except: print( 'Error. There is no piece you can pop out.\n' + 'Please look for another column to pop out or you can drop another piece.' ) break while drop_or_pop != 'drop' and drop_or_pop != 'pop': print('Missspell. Please try again, drop or pop?') break else: if winner == 'R': print('Red won.') elif winner == 'Y': print('Yellow won.')
def who_r_you(): state = connectfour.new_game_state() mode=input('Please enter what mode you want to play as (s: server, c: client, con: console): ') if mode == 's': return server(state) elif mode == 'c': return client(state) elif mode == 'con': return console() state = connectfour.new_game_state() winner = ' ' print_board(state.board) while winner == ' ': drop_or_pop = input('It\'s ' + state.turn +' turn. Do you want to drop or pop? ') while drop_or_pop == 'drop': column = input('Which column (0-6) do you want to drop? ') newstate = _drop_piece(state,column) if state != newstate: state = newstate winner = connectfour.winning_player(state) break while drop_or_pop == 'pop': try: column = input('Which column (0-6) do you want to pop? ') newstate = _pop_piece(state,column) if state != newstate: state = newstate winner = connectfour.winning_player(state) break if state.board == connectfour.new_game_state().board: break except: print('Error. There is no piece you can pop out.\n' + 'Please look for another column to pop out or you can drop another piece.') break while drop_or_pop != 'drop' and drop_or_pop != 'pop': print('Missspell. Please try again, drop or pop?') break else: if winner == 'R': print('Red won.') elif winner == 'Y': print ('Yellow won.')
def main_program() -> None: ## while True: State = C.new_game_state() while True: winner = C.winning_player(State) F.new_board(State.board) if winner != C.NONE: break try: print("It's " + F.full_name(State.turn) + " player's turn. ", end = '') command = input(F.Menu).upper() if command == 'A': column_number = int(input('please enter the column number: ')) State = C.drop_piece(State, column_number - 1) print() elif command == 'B': column_number = int(input('please enter the column number: ')) State = C.pop_piece(State, column_number - 1) print() else: F.invalid_command(command) except C.InvalidConnectFourMoveError: print('\nInvaild command. Please try again.\n') except ValueError: print('\nError. Column number must be between 1 and 7.\n') except C.ConnectFourGameOverError: print('\nError. Game is over.\n') if winner == C.RED: print('Red player wins!') elif winner == C.YELLOW: print('Yellow player wins!') print()
def __init__(self: object): """Init new game""" self.state = connectfour.new_game_state() while True: self.print_state() winner = connectfour.winning_player(self.state) if winner != connectfour.NONE: if winner == connectfour.RED: print("Red is the winner!") elif winner == connectfour.YELLOW: print("Yellow is the winner!") break input_is_vaild = False while not input_is_vaild: action = self._get_input() try: if action["action"] == "d": self.state = connectfour.drop_piece( self.state, action["column_number"] - 1) elif action["action"] == "p": self.state = connectfour.pop_piece( self.state, action["column_number"] - 1) else: raise connectfour.InvalidConnectFourMoveError() input_is_vaild = True except: print("Sorry, invalid move.")
def user_interface()->None: print("Welcome to Connect Four!") last_state = connectfour.new_game_state() linkui._rotate_board(last_state) while True: option = linkui.ask_for_option() if option == 'D': try: col = linkui.ask_for_col_drop() except: print('Invalid value.') else: last_state = linkui.option_d(last_state,col) elif option == 'P': try: col = linkui.ask_for_col_pop() except: print('Invalid value.') else: last_state = linkui.option_p(last_state,col) else: print('Invalid Command.') if connectfour.winning_player(last_state) != ' ': print("Player " + connectfour.winning_player(last_state) + " wins!") break rematch()
def init_game(): ''' Creates and instance of the game for 2 players and runs until there is a winner ''' game_state=connectfour.new_game_state() connectfourfunctions.print_board_game(game_state.board) winner = ' ' while True: try: player=(input(game_state.turn + ":Drop and a col number or Pop and a col number: ")) command,col=player.split() col=int(col) game_state=connectfourfunctions.give_turn(game_state,command,col) except: pass winner=connectfour.winning_player(game_state) connectfourfunctions.print_board_game(game_state.board) if (winner != ' '): print(winner+" Won") break
def run_game(): ''' Runs the program ''' game_board = connectfour.new_game_state() current_board(game_board) user_prompt(game_board)
def start_game() -> None: print("\nGame has started") #creates a brand new game state that can be modified game_state = connectfour.new_game_state() while True: #Outputs the game to the console gamelogic.display_board(game_state) if game_state.turn == 'R': print("\nRed Player: Make your move!") if game_state.turn == 'Y': print("\nYellow Player: Make your move!") column_number = gamelogic.ask_for_column() move_type = gamelogic.ask_for_type() #updates the game state with the new moves in place game_state = gamelogic.make_move(game_state, column_number, move_type) #checks to see if a player has won winning_player = connectfour.winning_player(game_state) if winning_player != " ": if winning_player == "R": print("\n### Red Is The Winner! ###") elif winning_player == "Y": print("\n### Yellow Is The Winner! ### ") gamelogic.display_board(game_state) print() break
def run_game(): ''' Runs the program ''' game_board = connectfour.new_game_state() current_board(game_board) handle_command(game_board, ' ')
def menu() -> None: 'This function is the main menu of the connect four console game' game = connectfour.new_game_state() winner = connectfour.NONE while True: while True: username = input('Please enter a username (no spaces!): ').strip() if username.find(' ') < 0: break else: print( 'Invalid input. Please try a username without spaces.\n') host = input('Please enter the host name: ') while True: try: port = int(input('Please enter the port: ')) break except: print('Invalid input, please enter a number.\n') print('Trying to connect...') try: connection = connectfour_protocol.connect(host, port) print('Connected Successfully!') if connectfour_protocol.login(connection, username) == True: print('Login Successful!') if connectfour_protocol.startGame(connection) == True: print('Game Started!') break else: print('Could not Start Game! Try again.\n') connectfour_protocol.close(connection) else: print('Could not log in! Try again.\n') connectfour_protocol.close(connection) except: print('Connection Failed! Try again.\n') try: while winner == connectfour.NONE: while game.turn == connectfour.RED: connectfour_game_functions.printBoard(game) game = connectfour_game_functions.directions_network( game, username, connection) winner = connectfour.winning_player(game) if winner == connectfour.RED: break connectfour_game_functions.printBoard(game) game = connectfour_game_functions.move_network_ai(game, connection) winner = connectfour.winning_player(game) connectfour_game_functions.printBoard(game) if winner == connectfour.RED: print('The winner is ' + username + '!') else: print('The winner is AI!') finally: connectfour_protocol.close(connection)
def main(): ''' Main function flow. ''' connectfour_ui.draw_current_board(connectfour.new_game_state()) current_game_state = connectfour.new_game_state() while True: if connectfour.winning_player(current_game_state) == 1: print("The winner is RED!") break elif connectfour.winning_player(current_game_state) == 2: print("The winner is YELLOW!") break move, column_number = connectfour_ui.move_input() current_game_state = connectfour_ui.move(move, column_number, current_game_state) connectfour_ui.draw_current_board(current_game_state)
def menu() -> None: 'This function is the main menu of the connect four console game' game = connectfour.new_game_state() winner = connectfour.NONE while True: while True: username = input('Please enter a username (no spaces!): ').strip() if username.find(' ') < 0: break else: print('Invalid input. Please try a username without spaces.\n') host = input('Please enter the host name: ') while True: try: port = int(input('Please enter the port: ')) break except: print('Invalid input, please enter a number.\n') print('Trying to connect...') try: connection = connectfour_protocol.connect(host, port) print('Connected Successfully!') if connectfour_protocol.login(connection, username) == True: print('Login Successful!') if connectfour_protocol.startGame(connection) == True: print('Game Started!') break else: print('Could not Start Game! Try again.\n') connectfour_protocol.close(connection) else: print('Could not log in! Try again.\n') connectfour_protocol.close(connection) except: print('Connection Failed! Try again.\n') try: while winner == connectfour.NONE: while game.turn == connectfour.RED: connectfour_game_functions.printBoard(game) game = connectfour_game_functions.directions_network(game, username, connection) winner = connectfour.winning_player(game) if winner == connectfour.RED: break connectfour_game_functions.printBoard(game) game = connectfour_game_functions.move_network_ai(game, connection) winner = connectfour.winning_player(game) connectfour_game_functions.printBoard(game) if winner == connectfour.RED: print('The winner is ' + username + '!') else: print('The winner is AI!') finally: connectfour_protocol.close(connection)
def main(): print('Starting a new game...!') newgamestate = connectfour.new_game_state() ## connectfour.BOARD_ROWS = int(input('How many columns would you like to play with?: ')) ## connectfour.BOARD_COLUMNS = int(input('How many rows would you like to play with?: ')) while True: if newgamestate.turn == 'R': red_turn = int( input( '''Red's turn!\n Please enter the corresponding numbers for desired action\n 1.) Drop piece, 2.)Pop Piece: ''' )) if red_turn == 1: red_drop = int( input( "Enter number of column to drop red piece from 0 to {}: " .format(connectfour.BOARD_COLUMNS - 1))) if red_drop > connectfour.BOARD_COLUMNS or red_drop < 0: raise connectfour.InvalidConnectFourMoveError() else: newgamestate = connectfour.drop_piece( newgamestate, red_drop) print_board(newgamestate) else: red_pop = int( input( 'Enter column number to pop red piece from 0 to {}: '. format(connectfour.BOARD_COLUMNS - 1))) if red_pop > connectfour.BOARD_COLUMNS or red_pop < 0: raise connectfour.InvalidConnectFourMoveError() else: newgamestate = connectfour.pop_piece(newgamestate, red_pop) print_board(newgamestate) else: yellow_turn = int( input( '''Yellow's turn!\n Please enter the corresponding numbers for desired action\n 1.) Drop piece, 2.)Pop Piece: ''' )) if yellow_turn == 1: yellow_drop = int( input( "Enter column number to drop yellow piece from 0 to {}: " .format(connectfour.BOARD_COLUMNS - 1))) newgamestate = connectfour.drop_piece(newgamestate, yellow_drop) print_board(newgamestate) else: yellow_pop = int( input( 'Enter column number to pop yellow piece from 0 to {}: ' .format(connectfour.BOARD_COLUMNS - 1))) newgamestate = connectfour.pop_piece(newgamestate, yellow_pop) print_board(newgamestate) if connectfour.winning_player(newgamestate) == 'R': print('RED player has won! Congradulations!') break elif connectfour.winning_player(newgamestate) == 'Y': print('YELLOW player has won! Congradulations!') break
def _run_user_interface() -> None: """ Main method for running player vs. player """ current_game_state = connectfour.new_game_state() while connectfour.winning_player(current_game_state) == connectfour.NONE: current_game_state = player_turn(current_game_state) print(connectfour_shared_methods.display_winner(current_game_state))
def startstatecheck(Connectionfour) -> connectfour.ConnectFourGameState: '''Handle all the start states protocol checking''' welcomemesg = _welcome_check() connect_four_socket.checkhello(Connectionfour, welcomemesg) connect_four_socket.checkAI(Connectionfour) ConnectFourGameState = connectfour.new_game_state() print('This is the gameboard:') connect_four_common.printboard(ConnectFourGameState) print('game start!') print('You are RED!') return ConnectFourGameState
def mainsingle(): """this is the main function frame that operate in the single player status""" ConnectFourGameState = connectfour.new_game_state() print('Game start!\n') printboard(ConnectFourGameState) print("Enter your move(for example, 'DROP column#' or 'POP column#'\n") while True: #check if there is a winner winnernum = connectfour.winning_player(ConnectFourGameState) if winnernum == connectfour.NONE: if not checkboard(ConnectFourGameState): while True: if ConnectFourGameState[1] == int(1): print("Red turn.") elif ConnectFourGameState[1] == int(2): print("Yellow turn.") try: originalcommand = input("Enter your move: ") order, colnum = command(originalcommand) if (order == 'POP' or order == 'pop') or (order == 'DROP' or order == 'drop'): break else: print( "\nInput format error (must be 'DROP column#' or 'POP column#')\n" ) except: print( "\nInput format error (must be 'DROP column#' or 'POP column#')\n" ) if order == 'DROP' or order == 'drop': try: ConnectFourGameState = connectfour.drop_piece( ConnectFourGameState, colnum - 1) except connectfour.InvalidMoveError: print('\nSorry, this column is already full\n') if order == 'POP' or order == 'pop': try: ConnectFourGameState = connectfour.pop_piece( ConnectFourGameState, colnum - 1) except connectfour.InvalidMoveError: print('\nSorry, this column is empty\n') printboard(ConnectFourGameState) else: print('\nThe board is already full, no winner.\n') return else: break printwinner(winnernum)
def console(): state = connectfour.new_game_state() winner = ' ' print_board(state.board) while winner == ' ': drop_or_pop = input('It\'s ' + state.turn +' turn. Please enter either drop or pop: ') while drop_or_pop == 'drop': column = input('Please enter one number from 0 to 6 for the column you want to drop: ') newstate = _drop_piece(state,column) if state != newstate: state = newstate winner = connectfour.winning_player(state) break while drop_or_pop == 'pop': try: column = input('Please enter one number from 0 to 6 for the column you want to pop: ') newstate = _pop_piece(state,column) if state != newstate: state = newstate winner = connectfour.winning_player(state) break if state.board == connectfour.new_game_state().board: break except: print('Error. There is no piece you can pop out.\n' + 'Please look for another column to pop out or you can drop another piece.') break while drop_or_pop != 'drop' and drop_or_pop != 'pop': print('Error. Please enter either drop or pop.') break else: if winner == 'R': print('Red won.') elif winner == 'Y': print ('Yellow won.')
def menu() -> None: 'This function is the main menu of the connect four console game' game = connectfour.new_game_state() winner = connectfour.NONE while winner == connectfour.NONE: connectfour_game_functions.printBoard(game) game = connectfour_game_functions.directions_console(game) winner = connectfour.winning_player(game) connectfour_game_functions.printBoard(game) if winner == connectfour.RED: print('The winner is Red!') else: print('The winner is Yellow!')
def who_r_you(): state = connectfour.new_game_state() mode=input('Please enter what mode you want to play (s: server, c: client, con: console): ') if mode == 's': return server(state) elif mode == 'c': return client(state) elif mode == 'con': return console() else: print('Error, please try again.') who_r_you()
def mainsingle(): #type in 'asfa' return error """this is the main function frame that operate in the single player status""" ConnectFourGameState = connectfour.new_game_state() print('Game start!\n') printboard(ConnectFourGameState) originalcommand = input( "Enter your move(for example, 'DROP column#' or 'POP column#'): ") # instruct player only once about the input format # print board once at the beginning # print whose turn it is # ? ask user to drop and pop (dont change this one now, lets finish other parts first) # accept both capital and lower case while True: #check if there is a winner winnernum = connectfour.winning_player(ConnectFourGameState) if winnernum == connectfour.NONE: if not checkboard(ConnectFourGameState): while True: try: originalcommand = input("Enter your move: ") commandlist = originalcommand.split() order = commandlist[0] colnum = int(commandlist[1]) if (order == 'POP') or (order == 'DROP'): break except: print( "Input format error (must be 'DROP column#' or 'POP column#')" ) if order == 'DROP': try: ConnectFourGameState = connectfour.drop_piece( ConnectFourGameState, colnum - 1) except (connectfour.InvalidMoveError): print('Sorry, this column is already full') if order == 'POP': try: ConnectFourGameState = connectfour.pop_piece( ConnectFourGameState, colnum - 1) except (connectfour.InvalidMoveError): print('Sorry, this column is empty') printboard(ConnectFourGameState) else: print('The board is already full, no winner.') return else: break printwinner(winnernum)
def run_game(connection: connectfour_network.Connection) -> None: ''' Runs the main loop for the game, sending the user turn to the server and reading the AI move. Breaking when someone wins. ''' game_state = connectfour.new_game_state() connectfour_game.display_board(game_state) while True: game_state = user_turn(connection, game_state) connectfour_game.display_board(game_state) server_message = connectfour_network.read_message(connection) if server_message == "OKAY": new_game_state = server_turn(connection, game_state) if game_state != new_game_state: game_state = new_game_state else: print( "The server has not made a valid move, the connection will close." ) break connectfour_game.display_board(game_state) elif server_message == "WINNER_RED": connectfour_game.game_over(connectfour.RED) break elif server_message == "WINNER_YELLOW": connectfour_game.game_over(connectfour.YELLOW) break else: print(server_message) server_message = connectfour_network.read_message(connection) if server_message == "READY": print("It is your turn again!") print() elif server_message == "WINNER_RED": connectfour_game.game_over(connectfour.RED) break elif server_message == "WINNER_YELLOW": connectfour_game.game_over(connectfour.YELLOW) break else: print(server_message)
def who_r_you(): state = connectfour.new_game_state() mode = input( 'Please enter what mode you want to play (s: server, c: client, con: console): ' ) if mode == 's': return server(state) elif mode == 'c': return client(state) elif mode == 'con': return console() else: print('Error, please try again.') who_r_you()
def main(): loaded = connectfour.new_game_state() while True: message = input( "message(enter 'DROP number(1-7)'or 'POP number(1-7)': ") test = loaded if len(message) == 6 and message[0:5] == "DROP ": if message[5] == "1" or message[5] == "2" or message[ 5] == "3" or message[5] == "4" or message[ 5] == "5" or message[5] == "6" or message[5] == "7": loaded = offline_library.DROP1(message, loaded) if loaded == None: loaded = test continue ###ask user to input POP or DROP, ###check if the operation is valid, ###if the action is invalid, ask user to input again if connectfour.winning_player(loaded) == True: winner = connectfour.winning_player(loaded) if winner == 1: print("Game over, Red wins!") break elif winner == 2: print("Game over, Yellow wins!") break ###call win function to check if there is a winner after user move elif len(message) == 5 and message[0:4] == "POP ": if message[4] == "1" or message[4] == "2" or message[ 4] == "3" or message[4] == "4" or message[ 4] == "5" or message[4] == "6" or message[4] == "7": loaded = offline_library.DROP1(message, loaded) if loaded == None: loaded = test continue if connectfour.winning_player(loaded) == True: winner = connectfour.winning_player(loaded) if winner == 1: print("Game over, Red wins!") break elif winner == 2: print("Game over, Yellow wins!") break else: print("Please enter valid operation.") continue
def game(): try: host = input("Please enter the host: ") port = input("Please enter the port: ") user_name = input("Please enter Username: "******"Invalid host/port") else: while connectfour.winning_player(game_state) == connectfour.NONE: try: print("[ This is "+'['+"R"+']'+" player's turn ]") print() drop_pop = connectfour_console.drop_input() column_number = input("Which column do you want to drop on? 1-7") print() connectfour_console.valid(game_state,drop_pop) game_state = connectfour_console.pop_n_drop(game_state, drop_pop,column_number) except: print('Please enter drop or pop and a column number from 1 - 7') else: print("[ This is "+'['+"Y"+']'+" player's turn ]") print() print( "THE SERVER'S RESPONSE:") write_to_server = (drop_pop.upper() + ' ' + str(column_number)) server_response = I32CFSP.server_drop(the_connection, write_to_server) if server_response == 'WINNER_RED': break if server_response != 'READY': #Adresses the server invalid server_drop = server_response.split()[0] server_col = server_response.split()[1] game_state = connectfour_console.pop_n_drop(game_state, server_drop, server_col) finally: print(' - CONGRATULATIONS PLAYER: '+connectfour.winning_player(game_state)) I32CFSP.socket_close(the_connection)
def main(): # Creat and return ConnectFourGame brand new game game_board = connectfour.new_game_state() while True: # print empty board and updated board after every click by each player user_and_network_module.print_board(game_board) # check the current state to determine the winner if win_check(game_board ) == ' ': # if no winner yet, the next turn is executed. (game_board, column_specified, action_input) = user_and_network_module.turn(game_board) elif win_check( game_board ) != ' ': # if winner is determined, print out the winner and quit the game if win_check(game_board) == 'R': print("Red is the winner of the game") else: print(" Yellow is the winner of the game") quit()
def gameplay() -> None: board = connectfour.new_game_state() display_board(board[0]) while True: try: if connectfour.winning_player(board) == ' ': if board[1] == 'R': print('\nRed\'s Turn') column = int(input('Enter column number: ')) if board[0][column - 1][-1] == 'R': board = pop_or_drop(board, column) else: board = connectfour.drop_piece(board, column - 1) display_board(board[0]) else: print('\nYellow\'s Turn') column = int(input('Enter column number: ')) if board[0][column - 1][-1] == 'Y': board = pop_or_drop(board, column) else: board = connectfour.drop_piece(board, column - 1) display_board(board[0]) else: player = "" if connectfour.winning_player(board) == 'R': player += "Red Player" else: player += "Yellow Player" print('Game is over! ' + player + ' is the winner!') break except IndexError: print('Column number you entered is not in the range of 1-7!\n') display_board(board[0]) except ValueError: print('Not an integer!\n') display_board(board[0]) except connectfour.InvalidConnectFourMoveError: print('Column is full! Please enter another column number!\n') display_board(board[0])
def players_phase(): connect = user_interface() game_board = connectfour.new_game_state() # Creat and return ConnectFourGame brand new game while True: # print empty board and updated board after every click by each player user_and_network_module.print_board(game_board) # check the current state to determine the winner if win_check(game_board)== ' ': # if no winner yet, the next turn is executed. (game_board,column_specified,action_input) = user_and_network_module.turn(game_board) #extract the column number and action input to store in a namedtuple each_turn_action= action_column(action_input,column_specified) #check if your last move indicates the winning move! if win_check(game_board)== 'R': print("Congratulations, You're the new champ!") quit() elif win_check(game_board) == 'Y': print("Yellow is the winner of the game") quit() #client send the action and column number to socket in protocol format client_move = str(each_turn_action.action).upper() + ' ' + str(each_turn_action.column) server_counter_move= protocolmodule.send_valid_move(connect,client_move) #extracted the server's move from the protocol and use it to make a move and update the current game state game_board = computer_move(server_counter_move,game_board) elif win_check(game_board)!= ' ':# if winner is determined, print out the winner and quit the game if win_check(game_board)== 'R': print ("Congratulations! You are the new champ!") else: print(" Yellow is the winner of the game") quit()
def conduct_connectfour_battle(user_host: str, user_port: int, user_id: str) -> None: the_winner = connectfour.NONE the_user = connectfour.RED try: s = connect_socket(user_host, user_port, user_id) setup_game(s) print('Connected! Let the Battle Begin!!!!!') game_state = connectfour.new_game_state() connectfour_consol.display_board(game_state.board) while the_winner == connectfour.NONE: if game_state.turn == the_user: print('%s this is your turn. Please make a move.' % user_id) move = int(input('where will you drop your piece? type 1~7.'))-1 else: game_state = connectfour.drop_piece(game_state, move) display_board(game_state.board) the_winner = connectfour.winning_player(game_state) except: print('Connection failed. Closing the game.') finally: s.close()
def _game() -> None: '''Runs the main game loop in which two players alternate taking turns.''' game_state = connectfour.new_game_state() while True: connectfour_game.display_board(game_state) if _turn(game_state) == connectfour.RED: print("It is Red Player's turn.") elif _turn(game_state) == connectfour.YELLOW: print("It is Yellow Player's turn.") print() column_number = connectfour_game.choose_column() move_type = connectfour_game.choose_move_type() game_state = connectfour_game.move(game_state, column_number, move_type) winning_player = connectfour.winning_player(game_state) if winning_player != connectfour.NONE: connectfour_game.game_over(winning_player) connectfour_game.display_board(game_state) break
def start_game()->connectfour.ConnectFourGameState: '''print an empty play board and create the starting state''' state = connectfour.new_game_state() print('Welcome to ConnectFour!') print_board(state.board) return state
# Jonathan Law and Tim Tandiyo # ICS 32 Project 2 import connectfour import C4S def move_menu(game): while True: C4S.print_board(game) print('Current turn: ' + game.turn) move = C4S.pop_or_drop() game = C4S.make_move(game, move) if connectfour.winning_player(game) == connectfour.RED or connectfour.winning_player(game) == connectfour.YELLOW: C4S.print_board(game) print('Winning player: ' + connectfour.winning_player(game)) break game = connectfour.new_game_state() move_menu(game)
def play_game(connection:i32cfsp.i32cfconnection, username: str) -> None: '''plays game''' last_state = connectfour.new_game_state() linkui._rotate_board(last_state) while True: print(username) option = linkui._ask_for_option() if option == 'D': try: col = linkui._ask_for_col_drop() except: print("invalid value.") else: last_state = linkui._option_d(last_state, col) if connectfour.winning_player(last_state) != ' ': print("Player " + username[1:] + " (Red) wins!") break print() if i32cfsp.login(connection, username): move = send_read_server(option, col, connection) if move[0] == 'D': col_server = int(move[-1]) last_state = linkui._option_d(last_state, col_server) i32cfsp._read_line(connection) elif move[0] == 'P': col_server = int(move[-1]) last_state = linkui._option_p(last_state, col_server) i32cfsp._read_line(connection) if connectfour.winning_player(last_state) != ' ': print("Computer (Yellow) wins!") break else: print("Connection lost") elif option == 'P': try: col = linkui._ask_for_col_pop() except: print("invalid value.") else: last_state = linkui._option_p(last_state, col) if connectfour.winning_player(last_state) != ' ': print("Player " + username[1:]+ " (Red) wins!") break print() if i32cfsp.login(connection, username): move = send_read_server(option, col, connection) if move[0] == 'D': col_server = int(move[-1]) last_state = linkui._option_d(last_state, col_server) i32cfsp._read_line(connection) elif move[0] == 'P': col_server = int(move[-1]) last_state = linkui._option_p(last_state, col_server) i32cfsp._read_line(connection) if connectfour.winning_player(last_state) != ' ': print("Computer (Yellow) wins!") break else: print("Connection lost") else: print('Invalid command')
def init_game(): '''start a fresh board for a new game''' new_game = connectfour.new_game_state() _create_game_board(new_game.board) return new_game
print('Invalid Move') else: return g def game_handler(s: 'Game State'): '''Takes the current game state as an argument and calls the game_play function and the board_display function for each players move and returns the winning player ''' while True: if connectfour.winning_player(s) == connectfour.NONE: if s.turn == connectfour.RED: print('Player RED') s = game_play(s) common_file.board_display(s) elif s.turn == connectfour.YELLOW: print('Player YELLOW') s = game_play(s) common_file.board_display(s) elif connectfour.winning_player(s) == connectfour.RED: return 'RED' elif connectfour.winning_player(s) == connectfour.YELLOW: return 'YELLOW' if __name__ == '__main__': print('Connect Four game') winner = game_handler(connectfour.new_game_state()) print('GAME OVER') print('Player ', winner, ' wins')
def main_program(host, port) -> None: username = _P_.confirm_username() game_socket = socket.socket() try: print('\nconnecting to server...\n') game_socket.connect((host, port)) print('Connected successfully.\n') _P_.send_message(game_socket, str('I32CFSP_HELLO ' + username)) reply1 = _P_.receive_message(game_socket) print(reply1, end = '\n\n') _P_.send_message(game_socket, 'AI_GAME') reply2 = _P_.receive_message(game_socket) print(reply2, end = '\n\n') except ConnectionRefusedError: print('Invalid port') return 'Invalid' ## while True: State = _X_.new_game_state() while True: winner = _X_.winning_player(State) _F_.new_board(State.board) if winner != _X_.NONE: break try: _F_.turn(State.turn) command = input(_F_.Menu).upper() if command == 'A': column_number = int(input('please enter the column number: ')) State = _X_.drop_piece(State, column_number - 1) print() AI = _P_.AI(game_socket, 'A', column_number).split() ## print(AI) ## _C_.new_board(State.board) ## print() ## _N_.AI_turn(State.turn) if AI[0] == 'DROP': State = _X_.drop_piece(State, int(AI[1]) - 1) elif AI[0] == 'POP': State = _X_.pop_piece(State, int(AI[1]) - 1) elif command == 'B': column_number = int(input('please enter the column number: ')) State = _X_.pop_piece(State, column_number - 1) print() AI = _P_.AI(game_socket, 'B', column_number).split() ## print(AI) ## _C_.new_board(State.board) ## print() ## _N_.AI_turn(State.turn) if AI[0] == 'DROP': State = _X_.drop_piece(State, int(AI[1]) - 1) elif AI[0] == 'POP': State = _X_.pop_piece(State, int(AI[1]) - 1) else: _F_.invalid_command(command) except _X_.InvalidConnectFourMoveError: print('\nInvaild command. Please try again.\n') except ValueError: print('\nError. Column number must be between 1 and 7.\n') except _X_.ConnectFourGameOverError: print('\nError. Game is over.\n') if winner == _X_.RED: print('Red player wins!') elif winner == _X_.YELLOW: print('Yellow player wins!') ## restart = input('\nPlay again? (Y/N)').upper() ## if restart == 'Y': ## print('game restart!\n\n') ## State = _X_.new_game_state() ## elif restart == 'N': ## print('\nThanks for playing, Bye!') ## break ## else: ## _C_.invalid_command(restart) print('\nGame is over.\nThanks for playing, Bye!')
def newgamestate(): return connectfour.new_game_state()
def gameplay(connection: connect_four_protocol.ConnectFourConnection) -> None: board = connectfour.new_game_state() connect_four_ver1.display_board(board[0]) while True: try: if connectfour.winning_player(board) == ' ': if board[1] == 'R': print('\nRed\'s Turn') column = int(input('Enter column number: ')) respond = True if board[0][column - 1][-1] == 'R': while respond: answer = input("Pop or Drop? (P/D) ") answer = answer.upper() if answer == 'P': board = connectfour.pop_piece( board, column - 1) connect_four_protocol.red_pop( connection, str(column)) respond = False elif answer == 'D': board = connectfour.drop_piece( board, column - 1) connect_four_protocol.red_drop( connection, str(column)) respond = False else: print( 'Not a valid command! Please type P or D!\n' ) connect_four_ver1.display_board(board[0]) else: board = connectfour.drop_piece(board, column - 1) connect_four_protocol.red_drop(connection, str(column)) connect_four_ver1.display_board(board[0]) else: print('\nYellow\'s Turn') yellow_turn = connect_four_protocol._read_line( connection).split() if yellow_turn[0] != 'DROP' and yellow_turn[0] != 'POP': yellow_turn = connect_four_protocol._read_line( connection).split() board = _handle_drop_n_pop(board, yellow_turn) connect_four_ver1.display_board(board[0]) else: board = _handle_drop_n_pop(board, yellow_turn) connect_four_ver1.display_board(board[0]) else: player = "" if connectfour.winning_player(board) == 'R': player += "Red Player" else: player += "Yellow Player" print('Game is over! ' + player + ' is the winner!') break except IndexError: print('Column number you entered is not in the range of 1-7!\n') connect_four_ver1.display_board(board[0]) except ValueError: print('Not an integer!\n') connect_four_ver1.display_board(board[0]) except connectfour.InvalidConnectFourMoveError: print('Column is full! Please enter another column number!\n') connect_four_ver1.display_board(board[0])
def _new_game() -> connectfour.ConnectFourGameState: '''Creates a new game''' game_state = connectfour.new_game_state() return game_state
def init_the_game() -> connectfour.ConnectFourGameState: """initialize the game and print the board""" display_board(connectfour.new_game_state().board) game_state = connectfour.new_game_state() return game_state
def run_it() : game_board = connectfour.new_game_state() current_board = display_board(game_board) print(current_board)
def connect_four(): print('Welcom to Connect Four!') Start_Game = connectfour.new_game_state() select_game_mode(Start_Game)