Skip to content

Commit

Permalink
Adjusted AI.
Browse files Browse the repository at this point in the history
  • Loading branch information
blixt committed Jun 10, 2008
1 parent 4bb43fd commit 959c15f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
4 changes: 2 additions & 2 deletions index.yaml
Expand Up @@ -10,15 +10,15 @@ indexes:
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.

# Used 1372 times in query history.
# Used 1478 times in query history.
- kind: Game
properties:
- name: players
- name: state
- name: last_update
direction: desc

# Used 1144 times in query history.
# Used 1250 times in query history.
- kind: Game
properties:
- name: state
Expand Down
83 changes: 43 additions & 40 deletions monkey.py
Expand Up @@ -59,7 +59,7 @@ class ForcedMove(Exception):
pass

class CpuPlayer(object):
def __init__(self, cleverness = 1.0):
def __init__(self, cleverness = 10.0):
self.player = Player.from_user(users.User('cpu@mnk'), 'CPU')
self.cleverness = cleverness

Expand All @@ -78,46 +78,49 @@ def check(self, cur_player, row_len, x, y, dx, dy):
else:
cur_player = 0

wl = self.win_length

if cur_player > 0 and cur_player == prev_player:
row_len += 1
else:
if prev_player > 0:
al, bl = True, True
af, bf = 0, 0
au, bu = 0, 0
ac, bc = None, None
for o in xrange(0, wl - row_len):
# After row.
if al:
ox, oy = x + dx * o, y + dy * o
if self.valid(ox, oy):
if not self.board[ox][oy]:
if o == 0: ac = (ox, oy)
af += 1
elif self.board[ox][oy] == prev_player:
au += 1
else:
al = False

# Before row.
if bl:
do = 1 + row_len + o
ox, oy = x - dx * do, y - dy * do
if self.valid(ox, oy):
if not self.board[ox][oy]:
if o == 0: bc = (ox, oy)
bf += 1
elif self.board[ox][oy] == prev_player:
bu += 1
else:
bl = False

if ac: self.handle_move(ac, prev_player, row_len + au, af, bf)
if bc: self.handle_move(bc, prev_player, row_len + bu, bf, af)
elif prev_player > 0:
al, bl = True, True
af, bf = 0, 0
au, bu = 0, 0
ac, bc = None, None
for o in xrange(0, self.win_length - row_len):
# After row.
if al:
ox, oy = x + dx * o, y + dy * o
if self.valid(ox, oy):
if not self.board[ox][oy]:
if o == 0: ac = (ox, oy)
af += 1
elif self.board[ox][oy] == prev_player:
au += 1
else:
al = False
else:
al = False

# Before row.
if bl:
do = 1 + row_len + o
ox, oy = x - dx * do, y - dy * do
if self.valid(ox, oy):
if not self.board[ox][oy]:
if o == 0: bc = (ox, oy)
bf += 1
elif self.board[ox][oy] == prev_player:
bu += 1
else:
bl = False
else:
bl = False

if ac: self.handle_move(ac, prev_player, row_len + au, af, bf)
if bc: self.handle_move(bc, prev_player, row_len + bu, bf, af)

row_len = 1
else:
row_len = 1

return cur_player, row_len

Expand All @@ -139,8 +142,8 @@ def handle_move(self, move, player, length, avail, oavail):
# Ignore the move if it cannot ever result in a win.
if length + avail + oavail >= self.win_length:
# Calculate the value of the move.
score = length * 3 + avail
if cpu: score += self.win_length / 2
score = length * 3.0 + avail
if cpu: score += self.win_length / 2.0
self.moves.append([score, move])

def move(self, game):
Expand Down Expand Up @@ -203,7 +206,7 @@ def move(self, game):
for b in xrange(a + 1, len(m)):
if m[a][1] == m[b][1]:
m[b][0] = (max(m[a][0], m[b][0]) +
min(m[a][0], m[b][0])) / 2
min(m[a][0], m[b][0]) / 2.0)
del m[a]
break

Expand Down

0 comments on commit 959c15f

Please sign in to comment.