From 4a9d92e94b3779410a75fadc16319afbfb0e9e74 Mon Sep 17 00:00:00 2001 From: Dhruv-Kakadiya <51689176+DZ521111@users.noreply.github.com> Date: Sat, 16 Jan 2021 09:22:05 +0530 Subject: [PATCH] tic tac toe using artificial intelligence --- AI_Tic_Tac_Toe.py | 142 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 AI_Tic_Tac_Toe.py diff --git a/AI_Tic_Tac_Toe.py b/AI_Tic_Tac_Toe.py new file mode 100644 index 0000000..e378182 --- /dev/null +++ b/AI_Tic_Tac_Toe.py @@ -0,0 +1,142 @@ +# -*- coding: utf-8 -*- +""" +Created on Mon Nov 29 21:05:40 2019 + +@author: DHRUV +""" + +# assign 10 empty space for board +board = [' ' for x in range(10)] + +def insertLetter(letter, pos): + board[pos] = letter + +# for free space +def spaceIsFree(pos): + return board[pos] == ' ' + +# for print the board +def printBoard(board): + print(' | |') + print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3]) + print(' | |') + print('-----------') + print(' | |') + print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6]) + print(' | |') + print('-----------') + print(' | |') + print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9]) + print(' | |') + +# predict the winner function +def isWinner(bo, le): + return (bo[7] == le and bo[8] == le and bo[9] == le) or (bo[4] == le and bo[5] == le and bo[6] == le) or(bo[1] == le and bo[2] == le and bo[3] == le) or(bo[1] == le and bo[4] == le and bo[7] == le) or(bo[2] == le and bo[5] == le and bo[8] == le) or(bo[3] == le and bo[6] == le and bo[9] == le) or(bo[1] == le and bo[5] == le and bo[9] == le) or(bo[3] == le and bo[5] == le and bo[7] == le) + +# move function for player movinf +def playerMove(): + run = True + while run: + move = input('Select a position to place an \'X\' (1-9): ') + try: + move = int(move) + if move > 0 and move < 10: + if spaceIsFree(move): + run = False + insertLetter('X', move) + else: + print('Sorry, this space is occupied!') + else: + print('Type a number within the range!') + except: + print('Type a number!') + + +# comparision of moves +def compMove(): + possibleMoves = [x for x, letter in enumerate(board) if letter == ' ' and x != 0] + move = 0 + + for let in ['O', 'X']: + for i in possibleMoves: + boardCopy = board[:] + boardCopy[i] = let + if isWinner(boardCopy, let): + move = i + return move + + cornersOpen = [] + for i in possibleMoves: + if i in [1,3,7,9]: + cornersOpen.append(i) + + if len(cornersOpen) > 0: + move = selectRandom(cornersOpen) + return move + + if 5 in possibleMoves: + move = 5 + return move + + edgesOpen = [] + for i in possibleMoves: + if i in [2,4,6,8]: + edgesOpen.append(i) + + if len(edgesOpen) > 0: + move = selectRandom(edgesOpen) + + return move + +# select random +def selectRandom(li): + import random + ln = len(li) + r = random.randrange(0,ln) + return li[r] + +# check whether board is full or not +def isBoardFull(board): + if board.count(' ') > 1: + return False + else: + return True + +# main function of program +def main(): + print('Welcome to Tic Tac Toe!') + printBoard(board) + + while not(isBoardFull(board)): + if not(isWinner(board, 'O')): + playerMove() + printBoard(board) + else: + print('Sorry, O\'s won this time!') + break + + if not(isWinner(board, 'X')): + move = compMove() + if move == 0: + print('Tie Game!') + else: + insertLetter('O', move) + print('Computer placed an \'O\' in position', move , ':') + printBoard(board) + else: + print('X\'s won this time! Good Job!') + break + + if isBoardFull(board): + print('Tie Game!') + +# loop for untill we wants to play +if __name__ == "__main__": + while True: + answer = input('Do you want to play again? (Y/N)') + if answer.lower() == 'y' or answer.lower == 'yes': + board = [' ' for x in range(10)] + print('-----------------------------------') + main() + else: + break \ No newline at end of file