Advertisement
Lov83

TicTacToe

Feb 23rd, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. def display_board(board):
  2.    
  3.     for i in range(7, 0, -3):
  4.         for n in range(0, 3):
  5.             print(board[i+n], end=' ')
  6.         print()
  7.  
  8.  
  9. def player_input():
  10.    
  11.     n=''
  12.     while n!='X' and n!='O':
  13.         n=input('X or O: ')
  14.     return n
  15.  
  16.  
  17. def place_marker(board, marker, position):
  18.     board[position]=marker
  19.     return board
  20.  
  21.  
  22. def win_check(board, a):
  23.     check=False
  24.     for i in range(7, 0, -3):
  25.         if board[i]==board[i+1]==board[i+2]==a:
  26.             check=True
  27.     for i in range(1, 4):
  28.         if board[i]==board[i+3]==board[i+6]==a:
  29.             check=True
  30.     if board[1]==board[5]==board[9]==a or board[3]==board[5]==board[7]==a:
  31.         check=True
  32.     return check
  33.  
  34.  
  35. def space_check(board, position):
  36.     if board[position]!='.':
  37.         return False
  38.     else:
  39.         return True
  40.  
  41. def full_board_check(board):
  42.     for i in board:
  43.         if i=='.':
  44.             return False
  45.     return True
  46.  
  47. if __name__=="__main__":
  48.     game_on='Y'
  49.     while game_on=='Y':
  50.         print('\n'*100)
  51.         testboard2 = ['.' for x in range(0, 10)]
  52.         display_board(testboard2)
  53.         i=''
  54.         while not win_check(testboard2, i):
  55.             print('\n'*100)
  56.             display_board(testboard2)
  57.             i=player_input()
  58.             loc=int(input('Enter position: '))
  59.             while not space_check(testboard2, loc):
  60.                 print('Choose again: ')
  61.                 loc=int(input('Enter position: '))
  62.             place_marker(testboard2, i, loc)
  63.             win_check(testboard2, i)
  64.             if full_board_check(testboard2):
  65.                 print('Het bang')
  66.                 break
  67.         print('\n'*100)
  68.         display_board(testboard2)
  69.         print('The player {} won the game'.format(i))
  70.         game_on=input('Play again? Y/N: ')
  71.     print("Thank you for playing this game. I've worked my butt off on this")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement