piece.py 3.33 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import random

# I know using non-abreviated strings is inefficient, but this is python, who cares?
# Oh, yeah, this stores the number of pieces of each type in a normal chess game
piece_types = {"pawn" : 8, "bishop" : 2, "knight" : 2, "rook" : 2, "queen" : 1, "king" : 1, "unknown" : 0}

# Class to represent a quantum chess piece
class Piece():
	def __init__(self, colour, x, y, types):
		self.colour = colour # Colour (string) either "white" or "black"
		self.x = x # x coordinate (0 - 8), none of this fancy 'a', 'b' shit here
		self.y = y # y coordinate (0 - 8)
		self.types = types # List of possible types the piece can be (should just be two)
		self.current_type = "unknown" # Current type
		self.choice = -1 # Index of the current type in self.types (-1 = unknown type)
		
Sam Moore's avatar
Sam Moore committed
17
		
Sam Moore's avatar
Sam Moore committed
18
		self.last_state = None
Sam Moore's avatar
Sam Moore committed
19
		
Sam Moore's avatar
Sam Moore committed
20
		self.move_pattern = None
Sam Moore's avatar
Sam Moore committed
21
		self.coverage = None
Sam Moore's avatar
Sam Moore committed
22
		self.possible_moves = {}
Sam Moore's avatar
Sam Moore committed
23 24 25 26 27 28 29 30 31
		

	def init_from_copy(self, c):
		self.colour = c.colour
		self.x = c.x
		self.y = c.y
		self.types = c.types[:]
		self.current_type = c.current_type
		self.choice = c.choice
Sam Moore's avatar
Sam Moore committed
32
		
Sam Moore's avatar
Sam Moore committed
33 34 35 36 37 38 39
		self.last_state = None
		self.move_pattern = None

	

	# Make a string for the piece (used for debug)
	def __str__(self):
40
		return str(self.colour) + " " + str(self.current_type) + " " + str(self.types) + " at " + str(self.x) + ","+str(self.y)  
Sam Moore's avatar
Sam Moore committed
41 42

	# Draw the piece in a pygame surface
Sam Moore's avatar
Sam Moore committed
43
	def draw(self, window, grid_sz = [80,80], style="quantum"):
Sam Moore's avatar
Sam Moore committed
44 45 46 47

		# First draw the image corresponding to self.current_type
		img = images[self.colour][self.current_type]
		rect = img.get_rect()
Sam Moore's avatar
Sam Moore committed
48 49 50 51
		if style == "classical":
			offset = [-rect.width/2, -rect.height/2]
		else:
			offset = [-rect.width/2,-3*rect.height/4] 
Sam Moore's avatar
Sam Moore committed
52 53 54
		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]))
		
		
Sam Moore's avatar
Sam Moore committed
55 56 57
		if style == "classical":
			return

Sam Moore's avatar
Sam Moore committed
58 59
		# Draw the two possible types underneath the current_type image
		for i in range(len(self.types)):
Sam Moore's avatar
Sam Moore committed
60
			if always_reveal_states == True or self.types[i][0] != '?':
Sam Moore's avatar
Sam Moore committed
61 62 63 64
				if self.types[i][0] == '?':
					img = small_images[self.colour][self.types[i][1:]]
				else:
					img = small_images[self.colour][self.types[i]]
Sam Moore's avatar
Sam Moore committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
			else:
				img = small_images[self.colour]["unknown"] # If the type hasn't been revealed, show a placeholder

			
			rect = img.get_rect()
			offset = [-rect.width/2,-rect.height/2] 
			
			if i == 0:
				target = (self.x * grid_sz[0] + grid_sz[0]/5 + offset[0], self.y * grid_sz[1] + 3*grid_sz[1]/4 + offset[1])				
			else:
				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])				
				
			window.blit(img, target) # Blit shit
	
	# Collapses the wave function!		
	def select(self):
Sam Moore's avatar
Sam Moore committed
81
		if self.current_type == "unknown" or not self.choice in [0,1]:
Sam Moore's avatar
Sam Moore committed
82
			self.choice = random.randint(0,1)
Sam Moore's avatar
Sam Moore committed
83 84
			if self.types[self.choice][0] == '?':
				self.types[self.choice] = self.types[self.choice][1:]
Sam Moore's avatar
Sam Moore committed
85 86 87 88 89 90 91
			self.current_type = self.types[self.choice]
		return self.choice

	# Uncollapses (?) the wave function!
	def deselect(self):
		#print "Deselect called"
		if (self.x + self.y) % 2 != 0:
Sam Moore's avatar
Sam Moore committed
92
			if (self.types[0] != self.types[1]) or (self.types[0][0] == '?' or self.types[1][0] == '?'):
Sam Moore's avatar
Sam Moore committed
93 94 95 96 97 98
				self.current_type = "unknown"
				self.choice = -1
			else:
				self.choice = 0 # Both the two types are the same

	# The sad moment when you realise that you do not understand anything about a subject you studied for 4 years...