arten_1337

Untitled

Sep 14th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. import time
  2. import os
  3. import textwrap
  4.  
  5.  
  6. def board_display(score):
  7.     print(score[6], '|', score[7], '|', score[8])
  8.     print('-'*10)
  9.     print(score[3], '|', score[4], '|', score[5])
  10.     print('-'*10)
  11.     print(score[0], '|', score[1], '|', score[2])
  12.  
  13.  
  14. def check(char, posi1, posi2, posi3):
  15.     if score[posi1] == char and score[posi2] == char and score[posi3] == char:
  16.         return True
  17.  
  18.  
  19. def checkall(char):
  20.     if check(char, 0, 1, 2):
  21.         return True
  22.     if check(char, 3, 4, 5):
  23.         return True
  24.     if check(char, 6, 7, 8):
  25.         return True
  26.     if check(char, 0, 3, 6):
  27.         return True
  28.     if check(char, 1, 4, 7):
  29.         return True
  30.     if check(char, 2, 5, 8):
  31.         return True
  32.     if check(char, 0, 4, 8):
  33.         return True
  34.     if check(char, 2, 4, 6):
  35.         return True
  36.     return False
  37.  
  38.  
  39. print('Tic Tac Toe\n\n'.center(70))
  40. print('The Boards are numbered in the form of the numpad keyboard as printed below.\n')
  41. score = ['1', '2', '3',
  42.          '4', '5', '6',
  43.          '7', '8', '9']
  44.  
  45. board_display(score)
  46.  
  47. 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.'
  48. wrapper = textwrap.TextWrapper(width=72)
  49. string = wrapper.fill(text=value)
  50. print('\n', string)
  51.  
  52. print('\nChoose your cells accordingly.')
  53. input('Enter any key to continue: ')
  54. score = [' ']*9
  55. while True:
  56.     os.system('clear')
  57.     board_display(score)
  58.     while True:
  59.         try:
  60.             player1 = int(input('\nPlayer X, choose your position: '))
  61.             if score[player1-1] != 'X' and score[player1-1] != 'O':
  62.                 score[player1-1] = 'X'
  63.                 break
  64.             else:
  65.                 print('\nThis position is already taken, please try again')
  66.                 continue
  67.         except:
  68.             print('\nKindly enter a valid value')
  69.             continue
  70.     os.system('clear')
  71.     board_display(score)
  72.     if checkall('X'):
  73.         print('Player X is the winner')
  74.         break
  75.     time.sleep(0.3)
  76.     while True:
  77.         try:
  78.             player2 = int(input('\nPlayer O, choose your position: '))
  79.             if score[player2-1] != 'X' and score[player2] != 'O':
  80.                 score[player2-1] = 'O'
  81.                 break
  82.             else:
  83.                 print('\nThis position is already taken, please try again')
  84.                 continue
  85.         except:
  86.             print('\nKindly enter a valid value')
  87.             continue
  88.  
  89.     if checkall('O'):
  90.         print('Player O is the winner')
  91.         break
  92.     time.sleep(0.3)
Advertisement
Add Comment
Please, Sign In to add comment