-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (69 loc) · 2.09 KB
/
Copy pathmain.py
File metadata and controls
77 lines (69 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Name: CSE 210 Tic-Tac-Toe Game
Author: Jacob Ewell
"""
# create a main function to run the tic-tac-toe game
def main():
current_player = "x"
board = make_board()
while not (winner(board) or draw(board)):
print_board(board)
board = user_move(current_player, board)
current_player = next_turn(current_player)
print_board(board)
print(f'Good Game! Thanks for Playing!')
# creates the intital tic-tac-toe board
def make_board():
board = []
for space in range(9):
board.append(space + 1)
return board
# prints out the tic-tac-toe board
def print_board(board):
print('')
print(f'{board[0]}|{board[1]}|{board[2]}')
print("-+-+-")
print(f'{board[3]}|{board[4]}|{board[5]}')
print("-+-+-")
print(f'{board[6]}|{board[7]}|{board[8]}')
print('')
# switches it to the next player, either x or o
def next_turn(player):
if player == 'x':
return 'o'
else:
return 'x'
# test all possible ways of winning tic-tac-toe and returns True if there is a winner
def winner(board):
if board[0] == board[1] and board[1] == board[2]:
return True
elif board[3] == board[4] == board[5]:
return True
elif board[6] == board[7] == board[8]:
return True
elif board[0] == board[3] == board[6]:
return True
elif board[1] == board[4] == board[7]:
return True
elif board[2] == board[5] == board[8]:
return True
elif board[0] == board[4] == board[8]:
return True
elif board[2] == board[4] == board[6]:
return True
else:
return False
# tests to see if all of the tic-tac-toe spaces have been filled with an x or an o
def draw(board):
for i in range(9):
if board[i] != 'x' and board[i] != 'o':
return False
else:
return True
# requests input from the players as to which move they would like to do and updates the board
def user_move(player, board):
move = int(input(f"{player}'s turn to chose a square (1-9): "))
board[move - 1] = player
return board
if __name__ == "__main__":
main()