Skip to content

Instantly share code, notes, and snippets.

@rarodriguezp
Created October 10, 2017 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rarodriguezp/1f44a9ad97f2470d430efd7b85058e0a to your computer and use it in GitHub Desktop.
Save rarodriguezp/1f44a9ad97f2470d430efd7b85058e0a to your computer and use it in GitHub Desktop.
import random
def crea_quadrantes():
quadrantes = dict()
for i in range(1,10):
quadrantes['q'+str(i)] = str(i)
return quadrantes
def imprime_board(quadrante):
print('\n')
print(' ', quadrante['q'+str(1)], ' |', quadrante['q'+str(2)], ' |', quadrante['q'+str(3)])
print('-----+----+-----')
print(' ', quadrante['q'+str(4)], ' |', quadrante['q'+str(5)], ' |', quadrante['q'+str(6)])
print('-----+----+-----')
print(' ', quadrante['q'+str(7)], ' |', quadrante['q'+str(8)], ' |', quadrante['q'+str(9)])
print('\n')
def ask_user():
global played_choices
try:
entrada = input('Enter position (1-9): ')
while int(entrada) < 1 or int(entrada) > 9 or entrada in played_choices:
print('\nInvalid value, you must choose between 1 and 9\nOr it has already been played\n')
imprime_board(quadrantes)
entrada = input('Enter position (1-9): ')
print('You entered ' + entrada)
played_choices.append(entrada)
return entrada
except:
print('\n*** Not valid entry. \n*** Shutting down now... \n')
def someone_wins(quadrantes):
if (quadrantes['q1'] == quadrantes['q2'] == quadrantes['q3'] or
quadrantes['q4'] == quadrantes['q5'] == quadrantes['q6'] or
quadrantes['q7'] == quadrantes['q8'] == quadrantes['q9'] or
quadrantes['q1'] == quadrantes['q5'] == quadrantes['q9'] or
quadrantes['q3'] == quadrantes['q5'] == quadrantes['q7'] or
quadrantes['q1'] == quadrantes['q4'] == quadrantes['q7'] or
quadrantes['q2'] == quadrantes['q5'] == quadrantes['q8'] or
quadrantes['q3'] == quadrantes['q6'] == quadrantes['q9']):
return True
else:
return False
def is_tied():
global played_choices
if len(played_choices) == 9:
print('Is a tied game')
exit(0)
def play_it(quadrantes, q_number, symbol):
for k,v in quadrantes.items():
if v == q_number:
quadrantes[k] = symbol
imprime_board(quadrantes)
if someone_wins(quadrantes):
print('\n*** We got a winner ***\n')
exit(0)
elif is_tied():
exit(0)
def user_turn(quadrantes, user_input, symbol):
play_it(quadrantes, user_input, symbol)
def computer_turn(quadrantes, symbol):
global played_choices
all_moves = ['1','2','3','4','5','6','7','8','9']
possible_moves = set(all_moves) - set(played_choices)
if len(possible_moves) > 1:
computer_moved = str(random.sample(possible_moves, 1))
print('Computer chose ', computer_moved)
else:
computer_moved = str(possible_moves)
computer_move = computer_moved[2]
played_choices.append(computer_move)
play_it(quadrantes, computer_move, symbol)
def x_or_o():
print('Welcome, hope you enjoy our little game.')
while True:
user_symbol = input('Please hit X or 0 to select your symbol: ')
if user_symbol == 'x' or user_symbol == 'X':
computer_symbol = 'O'
return user_symbol, computer_symbol
elif user_symbol == 'o' or user_symbol == 'O' or user_symbol == '0':
computer_symbol = 'X'
return user_symbol, computer_symbol
def who_first():
while True:
who = input('Who play first? Press 1 for USER or 2 for COMPUTER: ')
if who == '1':
return 1
elif who == '2':
return 2
played_choices = list()
symbols = x_or_o()
first_player = who_first()
quadrantes = crea_quadrantes()
while True:
if first_player == 1:
imprime_board(quadrantes)
user_input = ask_user()
user_turn(quadrantes, user_input, symbols[0])
computer_turn(quadrantes, symbols[1])
elif first_player == 2:
computer_turn(quadrantes, symbols[1])
user_input = ask_user()
user_turn(quadrantes, user_input, symbols[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment