summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtictactoe.py107
1 files changed, 53 insertions, 54 deletions
diff --git a/tictactoe.py b/tictactoe.py
index 2f161f7..b278518 100755
--- a/tictactoe.py
+++ b/tictactoe.py
@@ -134,63 +134,62 @@ class gtkTicTacToe (gtk.DrawingArea):
def computer_turn_smart (self):
'''Computer AI, much less random'''
+ def check_set (id):
+ diag1v = self._map[0][0]
+ diag2v = self._map[0][2]
+ if self._map[1][1] == id:
+ if diag1v == id and not self._map[2][2]:
+ self._map[2][2] = COMPUTER_ID
+ return True
+ elif diag2v == id and not self._map[2][0]:
+ self._map[2][0] = COMPUTER_ID
+ return True
+ elif not diag1v and self._map[2][2] == id:
+ self._map[0][0] = COMPUTER_ID
+ return True
+ elif not diag2v and self._map[2][0] == id:
+ self._map[0][2] = COMPUTER_ID
+ return True
+ elif ((diag1v == id and self._map[2][2] == id) \
+ or (diag2v == id and self._map[2][0] == id)) \
+ and not self._map[1][1]:
+ self._map[1][1] = COMPUTER_ID
+ return True
+ for i in range (3):
+ xv1 = self._map[i][0]
+ xv2 = self._map[i][1]
+ yv1 = self._map[0][i]
+ yv2 = self._map[1][i]
+ if xv1 == id and xv1 == xv2 and not self._map[i][2]:
+ self._map[i][2] = COMPUTER_ID
+ return True
+ elif xv1 == id and self._map[i][2] == xv1 \
+ and not self._map[i][1]:
+ self._map[i][1] = COMPUTER_ID
+ return True
+ elif xv2 == id and self._map[i][2] == xv2 \
+ and not self._map[i][0]:
+ self._map[i][0] = COMPUTER_ID
+ return True
+ if yv1 == id and yv1 == yv2 and not self._map[2][i]:
+ self._map[2][i] = COMPUTER_ID
+ return True
+ elif yv1 == id and self._map[2][i] == yv1 \
+ and not self._map[1][i]:
+ self._map[1][i] = COMPUTER_ID
+ return True
+ elif yv2 == id and self._map[2][i] == yv2 \
+ and not self._map[0][i]:
+ self._map[0][i] = COMPUTER_ID
+ return True
+ return False
if not self._map[1][1]:
self._map[1][1] = COMPUTER_ID
return
- for i in range (3):
- xv1 = self._map[i][0]
- xv2 = self._map[i][1]
- yv1 = self._map[0][i]
- yv2 = self._map[1][i]
- if xv1 == COMPUTER_ID and xv1 == xv2 and not self._map[i][2]:
- self._map[i][2] = COMPUTER_ID
- return
- elif xv1 == COMPUTER_ID and self._map[i][2] == xv1 \
- and not self._map[i][1]:
- self._map[i][1] = COMPUTER_ID
- return
- elif xv2 == COMPUTER_ID and self._map[i][2] == xv2 \
- and not self._map[i][0]:
- self._map[i][0] = COMPUTER_ID
- return
- if yv1 == COMPUTER_ID and yv1 == yv2 and not self._map[2][i]:
- self._map[2][i] = COMPUTER_ID
- return
- elif yv1 == COMPUTER_ID and self._map[2][i] == yv1 \
- and not self._map[1][i]:
- self._map[1][i] = COMPUTER_ID
- return
- elif yv2 == COMPUTER_ID and self._map[2][i] == yv2 \
- and not self._map[0][i]:
- self._map[0][i] = COMPUTER_ID
- return
- for i in range (3):
- xv1 = self._map[i][0]
- xv2 = self._map[i][1]
- yv1 = self._map[0][i]
- yv2 = self._map[1][i]
- if xv1 == PLAYER_ID and xv1 == xv2 and not self._map[i][2]:
- self._map[i][2] = COMPUTER_ID
- return
- elif xv1 == PLAYER_ID and self._map[i][2] == xv1 \
- and not self._map[i][1]:
- self._map[i][1] = COMPUTER_ID
- return
- elif xv2 == PLAYER_ID and self._map[i][2] == xv2 \
- and not self._map[i][0]:
- self._map[i][0] = COMPUTER_ID
- return
- if yv1 == PLAYER_ID and yv1 == yv2 and not self._map[2][i]:
- self._map[2][i] = COMPUTER_ID
- return
- elif yv1 == PLAYER_ID and self._map[2][i] == yv1 \
- and not self._map[1][i]:
- self._map[1][i] = COMPUTER_ID
- return
- elif yv2 == PLAYER_ID and self._map[2][i] == yv2 \
- and not self._map[0][i]:
- self._map[0][i] = COMPUTER_ID
- return
+ if check_set (COMPUTER_ID):
+ return
+ if check_set (PLAYER_ID):
+ return
if not self._map[0][0]:
self._map[0][0] = COMPUTER_ID
return