Drew bump

master
Drew Bednar 4 years ago
parent c64ac486d8
commit 0a7c6f3281

4
.gitignore vendored

@ -1 +1,5 @@
env/ env/
.idea
__pycache__/

@ -1,39 +1,38 @@
#----------global variables------------ # ----------global variables------------
#https://www.youtube.com/watch?v=BHh654_7Cmw # https://www.youtube.com/watch?v=BHh654_7Cmw
#Create a board # Create a board
import random import random
board = ['-','-','-', board = ['-', '-', '-',
'-','-','-', '-', '-', '-',
'-','-','-'] '-', '-', '-']
#if game is still going # if game is still going
GAME_GOING = True GAME_GOING = True
#who won? # who won?
WINNER = None WINNER = None
#define valid symbols # define valid symbols
SYMBOLS = {'X','O'} SYMBOLS = {'X', 'O'}
#define players # define players
PLAYER1 = "" PLAYER1 = ""
PLAYER2 = "" PLAYER2 = ""
#define how many players # define how many players
SINGLE_PLAYER = True SINGLE_PLAYER = True
CURRENT_PLAYER = random.choice(list(SYMBOLS)) CURRENT_PLAYER = random.choice(list(SYMBOLS))
def main(): def main():
global CURRENT_PLAYER global CURRENT_PLAYER
global SINGLE_PLAYER global SINGLE_PLAYER
global PLAYER1 global PLAYER1
global PLAYER2 global PLAYER2
mode = input("Single player mode y/n: ") mode = input("Single player mode y/n: ")
if mode not in "yn": if mode not in "yn":
print("You did not select a valid option no game for you") print("You did not select a valid option no game for you")
exit(1) exit(1)
if mode == "y": if mode == "y":
SINGLE_PLAYER = True SINGLE_PLAYER = True
else: else:
@ -47,50 +46,53 @@ def main():
PLAYER1 = player_1 PLAYER1 = player_1
PLAYER2 = (SYMBOLS - {player_1}).pop() PLAYER2 = (SYMBOLS - {player_1}).pop()
display_board () # while the game is still going
#while the game is still going
while GAME_GOING: while GAME_GOING:
#handle a turn display_board()
# handle a turn
handle_turn() handle_turn()
#check if he game has ended # import pdb; pdb.set_trace()
# check if he game has ended
check_if_game_over() check_if_game_over()
# cahnge to the other player # cahnge to the other player
CURRENT_PLAYER = (SYMBOLS - {CURRENT_PLAYER}).pop() CURRENT_PLAYER = (SYMBOLS - {CURRENT_PLAYER}).pop()
#the game has ended # the game has ended
display_board()
if WINNER == 'X' or WINNER == 'O': if WINNER == 'X' or WINNER == 'O':
print (WINNER + "won.") print(WINNER + " won.")
elif WINNER == None: elif WINNER == None:
print ("Tie.") print("Tie.")
exit(0)
def display_board (): def display_board():
print(board [0]+ '|'+ board[1]+'|'+ board [2]) print(board[0] + '|' + board[1] + '|' + board[2])
print(board [3]+ '|'+ board[4]+'|'+ board [5]) print(board[3] + '|' + board[4] + '|' + board[5])
print(board [6]+ '|'+ board[7]+'|'+ board [8]) print(board[6] + '|' + board[7] + '|' + board[8])
#play game of tic tac toe
# play game of tic tac toe
def handle_turn(): def handle_turn():
# import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
global board
print("Current Player {}".format(CURRENT_PLAYER)) print("Current Player {}".format(CURRENT_PLAYER))
while True: while True:
if SINGLE_PLAYER: if SINGLE_PLAYER:
if CURRENT_PLAYER == PLAYER1: if CURRENT_PLAYER == PLAYER1:
#take player input # take player input
choice = input('Choose a position from 1 to 9: ') choice = input('Choose a position from 1 to 9: ')
else: else:
choice = random.choice(find_open_spaces()) choice = random.choice(find_open_spaces())
else: else:
#take player input # take player input
choice = input('Choose a position from 1 to 9: ') choice = input('Choose a position from 1 to 9: ')
choice = int(choice) - 1
if validate_choice(choice): if validate_choice(choice):
break break
else: else:
print("Invalid choice") print("Invalid choice")
board[choice] = CURRENT_PLAYER
board[choice] == current_player
def validate_choice(choice): def validate_choice(choice):
@ -99,14 +101,11 @@ def validate_choice(choice):
choice = int(choice) choice = int(choice)
except ValueError: except ValueError:
return is_valid return is_valid
import pdb; pdb.set_trace()
if choice in list(range(0,9)) and choice in find_open_spaces():
valid = True
else:
brake
return is_valid if choice in list(range(0, 9)) and choice in find_open_spaces():
is_valid = True
return is_valid
def check_if_game_over(): def check_if_game_over():
@ -116,36 +115,37 @@ def check_if_game_over():
def check_if_win(): def check_if_win():
#call global variable # call global variable
global winner global WINNER
#check rows # check rows
row_winner = check_rows() row_winner = check_rows()
#check columns # check columns
column_winner = check_columns() column_winner = check_columns()
#check diagonals # check diagonals
diagonal_winner = check_diagonal() diagonal_winner = check_diagonal()
if row_winner: if row_winner:
#there is a winner # there is a winner
winner = row_winner WINNER = row_winner
elif column_winner: elif column_winner:
#there is a winner # there is a winner
winner = column_winner WINNER = column_winner
elif diagonal_winner: elif diagonal_winner:
#there is a winner # there is a winner
winner = diagonal_winner WINNER = diagonal_winner
else: else:
winner = None WINNER = None
return return
def check_rows(): def check_rows():
global GAME_GOING
row_1 = board[0] == board[1] == board[2] != '-' row_1 = board[0] == board[1] == board[2] != '-'
row_2 = board[3] == board[4] == board[5] != '-' row_2 = board[3] == board[4] == board[5] != '-'
row_3 = board[6] == board[7] == board[8] != '-' row_3 = board[6] == board[7] == board[8] != '-'
if row_1 or row_2 or row_3: if row_1 or row_2 or row_3:
game_going = False GAME_GOING = False
#display the winner # display the winner
if row_1: if row_1:
return board[0] return board[0]
if row_2: if row_2:
@ -154,11 +154,12 @@ def check_rows():
return board[6] return board[6]
return return
def check_columns(): def check_columns():
column_1 = board[0] == board[3] == board[6] != '-' column_1 = board[0] == board[3] == board[6] != '-'
column_2 = board[1] == board[4] == board[7] != '-' column_2 = board[1] == board[4] == board[7] != '-'
column_3 = board[2] == board[5] == board[8] != '-' column_3 = board[2] == board[5] == board[8] != '-'
#display the winner # display the winner
if column_1: if column_1:
return board[0] return board[0]
if column_2: if column_2:
@ -167,44 +168,36 @@ def check_columns():
return board[2] return board[2]
return return
def check_diagonal(): def check_diagonal():
diagonal_1 = board[0] == board[4] == board[8] != '-' diagonal_1 = board[0] == board[4] == board[8] != '-'
diagonal_2 = board[2] == board[4] == board[6] != '-' diagonal_2 = board[2] == board[4] == board[6] != '-'
#display the winner # display the winner
if diagonal_1: if diagonal_1:
return board[0] return board[0]
if diagonal_2: if diagonal_2:
return board[2] return board[2]
return return
#check_if_tie
# check_if_tie
def check_if_tie(): def check_if_tie():
global game_going global GAME_GOING
if "-" not in board: if "-" not in board:
game_going = False GAME_GOING = False
return return
#change between players
def change_player():
#call global variable
global current_player
#if the current player was X we change to O
if current_player == 'X':
current_player = 'O'
#if current player is O, change to X
elif current_player == 'O':
current_player = 'X'
return
def find_open_spaces(): def find_open_spaces():
#find a random space to select # find a random space to select
open_spaces = [] open_spaces = []
for i in board: for i in range(0, len(board)):
if i == "-": if board[i] == "-":
open_spaces.append(i) open_spaces.append(i)
return open_spaces return open_spaces
if __name__ == "__main__": if __name__ == "__main__":
main() main()
Loading…
Cancel
Save