0% found this document useful (0 votes)
38 views3 pages

Aim - To Build A Player Vs Computer Tic-Tac-Toe Game in Python. Theory - Source Code

This document describes a Python program to build a Tic-Tac-Toe game between a player and computer. The program uses NumPy to create the game board and defines functions to place marks, check for wins by row, column or diagonal, evaluate the board for a winner after each turn, and play the full game with input from the player and random moves from the computer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Aim - To Build A Player Vs Computer Tic-Tac-Toe Game in Python. Theory - Source Code

This document describes a Python program to build a Tic-Tac-Toe game between a player and computer. The program uses NumPy to create the game board and defines functions to place marks, check for wins by row, column or diagonal, evaluate the board for a winner after each turn, and play the full game with input from the player and random moves from the computer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Aim – To build a Player vs Computer Tic-Tac-Toe game in python.

 
Theory - In order to win the game, a player must place three of their marks in a horizontal,
vertical, or diagonal row. 
Source Code:

import numpy as np  
import random  
from time import sleep  
def create_board():  
    return(np.array([[0, 0, 0],  
                     [0, 0, 0],  
                     [0, 0, 0]]))  
def possibilities(board):  
    l = []  
    for i in range(len(board)):  
        for j in range(len(board)):  
            if board[i][j] == 0:  
                l.append((i, j))  
    return(l)  
def random_place(board, player):  
    selection = possibilities(board)  
    current_loc = random.choice(selection)  
    board[current_loc] = player  
    return(board)  
def row_win(board, player):  
    for x in range(len(board)):  
        win = True 
        for y in range(len(board)):  
            if board[x, y] != player:  
                win = False 
                continue 
        if win == True:  
            return(win)  
    return(win)  
def col_win(board, player):  
    for x in range(len(board)):  
        win = True 
        for y in range(len(board)):  
            if board[y][x] != player:  
                win = False 
                continue 
        if win == True:  
            return(win)  
    return(win)  
def diag_win(board, player):  
    win = True 
    y = 0 
    for x in range(len(board)):  
        if board[x, x] != player:  
            win = False 
    win = True 
    if win:  
        for x in range(len(board)):  
            y = len(board) - 1 - x  
            if board[x, y] != player:  
                win = False 
    return win  
def evaluate(board):  
    winner = 0 
    for player in [1, 2]:  
        if (row_win(board, player) or 
            col_win(board,player) or  
            diag_win(board,player)):         
            winner = player     
    if np.all(board != 0) and winner == 0:  
        winner = -1 
    return winner  
def play_game(name):  
    board, winner, counter = create_board(), 0, 1 
    print("INITIAL SETUP")  
    print(board)  
    sleep(2)  
    while winner == 0:  
        for player in [1, 2]:  
            if(player==1): 
                correct=0 
                while correct==0: 
                    print("Choice for Chance ("+name+") (0-8): ") 
                    choice = int(input()) 
                    row = int(choice/3) 
                    col = int(choice%3) 
                    if(board[row][col])==0: 
                        board[row][col]=1 
                        correct=1 
                    else: 
                        print("Already taken") 
            else: 
                board = random_place(board, player) 
            print("Board after " + str(counter) + " move")  
            print(board)  
            sleep(2)  
            counter += 1 
            winner = evaluate(board)  
            if winner != 0:  
                break 
    abc = name if winner ==1 else "Computer" 
    return(abc)  
print("Enter Player Name : ") 
name = input() 
print("Player 1 - "+name) 
print("Player 2 - Computer") 
print("Winner is: " + str(play_game(name))) 
 
Output :  

  

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy