Lots of stuff happened
[progcomp2013.git] / qchess / 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                 self.types_revealed = [True, False] # Whether the types are known (by default the first type is always known at game start)
17                 
18
19                 # 
20                 self.last_state = None
21                 self.move_pattern = 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                 self.types_revealed = c.types_revealed[:]
33
34                 self.last_state = None
35                 self.move_pattern = None
36
37         
38
39         # Make a string for the piece (used for debug)
40         def __str__(self):
41                 return str(self.current_type) + " " + str(self.types) + " at " + str(self.x) + ","+str(self.y)  
42
43         # Draw the piece in a pygame surface
44         def draw(self, window, grid_sz = [80,80], style="quantum"):
45
46                 # First draw the image corresponding to self.current_type
47                 img = images[self.colour][self.current_type]
48                 rect = img.get_rect()
49                 if style == "classical":
50                         offset = [-rect.width/2, -rect.height/2]
51                 else:
52                         offset = [-rect.width/2,-3*rect.height/4] 
53                 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]))
54                 
55                 
56                 if style == "classical":
57                         return
58
59                 # Draw the two possible types underneath the current_type image
60                 for i in range(len(self.types)):
61                         if self.types_revealed[i] == True:
62                                 img = small_images[self.colour][self.types[i]]
63                         else:
64                                 img = small_images[self.colour]["unknown"] # If the type hasn't been revealed, show a placeholder
65
66                         
67                         rect = img.get_rect()
68                         offset = [-rect.width/2,-rect.height/2] 
69                         
70                         if i == 0:
71                                 target = (self.x * grid_sz[0] + grid_sz[0]/5 + offset[0], self.y * grid_sz[1] + 3*grid_sz[1]/4 + offset[1])                             
72                         else:
73                                 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])                           
74                                 
75                         window.blit(img, target) # Blit shit
76         
77         # Collapses the wave function!          
78         def select(self):
79                 if self.current_type == "unknown":
80                         self.choice = random.randint(0,1)
81                         self.current_type = self.types[self.choice]
82                         self.types_revealed[self.choice] = True
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_revealed[0] == False or self.types_revealed[1] == False):
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