Snow
Forest
Mountains
Snow
Snow

Project Report on Tic-Tac-Toe Game by Muttashim Sarkar (ICSS Student)

Project Report

On

[Tic-Tac-Toe Game]

Project Report is submitted to Indian Cyber Security Solutions under the guidance of Himalaya Sir.

Submitted By – Muttashim Sarkar (ICSS Student)

 

Project

 

INDEX

 

  1. Introduction
  1. Project Ideas
  1. Reference
  1. Conclusion

 

INTRODUCTION

Python is a high-level, interpreted and general-purpose dynamic programming language that focuses on code readability. The syntax in Python helps the programmers to do coding in fewer steps as compared to Java or C++. The Python is widely used in bigger organizations because of its multiple programming paradigms. They usually involve imperative and object-oriented functional programming. It has a comprehensive and large standard library that has automatic memory management and dynamic features.

Our project is to implement the Tic-Tac-Toe game which will features like single player and two player modes. In single player mode we have used game theory logics like minimax algorithms to determine the best move that the computer plays.

The project was tested and it turned out to be 72.33% accurate.

To run the game just click on the ticTacToe.py file.

 

 

PROJECT IDEAS

First of all we need to implement the best move logic for the computer to play in single player mode. As already stated we have used minimax algorithms to determine the best move. Please check the references for more knowledge on the algorithms. This section of code is implemented on the file AIplayer.py as given below.

 

import math

 

def check(player):

# Checks if the player wins the board or not

winPoints = [[1,2,3], [1,4,7], [1,5,9],

[2,5,8],

[3,6,9], [3,5,7],

[4,5,6],

[7,8,9]]

for i in winPoints:

won = True

for j in i:

if j not in player:

won = False

break

if won:

return True

return False

 

def isMoveLeft(board):

# Checks if any more move is left

for i in range(3):

if ‘_’ in board[i]:

return True

return False

 

def evaluate(board):

# Evaluates the board provided and returns score or checks for draw

player1 = []

player2 = []

for i in range(3):

for j in range(3):

if board[i][j] == ‘X’:

player1.append((i*3) + j + 1)

elif board[i][j] == ‘O’:

player2.append((i*3) + j + 1)

if check(player1):

return 10

elif check(player2):

return -10

return 0

 

def findmoves(board,depth,isP1):

# Recursive function to determine score if a move is done

# depth is the number of moves done, isP1 is boolean value that informs if turn is for Player1 or not

 

score = evaluate(board)

if score == 10:

return score

if score == -10:

return score

if not isMoveLeft(board):

return 0

 

if isP1:

best = -1000

for i in range(3):

for j in range(3):

if board[i][j] == ‘_’:

board[i][j] = ‘X’

best = max(best, findmoves(board, depth + 1,not

isP1))

board[i][j] = ‘_’

return best

 

else:

best = 1000

for i in range(3):

for j in range(3):

if board[i][j] == ‘_’:

board[i][j] = ‘O’

best = min(best, findmoves(board, depth+1, not

isP1))

board[i][j] = ‘_’

return best

 

def find_best_move(board):

# Determines the best move possible

# We will check for the minimum possible score as computer is the minimizer here

 

bestVal = 1000

pos = -1

 

for i in range(3):

for j in range(3):

if board[i][j] == ‘_’:

board[i][j] = ‘O’

moveVal = findmoves(board,0,True)

board[i][j] = ‘_’

if moveVal<bestVal:

pos = (i*3) + j + 1

bestVal = moveVal

return pos

 

def get_board(player1,player2):

# Creates a replica of the current board status

board = [[],[],[]]

pos = 1

for i in range(3):

for j in range(3):

if pos in player1:

board[i].append(‘X’)

elif pos in player2:

board[i].append(‘O’)

else:

board[i].append(‘_’)

pos = pos+1

return board

 

 

 

 

Now we need to implement the main UI and use the game logics. We have used images for “circle” and “cross” that are displayed on the buttons when clicked. These image files are kept on assets directory. The implementation of ticTacToe.py is as given below.

 

 

from tkinter import *

from tkinter import messagebox

import sys,os,random

from PIL import Image

from PIL import ImageTk

from AIplayer import *

 

def restart():

# Function to restart the program

python = sys.executable

os.execl(python, python, *sys.argv)

 

def displayresult(result):

# Function to display the result of the game

messagebox.showinfo(‘Result’, result)

res = messagebox.askyesno(‘Play Again’, ‘Do You Want To Play Again?’)

if res:

restart()

else:

sys.exit()

 

def buttonPressed(btn, point):

# Button press event handler function

global remainingButtons,player,turn,width,height,singlePlayer

result = ”

if turn == ‘circle’:

global circle,player2

player2.append(point)

image = circle

if check(player2):

result = ‘Player 2 wins!’

if singlePlayer:

result = ‘You Lost!\nBetter Luck Next Time.’

turn = ‘cross’

player.set(‘Player 1’)

else:

global cross,player1

player1.append(point)

image = cross

if check(player1):

result = ‘Player 1 wins!’

if singlePlayer:

result = ‘Congratulations!\nYou won.’

turn = ‘circle’

player.set(‘Player 2’)

 

btn.config(image = image, width = width, height = height)

btn[‘command’] = 0

remainingButtons.remove(btn)

if result != ”:

displayresult(result)

checkDraw()

if singlePlayer and turn == ‘circle’:

autoplay()

 

def getButton():

# Gets the best move by the AIplayer

global remainingButtons,player1,player2,buttonList

 

for i in remainingButtons:

machine = player2.copy()

point = buttonList.index(i) + 1

machine.append(point)

if check(machine):

return i

 

for i in remainingButtons:

human = player1.copy()

point = buttonList.index(i) + 1

human.append(point)

if check(human):

return i

 

board = get_board(player1,player2)

pos = find_best_move(board)

return buttonList[pos-1]

 

def autoplay():

# Autoplay function for the AIplayer

global buttonList

btn = getButton()

pos = buttonList.index(btn) + 1

buttonPressed(btn, pos)

 

def checkDraw():

# Checks if the move results in a draw

global remainingButtons

if len(remainingButtons) == 0:

displayresult(‘Draw!’)

 

def check(player):

# Checks if the player wins or not with this move

global winPoints

for i in winPoints:

won = True

for j in i:

if j not in player:

won = False

break

if won:

return True

return False

 

if __name__ == ‘__main__’:

root = Tk()

singlePlayer = messagebox.askyesno(‘Select Mode’,’Singleplayer?’)      # True if yes

root.title(‘Tic-Tac-Toe’)

width,height = 60,70

 

crossImg = Image.open(‘assets/cross.png’)

crossImg = crossImg.resize((width,height), Image.ANTIALIAS)

circleImg = Image.open(‘assets/circle.png’)

circleImg = circleImg.resize((width,height), Image.ANTIALIAS)

 

cross = ImageTk.PhotoImage(crossImg)

circle = ImageTk.PhotoImage(circleImg)

 

turn = ‘cross’

player = StringVar()

player.set(‘Player 1’)

 

w,h = 7          ,3

 

Label(root, text = ‘Tic-Tac-Toe’, font = (‘bold’, 20), fg = ‘darkblue’).pack(side = TOP)

labelframe = Frame(root)

labelframe.pack(side = TOP)

Label(labelframe, text = ‘Turn : ‘, fg = ‘red’).pack(side = LEFT)

Label(labelframe, textvariable = player, fg = ‘green’).pack(side = LEFT)

 

row0 = Frame(root)

row0.pack(side = TOP)

row1 = Frame(root)

row1.pack(side = TOP)

row2 = Frame(root)

row2.pack(side = TOP)

 

b1 = Button(row0,width = w, height = h, command = lambda : buttonPressed(b1, 1))

b1.pack(side = LEFT)

b2 = Button(row0, width = w, height = h,  command = lambda : buttonPressed(b2, 2))

b2.pack(side = LEFT)

b3 = Button(row0, width = w, height = h, command = lambda : buttonPressed(b3, 3))

b3.pack(side = LEFT)

 

b4 = Button(row1, width = w, height = h, command = lambda : buttonPressed(b4, 4))

b4.pack(side = LEFT)

b5 = Button(row1, width = w, height = h, command = lambda : buttonPressed(b5, 5))

b5.pack(side = LEFT)

b6 = Button(row1, width = w, height = h, command = lambda : buttonPressed(b6, 6))

b6.pack(side = LEFT)

 

b7 = Button(row2, width = w, height = h, command = lambda : buttonPressed(b7, 7))

b7.pack(side = LEFT)

b8 = Button(row2, width = w, height = h, command = lambda : buttonPressed(b8, 8))

b8.pack(side = LEFT)

b9 = Button(row2, width = w, height = h, command = lambda : buttonPressed(b9, 9))

b9.pack(side = LEFT)

 

remainingButtons = [b1,b2,b3,b4,b5,b6,b7,b8,b9]

buttonList = remainingButtons.copy()

 

winPoints = [[1,2,3], [1,4,7], [1,5,9],

[2,5,8],

[3,6,9], [3,5,7],

[4,5,6],

[7,8,9]]

player1 = []

player2 = []

 

root.mainloop()

 

 

 

REFERENCE

 

Documentation of tkintermodule used in the program:

https://docs.python.org/3/library/tk.html

 

Documentation of sys module used in the program:

https://docs.python.org/2/library/sys.html

 

Documentation of os module used in the program:

https://docs.python.org/2/library/os.html

 

Documentation of random module used in the program:

https://docs.python.org/2/library/random.html

 

Documentation of PIL module used in the program:

https://pillow.readthedocs.io/en/5.3.x/?badge=latest

 

Learn more about minimax algorithms:

https://www.geeksforgeeks.org/minimax-algorithm-in-game-theory-set-1-introduction/

https://www.geeksforgeeks.org/minimax-algorithm-in-game-theory-set-3-tic-tac-toe-ai-finding-optimal-move/

 

 

CONCLUSION

In the conclusion of this prroject, I would like to say that Python is a fun and easy programming language and while creating a project like this, it has not just been a good experience butit also helped in the development of my creativity and logical thinking. I would be more than happy to work on other projects in Python because it’s just amazing to work with Python. The program is working and I hope, it’s also bug-free.

 

Thank you for your attention

 

 

 

Highest Selling Technical Courses of Indian Cyber Security Solutions:

Certified Ethical Hacker Training in Bhubaneswar

Ethical Hacking Training in Bhubaneswar

Certified Ethical Hacker Training in Bangalore

Ethical Hacking Training in Bangalore

Certified Ethical Hacker Training in Hyderabad

Ethical Hacking Training in Hyderabad

Ethical Hacking Training in Dhanbad

Certified Ethical Hacker Training in Dubai

Ethical Hacking Training in Dubai

Placement

Python Training in Bangalore

Python Training in Hyderabad

Python Training in Bhubaneswar

Microsoft Azure Training in Hyderabad

Microsoft Azure Training in Bangalore

Microsoft Azure Training in Bhubaneswar

Networking Training in Bangalore

Networking Training in Hyderabad

Networking Training in Bhubaneswar

Advance Python Training in Hyderabad

Advance Python Training in Bangalore

Advance Python Training in Bhubaneswar

Amazon Web Services Training in Hyderabad

Amazon Web Services Training in Bangalore

Amazon Web Services Training in Bhubaneswar

Certified Ethical Hacker Certification – C | EH v10

Computer Forensic Training in Kolkata

Summer Training for CSE, IT, BCA & MCA Students 

Network Penetration Testing training

Ethical Hacking  training

Internet Of Things Training

Data Analysis

Internet Of Things Training Hyderabad

Internet Of Things Training in Bhubaneswar

Internet Of Things Training in Bangalore

Embedded System Training

Digital Marketing Training

Machine Learning Training

Python Programming training

Android Training in Bangalore

Android Training in Hyderabad

Android Training in Bhubaneswar

Diploma in Network Security Training

Android Development  training

Secured Coding in Java

Certified Network Penetration Tester 

Diploma in Web Application Security 

Certified Web Application Penetration Tester 

Certified Android Penetration Tester 

Certified Python Programming 

Advance Python Training 

Reverse Engineering Training  

Amazon Web Services Training  

VMware Training 

 

Cybersecurity services that can protect your company:

Web Security | Web Penetration Testing

Web Penetration Testing Company in Bangalore

Network Penetration Testing – NPT

Network Penetration Testing Service in Bangalore

Android App Penetration Testing

Source Web Development

Source Code Review

Android App Development

Digital Marketing Consultancy

Data Recovery

 

Other Location for Online Courses:

Bhubaneswar

Bangalore

Hyderabad

Dhanbad

Dubai

 

 


Show Buttons
Hide Buttons