From cb7e068343330b7337768fe2c9a735a831ab43f8 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 7 Dec 2016 19:14:04 +0100 Subject: [PATCH] fixed ai :D --- tic_tac_toe.py | 78 +++++++++++++++++++++++++++++++++++++++++++++----- updater.py | 2 +- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/tic_tac_toe.py b/tic_tac_toe.py index 5723a33..c0c01da 100644 --- a/tic_tac_toe.py +++ b/tic_tac_toe.py @@ -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 @@ -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: @@ -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 diff --git a/updater.py b/updater.py index 6d86903..d9adbd3 100644 --- a/updater.py +++ b/updater.py @@ -1,4 +1,4 @@ import git -g = git.cmd.Git(git_dir) +g = git.cmd.Git('./') g.pull()