Permalink
Browse files

Now have a fully flegded lines class that works!

You can now instantiate a lines object and use it to play a game against the PC.
The previous bug (where it didn't think the empty sub-partition of [1,2] was a winning position) has been fixed.
There's also a little game_board_disply method that gives a pseudo graphical representation of the board!
Now onto the Tkinter front end ...
  • Loading branch information...
1 parent f394fd2 commit 078844bde1bf646c56a12cc71612c6482a3c2a36 @heyese committed Jul 18, 2011
Showing with 35 additions and 7 deletions.
  1. +35 −7 lines.py
View
@@ -278,6 +278,9 @@ def win_or_lose(partition, max_adj):
# Right guys, this is the daddy. Given a position, can I win?
# Like list_games, this is iterative, but we've used the idea of partitions and hopefully a hash table to try and speed it up.
+ # win_or_lose on any empty partition should be a winning position - if it's your turn and there are no moves left, you've won.
+ if len(partition) == 0 : return 1
+
# First, the special case:
if len(partition) == 1 and sorted(partition)[0] <= max_adj + 1:
if 1 < sorted(partition)[0]:
@@ -297,6 +300,9 @@ def win_or_lose_v2(partition, max_adj):
# Right guys, this is the daddy. Given a position, can I win?
# Like list_games, this is iterative, but we've used the idea of partitions and hopefully a hash table to try and speed it up.
+ # win_or_lose on any empty partition should be a winning position - if it's your turn and there are no moves left, you've won.
+ if len(partition) == 0 : return 1
+
# First, the special case:
if len(partition) == 1 and sorted(partition)[0] <= max_adj + 1:
if 1 < sorted(partition)[0]:
@@ -357,7 +363,12 @@ def jumble(partition):
# moves_list - a list of the currently available moves
#
+
+# There's a bug!! When we get to the last moves of the game, the winning and losing functions stop working properly.
+
+
class lines: # class(partition,max_adj)
+ import random
def __init__(self,partition,max_adj):
# I think it's best to have as few attributes as possible and use methods to calculate what's asked for
# based on these values when possible.
@@ -420,6 +431,15 @@ def game_make_losing_move(self):
self.game_take_turn(losing_moves[0])
else: return -1
+ def game_board_display(self):
+ lines_crossed_off = [ line for move in self.moves_history for line in move ]
+ total_lines = sorted(lines_crossed_off + self.board)
+ graphic = []
+ for i in total_lines:
+ if i in self.board: graphic.append(' I ')
+ else: graphic.append(' X ')
+ return graphic
+
################# Private functions ################################################
def moves_list_special(self,total,max):
# total : total number of lines in the game
@@ -522,6 +542,7 @@ def give_partition(self, moves_list):
partition = []
single_lines = []
length = 0
+ if len(moves_list) == 0: return []
# First step - make a list of the available single moves
for move in sorted(moves_list):
if len(move) == 1: single_lines.append(move[0])
@@ -674,14 +695,18 @@ def win_or_lose(self, partition, max_adj):
# Right guys, this is the daddy. Given a position, can I win?
# Like list_games, this is iterative, but we've used the idea of partitions and hopefully a hash table to try and speed it up.
- # First, the special case:
+ # win_or_lose on any empty partition should be a winning position - if it's your turn and there are no moves left, you've won.
+ if len(partition) == 0 : return 1
+
+ # First, the special cases:
if len(partition) == 1 and sorted(partition)[0] <= max_adj + 1:
if 1 < sorted(partition)[0]:
return 1 # If you can cross off all but the last line on your go, you've won
else: return -1 # if there's only one line left, I'm afraid you're forced to cross it off and you've lost
+
# Now the general case:
- for part in poss_partitions(sorted(partition), max_adj):
+ for part in self.poss_partitions(sorted(partition), max_adj):
if tuple(part) not in win_or_lose_hash:
win_or_lose_hash[tuple(part)] = win_or_lose(part, max_adj) # I don't know what the syntax is, but building up a DB of winners / losers should help speed this up a lot
if win_or_lose_hash[tuple(part)] == -1:
@@ -693,14 +718,17 @@ def win_or_lose_v2(self, partition, max_adj):
# Right guys, this is the daddy. Given a position, can I win?
# Like list_games, this is iterative, but we've used the idea of partitions and hopefully a hash table to try and speed it up.
+ # win_or_lose on any empty partition should be a winning position - if it's your turn and there are no moves left, you've won.
+ if len(partition) == 0 : return 1
+
# First, the special case:
if len(partition) == 1 and sorted(partition)[0] <= max_adj + 1:
if 1 < sorted(partition)[0]:
return 1 # If you can cross off all but the last line on your go, you've won
else: return -1 # if there's only one line left, I'm afraid you're forced to cross it off and you've lost
# Now the general case:
- for part in poss_partitions_v2(sorted(partition), max_adj):
+ for part in self.poss_partitions_v2(sorted(partition), max_adj):
if tuple(part) not in win_or_lose_v2_hash:
win_or_lose_v2_hash[tuple(part)] = win_or_lose_v2(part, max_adj) # I don't know what the syntax is, but building up a DB of winners / losers should help speed this up a lot
if win_or_lose_v2_hash[tuple(part)] == -1:
@@ -709,17 +737,17 @@ def win_or_lose_v2(self, partition, max_adj):
def winning_sub_parts(self, partition, max_adj):
- list = poss_partitions(sorted(partition),max_adj)
+ list = self.poss_partitions(sorted(partition),max_adj)
new_list = []
for entry in list:
- if win_or_lose(entry, max_adj) == 1: new_list.append(entry)
+ if self.win_or_lose(entry, max_adj) == 1: new_list.append(entry)
return new_list
def losing_sub_parts(self, partition, max_adj):
- list = poss_partitions(sorted(partition),max_adj)
+ list = self.poss_partitions(sorted(partition),max_adj)
new_list = []
for entry in list:
- if win_or_lose(entry, max_adj) == -1: new_list.append(entry)
+ if self.win_or_lose(entry, max_adj) == -1: new_list.append(entry)
return new_list
def part2board(self, partition):

0 comments on commit 078844b

Please sign in to comment.