Welcome, guest | Sign In | My Account | Store | Cart

A simple TicTacToe script including AI (Artificial Intelligence)

Python, 129 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from random import choice, randrange as rrange
from time import sleep
class Game:
    def _init__(self):
        self.p1 = ''
        self.p2 = ''
        self.map = []
        self.game = True
        self.done = False #For CPU
        self.count = 0
    def set_up(self):
        self.done = False
        self.game = True
        self.count = 0
        self.p1 = choice(['X','O'])
        if self.p1 =='X':
            self.p2 = 'O'
        else:
            self.p2 = 'X'
        print("You are ",self.p1)
        sleep(2)
        self.map = [0,1,2,3,4,5,6,7,8]
        self.cycle()
    def display(self):
        a = str(self.map).replace("[",'')
        b = a.replace(',','|')
        c = b.replace("'",'')
        d = c.replace("]",'')
        print(d[:7])
        print('-'*8)
        print(d[9:16])
        print('-'*8)
        print(d[18:])
    def check_map(self,a,b,c):
        if self.map[a]==self.map[b]==self.map[c]:
            if self.map[a] ==self.p1:
                print("YOU WIN")
                sleep(3)
                self.game = False
            else:
                print("COM WINS")
                sleep(3)
                self.game = False
        if self.count >=9:
            self.game = False
        if self.game==False:
            self.set_up()
    def play1(self):
        self.display()
        space = 0
        try:
            space = eval(input("Pick a space: "))
        except:
            NameError
            SyntaxError
            TypeError
            ValueError
            print("Invalid!")
        if space > 8:
            print("TRY AGAIN")
            self.play1()
        elif self.map[space] in ['X','O']:
            print("Space is used")
            sleep(1)
            self.play1()
        else:
            self.map[space] = self.p1
        self.check_map(0,1,2)
        self.check_map(3,4,5)
        self.check_map(6,7,8)
        self.check_map(0,3,6)
        self.check_map(1,4,7)
        self.check_map(2,5,8)
        self.check_map(0,4,8)
        self.check_map(2,4,6)
        self.count +=1
    def AI(self,a,b,c):
        if self.done ==False:
            if self.map[a]==self.map[b]:
                if self.map[c] not in ['X','O']:
                    self.map[c] = self.p2
                print("COM picked ",c)
                self.done = True
    def play2(self):
        print("")
        sm = choice([True,False])
        if sm ==True:
            self.AI(0,1,2)
            self.AI(3,4,5)
            self.AI(6,7,8)
            self.AI(0,3,6)
            self.AI(1,4,7)
            self.AI(2,5,8)
            self.AI(2,4,6)
            self.AI(0,4,8)
            self.AI(6,4,2)
            self.AI(8,4,0)
            self.AI(5,4,3)
            self.AI(2,1,0)
            self.AI(8,7,6)
        else:
            cpu = rrange(0,9)
            if self.map[cpu] not in ['X','O']:
                self.map[cpu] = self.p2
                print("COM picked ",cpu)
                self.done = True
        while self.done !=True:
            cpu = rrange(0,9)
            if self.map[cpu] not in ['X','O']:
                self.map[cpu] = self.p2
                print("COM picked ",cpu)
                self.done =True
        self.check_map(0,1,2)
        self.check_map(3,4,5)
        self.check_map(6,7,8)
        self.check_map(0,3,6)
        self.check_map(1,4,7)
        self.check_map(2,5,8)
        self.check_map(0,4,8)
        self.check_map(2,4,6)
        self.count +=1
    def cycle(self):
        while self.game==True:
            self.done =False
            self.play1()
            self.play2()
            print()
game = Game()
game.set_up()

I would like for you to try this script. It works perfect!

1 comment

Nestor Gutierrez 9 years, 3 months ago  # | flag

Master I like your game it is very fun.

I have edit the code because it didn't work for me in the beginning I changed:

space = eval(input("Pick a space: "))

For this: space = int(input("Pick a space: "))

Hope helps other too