Sale!

Tic Tac Toe Game – Python Programming

Rs.5,999.00Rs.8,999.00

Requirement Specification

It’s an Python-programming – project internship, which in advance blended mode comes bundled with Black Jack Game Project.

Complete the implementations of player, actions, result, winner, terminal, utility etc.

  • The player function should take a board state as input, and return which player’s turn it is (either X or O).
    • In the initial game state for example, X gets the first move. Subsequently, the player alternates with each additional move.
    • Any return value is acceptable if a terminal board is provided as input (i.e., the game is already over).
  • The actions function should return a set of all of the possible actions that can be taken on a given board.
    • Each action should be represented as a tuple (i, j) where i corresponds to the row of the move (0, 1, or 2) and j corresponds to which cell in the row corresponds to the move (also 0, 1, or 2).
    • Possible moves are any cells on the board that do not already have an X or an O in them.
    • Any return value is acceptable if a terminal board is provided as input.
  • The result function takes a board and an action as input, and should return a new board state, without modifying the original board.
    • If action is not a valid action for the board, your program should raise an exception.
    • The returned board state should be the board that would result from taking the original input board, and letting the player whose turn it is make their move at the cell indicated by the input action.
    • Importantly, the original board should be left unmodified: since Minimax will ultimately require considering many different board states during its computation. This means that simply updating a cell in board itself is not a correct implementation of the result function. You’ll likely want to make a deep copy of the board first before making any changes.

On Similar way winner,terminal and utility implementation may be thought of.

For all functions that accept a board as input, you may assume that it is a valid board (namely, that it is a list that contains three rows, each with three values of either X, O, or EMPTY). You should not modify the function declarations (the order or number of arguments to each function) provided.

Once all functions are implemented correctly, you should be able to run python runner.py and play against your AI. And, since Tic-Tac-Toe is a tie given optimal play by both sides, you should never be able to beat the AI (though if you don’t play optimally as well, it may beat you!)

Clear
SKU: 10002 Category:

Description

Introduction

tictactoe

Tic-Tac-Toe is a very popular game, so let’s implement an automatic Tic-Tac-Toe game using Python.

The game is automatically played by the program and hence, no user input is needed. Still, developing an automatic game will be lots of fun. Let’s see how to do this.

Numpy and random Python libraries are used to build this game. Instead of asking the user to put a mark on the board, code randomly chooses a place on the board and put the mark. It will display the board after each turn unless a player wins. If the game gets a draw, then it returns -1.

Explanation :

play_game() is the main function, which performs following tasks :

  • Calls create_board() to create a 9×9 board and initializes with 0.
  • For each player (1 or 2), calls the random_place() function to randomly choose a location on board and mark that location with the player number, alternatively.
  • Print the board after each move.
  • Evaluate the board after each move to check whether a row or column or a diagonal has the same player number. If so, displays the winner name. If after 9 moves, there are no winner then displays -1.

Below is the sample code for the above game :

filter_none

edit

play_arrow

brightness_4

# Tic-Tac-Toe Program using

# random number in Python

  

# importing all necessary libraries

import numpy as np

import random

from time import sleep

  

# Creates an empty board

def create_board():

    return(np.array([[0, 0, 0],

                     [0, 0, 0],

                     [0, 0, 0]]))

  

# Check for empty places on board

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)

  

# Select a random place for the player

def random_place(board, player):

    selection = possibilities(board)

    current_loc = random.choice(selection)

    board[current_loc] = player

    return(board)

  

# Checks whether the player has three 

# of their marks in a horizontal row

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)

  

# Checks whether the player has three

# of their marks in a vertical row

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)

  

# Checks whether the player has three

# of their marks in a diagonal row

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

  

# Evaluates whether there is

# a winner or a tie 

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

  

# Main function to start the game

def play_game():

    board, winner, counter = create_board(), 0, 1

    print(board)

    sleep(2)

      

    while winner == 0:

        for player in [1, 2]:

            board = random_place(board, player)

            print(“Board after ” + str(counter) + ” move”)

            print(board)

            sleep(2)

            counter += 1

            winner = evaluate(board)

            if winner != 0:

                break

    return(winner)

  

# Driver Code

print(“Winner is: ” + str(play_game()))

Additional information

Type

Basic Blended, Advanced Blended

Reviews

There are no reviews yet.

Be the first to review “Tic Tac Toe Game – Python Programming”

Your email address will not be published. Required fields are marked *