Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys, time
- write = sys.stdout.write
- from types import NoneType
- set_1 = 10
- set_2 = 10
- set_3 = 10
- set_4 = 10
- set_5 = 10
- set_6 = 10
- set_7 = 10
- set_8 = 10
- set_9 = 10
- set_1_set = 10
- set_2_set = 10
- set_3_set = 10
- set_4_set = 10
- set_5_set = 10
- set_6_set = 10
- set_7_set = 10
- set_8_set = 10
- set_9_set = 10
- set_1_win = 10
- set_2_win = 10
- set_3_win = 10
- set_4_win = 10
- set_5_win = 10
- set_6_win = 10
- set_7_win = 10
- set_8_win = 10
- set_9_win = 10
- play_again = 10
- win_cond = False
- # https://www.delftstack.com/howto/python/python-clear-console/
- def clearConsole():
- command = 'clear'
- if os.name in ('nt', 'dos'): # If Machine is running on Windows, use cls
- command = 'cls'
- os.system(command)
- clearConsole()
- def board(xo,key_pressed,win_cond):
- picture = [
- [0,0,0,'|',0,0,0,'|',0,0,0],
- [0,7,0,'|',0,8,0,'|',0,9,0],
- [0,0,0,'|',0,0,0,'|',0,0,0],
- ['-','-','-',0,'-','-','-',0,'-','-','-'],
- [0,0,0,'|',0,0,0,'|',0,0,0],
- [0,4,0,'|',0,5,0,'|',0,6,0],
- [0,0,0,'|',0,0,0,'|',0,0,0],
- ['-','-','-',0,'-','-','-',0,'-','-','-'],
- [0,0,0,'|',0,0,0,'|',0,0,0],
- [0,1,0,'|',0,2,0,'|',0,3,0],
- [0,0,0,'|',0,0,0,'|',0,0,0]
- ]
- global set_1
- global set_2
- global set_3
- global set_4
- global set_5
- global set_6
- global set_7
- global set_8
- global set_9
- global set_1_set
- global set_2_set
- global set_3_set
- global set_4_set
- global set_5_set
- global set_6_set
- global set_7_set
- global set_8_set
- global set_9_set
- global set_1_win
- global set_2_win
- global set_3_win
- global set_4_win
- global set_5_win
- global set_6_win
- global set_7_win
- global set_8_win
- global set_9_win
- for image in picture:
- for k in image:
- if k == '|':
- print('|', end='')
- continue
- if k == '-':
- print('-', end='')
- continue
- if k == int(key_pressed):
- # print(xo, end='')
- if int(key_pressed) == 1:
- set_1 = xo
- if int(key_pressed) == 2:
- set_2 = xo
- if int(key_pressed) == 3:
- set_3 = xo
- if int(key_pressed) == 4:
- set_4 = xo
- if int(key_pressed) == 5:
- set_5 = xo
- if int(key_pressed) == 6:
- set_6 = xo
- if int(key_pressed) == 7:
- set_7 = xo
- if int(key_pressed) == 8:
- set_8 = xo
- if int(key_pressed) == 9:
- set_9 = xo
- if k == 1 and (set_1 == 'X' or set_1 == 'O'):
- print(set_1, end='')
- set_1_set = 1
- if win_cond == True and set_1_win == 1:
- write('\b')
- print('\x1b[6;30;42m' + set_1 + '\x1b[0m', end='')
- continue
- if k == 2 and (set_2 == 'X' or set_2 == 'O'):
- print(set_2, end='')
- set_2_set = 1
- if win_cond == True and set_2_win == 1:
- write('\b')
- print('\x1b[6;30;42m' + set_2 + '\x1b[0m', end='')
- continue
- if k == 3 and (set_3 == 'X' or set_3 == 'O'):
- print(set_3, end='')
- set_3_set = 1
- if win_cond == True and set_3_win == 1:
- write('\b')
- print('\x1b[6;30;42m' + set_3 + '\x1b[0m', end='')
- continue
- if k == 4 and (set_4 == 'X' or set_4 == 'O'):
- print(set_4, end='')
- set_4_set = 1
- if win_cond == True and set_4_win == 1:
- write('\b')
- print('\x1b[6;30;42m' + set_4 + '\x1b[0m', end='')
- continue
- if k == 5 and (set_5 == 'X' or set_5 == 'O'):
- print(set_5, end='')
- set_5_set = 1
- if win_cond == True and set_5_win == 1:
- write('\b')
- print('\x1b[6;30;42m' + set_5 + '\x1b[0m', end='')
- continue
- if k == 6 and (set_6 == 'X' or set_6 == 'O'):
- print(set_6, end='')
- set_6_set = 1
- if win_cond == True and set_6_win == 1:
- write('\b')
- print('\x1b[6;30;42m' + set_6 + '\x1b[0m', end='')
- continue
- if k == 7 and (set_7 == 'X' or set_7 == 'O'):
- print(set_7, end='')
- set_7_set = 1
- if win_cond == True and set_7_win == 1:
- write('\b')
- print('\x1b[6;30;42m' + set_7 + '\x1b[0m', end='')
- continue
- if k == 8 and (set_8 == 'X' or set_8 == 'O'):
- print(set_8, end='')
- set_8_set = 1
- if win_cond == True and set_8_win == 1:
- write('\b')
- print('\x1b[6;30;42m' + set_8 + '\x1b[0m', end='')
- continue
- if k == 9 and (set_9 == 'X' or set_9 == 'O'):
- print(set_9, end='')
- set_9_set = 1
- if win_cond == True and set_9_win == 1:
- write('\b')
- print('\x1b[6;30;42m' + set_9 + '\x1b[0m', end='')
- continue
- else:
- print(' ', end='')
- print('')
- return(set_1_set, set_2_set, set_3_set, set_4_set, set_5_set, set_6_set, set_7_set, set_8_set, set_9_set, set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
- def key_press():
- key_pressed = 10
- global set_1_set
- global set_2_set
- global set_3_set
- global set_4_set
- global set_5_set
- global set_6_set
- global set_7_set
- global set_8_set
- global set_9_set
- while int(key_pressed) not in (0,1,2,3,4,5,6,7,8,9):
- key_pressed = input("\nPlease enter a number from 1 and 9 or enter 0 to quit program: ")
- if key_pressed.isdigit():
- pass
- else:
- key_pressed = 10
- if int(key_pressed) == 0:
- return key_pressed
- if int(key_pressed) == 1 and set_1_set == 10:
- return key_pressed
- if int(key_pressed) == 2 and set_2_set == 10:
- return key_pressed
- if int(key_pressed) == 3 and set_3_set == 10:
- return key_pressed
- if int(key_pressed) == 4 and set_4_set == 10:
- return key_pressed
- if int(key_pressed) == 5 and set_5_set == 10:
- return key_pressed
- if int(key_pressed) == 6 and set_6_set == 10:
- return key_pressed
- if int(key_pressed) == 7 and set_7_set == 10:
- return key_pressed
- if int(key_pressed) == 8 and set_8_set == 10:
- return key_pressed
- if int(key_pressed) == 9 and set_9_set == 10:
- return key_pressed
- def close(key_pressed):
- if int(key_pressed) == 0:
- print('0 enterd. Program has terminated')
- exit()
- def win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9):
- global win_cond
- global set_1_win
- global set_2_win
- global set_3_win
- global set_4_win
- global set_5_win
- global set_6_win
- global set_7_win
- global set_8_win
- global set_9_win
- if set_7 == set_8 == set_9 == 'X':
- print("The game has been won by X")
- win_cond = True
- set_7_win = 1
- set_8_win = 1
- set_9_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_4 == set_5 == set_6 == 'X':
- print("The game has been won by X")
- win_cond = True
- set_4_win = 1
- set_5_win = 1
- set_6_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_1 == set_2 == set_3 == 'X':
- print("The game has been won by X")
- win_cond = True
- set_1_win = 1
- set_2_win = 1
- set_3_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_1 == set_4 == set_7 == 'X':
- print("The game has been won by X")
- win_cond = True
- set_1_win = 1
- set_4_win = 1
- set_7_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_2 == set_5 == set_8 == 'X':
- print("The game has been won by X")
- win_cond = True
- set_2_win = 1
- set_5_win = 1
- set_8_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_3 == set_6 == set_9 == 'X':
- print("The game has been won by X")
- win_cond = True
- set_3_win = 1
- set_6_win = 1
- set_9_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_1 == set_5 == set_9 == 'X':
- print("The game has been won by X")
- win_cond = True
- set_1_win = 1
- set_5_win = 1
- set_9_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_7 == set_5 == set_3 == 'X':
- print("The game has been won by X")
- win_cond = True
- set_7_win = 1
- set_5_win = 1
- set_3_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_7 == set_8 == set_9 == 'O':
- print("The game has been won by O")
- win_cond = True
- set_7_win = 1
- set_8_win = 1
- set_9_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_4 == set_5 == set_6 == 'O':
- print("The game has been won by O")
- win_cond = True
- set_4_win = 1
- set_5_win = 1
- set_6_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_1 == set_2 == set_3 == 'O':
- print("The game has been won by O")
- win_cond = True
- set_1_win = 1
- set_2_win = 1
- set_3_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_1 == set_4 == set_7 == 'O':
- print("The game has been won by O")
- win_cond = True
- set_1_win = 1
- set_4_win = 1
- set_7_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_2 == set_5 == set_8 == 'O':
- print("The game has been won by O")
- win_cond = True
- set_2_win = 1
- set_5_win = 1
- set_8_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_3 == set_6 == set_9 == 'O':
- print("The game has been won by O")
- win_cond = True
- set_3_win = 1
- set_6_win = 1
- set_9_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_1 == set_5 == set_9 == 'O':
- print("The game has been won by O")
- win_cond = True
- set_1_win = 1
- set_5_win = 1
- set_9_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- if set_7 == set_5 == set_3 == 'O':
- print("The game has been won by O")
- win_cond = True
- set_7_win = 1
- set_5_win = 1
- set_3_win = 1
- board(10,10,win_cond)
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('0 enterd. Program has terminated')
- exit()
- return
- while play_again != 0:
- clearConsole()
- set_1 = 10
- set_2 = 10
- set_3 = 10
- set_4 = 10
- set_5 = 10
- set_6 = 10
- set_7 = 10
- set_8 = 10
- set_9 = 10
- set_1_set = 10
- set_2_set = 10
- set_3_set = 10
- set_4_set = 10
- set_5_set = 10
- set_6_set = 10
- set_7_set = 10
- set_8_set = 10
- set_9_set = 10
- set_1_win = 10
- set_2_win = 10
- set_3_win = 10
- set_4_win = 10
- set_5_win = 10
- set_6_win = 10
- set_7_win = 10
- set_8_win = 10
- set_9_win = 10
- play_again = 10
- win_cond = False
- board(10,10,win_cond)
- key_pressed = key_press()
- close(key_pressed)
- clearConsole()
- board('X', key_pressed, win_cond)
- key_pressed = key_press()
- while type(key_pressed) == NoneType:
- key_pressed = key_press()
- close(key_pressed)
- clearConsole()
- board('O', key_pressed, win_cond)
- key_pressed = key_press()
- while type(key_pressed) == NoneType:
- key_pressed = key_press()
- close(key_pressed)
- clearConsole()
- board('X', key_pressed, win_cond)
- key_pressed = key_press()
- while type(key_pressed) == NoneType:
- key_pressed = key_press()
- close(key_pressed)
- clearConsole()
- board('O', key_pressed, win_cond)
- key_pressed = key_press()
- while type(key_pressed) == NoneType:
- key_pressed = key_press()
- close(key_pressed)
- clearConsole()
- board('X', key_pressed, win_cond)
- win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
- if win_cond == True:
- board('X', key_pressed, win_cond)
- if win_cond != True:
- key_pressed = key_press()
- while type(key_pressed) == NoneType:
- key_pressed = key_press()
- close(key_pressed)
- clearConsole()
- board('O', key_pressed, win_cond)
- win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
- if win_cond == True:
- board('O', key_pressed, win_cond)
- if win_cond != True:
- key_pressed = key_press()
- while type(key_pressed) == NoneType:
- key_pressed = key_press()
- close(key_pressed)
- clearConsole()
- board('X', key_pressed, win_cond)
- win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
- if win_cond == True:
- board('X', key_pressed, win_cond)
- if win_cond != True:
- key_pressed = key_press()
- while type(key_pressed) == NoneType:
- key_pressed = key_press()
- close(key_pressed)
- clearConsole()
- board('O', key_pressed, win_cond)
- win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
- if win_cond == True:
- board('X', key_pressed, win_cond)
- if win_cond != True:
- key_pressed = key_press()
- while type(key_pressed) == NoneType:
- key_pressed = key_press()
- close(key_pressed)
- clearConsole()
- board('X', key_pressed, win_cond)
- win_condition(set_1, set_2, set_3, set_4, set_5, set_6, set_7, set_8, set_9)
- if win_cond == True:
- board('X', key_pressed, win_cond)
- if win_cond == False:
- print('There is no winner.')
- play_again = input("\nWould you like to play again. 0 to exit: ")
- if play_again.isdigit():
- if int(play_again) == 0:
- print('\n0 enterd. Program has terminated')
- exit()
- else:
- pass
Advertisement
Add Comment
Please, Sign In to add comment