|
| 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