repl.it
@JamesMacdonald1/

Project 4B: Tic Tac Toe Gameplay-2

Python

No description

fork
loading
Files
  • main.py
main.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copy your logic functions from part A below:
def is_board_full():
	 valid_characters = ['X', 'O']
	 for i in board:
	 	for j in i:
	 		if not(j in valid_characters):
	 			return False
	 else:
	 	return True

def is_valid_move(board,position):
	valid_characters = ['X', 'O']
	r = (position - 1)
	r = r//3
	c = (position - 1)
	c = c%3
	if board[r][c] in valid_characters:
		return False
	else:
		return True

def winning_move(board):
	if board[0][0] == 'X' and board[0][1] == 'X' and board[0][2] == 'X':
		return True
	if board[1][0] == 'X' and board[1][1] == 'X' and board[1][2] == 'X':
		return True
	if board[2][0] == 'X' and board[2][1] == 'X' and board[2][2] == 'X':
		return True
	if board[0][0] == 'O' and board[0][1] == 'O' and board[0][2] == 'O':
		return True
	if board[1][0] == 'O' and board[1][1] == 'O' and board[1][2] == 'O':
		return True
	if board[2][0] == 'O' and board[2][1] == 'O' and board[2][2] == 'O':
		return True
	if board[0][0] == 'X' and board[1][0] == 'X' and board[2][0] == 'X':
		return True
	if board[0][1] == 'X' and board[1][1] == 'X' and board[2][1] == 'X':
		return True
	if board[0][2] == 'X' and board[1][2] == 'X' and board[2][2] == 'X':
		return True
	if board[0][0] == 'O' and board[1][0] == 'O' and board[2][0] == 'O':
		return True
	if board[0][1] == 'O' and board[1][1] == 'O' and board[2][1] == 'O':
		return True
	if board[0][2] == 'O' and board[1][2] == 'O' and board[2][2] == 'O':
		return True
	if board[0][0] == 'X' and board[1][1] == 'X' and board[2][2] == 'X':
		return True
	if board[0][2] == 'X' and board[1][1] == 'X' and board[2][0] == 'X':
		return True
	if board[0][0] == 'O' and board[1][1] == 'O' and board[2][2] == 'O':
		return True
	if board[0][2] == 'O' and board[1][1] == 'O' and board[2][0] == 'O':
		return True
	else:
		return False
# END logic functions

# N - print_board
# P - print the board with the current state of X, O, and blanks
# I - board: the 3x3 gameboard with the current state
# R - printed board
def print_board(board):
		return	"""
		 {} | {} | {}
		---|---|--- 
		 {} | {} | {}
		---|---|---
		 {} | {} | {}""".format(board[0][0], board[0][1], board[0][2], board[1][0], board[1][1], board[1][2], board[2][0], board[2][1], board[2][2])


welcome_message = """Welcome to Tic Tac Toe!

This game supports two players.
Each player will alternate placing an X or O on the 3x3 grid until one player gets 3 of their mark in a row, column, or diagonal.
If the board fills up without anyone getting 3 in a row, the players will tie.

You will tell the computer where to put your mark by using the numbering system below:

		 1 | 2 | 3 
		---|---|---
		 4 | 5 | 6 
		---|---|---
		 7 | 8 | 9

Good luck!
"""

print(welcome_message)

# Main game loop
player1 = input("Player one, enter your name:  ")
player2 = input("Player two, enter your name:  ")
current_player = player1
current_move = 'X'
game_over = False
# The board starts empty
board = [ [" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
print(print_board(board))
while not game_over:
		# 1. Ask the user (by name) to select a position on the board
	position = int(input(current_player + "select a position to mark(1,9)  "))
	# 2. Check if the position is valid (not already marked) and update the board.
	#		If it's not valid, ask the user again until they give you a valid position.
	while not is_valid_move(board, position):
		print('That is not a valid move')
		position = int(input(current_player + "select a position to mark(1,9)  "))
	row = (position - 1) // len(board)
	col = (position - 1) % len(board)
	board[row][col] = current_move
		
	# 3. Print the current board
	print(print_board(board))
	if winning_move(board):
		print(current_player,'has won the game')
		game_over = True
	elif is_board_full():
		print("Tie Game.")
		game_over = True
	else:
#  		print_board()
		if current_player == player1:
			current_player = player2
			current_move = 'O'
		else:
			current_player = player1
			current_move = 'X'
	
	# position2 = int(input(player2 + "select a position to mark(1,9)  "))
	# if position2 == 1 or position2 == 2 or position2 == 3:
	# 	row = 0
	# 	if not is_valid_move(board[row][position2]):
	# 		print("that is not a valid move, try again")
	# 	# if winning_move(board[row][position2]):
	# 	# 	print("Congradulations " + player2 + ", YOU WON!")
	# 		game_over = True
	# 	if is_board_full():
	# 		print("Tie Game.")
	# 		game_over = True
	# 	else:
	# 		board[row][position2-1] = 'O'
	# 		print_board()

	# if position2 == 4 or position2 == 5 or position2 == 6:
	# 	row = 1
	# 	position2 = position2-3
	# 	if not is_valid_move(board[row][position2]):
	# 		print("that is not a valid move, try again")
	# 	# if winning_move(board[row][position2]):
	# 	# 	print("Congradulations " + player2 + ", YOU WON!")
	# 		game_over = True
	# 	if is_board_full():
	# 		print("Tie Game.")
	# 		game_over = True
	# 	else:
	# 		board[row][position2-1] = 'O'
	# 		print_board()

	# if position2 == 7 or position2 == 8 or position2 == 8:
	# 	row = 2 
	# 	position2 = position2-6
	# 	if not is_valid_move(board[row][position2]):
	# 		print("that is not a valid move, try again")
	# 	# if winning_move(board[row][position2]):
	# 	# 	print("Congradulations " + player2 + ", YOU WON!")
	# 		game_over = True
	# 	if is_board_full():
	# 		print("Tie Game.")
	# 		game_over = True
	# 	else:
	# 		board[row][position2-1] = 'O'
	# 		print_board()

 
		
	
	
	
		# 2. Check if the position is valid (not already marked) and update the board.
		#    If it's not valid, ask the user again until they give you a valid position.
		
		
		# 3. Print the current board
		
		# 4. Check if the player won the game
		#        Print which player won
		
		# 5. Check if the game is a tie
		#        Print that it is a tie
		#        For extra credit, check if there are no more possible moves to win the game and print that the game is ending early if so.
		
		# 6. If there's a winner or tie
		#        End the game
		#        Ask if the user wants to play again
		#            If yes, clear the board
		#            If no, game_over = True
		
		# 7. Switch the players
		#if current_player == player1:
		    #current_player = player2
		#else:
		    #current_player = player1

# END main while loop
print("Thanks for playing!")
?