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

Classic Tic Tac Toe game with AI. Created using Python 2.7.6

Python, 198 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
##Tic-Tac_Toe
##Gaurav Chandarana, gmchandarana@gmail.com


import random
new = ['','','','','','','','','']
man = ''
machine = ''
null = ''


def sign(man, machine):
    man = raw_input("What team you want to be? X or O ")
    while man not in ('x','X','o','O'):
        print "Invalid Choice!"
        man = raw_input("What team you want to be? X or O ")
    if man == 'x' or man == 'X':
        print "Ok, X is yours!"
        machine = 'o'
    else:
        print "Ok, O is yours!"
        machine = 'x'
    return man.upper(), machine.upper()
    
    

def decide_turn():
    turn = None
    while turn not in ('y','Y','n','N'):
        turn = raw_input("Do you want to go first? ")
        if turn == 'y' or turn == 'Y':
            return 1
        elif turn == 'n' or turn == 'N':
            return 0
        else:
            print "its an invalid choice."

def draw(a):
    
    print "\n\t",a[0],"|",a[1],"|",a[2]
    print "\t", "--------"
    print "\n\t",a[3],"|",a[4],"|",a[5]
    print "\t", "--------"
    print "\n\t",a[6],"|",a[7],"|",a[8], "\n"

def congo_man():
    print "You won!!"

def congo_machine():
    print "Hahha, I won!!!"

def man_first(man, machine, new):
    while winn(man, machine, new) is None:
        move = man_move(man, new)
        new[int(move)] = man
        draw(new)
        if winn(man, machine, new) != None:
            break
        else:
            pass
        print "ummmm....i'll take.."
        p_move = machine_move(man, machine, new)
        print p_move
        new[int(p_move)] = machine
        draw(new)
    q = winn(man, machine, new)
    if q == 1:
        congo_man()
    elif q == 0:
        congo_machine()
    else:
        print "Its tie man..."
   


def machine_first(man, machine, new):
    while not winn(man, machine, new):
        print "i'll take..."
        p_move = machine_move(man, machine, new)
        print p_move
        new[p_move] = machine
        draw(new)
        if winn(man, machine, new) != None:
            break
        else:
            pass
        move = man_move(man, new)
        new[int(move)] = man
        draw(new)
    q = winn(man, machine, new)
    if q == 1:
        congo_man()
    elif q == 0:
        congo_machine()
    else:
        print "Its tie man..."


def winn(man, machine, new):
    ways = ((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))
    for i in ways:
        if new[i[0]] == new[i[1]] == new[i[2]] != null:
            winner = new[i[0]]
            if winner == man:
                return 1
            elif winner == machine:
                return 0
            if null not in new: 
                return 'TIE'
    if null not in new: 
        return 'TIE'    
    return None


def man_move(man, new): 
    a = raw_input("where you want to move? ")
    while True:
        if a not in ('0','1','2','3','4','5','6','7','8'):
            print "Sorry, invalid move"
            a = raw_input("where you want to move? ")
        elif new[int(a)] != null:
            print "Sorry, the place is already taken"
            a = raw_input("where you want to move? ")
        else:
            return int(a)



    
def machine_move(man, machine, new):
    best = [4, 0, 2, 6, 8]
    blank = []
    for i in range(0,9):
        if new[i] == null:
            blank.append(i)
    
    for i in blank:
        new[i] = machine
        if winn(man, machine, new) is 0:

            return i
        new[i] = null

    for i in blank:
        new[i] = man
        if winn(man, machine, new) is 1:

            return i
        new[i] = null

    return int(blank[random.randrange(len(blank))])
        



def display_instruction():
      """ Displays Game Instuructions. """
      print 
      """
      Welcome to the Game...
      You will make your move known by entering a number, 0 - 8.
      The will correspond to the board position as illustrated:


                          0 | 1 | 2            
                         -----------
                          3 | 4 | 5            
                         -----------
                          6 | 7 | 8

                          
      Prepare yourself, the ultimate bettel is about to begin.....
      """

  
def main(man, machine, new):
    display_instruction()
    print "so lets begin.."
    a = sign(man, machine)
    man = a[0]
    machine = a[1]
    b = decide_turn()
    if b == 1:
        print "Ok, you are first!"
        print "Lets get started, here's a new board!"
        draw(new)
        man_first(man, machine, new)
    elif b == 0:
        print "Ok, I'll be the first!"
        print "So, lets start.."
        draw(new)
        machine_first(man, machine, new)
    else:
        pass


main(man, machine, new)
raw_input("Press enter to exit")
Created by Gaurav on Thu, 10 Jul 2014 (MIT)
Python recipes (4591)
Gaurav's recipes (1)

Required Modules

Other Information and Tasks