# constants WIN_COMBOS = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]] BOARD = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] #makes board def make_board(): """This function creates the board""" print("\n--- Noughts and Crosses ---") print(f'{BOARD[0]:<2}: {BOARD[1]:<2}: {BOARD[2]:>1}') print("..........") print(f'{BOARD[3]:<2}: {BOARD[4]:<2}: {BOARD[5]:>1}') print("..........") print(f'{BOARD[6]:<2}: {BOARD[7]:<2}: {BOARD[8]:>1}\n') #runs game def main(): # Main menu print("*" * 50) print("Welcome to our Noughts and Crosses Game!") print("*" * 50) #tracks move history play_loc_list1 = [] play_loc_list2 = [] # Get player's names player_1 = input("Enter the name of the first player:\n> ") player_2 = input("Enter the name of the second player:\n> ") make_board() global over #this variable will determine if the game is still on over = False def turn(player, list, symbol, symbol_word): #simulates a turn print(player + "'s turn\n") while True: # User input validation loop: loc = input(symbol_word + " location(choose 1 - 9):\n> ") if not loc.isdigit() or int(loc) not in [*range(1, 10)]: print('>>> Invalid input!') continue loc = int(loc) if BOARD[loc - 1] == 'X' or BOARD[loc - 1] == 'O': print('>>> Position taken, try again!') continue BOARD[loc - 1] = symbol #if input is valid it will replace the inputted location with an X or O in the BOARD list make_board() list.append(loc - 1) #and add the location to the move history list.sort() break global over for combo in WIN_COMBOS: #checks if player has won winner = True for number in combo: if number not in list: winner = False break if winner: print("\n" + player + " Wins!") over = True break if (not over and all(ele.isalpha() for ele in BOARD)): #checks for tie print("Tie game.") over = True while not over: #plays each necessary round until game is over, technically checks if player two has won turn(player_1, play_loc_list1, "X", "Cross") #player 1's turn if over: #if player one wins or ends in tie break turn(player_2, play_loc_list2, "O", "Nought") #players 2's turn if __name__ == '__main__': main() #runs game #Anthony Cooper
- # constants
- WIN_COMBOS = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
- BOARD = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
- #makes board
- def make_board():
- """This function creates the board"""
print("--- Noughts and Crosses ---")- print("\n--- Noughts and Crosses ---")
- print(f'{BOARD[0]:<2}: {BOARD[1]:<2}: {BOARD[2]:>1}')
- print("..........")
- print(f'{BOARD[3]:<2}: {BOARD[4]:<2}: {BOARD[5]:>1}')
- print("..........")
print(f'{BOARD[6]:<2}: {BOARD[7]:<2}: {BOARD[8]:>1}')- print(f'{BOARD[6]:<2}: {BOARD[7]:<2}: {BOARD[8]:>1}\n')
- #runs game
- def main():
- # Main menu
- print("*" * 50)
- print("Welcome to our Noughts and Crosses Game!")
- print("*" * 50)
- #tracks move history
- play_loc_list1 = []
- play_loc_list2 = []
- # Get player's names
- player_1 = input("Enter the name of the first player:\n> ")
- player_2 = input("Enter the name of the second player:\n> ")
while True:turn = 1# Player 1 turnwhile turn == 1:print("Hi", player_1)- make_board()
- global over #this variable will determine if the game is still on
- over = False
- def turn(player, list, symbol, symbol_word): #simulates a turn
- print(player + "'s turn\n")
- while True:
make_board()while True:- # User input validation loop:
loc_x = input("Cross location(choose 1 - 9):> ")if not loc_x.isdigit() or int(loc_x) not in range(10):- loc = input(symbol_word + " location(choose 1 - 9):
- > ")
- if not loc.isdigit() or int(loc) not in [*range(1, 10)]:
- print('>>> Invalid input!')
- continue
else:loc_x = int(loc_x)break # break User input validation loop:# Check if position is empty:if BOARD[loc_x - 1] == 'X' or BOARD[loc_x - 1] == 'O':print('>>> Position taken, try again!')continueelse:BOARD[loc_x - 1] = "X"break # Break player 1 turn:# Check win combos:loc_attempt = loc_x - 1play_loc_list1.append(loc_attempt)play_loc_list1.sort()for i in range(0, len(WIN_COMBOS)):if WIN_COMBOS[i] == play_loc_list1:print("You have won!")breakmake_board()turn = 2# Player 2 turn:while turn == 2:print("Hi", player_2)while True:make_board()while True:# User input validation loop:loc_y = input("Noughts location(choose 1 - 9):\n> ")if not loc_y.isdigit() or int(loc_y) not in range(10):print('>>> Invalid input')- loc = int(loc)
- if BOARD[loc - 1] == 'X' or BOARD[loc - 1] == 'O':
- print('>>> Position taken, try again!')
- continue
else:loc_y = int(loc_y)break # break User input validation loop:# Check if position is empty:if BOARD[loc_y - 1] == 'X' or BOARD[loc_y - 1] == 'O':print('>>> Position taken, try again!')continueelse:BOARD[loc_y - 1] = "O"break # Break player 2 turn:# Check win combos:loc_attempt = loc_y - 1play_loc_list2.append(loc_attempt)play_loc_list2.sort()for i in range(0, len(WIN_COMBOS)):if WIN_COMBOS[i] == play_loc_list2:print("You have won!")- BOARD[loc - 1] = symbol #if input is valid it will replace the inputted location with an X or O in the BOARD list
- make_board()
- list.append(loc - 1) #and add the location to the move history
- list.sort()
- break
make_board()turn = 1- global over
- for combo in WIN_COMBOS: #checks if player has won
- winner = True
- for number in combo:
- if number not in list:
- winner = False
- break
- if winner:
- print("\n" + player + " Wins!")
- over = True
- break
- if (not over and all(ele.isalpha() for ele in BOARD)): #checks for tie
- print("Tie game.")
- over = True
- while not over: #plays each necessary round until game is over, technically checks if player two has won
- turn(player_1, play_loc_list1, "X", "Cross") #player 1's turn
- if over: #if player one wins or ends in tie
- break
- turn(player_2, play_loc_list2, "O", "Nought") #players 2's turn
- if __name__ == '__main__':
main()- main() #runs game
- #Anthony Cooper