Last active
August 7, 2018 07:31
-
-
Save solen003/e7c6ab73e5d1fcf95969dc8c64fb9b6d to your computer and use it in GitHub Desktop.
tic tac toe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def display_board(a_board): | |
print(a_board[7] + '|' + a_board[8] + '|' + a_board[9]) | |
print('-'*5) | |
print(a_board[4] + '|' + a_board[5] + '|' + a_board[6]) | |
print('-'*5) | |
print(a_board[1] + '|' + a_board[2] + '|' + a_board[3]) | |
def player_input(): | |
player_choice = '' | |
while not (player_choice == 'X' or player_choice == 'O'): | |
player_choice = input('Human, choose your marker: X or O: ').upper() | |
if player_choice == 'X': | |
return ('O', 'X') | |
else: | |
return ('X', 'O') | |
def place_marker(a_board, a_marker, a_position): | |
a_board[a_position] = a_marker | |
def win_check(a_board, a_marker): | |
return (a_board[1] == a_board[2] == a_board[3] == a_marker or | |
a_board[4] == a_board[5] == a_board[6] == a_marker or | |
a_board[7] == a_board[8] == a_board[9] == a_marker or | |
a_board[1] == a_board[4] == a_board[7] == a_marker or | |
a_board[2] == a_board[5] == a_board[8] == a_marker or | |
a_board[3] == a_board[6] == a_board[9] == a_marker or | |
a_board[1] == a_board[5] == a_board[9] == a_marker or | |
a_board[3] == a_board[5] == a_board[7] == a_marker) | |
def space_check(a_board, a_position): | |
return a_board[a_position] == ' ' | |
def full_board_check(a_board): | |
return ' ' in a_board | |
def player_choice(a_board): | |
chosen_position = int(input('Choose your desired position: ')) | |
while space_check(a_board, chosen_position) == False: | |
chosen_position = int(input('Choose your desired position: ')) | |
return chosen_position | |
def ai_choice(a_board): | |
chosen_position = random.randint(1,9) | |
while space_check(a_board, chosen_position) == False: | |
chosen_position = random.randint(1,9) | |
return chosen_position | |
def replay(): | |
return input('Play again? Y/N: ').upper() == 'Y' | |
print('Welcome to Tic Tac Toe!') | |
while True: | |
the_board = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' '] | |
ai_marker, h_marker = player_input() | |
game_on = True | |
turn = 'ai_player' | |
while game_on: | |
if turn == 'ai_player': | |
selected_position = ai_choice(the_board) | |
place_marker(the_board, ai_marker, selected_position) | |
if win_check(the_board, ai_marker) == True: | |
display_board(the_board) | |
print('AI wins!') | |
game_on = False | |
elif full_board_check(the_board) == False: | |
display_board(the_board) | |
print('Tie') | |
game_on = False | |
else: | |
turn = 'h_player' | |
elif turn == 'h_player': | |
display_board(the_board) | |
selected_position = player_choice(the_board) | |
place_marker(the_board, h_marker, selected_position) | |
if win_check(the_board, h_marker) == True: | |
display_board(the_board) | |
print('Human wins!') | |
game_on = False | |
elif full_board_check(the_board) == False: | |
display_board(the_board) | |
print('Tie') | |
game_on = False | |
else: | |
turn = 'ai_player' | |
if not replay(): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment