arten_1337

Tic-Tac-Toe

Sep 14th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | None | 0 0
  1. import time
  2. import os
  3. import textwrap
  4.  
  5.  
  6. def full():
  7.     if ' ' not in score:
  8.         print('\nThe game has resulted into a cat game.')
  9.         return True
  10.  
  11.  
  12. def board_display(score):
  13.     print(' '+score[6], '|', score[7], '|', score[8])
  14.     print('---+---+---')
  15.     print(' '+score[3], '|', score[4], '|', score[5])
  16.     print('---+---+---')
  17.     print(' '+score[0], '|', score[1], '|', score[2])
  18.  
  19.  
  20. def check(char, posi1, posi2, posi3):
  21.     if score[posi1] == char and score[posi2] == char and score[posi3] == char:
  22.         return True
  23.  
  24.  
  25. def checkall(char):
  26.     if check(char, 0, 1, 2):
  27.         return True
  28.     if check(char, 3, 4, 5):
  29.         return True
  30.     if check(char, 6, 7, 8):
  31.         return True
  32.     if check(char, 0, 3, 6):
  33.         return True
  34.     if check(char, 1, 4, 7):
  35.         return True
  36.     if check(char, 2, 5, 8):
  37.         return True
  38.     if check(char, 0, 4, 8):
  39.         return True
  40.     if check(char, 2, 4, 6):
  41.         return True
  42.     return False
  43.  
  44.  
  45. # print('Tic Tac Toe\n'.center(70))
  46. banner = r'''
  47.              _____  _  ____     _____  ____  ____     _____  ____  _____
  48.             /__ __\/ \/   _\  /__ __\/  _ \/   _\  /__ __\/  _ \/  __/
  49.               / \ | ||  / _____ / \ | / \||  / _____ / \ | / \||  \
  50.               | |  | ||  \_\____\| |  | |-|||  \_\____\| |  | \_/||  /_
  51.               \_/  \_/\____/     \_/  \_/ \|\____/     \_/  \____/\____\ '''
  52.  
  53. print(banner)
  54. print('\nThe board is numbered in the form of the numpad keyboard as printed below.\n')
  55. time.sleep(0.01)
  56. score = ['1', '2', '3',
  57.          '4', '5', '6',
  58.          '7', '8', '9']
  59.  
  60. board_display(score)
  61.  
  62. value = 'The object of Tic Tac Toe is to get three in a row. You play on a three by three game board. The first player is known as X and the second is O. Players alternate placing Xs and Os on the game board until either opponent has three in a row or all nine squares are filled. X always goes first, and in the event that no one has three in a row, the stalemate is called a cat game.'
  63. wrapper = textwrap.TextWrapper(width=72)
  64. string = wrapper.fill(text=value)
  65. print('\n', string)
  66.  
  67. print('\nChoose your cells accordingly.The game will begin in 10 seconds.')
  68. time.sleep(10)
  69.  
  70. while True:
  71.     score = [' ']*9
  72.     while True:
  73.         os.system('clear')
  74.         board_display(score)
  75.         if full():
  76.             break
  77.         while True:
  78.             try:
  79.                 player1 = int(input('\nPlayer X, choose your position: '))
  80.                 if score[player1-1] != 'X' and score[player1-1] != 'O':
  81.                     score[player1-1] = 'X'
  82.                     break
  83.                 else:
  84.                     print('\nThis position is already taken, please try again.')
  85.                     continue
  86.             except:
  87.                 print('\nKindly enter a valid value')
  88.                 continue
  89.         os.system('clear')
  90.         board_display(score)
  91.         if checkall('X'):
  92.             print('\nPlayer X is the winner')
  93.             time.sleep(0.1)
  94.             break
  95.         if full():
  96.             break
  97.         while True:
  98.             try:
  99.                 player2 = int(input('\nPlayer O, choose your position: '))
  100.                 if score[player2-1] != 'X' and score[player2-1] != 'O':
  101.                     score[player2-1] = 'O'
  102.                     break
  103.                 else:
  104.                     print('\nThis position is already taken, please try again.')
  105.                     continue
  106.             except:
  107.                 print('\nKindly enter a valid value')
  108.                 continue
  109.         os.system('clear')
  110.         board_display(score)
  111.         if checkall('O'):
  112.             print('\nPlayer O is the winner')
  113.             time.sleep(0.1)
  114.             break
  115.         if full():
  116.             break
  117.     again = input('\nDo you want to play again? Enter Yes or No: ')
  118.     if again.lower() == 'yes':
  119.         continue
  120.     elif again.lower() == 'no':
  121.         print()
  122.         print('Thanks for playing TIC TAC TOE, have a nice day!!'.center(80))
  123.         print('\nExiting in 3..')
  124.         time.sleep(1)
  125.         print('           2..')
  126.         time.sleep(1)
  127.         print('           1..')
  128.         time.sleep(1)
  129.         break
  130.     else:
  131.         print('\nEnter a correct choice')
Advertisement
Add Comment
Please, Sign In to add comment