summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Seguin <guillaume@segu.in>2007-07-24 04:48:32 +0200
committerGuillaume Seguin <guillaume@segu.in>2007-07-24 04:48:32 +0200
commit8a591d4d903430620f5961c339079cf893d6ef79 (patch)
treedcbafd4dc26d139fe990da966f07b1d8363c7c7f
parent85e3a8bea71ad0bc26f592f1c3bccace5a96820a (diff)
downloadtictactoe-8a591d4d903430620f5961c339079cf893d6ef79.tar.gz
tictactoe-8a591d4d903430620f5961c339079cf893d6ef79.tar.bz2
* "Improve" IA
-rwxr-xr-xtictactoe.py66
1 files changed, 65 insertions, 1 deletions
diff --git a/tictactoe.py b/tictactoe.py
index 0597d8d..cf7a954 100755
--- a/tictactoe.py
+++ b/tictactoe.py
@@ -95,7 +95,7 @@ class gtkTicTacToe (gtk.DrawingArea):
return NO_WINNER
return False
- def computer_turn (self):
+ def computer_turn_random (self):
'''Computer IA - yes, it's _very_ random'''
cells = self.empty_cells ()
cell = random.randint (1, cells)
@@ -108,6 +108,70 @@ class gtkTicTacToe (gtk.DrawingArea):
break
if cell == 0:
break
+
+ def computer_turn_smart (self):
+ '''Computer IA, much less random'''
+ for i in range (3):
+ xv1 = self._map[i][0]
+ xv2 = self._map[i][1]
+ yv1 = self._map[0][i]
+ yv2 = self._map[0][1]
+ if xv1 == COMPUTER_ID and xv1 == xv2:
+ self._map[i][2] = COMPUTER_ID
+ return
+ elif xv1 == COMPUTER_ID and self._map[i][2] == xv1:
+ self._map[i][1] = COMPUTER_ID
+ return
+ elif xv2 == COMPUTER_ID and self._map[i][2] == xv2:
+ self._map[i][0] = COMPUTER_ID
+ return
+ if yv1 == COMPUTER_ID and yv1 == yv2:
+ self._map[2][i] = COMPUTER_ID
+ return
+ elif yv1 == COMPUTER_ID and self._map[2][i] == yv1:
+ self._map[1][i] = COMPUTER_ID
+ return
+ elif yv2 == COMPUTER_ID and self._map[2][i] == yv2:
+ 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[0][1]
+ if xv1 == PLAYER_ID and xv1 == xv2:
+ self._map[i][2] = COMPUTER_ID
+ return
+ elif xv1 == PLAYER_ID and self._map[i][2] == xv1:
+ self._map[i][1] = COMPUTER_ID
+ return
+ elif xv2 == PLAYER_ID and self._map[i][2] == xv2:
+ self._map[i][0] = COMPUTER_ID
+ return
+ if yv1 == PLAYER_ID and yv1 == yv2:
+ self._map[2][i] = COMPUTER_ID
+ return
+ elif yv1 == PLAYER_ID and self._map[2][i] == yv1:
+ self._map[1][i] = COMPUTER_ID
+ return
+ elif yv2 == PLAYER_ID and self._map[2][i] == yv2:
+ self._map[0][i] = COMPUTER_ID
+ return
+ cells = self.empty_cells ()
+ cell = random.randint (1, cells)
+ for j in range (3):
+ for i in range (3):
+ if not self._map[i][j]:
+ cell -= 1
+ if cell == 0:
+ self._map[i][j] = COMPUTER_ID
+ break
+ if cell == 0:
+ break
+
+ def computer_turn (self):
+ '''Computer plays!'''
+ self.computer_turn_smart ()
self.redraw (queue = True)
def button_press (self, widget, event):