Skip to content

Instantly share code, notes, and snippets.

@jose-marquez89
Created August 4, 2021 23:21
Show Gist options
  • Save jose-marquez89/d60cffd2a8a0752432c0f68c50234a9e to your computer and use it in GitHub Desktop.
Save jose-marquez89/d60cffd2a8a0752432c0f68c50234a9e to your computer and use it in GitHub Desktop.
NE Toe
from collections import Counter
import random
def display_board(board,clearYorN):
if clearYorN == 'Y':
print('\n'*100)
print(f' | | \n {board[7]} | {board[8]} | {board[9]} \n___|___|___\n | | \n {board[4]} | {board[5]} | {board[6]} \n___|___|___\n | | \n {board[1]} | {board[2]} | {board[3]}\n | | ')
def player_input():
#Default Variables
P1 = 'WRONG'
P2 = 'WRONG'
while P1 not in ['X','O']:
P1 = input('What marker would you like Player 1 to be (X or O)? ')
if P1.upper() not in ['X','O']:
print('That is not a valid response!')
elif P1.upper() == 'X':
print("\nGreat choice! Player 1 is going to be the X's and Player 2 is going to be O's")
P1 = 'X'
P2 = 'O'
elif P1.upper() == 'O':
print("Great choice! Player 1 is going to be the O's and Player 2 is going to be X's")
P1 = 'O'
P2 = 'X'
return [P1,P2]
def place_marker(board, marker, position):
board[position] = marker
def win_check2(board, mark):
win_c_1 = board[7] == mark and board[8] == mark and board[9] == mark
win_c_2 = board[4] == mark and board[5] == mark and board[6] == mark
win_c_3 = board[1] == mark and board[2] == mark and board[3] == mark
win_c_4 = board[7] == mark and board[5] == mark and board[3] == mark
win_c_5 = board[1] == mark and board[5] == mark and board[9] == mark
win_c_6 = board[7] == mark and board[4] == mark and board[1] == mark
win_c_7 = board[8] == mark and board[5] == mark and board[2] == mark
win_c_8 = board[9] == mark and board[6] == mark and board[3] == mark
if win_c_1 == True or win_c_2 == True or win_c_3 == True or win_c_4 == True or win_c_5 == True or win_c_6 == True or win_c_7 == True or win_c_8 == True:
#print(f"Congratulations {mark}'s is the winner!")
return True
def win_check(board, mark):
# count the board
counter = Counter(board)
# no need to check board with less than five marked positions
if counter[mark] < 3:
return
winning_combos = [[1, 2, 3], [4, 5, 6], [7, 8, 9],
[1, 4, 7], [2, 5, 8], [3, 6, 9],
[1, 5, 9], [3, 5, 7]]
mark_positions = []
for idx, cur_mark in enumerate(board):
if cur_mark == mark:
mark_positions.append(idx)
for i in range(0, len(mark_positions)-2):
# check for winning combos
if mark_positions[i:i+3] in winning_combos:
return True
def choose_first():
if random.randint(0,1) == 0:
return "Player 1 goes first"
else:
return "Player 2 goes first"
def space_check(board, position):
return board[position] in ['X','O']
def full_board_check(board):
lstcnt = 0
for item in board:
if item in ['X','O']:
lstcnt += 1
return lstcnt == 9
def player_choice(board, marker):
position = "invalid"
while position.isdigit() == False:
userinput = input(f"{marker}!, Where would you like your next position to be? Enter (1-9): ")
if userinput.isdigit() == True and int(userinput) in range(1,10):
if space_check(board,int(userinput)) == True:
print(f"Sorry, but position '{userinput}' is taken!")
position = "invalid"
else:
position = userinput
return int(position)
else:
print("That is an invalid selection: ")
def replay():
userinput = input("\nWould either of you like to play again? Enter (Y or N): ")
while userinput.upper() not in ['Y','N']:
userinput = input("\nSorry, I don't understand your answer. Would either of you like to play again? Enter (Y or N): ")
if userinput.upper() == 'Y':
print("\nSounds Great!, let's get this game rolling")
return True
else:
print("\nThanks for playing!")
return False
def replay():
userinput = input("\nWould either of you like to play again? Enter (Y or N): ")
while userinput.upper() not in ['Y','N']:
userinput = input("\nSorry, I don't understand your answer. Would either of you like to play again? Enter (Y or N): ")
if userinput.upper() == 'Y':
print("\nSounds Great!, let's get this game rolling")
return True
else:
print("\nThanks for playing!")
return False
print('Welcome to Tic Tac Toe!')
board = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ']
p1marker, p2marker = player_input()
game_on = True
if choose_first() == "\nPlayer 1 goes first":
print("Player 1 goes first")
first_player = p1marker
second_player = p2marker
else:
print("\nPlayer 2 goes first")
first_player = p2marker
second_player = p1marker
display_board(board,'N')
playerlist = [first_player,second_player]
while game_on == True:
for player in playerlist:
if game_on == False:
break
position = player_choice(board, player)
place_marker(board,player,position)
display_board(board,'Y')
if win_check(board, player) == True:
print(f"Congratulations {player} is the winner!")
if replay() == True:
board = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ']
display_board(board,'Y')
else:
game_on = False
break
if full_board_check(board) == True:
print(f"Sorry, there is no winner this game!")
if replay() == True:
board = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ']
display_board(board,'Y')
else:
game_on = False
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment