a72937c085ae5ae8b9b9bd19ae178d197b344ef3
[progcomp2013.git] / qchess / src / piece.py
1 import random
2
3 # I know using non-abreviated strings is inefficient, but this is python, who cares?
4 # Oh, yeah, this stores the number of pieces of each type in a normal chess game
5 piece_types = {"pawn" : 8, "bishop" : 2, "knight" : 2, "rook" : 2, "queen" : 1, "king" : 1, "unknown" : 0}
6
7 # Class to represent a quantum chess piece
8 class Piece():
9         def __init__(self, colour, x, y, types):
10                 self.colour = colour # Colour (string) either "white" or "black"
11                 self.x = x # x coordinate (0 - 8), none of this fancy 'a', 'b' shit here
12                 self.y = y # y coordinate (0 - 8)
13                 self.types = types # List of possible types the piece can be (should just be two)
14                 self.current_type = "unknown" # Current type
15                 self.choice = -1 # Index of the current type in self.types (-1 = unknown type)
16                 
17                 
18                 self.last_state = None
19                 
20                 self.move_pattern = None
21                 self.coverage = None
22
23                 
24
25         def init_from_copy(self, c):
26                 self.colour = c.colour
27                 self.x = c.x
28                 self.y = c.y
29                 self.types = c.types[:]
30                 self.current_type = c.current_type
31                 self.choice = c.choice
32                 
33                 self.last_state = None
34                 self.move_pattern = None
35
36         
37
38         # Make a string for the piece (used for debug)
39         def __str__(self):
40                 return str(self.colour) + " " + str(self.current_type) + " " + str(self.types) + " at " + str(self.x) + ","+str(self.y)  
41
42         # Draw the piece in a pygame surface
43         def draw(self, window, grid_sz = [80,80], style="quantum"):
44
45                 # First draw the image corresponding to self.current_type
46                 img = images[self.colour][self.current_type]
47                 rect = img.get_rect()
48                 if style == "classical":
49                         offset = [-rect.width/2, -rect.height/2]
50                 else:
51                         offset = [-rect.width/2,-3*rect.height/4] 
52                 window.blit(img, (self.x * grid_sz[0] + grid_sz[0]/2 + offset[0], self.y * grid_sz[1] + grid_sz[1]/2 + offset[1]))
53                 
54                 
55                 if style == "classical":
56                         return
57
58                 # Draw the two possible types underneath the current_type image
59                 for i in range(len(self.types)):
60                         if always_reveal_states == True or self.types[i][0] != '?':
61                                 img = small_images[self.colour][self.types[i]]
62                         else:
63                                 img = small_images[self.colour]["unknown"] # If the type hasn't been revealed, show a placeholder
64
65                         
66                         rect = img.get_rect()
67                         offset = [-rect.width/2,-rect.height/2] 
68                         
69                         if i == 0:
70                                 target = (self.x * grid_sz[0] + grid_sz[0]/5 + offset[0], self.y * grid_sz[1] + 3*grid_sz[1]/4 + offset[1])                             
71                         else:
72                                 target = (self.x * grid_sz[0] + 4*grid_sz[0]/5 + offset[0], self.y * grid_sz[1] + 3*grid_sz[1]/4 + offset[1])                           
73                                 
74                         window.blit(img, target) # Blit shit
75         
76         # Collapses the wave function!          
77         def select(self):
78                 if self.current_type == "unknown" or not self.choice in [0,1]:
79                         self.choice = random.randint(0,1)
80                         if self.types[self.choice][0] == '?':
81                                 self.types[self.choice] = self.types[self.choice][1:]
82                         self.current_type = self.types[self.choice]
83                 return self.choice
84
85         # Uncollapses (?) the wave function!
86         def deselect(self):
87                 #print "Deselect called"
88                 if (self.x + self.y) % 2 != 0:
89                         if (self.types[0] != self.types[1]) or (self.types[0][0] == '?' or self.types[1][0] == '?'):
90                                 self.current_type = "unknown"
91                                 self.choice = -1
92                         else:
93                                 self.choice = 0 # Both the two types are the same
94
95         # The sad moment when you realise that you do not understand anything about a subject you studied for 4 years...

UCC git Repository :: git.ucc.asn.au