Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- choices = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
- Is_Current_One = True
- won = False
- wrongEntryPlayer1 = False
- wrongEntryPlayer2 = False
- while not won:
- if not wrongEntryPlayer1 and not wrongEntryPlayer2:
- print('\n')
- print('|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
- print('----------')
- print('|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
- print('----------')
- print('|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
- # above code is to print board layouts
- if Is_Current_One:
- print("Player 1's turn.\nChoose a number to put an X there.")
- try:
- choice = int(input("> ").strip())
- wrongEntryPlayer1 = False
- except ValueError:
- print("Please enter only valid fields from board (0-8)")
- wrongEntryPlayer1 = True
- continue
- else:
- print("Player 2's turn.\nChoose a number to put an O there.")
- try:
- choice = int(input("> ").strip())
- wrongEntryPlayer2 = False
- except ValueError:
- print("Please enter only valid fields from board (0-8)")
- wrongEntryPlayer2 = True
- continue
- if Is_Current_One:
- print(choices[choice-1])
- if choices[choice - 1].isnumeric():
- try:
- choices[choice - 1] = 'X'
- wrongEntryPlayer1 = False
- except IndexError:
- print("Please enter only valid fields from board (0-8)")
- wrongEntryPlayer1 = True
- else:
- print("Please choose a number that is empty.")
- wrongEntryPlayer1 = True
- else:
- if choices[choice - 1].isnumeric():
- try:
- choices[choice - 1] = 'O'
- wrongEntryPlayer2 = False
- except IndexError:
- print("Please enter only valid fields from board (0-8)")
- wrongEntryPlayer2 = True
- else:
- print("Please choose a number that is empty.")
- wrongEntryPlayer2 = True
- # code to toggle between True and False
- if not wrongEntryPlayer1 and not wrongEntryPlayer2:
- Is_Current_One = not Is_Current_One
- for pos_x in range(0, 3):
- pos_y = pos_x * 3
- # for row condition:
- if (choices[pos_y] == choices[(pos_y + 1)]) and (
- choices[pos_y] == choices[(pos_y + 2)]):
- won = True
- # column condition:
- if (choices[pos_x] == choices[(pos_x + 3)]) and (
- choices[pos_x] == choices[(pos_x + 6)]):
- won = True
- if ((choices[0] == choices[4] and choices[0] == choices[8])
- or (choices[2] == choices[4] and choices[4] == choices[6])):
- won = True
- print("Player " + str(int(Is_Current_One + 1)) + " won, Congratulations!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement