Created
December 16, 2017 11:31
-
-
Save dpakach/a4ff150dfc72edd81ea32be7e49040cf to your computer and use it in GitHub Desktop.
tic tac toe game in python
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 | |
import time | |
print('welcome to the tic-tac-toe game'.upper()) | |
print('\n\n\n') | |
game_list = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] | |
player_chars = ['1', '2'] | |
over = False | |
turn = 'P1' | |
positions = [0, 1, 2] | |
def over(): | |
for char in player_chars: | |
for alist in game_list: | |
if alist.count(char) == 3: | |
return True | |
if (game_list[0][0] == game_list[1][1] == game_list[2][2] == char) or (game_list[2][0] == game_list[1][1] == game_list[0][2] == char): | |
return True | |
for i in range(3): | |
test_list = list() | |
for alist in game_list: | |
test_list.append(alist[i]) | |
for char in player_chars: | |
if test_list.count(char) == 3: | |
return True | |
return False | |
def draw(): | |
for lis in game_list: | |
if 0 in lis: | |
return False | |
break | |
return True | |
def check_position(x, y): | |
x = int(x) | |
y = int(y) | |
if (x not in positions or y not in positions): | |
print('improper index') | |
return False | |
elif game_list[x][y] != 0: | |
print('position already taken') | |
return False | |
else: | |
return True | |
def display_chars(x): | |
if x == '1': | |
return 'X' | |
elif x == '2': | |
return 'O' | |
else: | |
return ' ' | |
def print_game(): | |
count = 1 | |
print(' '*7 + 'columns --> 1 2 3') | |
print(' '*10 + 'rows') | |
print(' '*12 + '|' ) | |
print(' '*12 + 'v' + ' ' + '-'*22, end = '') | |
for lis in game_list: | |
print(""" | |
| | | | | |
{} | {} | {} | {} | | |
| | | | | |
----------------------""".format(count, display_chars(lis[0]), display_chars(lis[1]), display_chars(lis[2])), end='') | |
count += 1 | |
# for l1 in game_list: | |
# for l2 in l1: | |
# print(str(l2) + ' ', end='') | |
# print('\n') | |
print('GAME') | |
while True: | |
print_game() | |
if over(): | |
print('\n\n\n'+' '*17+'='*8+turn+' wins'+'='*8) | |
break | |
if draw(): | |
print('\n\n\n' + ' '*17 + '='*10 + 'Draw' + '='*10) | |
break | |
if turn == 'P1': | |
turn = 'P2' | |
else: | |
turn = 'P1' | |
print("\n\n\t\t\t " + turn + "'s turn") | |
x_pos = 0 | |
y_pos = 0 | |
while turn == 'P1': | |
x_pos = input('Enter the row : ') | |
y_pos = input('Enter the column : ') | |
x_pos = int(x_pos) - 1 | |
y_pos = int(y_pos) - 1 | |
if check_position(x_pos, y_pos): | |
break | |
else: | |
continue | |
while turn == 'P2': | |
print('\n\t\tthnking.....Please wait\n\n'.upper()) | |
time.sleep(1) | |
x_pos = random.randrange(0,3) | |
y_pos = random.randrange(0, 3) | |
if check_position(x_pos, y_pos): | |
break | |
else: | |
continue | |
if turn == 'P1': | |
val = '1' | |
else: | |
val = '2' | |
game_list[x_pos][y_pos] = val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment