Beispiel #1
0
	def game_type(self):
		mode = None
		end = 0

		File.prompt("Is this a test?")
		File.print("y) yes, this is a test")
		File.print("n) no, start a new game")
		mode = input("Select (Y/n): ")

		File.prompt("MAX NUMBER OF MOVES?")
		end = input("Input (default 35): ")

		if re.match(r"[Nn]|no|NO", mode):
			File.prompt("NEW GAME")
			mode = True
		else:
			File.prompt("TEST MODE ACTIVATED")
			mode = False

		if not re.match(r"[1-99]", end):
			end = 35
		else:
			File.prompt("NOTE: GAME ENDS IN " + end + " MOVES")

		return mode, end
Beispiel #2
0
	def ask_piece(self, board, player_x, player_y, remain):
		File.prompt("ADD PIECE TO BOARD?")

		i = 0
		for p in remain:
			i += 1
			File.print(str(i) + ") " + p)

		n = len(remain)
		option = input("Select [1-" + str(n) + "]: ")
		try:
			option = int(option)
		except ValueError: self.ask_piece(board, player_x, player_y, remain)

		if option <= n and option > 0 and n > 1:
			piece_name = remain[option-1]
			self.insert_piece(board, piece_name, player_x, player_y)
			remain.pop(option-1)
			self.ask_piece(board, player_x, player_y, remain)
		elif option <= n and option > 0:
			piece_name = remain[option-1]
			self.insert_piece(board, piece_name, player_x, player_y)
			remain.pop(option-1)
		else:
			File.error("Try again")
			self.ask_piece(board, player_x, player_y, remain)
Beispiel #3
0
	def move(self, board, player):
		self.root_node = None
		if not board.find_legal_moves(board.player_y.pieces['K']):
			File.prompt("GAME OVER")
			exit(0)
		else:
			self.root_node = State(board)
			if player.id == 'x':
				self.create_state_tree(board, player, 0, self.root_node, True)
			else:
				self.create_state_tree(board, player, 0, self.root_node, False)
				
			best_state = self.root_node.children_nodes[0]
			if player.id == 'x':
				for s in self.root_node.children_nodes:
					if s.value <= best_state.value:
						best_state = s
			else:
				for s in self.root_node.children_nodes:
					if s.value >= best_state.value:
						best_state = s

			piece = player.pieces[best_state.piece_to_move.type]
			moveH = best_state.new_coords[0]
			moveV = best_state.new_coords[1]
			File.print('\n')
			File.prompt("Best move " + player.id + piece.type + " to " + str(moveH) + "," + str(moveV))
			board.make_move(player, piece, best_state.new_coords)
Beispiel #4
0
 def display(self):
     for p in self.player_x.pieces.values():
         self.state[p.row][p.col] = p.player + p.type
     for p in self.player_y.pieces.values():
         self.state[p.row][p.col] = p.player + p.type
     if self.move_log == "":
         self.state[0][3] = str(0)
     else:
         self.state[0][3] = str(self.player_x.turn)
     File.print("")
     File.print("\n".join("".join(["{:3}".format(item) for item in row]) for row in self.state))
Beispiel #5
0
	def opponent_move(self, player, board):
		horizontal = 0
		vertical = 0
		piece = player.pieces['K']

		if player.id == 'x':
			File.prompt("Move which PlayerX piece?")
			File.print("1) Rook")
			File.print("2) King")

			option = input("Select [1-2]: ")
			try:
				option = int(option)
			except ValueError:
				File.error("Try again")
				self.opponent_move(player, board)

			if option == 1:
				piece = player.pieces['R']
			elif option == 2:
				piece = player.pieces['K']
			else:
				File.error("Try again")
				self.opponent_move(player, board)

		legal_moves = board.find_legal_moves(piece)

		if len(legal_moves) == 0:
			File.print('')
			File.prompt("No legal moves for " + player.id + piece.type)
			board.display()
			File.prompt("Game Over")
			exit(0)

		for move in legal_moves:	#put X's where valid moves are
			horizontal, vertical = move
			board.state[horizontal][vertical] = '+'

		board.display()

		validInput = False
		while not validInput:
			if piece.type == 'K':
				name = "King"
			elif piece.type == 'R':
				name = "Rook"

			File.prompt("Move " + name + " to coordinates")
			horizontal= input("Horizontal [1-8]: ")
			vertical= input("Vertical [1-8]: ")
			try:	#validate input
				horizontal = int(horizontal)
				vertical = int(vertical)
			except ValueError: validInput = False
			for move in legal_moves:	#check if moves match a legal move
				temp_hor, temp_vert = move
				if horizontal == temp_hor and vertical == temp_vert:
					validInput = True
			if validInput != True:
				File.error("Please select a legal move.")


		for move in legal_moves:	#put *'s back where X's were
			temp_hor, temp_vert = move
			board.state[temp_hor][temp_vert] = '*'
		board.make_move(player,piece, (horizontal, vertical))

		board.move_log = piece.player + piece.type + ' to ' + \
						str(horizontal) + ',' + str(vertical)