inital commit

This commit is contained in:
Michael Loveys 2021-09-08 11:43:29 -05:00
commit df436f3eef

137
main.py Normal file
View file

@ -0,0 +1,137 @@
def clear_output():
print("\n"*100)
def print_banner():
print("TicTacToe - v1.0\n")
def play_again():
global board, marker, current_player
while True:
play_again = input("Do you want to play again? [Y/N]: ").upper()
if play_again == 'Y':
board = {1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9'}
marker = {'player1': 'A', 'player2': 'B'}
current_player = '1'
return True
elif play_again == 'N':
return False
def game_over():
winner = 'N'
# Check for X/O winner
for player, mark in marker.items():
# Horizontal
if board[7] == mark and board[8] == mark and board[9] == mark:
winner = (player, mark)
elif board[4] == mark and board[5] == mark and board[6] == mark:
winner = (player, mark)
elif board[1] == mark and board[2] == mark and board[3] == mark:
winner = (player, mark)
# Vertical
elif board[7] == mark and board[4] == mark and board[1] == mark:
winner = (player, mark)
elif board[8] == mark and board[5] == mark and board[2] == mark:
winner = (player, mark)
elif board[9] == mark and board[6] == mark and board[3] == mark:
winner = (player, mark)
# Diagonal
elif board[7] == mark and board[5] == mark and board[3] == mark:
winner = (player, mark)
elif board[9] == mark and board[5] == mark and board[1] == mark:
winner = (player, mark)
# Check if winner has been found
if winner != 'N':
clear_output()
print(f"*** THE WINNER IS {winner[0]}/{winner[1]} ***")
return not play_again()
# Check if it is a draw
elif not board[1].isdigit() and not board[2].isdigit() and not board[3].isdigit()\
and not board[4].isdigit() and not board[5].isdigit() and not board[6].isdigit()\
and not board[7].isdigit() and not board[8].isdigit() and not board[9].isdigit():
clear_output()
print(f"** THE GAME ENDS IN DRAW **")
return not play_again()
else:
return False
def print_board(board):
print(f" {board[7]} | {board[8]} | {board[9]} ")
print(f" {board[4]} | {board[5]} | {board[6]} ")
print(f" {board[1]} | {board[2]} | {board[3]} ")
print(f"\n P1 - {marker['player1']}")
print(f" P2 - {marker['player2']}")
def start_game():
while True:
player1 = input("(Player 1) What marker would you like to be? [X/O]: ").upper()
if player1 == 'X':
player2 = 'O'
break
elif player1 == 'O':
player2 = 'X'
break
else:
clear_output()
print("Invalid Marker! Please enter x or o")
return (player1, player2)
def select_position(playerid):
player = "player" + playerid
position = input(f"(Player {playerid}, [{marker[player]}]) Select Position (1-9): ")
if position == "q":
print("Exiting...")
exit()
if position.isdigit():
position = int(position)
# Verify position is in range
if position >= 1 and position <= 9:
# Position is available
if board[position].isdigit():
board[position] = marker[player]
if playerid == '1':
return '2'
else:
return '1'
# Position is taken
else:
print("Uh oh... already selected!")
return playerid
# Store board in key:value pairs, isnum = not taken
board = {1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9'}
# Current Player is either 1 or 2
current_player = '1'
# Store the Player Makers (X/O)
marker = {'player1': 'A', 'player2': 'B'}
# Start Game Loop
while not game_over():
if marker['player1'] == 'A' or marker['player2'] == 'B':
marker['player1'], marker['player2'] = start_game()
clear_output()
print_banner()
print_board(board)
current_player = select_position(current_player)