Skip to content

Commit

Permalink
fixed ai :D
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthMDev committed Dec 7, 2016
1 parent ddd05d0 commit cb7e068
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
78 changes: 71 additions & 7 deletions tic_tac_toe.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,43 @@ def checkLine(char, spot1, spot2, spot3):
if board[spot1] == char and board[spot2] == char and board[spot3] == char:
return True

def x_is_about_to_win(spot1, spot2, spot3):
if board[spot1] == 'x' and board[spot2] == 'x' and board[spot3] != 'o' and board[spot3] !='x':
return True
def checkAlmostWin():
if x_is_about_to_win(2,1,0):
return 0
if x_is_about_to_win(6,3,0):
return 0
if x_is_about_to_win(8,4,0):
return 0
if x_is_about_to_win(7,4,1):
return 1
if x_is_about_to_win(0,1,2):
return 2
if x_is_about_to_win(6,4,2):
return 2
if x_is_about_to_win(8,5,2):
return 2
if x_is_about_to_win(5,4,3):
return 3
if x_is_about_to_win(3,4,5):
return 5
if x_is_about_to_win(0,3,6):
return 6
if x_is_about_to_win(2,4,6):
return 6
if x_is_about_to_win(8,7,6):
return 6
if x_is_about_to_win(1,4,7):
return 7
if x_is_about_to_win(0,4,8):
return 8
if x_is_about_to_win(2,5,8):
return 8
if x_is_about_to_win(6,7,8):
return 8

def checkAll(char):
if checkLine(char, 0, 1, 2):
return True
Expand Down Expand Up @@ -83,15 +120,36 @@ def single_player():


while True:
random.seed() #give a random generator
opponent = random.randint(0, 8)
if checkAlmostWin() == 0:
opponent = 0
elif checkAlmostWin() == 1:
opponent = 1
elif checkAlmostWin() == 2:
opponent = 2
elif checkAlmostWin() == 3:
opponent = 3
elif checkAlmostWin() == 4:
opponent = 4
elif checkAlmostWin() == 5:
opponent = 5
elif checkAlmostWin() == 6:
opponent = 6
elif checkAlmostWin() == 7:
opponent = 7
elif checkAlmostWin() == 8:
opponent = 8
else:
random.seed()
opponent = random.randint(0, 8)
if board[opponent] != 'o' and board[opponent] != 'x':
board[opponent] = 'o'

#Check
if checkAll('o') == True:
print 'O WINS!'
break
elif board[0] != 0 and board[1] != 1 and board[2] != 2 and board[3] != 3 and board[4] !=4 and board[5]!= 5 and board[6] != 6 and board[7] != 7 and board[8] != 8 and checkAll('o') != True and checkAll('x') != True:
print 'Its a tie'
break
break

else:
Expand All @@ -100,12 +158,18 @@ def single_player():
if selectmode == 'single player':
while True:
single_player()
#if win == True:
# askplayagain = raw_input('Want to play again? Enter y for yes or n for no.')

#playagain()
askplayagain = raw_input('Want to play again? Enter y for yes or n for no.')
if askplayagain.lower() == 'y':
single_player()
elif askplayagain.lower() == 'n':
break
break
elif selectmode == 'multiplayer':
while True:
multiplayer()
askplayagain2 = raw_input('Want to play again? Enter y for yes or n for no.')
if askplayagain2.lower() == 'y':
multiplayer()
elif askplayagain2.lower() == 'n':
break
break
2 changes: 1 addition & 1 deletion updater.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import git

g = git.cmd.Git(git_dir)
g = git.cmd.Git('./')
g.pull()

0 comments on commit cb7e068

Please sign in to comment.