how to code a tic tac toe game with ai python

Solutions on MaxInterview for how to code a tic tac toe game with ai python by the best coders in the world

showing results for - "how to code a tic tac toe game with ai python"
Jaya
13 Jul 2017
1#Tic Tac Toe game in python by techwithtim
2
3board = [' ' for x in range(10)]
4
5def insertLetter(letter, pos):
6    board[pos] = letter
7
8def spaceIsFree(pos):
9    return board[pos] == ' '
10
11def printBoard(board):
12    print('   |   |')
13    print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
14    print('   |   |')
15    print('-----------')
16    print('   |   |')
17    print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
18    print('   |   |')
19    print('-----------')
20    print('   |   |')
21    print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
22    print('   |   |')
23    
24def isWinner(bo, le):
25    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)
26
27def playerMove():
28    run = True
29    while run:
30        move = input('Please select a position to place an \'X\' (1-9): ')
31        try:
32            move = int(move)
33            if move > 0 and move < 10:
34                if spaceIsFree(move):
35                    run = False
36                    insertLetter('X', move)
37                else:
38                    print('Sorry, this space is occupied!')
39            else:
40                print('Please type a number within the range!')
41        except:
42            print('Please type a number!')
43            
44
45def compMove():
46    possibleMoves = [x for x, letter in enumerate(board) if letter == ' ' and x != 0]
47    move = 0
48
49    for let in ['O', 'X']:
50        for i in possibleMoves:
51            boardCopy = board[:]
52            boardCopy[i] = let
53            if isWinner(boardCopy, let):
54                move = i
55                return move
56
57    cornersOpen = []
58    for i in possibleMoves:
59        if i in [1,3,7,9]:
60            cornersOpen.append(i)
61            
62    if len(cornersOpen) > 0:
63        move = selectRandom(cornersOpen)
64        return move
65
66    if 5 in possibleMoves:
67        move = 5
68        return move
69
70    edgesOpen = []
71    for i in possibleMoves:
72        if i in [2,4,6,8]:
73            edgesOpen.append(i)
74            
75    if len(edgesOpen) > 0:
76        move = selectRandom(edgesOpen)
77        
78    return move
79
80def selectRandom(li):
81    import random
82    ln = len(li)
83    r = random.randrange(0,ln)
84    return li[r]
85    
86
87def isBoardFull(board):
88    if board.count(' ') > 1:
89        return False
90    else:
91        return True
92
93def main():
94    print('Welcome to Tic Tac Toe!')
95    printBoard(board)
96
97    while not(isBoardFull(board)):
98        if not(isWinner(board, 'O')):
99            playerMove()
100            printBoard(board)
101        else:
102            print('Sorry, O\'s won this time!')
103            break
104
105        if not(isWinner(board, 'X')):
106            move = compMove()
107            if move == 0:
108                print('Tie Game!')
109            else:
110                insertLetter('O', move)
111                print('Computer placed an \'O\' in position', move , ':')
112                printBoard(board)
113        else:
114            print('X\'s won this time! Good Job!')
115            break
116
117    if isBoardFull(board):
118        print('Tie Game!')
119
120while True:
121    answer = input('Do you want to play again? (Y/N)')
122    if answer.lower() == 'y' or answer.lower == 'yes':
123        board = [' ' for x in range(10)]
124        print('-----------------------------------')
125        main()
126    else:
127        break
queries leading to this page
tic tac toe python favtutortic tac toe python code lazy programmertic tac toe game in pythontac tic toe pythoncreate tic tac toe in python program tic tac toe python freehow to make tic tac toe in python with aihow to make an ai tic tac toe using pythonalgorithm for understanding tic tac toe pythontic tac toe python aitic tac toe code in pythontic tac toe source code in pythonwrite a program to implement tic tac toe game using pythonhow to build a tic tac toe game in pythontic tac toe python tutorialpython tic tac toe codetic tac toe python that always win algorithm using aicreate a tic tac toe pythontic tac toe python machine learningtic tac toe written in pythontic tac toe python source codepython tic tac toe ai example codetic tac toe full game in python odetic tac toe ai in pythonpython tic tac toe interfacetic tac toe game source code in python with imagebest ai algorithm for tic tac toe pythonprogram for tic tac toe in pythontic tac to pythonhow to build a tic tac toe aiin pythontic tac toe game in python tkinterlets create a python tic tac toe ai from scratchtic tac toe pythontic tac toe minimax pythonpythin tic tac toe gamepython tic tac toe gameplaypython code for tic tac toepython tic tac toehow to make a tic tac toe game in pythontic tac toe bot pythontic tac toe python with aihow to output the tic tac toe in pythonminimax tic tac toe pythonpython tic tac toe programcode for tic tac toe in pythontic tac toe python beginneralgorithm for tic tac toe game in pythontic tac toe game pythontic tac toe with ai pythontic tac toe python program source codetic tac toe python projecttext based python tic tac toe gamesimple tic tac toe python codehow to create tic tac toe in pythonpython tic tac toe aipython code tic tac toepython tic tac toe gamehow to build tic tac toe pythontic tac toe board pythonpython tic tac toe project source codehow can i make a tic tac toe game in pythonsimple tic tac toe game in pythonmake tic tac toe pythonpython tutorial tic tacc toehow to make tic tac toe pythontic tac toe python refactoringhow to program a tic tac toe game in python where computer never losespython tic tact toetictactoe python with aitic tac toe game with help of pythontic tac toe library used in pythontic tac toe using a 2a algorithm in pythonpython tic tac toe downloadtic tac toe game program in pythonhow to code tic tac toe in pythonhow to add a computer player in my tic tac toe with pythoncoding a tic tac toe board game pythontic tac toe board in pythontic tac toe with computer pythonpython ai to play tic tac toepython algorithm to win tic tac toetic tac toe game using ai in pythonpython tik tak toe utility functionadvanced tic tac toe game python codepython tic tac toe ai codetic tac toe ai python codetice tac toe python codebasic tic tac toe pythonmin max algorithm tic tac toe pythonpython code for tic tac toe aimake tic tac toe in pythoncoding ai for tic tac toe in pythonwhat features can be added to tic tac toe code in pythonhow to create a tic tac toe game in python in bottic tac toe python programhow to make a tic tac toe ai in pythontic tac toe game project using pythoon tkintrehow to make ai for tic tac toe pythontic tac toe program in pythonpython tic tac toe game source codecreating a tic tac toe game in pythonminimax algorithm python tic tac toetic tac toe pythonpython ai to always win gamehow to create simple tic tac toe game using pythontic tac toe ai in pythontic tac toe python codepython tic tac toe board codepython ai tic tac toegame playing 2c minmax on tictactoe python codetic tac toe code python artificial intelligenceimplementation of tic tac toe using game playing algorithm pythonpython puzzle 3a tic tac toepython tic tac toe code exampletic tac toe pytohn computertic tac toe python iaai python tik tac toepython make perfect tic tac toe aitic tac toe algorithm pythontic tac toe python runtic tac toe in python using machine learningpython tic tac toe projecthow to make tic tac toe in pythontic tac toe pyrhon aicustom tic tac toe pythonconsole based tic tac toe for ai 2b human pythontic tac toe ai pythonhow to create an interactive tic tac toe game in python with tynkertic tac toe tutorial pythonai for tic tac toe pythontic tac toe where does the computer play algorithm pythontic tac toe game in pythontic tac toe with classes pythontic tac toe in pythonpython tic tac toe game codessimple tic tac toe using pythontic tac toe ai python tkinter codemake tic tac toe in python with ai opponentminimax tic tac toe size varable pythontic tac toe using pythonai tic tac toe python boardtic tac toe app in pythonhow to build a tic tac toe game in python with guitic tac toe gameboard pythonsimple games like tic tac toe pythonbasic python tic tac toetic tac toe in python codetic tac toe game in python with source codetic tac toe in python source codetic tac toe python codetic tac toe game in python tutorialpygletmake tic tac toe pythontic tac toe ai pythonhow to make a tic tac toe board in pythonai in python to play xohow to code tic tac toe game in pythn tkinertic tac toe app pythontic tac toe strategy pythontic tac toe ai pythonsimple tic tac toe game in pythontic tac toe with computer python codetic tac toe code python codesnailhow to code a tic tac toe game with ai pythontic tac toe python code for beginnerstic tac toe python full codepython tic tac toe tutorialtictactoe ai pythonpython simple tic tac toetic tac toe game using python normaltic tac toe api pythonpython how to display tic tac toetic tac toe code pythobntic tac toe with python the most advanced best codetic tac toe python rulestic tac toe python run gamehow to create python tic tac toe boardtic tac toe with computer and person python codepython team tic tac toetic tac toe project in pythontic tac toe game in python for beginnerstic tac toe code pythontic tac to game scripte pythonai tic tac toe pythonhow to create a tic tac toe game with ai pythontic tat toe pythontic tac toe with pythontic tac toe in python how to code a tic tac toe game with ai python