# TICTACTOE

Jul 30th, 2021
710
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # tic tac toe
2. import random
3.
4.
5. player_choice = 0
6. cpu_choice = 0
7. player_piece = "X"
8. ai_piece = "O"
9. play_again = True
10.
11.
12. def Player_Piece_Choice():
13.     choice = 0
14.     while True:
15.         choice = input("Please choose 1 for X or 2 for O. ")
16.         if choice == "1":
17.             player_piece = "X"
18.             ai_piece = "O"
19.             break
20.         elif choice == "2":
21.             player_piece = "O"
22.             ai_piece = "X"
23.             break
24.
25.
26. def safe_selection(board, loc):
27.     if board[loc] == " ":
28.         print("False")
29.         return False
30.     else:
31.         return True
32.
33.
34. def Print_Board(board):
35.     print(board[1] + " | " + board[2] + " | " + board[3])
36.     print(board[4] + " | " + board[5] + " | " + board[6])
37.     print(board[7] + " | " + board[8] + " | " + board[9])
38.
39.
40. def Print_Board_Start():
41.     print(str(1) + " | " + str(2) + " | " + str(3))
42.     print(str(4) + " | " + str(5) + " | " + str(6))
43.     print(str(7) + " | " + str(8) + " | " + str(9))
44.
45.
46. def Make_Selection(board):
47.     run = True
48.     while run == True:
50.         run = safe_selection(board, int(value))
51.
52.     board[int(value)] = player_piece
53.
54.
55. def Check_for_Win(board):
56.     if board[1] == board[2] == board[3] != " ":
57.         if board[1] == player_piece:
58.             return "The player has won."
59.         else:
60.             return "The AI has won."
61.     elif board[4] == board[5] == board[6] != " ":
62.         if board[4] == player_piece:
63.             return "The player has won."
64.         else:
65.             return "The AI has won."
66.     elif board[7] == board[8] == board[9] != " ":
67.         if board[7] == player_piece:
68.             return "The player has won."
69.         else:
70.             return "The AI has won."
71.     elif board[1] == board[5] == board[9] != " ":
72.         if board[1] == player_piece:
73.             return "The player has won."
74.         else:
75.             return "The AI has won."
76.     elif board[3] == board[5] == board[7] != " ":
77.         if board[3] == player_piece:
78.             return "The player has won."
79.         else:
80.             return "The AI has won."
81.     elif board[1] == board[4] == board[7] != " ":
82.         if board[1] == player_piece:
83.             return "The player has won."
84.         else:
85.             return "The AI has won."
86.     elif board[2] == board[5] == board[8] != " ":
87.         if board[2] == player_piece:
88.             return "The player has won."
89.         else:
90.             return "The AI has won."
91.     elif board[3] == board[6] == board[9] != " ":
92.         if board[3] == player_piece:
93.             return "The player has won."
94.         else:
95.             return "The AI has won."
96.     elif not " " in board.values():
97.         return "Stalemate"
98.     else:
99.         return "inprog"
100.
101.
102. def AI_choice_dum(board):
103.     while True:
104.         ai_choice = random.randint(1, 9)
105.
106.         if board[ai_choice] == " ":
107.             board[ai_choice] = ai_piece
108.             print(ai_choice)
109.             break
110.
111.
112. while play_again == True:
113.     board = {1: " ", 2: " ", 3: " ", 4: " ", 5: " ", 6: " ", 7: " ", 8: " ", 9: " "}
114.
115.     Player_Piece_Choice()
116.
117.     Print_Board_Start()
118.
119.     while True:
120.         Make_Selection(board)
121.         Print_Board(board)
122.         winner_check = Check_for_Win(board)
123.         if winner_check != "inprog":
124.             print(winner_check)
125.             break
126.
127.         AI_choice_dum(board)
128.         winner_check = Check_for_Win(board)
129.         Print_Board(board)
130.         if winner_check != "inprog":
131.             print(winner_check)
132.             break
133.
134.     if input("Will you play again? ") != "y":
135.         play_again == False
136.     else:
137.         play_again == True
138.
RAW Paste Data