Advertisement
here2share

# b_tictactoe_replace.py

Feb 2nd, 2020
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. # b_tictactoe_replace.py
  2.  
  3. from time import sleep as x
  4. import os
  5. cls=lambda: os.system('cls')
  6.  
  7. r=range
  8. def m(s=''):
  9.     cls()
  10.     t=t2
  11.     if '!' in s:
  12.         t=''
  13.     mv="Player %s's Turn..."%p
  14.     print(b[0]+t.replace('@',mv)+s)
  15.     if not t:
  16.         raw_input('\n\n\tPress Enter To Continue\n')
  17.  
  18. play = ''
  19. while play.lower() != 'q':
  20.     p='X'
  21.     b = {}
  22.     t2='''
  23.     To Restart: 'Spacebar'
  24.     To Quit: 'Q'
  25.    
  26.     @
  27.     '''
  28.     b[0]='''
  29.      1 | 2 | 3        |   |  
  30.     ---+---+---    ---+---+---
  31.      4 | 5 | 6        |   |  
  32.     ---+---+---    ---+---+---
  33.      7 | 8 | 9        |   |  
  34.      '''
  35.     b[1]='123 456 789 147 258 369 159 357' ### for win checking
  36.     while 1:
  37.         m()
  38.         play=raw_input('\tTo Play A Square Enter Available Number Here: ')
  39.         if any([z in play.lower() for z in 'q ']):
  40.             print('\n\n\t*** Game Cancelled...\n')
  41.             x(3)
  42.             break
  43.         elif play and play in b[1]:
  44.             t=list(b[0])
  45.             t[b[0].index(play)+15]=p
  46.             b[0]=''.join(t)
  47.            
  48.            
  49.             ### a minimalistic approach is using the replace method
  50.             for z in (0,1):
  51.                 b[z]=b[z].replace(play,['.',p][z])
  52.             if p*3 in b[1]:
  53.                 m('\n\tPlayer "'+p+'" Won !!!')
  54.                 break
  55.             elif b[0].count('.') == 9:
  56.                 m("\n\n\tNo More Moves... Player 'O' Wins By Half Point !!!")
  57.                 break
  58.             else:
  59.                 p=('X' if p == 'O' else 'O')
  60.                 m()
  61.             ### <<<
  62.            
  63.                
  64.         else:
  65.             m('\n\t*** Invalid Entry ***')
  66.             x(3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement