Advertisement
Guest User

TicTacToe

a guest
Jan 16th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. choices = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
  2. Is_Current_One = True
  3. won = False
  4. wrongEntryPlayer1 = False
  5. wrongEntryPlayer2 = False
  6. while not won:
  7.     if not wrongEntryPlayer1 and not wrongEntryPlayer2:
  8.         print('\n')
  9.         print('|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
  10.         print('----------')
  11.         print('|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
  12.         print('----------')
  13.         print('|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
  14.     # above code is to print board layouts
  15.     if Is_Current_One:
  16.         print("Player 1's turn.\nChoose a number to put an X there.")
  17.         try:
  18.             choice = int(input("> ").strip())
  19.             wrongEntryPlayer1 = False
  20.         except ValueError:
  21.             print("Please enter only valid fields from board (0-8)")
  22.             wrongEntryPlayer1 = True
  23.             continue
  24.     else:
  25.         print("Player 2's turn.\nChoose a number to put an O there.")
  26.         try:
  27.             choice = int(input("> ").strip())
  28.             wrongEntryPlayer2 = False
  29.         except ValueError:
  30.             print("Please enter only valid fields from board (0-8)")
  31.             wrongEntryPlayer2 = True
  32.             continue
  33.  
  34.     if Is_Current_One:
  35.         print(choices[choice-1])
  36.         if choices[choice - 1].isnumeric():
  37.             try:
  38.                 choices[choice - 1] = 'X'
  39.                 wrongEntryPlayer1 = False
  40.             except IndexError:
  41.                 print("Please enter only valid fields from board (0-8)")
  42.                 wrongEntryPlayer1 = True
  43.         else:
  44.             print("Please choose a number that is empty.")
  45.             wrongEntryPlayer1 = True
  46.     else:
  47.         if choices[choice - 1].isnumeric():
  48.             try:
  49.                 choices[choice - 1] = 'O'
  50.                 wrongEntryPlayer2 = False
  51.             except IndexError:
  52.                 print("Please enter only valid fields from board (0-8)")
  53.                 wrongEntryPlayer2 = True
  54.         else:
  55.             print("Please choose a number that is empty.")
  56.             wrongEntryPlayer2 = True
  57.     # code to toggle between True and False
  58.     if not wrongEntryPlayer1 and not wrongEntryPlayer2:
  59.         Is_Current_One = not Is_Current_One
  60.  
  61.     for pos_x in range(0, 3):
  62.         pos_y = pos_x * 3
  63.  
  64.         # for row condition:
  65.         if (choices[pos_y] == choices[(pos_y + 1)]) and (
  66.                 choices[pos_y] == choices[(pos_y + 2)]):
  67.             won = True
  68.  
  69.         # column condition:
  70.         if (choices[pos_x] == choices[(pos_x + 3)]) and (
  71.                 choices[pos_x] == choices[(pos_x + 6)]):
  72.             won = True
  73.     if ((choices[0] == choices[4] and choices[0] == choices[8])
  74.             or (choices[2] == choices[4] and choices[4] == choices[6])):
  75.         won = True
  76.  
  77. print("Player " + str(int(Is_Current_One + 1)) + " won, Congratulations!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement