Skip to content

Instantly share code, notes, and snippets.

@viniciuspereiras
Created December 3, 2021 01:20
Show Gist options
  • Save viniciuspereiras/41e7f2ab310e9b57d80d07388ac8733a to your computer and use it in GitHub Desktop.
Save viniciuspereiras/41e7f2ab310e9b57d80d07388ac8733a to your computer and use it in GitHub Desktop.
tictactoe
import random
class Map:
def __init__(self):
self.pos_a = ' '
self.pos_b = ' '
self.pos_c = ' '
self.pos_d = ' '
self.pos_e = ' '
self.pos_f = ' '
self.pos_g = ' '
self.pos_h = ' '
self.pos_i = ' '
pass
def _write(self, pos, value):
if pos == 'a':
self.pos_a = value
elif pos == 'b':
self.pos_b = value
elif pos == 'c':
self.pos_c = value
elif pos == 'd':
self.pos_d = value
elif pos == 'e':
self.pos_e = value
elif pos == 'f':
self.pos_f = value
elif pos == 'g':
self.pos_g = value
elif pos == 'h':
self.pos_h = value
elif pos == 'i':
self.pos_i = value
else:
print('error')
def _print(self):
print('________________________________________________________')
print('\n a | b | c \n d | e | f \n g | h | i \n')
print(f' {self.pos_a} | {self.pos_b} | {self.pos_c}\n {self.pos_d} | {self.pos_e} | {self.pos_f}\n {self.pos_g} | {self.pos_h} | {self.pos_i}')
print('________________________________________________________')
print('\n\n')
def empate(i):
if i == 9:
print('EMPATE')
exit()
else:
return False
# a b c
# d e f
# g h i
def win_verify(map) -> dict:
# horizontais
if map.pos_a == map.pos_b and map.pos_a == map.pos_c and map.pos_a != ' ':
return {'win': True, 'winner': map.pos_a}
elif map.pos_d == map.pos_e and map.pos_d == map.pos_f and map.pos_d != ' ':
return {'win': True, 'winner': map.pos_d}
elif map.pos_g == map.pos_h and map.pos_g == map.pos_i and map.pos_g != ' ':
return {'win': True, 'winner': map.pos_g}
# verticais
elif map.pos_a == map.pos_d and map.pos_a == map.pos_g and map.pos_a != ' ':
return {'win': True, 'winner': map.pos_a}
elif map.pos_b == map.pos_e and map.pos_b == map.pos_h and map.pos_b != ' ':
return {'win': True, 'winner': map.pos_b}
elif map.pos_c == map.pos_f and map.pos_c == map.pos_i and map.pos_c != ' ':
return {'win': True, 'winner': map.pos_c}
# diagonais
elif map.pos_a == map.pos_e and map.pos_a == map.pos_i and map.pos_a != ' ':
return {'win': True, 'winner': map.pos_a}
elif map.pos_c == map.pos_e and map.pos_c == map.pos_g and map.pos_c != ' ':
return {'win': True, 'winner': map.pos_c}
else:
return {'win': False, 'winner': None}
game = Map()
positions = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
for i in range(0, 9):
if i == 0:
heads_or_tails = random.choice(['heads', 'tails'])
if heads_or_tails == 'heads':
cpu_played = True
print('CPU starts')
choice = random.choice(positions)
game._write(choice, 'X')
game._print()
continue
else:
print('You start')
print('\n a | b | c \n d | e | f \n g | h | i \n')
cpu_played = False
choice = input('Your turn: ')
game._write(choice, 'O')
game._print()
continue
if cpu_played:
if win_verify(game)['win']:
print(f'{win_verify(game)["winner"]} wins')
break
cpu_played = False
choice = input('Your turn: ')
game._write(choice, 'O')
game._print()
empate(i)
continue
else:
if win_verify(game)['win']:
print(f'{win_verify(game)["winner"]} wins')
break
cpu_played = True
choice = random.choice(positions)
game._write(choice, 'X')
game._print()
empate(i)
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment