• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Aug 22nd, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # A function that prints the game board as it currently stands
2. def make_grid(board):
3.     print("\t       GAME        \t\t        KEY         \n")
4.     print("\t  {}  |   {}  |  {}   \t\t  1  |   2  |  3  ".format(board['1'], board['2'], board['3']))
5.
6.     lineA = "\t_____|______|_____\t\t_____|______|_____"
7.     lineB = "\t     |      |     \t\t     |      |     "
8.
9.     print(lineA)
10.     print(lineB)
11.     print("\t  {}  |   {}  |  {}  \t\t  4  |   5  |  6  ".format(board['4'], board['5'], board['6']))
12.     print(lineA)
13.     print(lineB)
14.     print("\t  {}  |   {}  |  {}  \t\t  7  |   8  |  9  ".format(board['7'], board['8'], board['9']))
15.
16. # A function that prompts the player for a valid move
17. def get_move(turn,board):
18.     # Find out which player's turn it is
19.     if turn % 2 == 1:
20.         player = "Player 1"
21.     else:
22.         player = "Player 2"
23.     # Set up the prompt statement
24.     prompt = player + ", what is your move? "
25.
26.
27.     # Until we get a valid move, keep asking for one.
28.     # Find out which spots are unoccupied.
29.     free = [idx for idx in board if board[idx] == " "]
30.     occupied =[idx for idx in board if not board[idx] == " "]
31.     valid_move = 0
32.     while valid_move == 0:
33.         move = input(prompt)
34.
35.         if move in free:
36.             valid_move = 1
37.         elif move in occupied:
38.             print("Invalid move! That space is occupied.")
39.         else:
40.             print("Invalid move! Please enter a number 1-9.")
41.
42.     # Return a string containing the move
43.     return(move)
44.
45. # A function that initializes the game board
46. def initialize_board():
47.     board = {'1': " ",'2': " ",'3': " ", '4':" ",
48.           '5':" ",'6':" ",'7':" ",'8':" ",'9':" "}
49.     return(board)
50.
51. # A function to update the game board
52. def update_board(move, turn):
53.     # Find out which marker we're using
54.     if turn % 2 == 1:
55.         marker = "X"
56.     else:
57.         marker = "O"
58.
59.     # Update the board
60.     board[move] = marker
61.     return(board)
62.
63. # A function that takes the game board in its current state and looks for a winner
64. def check_for_win(board):
65.
66.     # Horizontal wins
67.     h1 = [board['1'], board['2'], board['3']]
68.     h2 = [board['4'], board['5'], board['6']]
69.     h3 = [board['7'], board['8'], board['9']]
70.
71.     # Vertical wins
72.     v1 = [board['1'], board['4'], board['7']]
73.     v2 = [board['2'], board['5'], board['8']]
74.     v3 = [board['3'], board['6'], board['9']]
75.
76.     # diagonal wins
77.     d1 = [board['1'], board['5'], board['9']]
78.     d2 = [board['3'], board['5'], board['7']]
79.
80.     # Loop over all the possible winning strategies to check if they're on the board
81.     wins =  [h1,h2,h3,v1,v2,v3,d1,d2]
82.     for w in wins:
83.         if list(set(w)) == ["X"]:
84.             the_winner = "Player 1"
85.             break
86.         elif list(set(w)) == ["O"]:
87.             the_winner = "Player 2"
88.             break
89.         else:
90.             the_winner = "nobody"
91.     return(the_winner)
92.
93.
94. if __name__== "__main__":
95.     board = initialize_board()
96.
97.     turn = 1 # intialize the turn counter
98.     winner = "nobody" # intialize the winner as....nobody!
99.
100.     while winner == "nobody" and turn < 10: # can't have more than 9 turns
101.         make_grid(board)
102.         move = get_move(turn, board)
103.         update_board(move,turn)
104.         winner = check_for_win(board)
105.         turn += 1
106.
107.     # Display the winning board and a pronounement of victory
108.     make_grid(board)
109.     print("The winner is " + winner + "!!!")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top