|
1 | | -import itertools |
2 | | - |
3 | | -from colorama import Fore, Style, init |
4 | | - |
5 | | -init() |
6 | | - |
7 | | - |
8 | | -def win(current_game): |
9 | | - def all_same(l): |
10 | | - if l.count(l[0]) == len(l) and l[0] != 0: |
11 | | - return True |
12 | | - else: |
13 | | - return False |
14 | | - |
15 | | - # horizontal |
16 | | - for row in game: |
17 | | - if all_same(row): |
18 | | - print("Player {} is the winner horizontally!".format(row[0])) |
19 | | - print("Congrats") |
20 | | - return True |
21 | | - |
22 | | - # vertical |
23 | | - for col in range(len(game)): |
24 | | - check = [] |
25 | | - for row in game: |
26 | | - check.append(row[col]) |
27 | | - if all_same(check): |
28 | | - print("Player {} is the winner vertically!".format(check[0])) |
29 | | - return True |
30 | | - |
31 | | - # / diagonal |
32 | | - diags = [] |
33 | | - for idx, reverse_idx in enumerate(reversed(range(len(game)))): |
34 | | - diags.append(game[idx][reverse_idx]) |
35 | | - if all_same(diags): |
36 | | - print("Player {} is the winner diagonally(/)!".format(diags[0])) |
37 | | - return True |
38 | | - |
39 | | - # \ diagonal |
40 | | - diags = [] |
41 | | - for idx in range(len(game)): |
42 | | - diags.append(game[idx][idx]) |
43 | | - |
44 | | - if all_same(diags): |
45 | | - print("Player {diags[0]} has won Diagonally (\\)") |
46 | | - return True |
47 | | - |
48 | | - return False |
49 | | - |
50 | | - |
51 | | -def game_board(game_map, player=0, row=0, column=0, just_display=False): |
52 | | - try: |
53 | | - |
54 | | - if game_map[row][column] != 0: |
55 | | - print("This space is occupied, try another!") |
56 | | - return False |
57 | | - |
58 | | - print(" " + " ".join([str(i) for i in range(len(game_map))])) |
59 | | - if not just_display: |
60 | | - game_map[row][column] = player |
61 | | - |
62 | | - for count, row in enumerate(game_map): |
63 | | - colored_row = "" |
64 | | - for item in row: |
65 | | - if item == 0: |
66 | | - colored_row += " " |
67 | | - elif item == 1: |
68 | | - colored_row += Fore.GREEN + " X " + Style.RESET_ALL |
69 | | - elif item == 2: |
70 | | - colored_row += Fore.MAGENTA + " O " + Style.RESET_ALL |
71 | | - print(count, colored_row) |
72 | | - |
73 | | - return game_map |
74 | | - |
75 | | - except IndexError: |
76 | | - print("Did you attempt to play a row or column outside the range of 0,1 or 2? (IndexError)") |
77 | | - return False |
78 | | - except Exception as e: |
79 | | - print(str(e)) |
80 | | - return False |
81 | | - |
82 | | - |
83 | | -play = True |
84 | | -Players = [1, 2] |
85 | | - |
86 | | -while play: |
87 | | - game_size = int(input("What size game TicTacToe? ")) |
88 | | - game = [[0 for i in range(game_size)] for i in range(game_size)] |
89 | | - |
90 | | - game_won = False |
91 | | - player_cycle = itertools.cycle([1, 2]) |
92 | | - game_board(game, just_display=True) |
93 | | - while not game_won: |
94 | | - current_player = next(player_cycle) |
95 | | - Played = False |
96 | | - |
97 | | - while not Played: |
98 | | - print("Player: {}".format(current_player)) |
99 | | - row_choice = int(input("Which row? ")) |
100 | | - column_choice = int(input("Which column? ")) |
101 | | - Played = game_board(game, player=current_player, row=row_choice, column=column_choice) |
102 | | - |
103 | | - if win(game): |
104 | | - game_won = True |
105 | | - again = input("The game is over,would you like to play again? (y/n) ") |
106 | | - if again.lower() == "y": |
107 | | - print("restarting!") |
108 | | - elif again.lower() == "n": |
109 | | - print("Byeeeee!!!") |
110 | | - play = False |
111 | | - else: |
112 | | - print("not a valid answer!!") |
113 | | - play = False |
| 1 | +# Tic-Tac-Toe Program using |
| 2 | +# random number in Python |
| 3 | + |
| 4 | +# importing all necessary libraries |
| 5 | +import numpy as np |
| 6 | +import random |
| 7 | +from time import sleep |
| 8 | + |
| 9 | +# Creates an empty board |
| 10 | +def create_board(): |
| 11 | + return(np.array([[0, 0, 0], |
| 12 | + [0, 0, 0], |
| 13 | + [0, 0, 0]])) |
| 14 | + |
| 15 | +# Check for empty places on board |
| 16 | +def possibilities(board): |
| 17 | + l = [] |
| 18 | + |
| 19 | + for i in range(len(board)): |
| 20 | + for j in range(len(board)): |
| 21 | + |
| 22 | + if board[i][j] == 0: |
| 23 | + l.append((i, j)) |
| 24 | + return(l) |
| 25 | + |
| 26 | +# Select a random place for the player |
| 27 | +def random_place(board, player): |
| 28 | + selection = possibilities(board) |
| 29 | + current_loc = random.choice(selection) |
| 30 | + board[current_loc] = player |
| 31 | + return(board) |
| 32 | + |
| 33 | +# Checks whether the player has three |
| 34 | +# of their marks in a horizontal row |
| 35 | +def row_win(board, player): |
| 36 | + for x in range(len(board)): |
| 37 | + win = True |
| 38 | + |
| 39 | + for y in range(len(board)): |
| 40 | + if board[x, y] != player: |
| 41 | + win = False |
| 42 | + continue |
| 43 | + |
| 44 | + if win == True: |
| 45 | + return(win) |
| 46 | + return(win) |
| 47 | + |
| 48 | +# Checks whether the player has three |
| 49 | +# of their marks in a vertical row |
| 50 | +def col_win(board, player): |
| 51 | + for x in range(len(board)): |
| 52 | + win = True |
| 53 | + |
| 54 | + for y in range(len(board)): |
| 55 | + if board[y][x] != player: |
| 56 | + win = False |
| 57 | + continue |
| 58 | + |
| 59 | + if win == True: |
| 60 | + return(win) |
| 61 | + return(win) |
| 62 | + |
| 63 | +# Checks whether the player has three |
| 64 | +# of their marks in a diagonal row |
| 65 | +def diag_win(board, player): |
| 66 | + win = True |
| 67 | + y = 0 |
| 68 | + for x in range(len(board)): |
| 69 | + if board[x, x] != player: |
| 70 | + win = False |
| 71 | + if win: |
| 72 | + return win |
| 73 | + win = True |
| 74 | + if win: |
| 75 | + for x in range(len(board)): |
| 76 | + y = len(board) - 1 - x |
| 77 | + if board[x, y] != player: |
| 78 | + win = False |
| 79 | + return win |
| 80 | + |
| 81 | +# Evaluates whether there is |
| 82 | +# a winner or a tie |
| 83 | +def evaluate(board): |
| 84 | + winner = 0 |
| 85 | + |
| 86 | + for player in [1, 2]: |
| 87 | + if (row_win(board, player) or |
| 88 | + col_win(board,player) or |
| 89 | + diag_win(board,player)): |
| 90 | + |
| 91 | + winner = player |
| 92 | + |
| 93 | + if np.all(board != 0) and winner == 0: |
| 94 | + winner = -1 |
| 95 | + return winner |
| 96 | + |
| 97 | +# Main function to start the game |
| 98 | +def play_game(): |
| 99 | + board, winner, counter = create_board(), 0, 1 |
| 100 | + print(board) |
| 101 | + sleep(2) |
| 102 | + |
| 103 | + while winner == 0: |
| 104 | + for player in [1, 2]: |
| 105 | + board = random_place(board, player) |
| 106 | + print("Board after " + str(counter) + " move") |
| 107 | + print(board) |
| 108 | + sleep(2) |
| 109 | + counter += 1 |
| 110 | + winner = evaluate(board) |
| 111 | + if winner != 0: |
| 112 | + break |
| 113 | + return(winner) |
| 114 | + |
| 115 | +# Driver Code |
| 116 | +print("Winner is: " + str(play_game())) |
0 commit comments