예제 #1
0
파일: Game.py 프로젝트: mittman/pycheckmate
	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
예제 #2
0
파일: Game.py 프로젝트: mittman/pycheckmate
	def insert_piece(self, board, piece_name, player_x, player_y):
		File.prompt("STARTING POSITION FOR " + piece_name + " ?")

		moveH = input("Horizontal [1-8]: ")
		moveH = int(moveH)
		while moveH < 1 or moveH > 8:
			File.error("expected [1-8]")
			moveH = input("Horizontal [1-8]: ")
			moveH = int(moveH)

		moveV = input("Vertical [1-8]: ")
		moveV = int(moveV)
		while moveV < 1 or moveV > 8:
			File.error("expected [1-8]")
			moveV = input("Vertical [1-8]: ")
			moveV = int(moveV)

		if board.state[moveH][moveV] != '*':
			File.error("space occupied")
			exit(2)

		if piece_name == "PlayerX King":
			piece_id = Piece(player_x, 'K', moveH, moveV)
			player_x.add_piece(piece_id)
		elif piece_name == "PlayerX Rook":
			piece_id = Piece(player_x, 'R', moveH, moveV)
			player_x.add_piece(piece_id)
		elif piece_name == "PlayerY King":
			piece_id = Piece(player_y, 'K', moveH, moveV)
			player_y.add_piece(piece_id)
		else:
			File.error("expected valid piece")
			exit(3)

		File.prompt("OK " + piece_name + " to " + str(moveH) + "-" + str(moveV))
예제 #3
0
    def ai_move(self, player):
        if player.id == "x":
            key = random.randint(0, 1)
            if key == 0 or "R" not in player.pieces:
                piece = player.pieces["K"]
            else:
                piece = player.pieces["R"]
        else:
            piece = player.pieces["K"]

        legal_moves = self.find_legal_moves(piece)

        ### if len(legal_moves) == 0: *CHECKMATE OR TIE* ###
        if len(legal_moves) == 0:
            File.prompt("game over")
            exit(0)
        if len(legal_moves) == 1:
            new_destination = legal_moves[0]
        else:
            new_destination = legal_moves[random.randint(0, len(legal_moves) - 1)]

            # move:
        self.make_move(player, piece, (new_destination[0], new_destination[1]))

        self.move_log = piece.player + piece.type + " to " + str(new_destination[0]) + "," + str(new_destination[1])
예제 #4
0
파일: Game.py 프로젝트: mittman/pycheckmate
	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)
예제 #5
0
파일: Ai.py 프로젝트: mittman/pycheckmate
	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)
예제 #6
0
파일: main.py 프로젝트: mittman/pycheckmate
def interactive(ply):
    g = Game()
    mode, end = g.game_type()
    end = int(end)

    if mode:  # mode is true means new game
        player_x = Player("x")
        player_y = Player("y")
        b = Board(player_x, player_y)
        ai = Ai(ply)
        remain = ["PlayerX King", "PlayerX Rook", "PlayerY King"]
        g.ask_piece(b, player_x, player_y, remain)
        b.display()

        File.prompt("Who am I, PlayerX or PlayerY?")
        localPlayer = input("Player [x/y]: ")
        if re.match(r"[Xx]", localPlayer):
            localPlayer = "x"
        else:
            localPlayer = "y"

            # if local player is playerX, PlayerX is our ai moves
            # PlayerY is opponents moves inputted by us
        if localPlayer == "x":
            for i in range(0, end):
                # b.ai_move(player_x)
                ai.move(b, player_x)
                b.display()
                File.debug(ai.value(b))
                File.debug(ai.number_of_states)
                ai.opponent_move(player_y, b)
                b.display()
        else:
            for i in range(0, end):
                ai.opponent_move(player_x, b)
                b.display()
                # b.ai_move(player_y)
                ai.move(b, player_y)
                b.display()
                File.debug(ai.value(b))
                File.debug(ai.number_of_states)
    else:
        player_x = Player("x")
        player_y = Player("y")
        b = Board(player_x, player_y)
        File.test_file(b, g, player_x, player_y)

        ai = Ai(ply)

        # AI random moves test:
        for i in range(0, end):
            ai.move(b, player_x)
            b.display()
            File.debug(ai.value(b))
            File.debug(ai.number_of_states)
            ai.move(b, player_y)
            b.display()
            File.debug(ai.value(b))
            File.debug(ai.number_of_states)
예제 #7
0
파일: Game.py 프로젝트: mittman/pycheckmate
	def add_or_move(self, board, player, piece_id, moveH, moveV, num):
		if num == 1:
			new_piece = Piece(player, piece_id, moveH, moveV)
			if player.id == 'x':
				player.add_piece(new_piece)
			elif player.id == 'y':
				player.add_piece(new_piece)
			else:
				File.error("invalid player")
		else:
			if board.state[moveH][moveV] != player.id + piece_id:
				print('\n\n')
				File.prompt("MOVE " + player.id + piece_id + " to " + str(moveH) + "," + str(moveV))
				board.player_move(player, piece_id, moveH, moveV)
예제 #8
0
    def make_move(self, player, piece, new_coords, testing=False):
        new_row = new_coords[0]
        new_col = new_coords[1]
        # If playerY eats playerX's rook:
        if (
            "R" in self.player_x.pieces
            and new_row == self.player_x.pieces["R"].row
            and new_col == self.player_x.pieces["R"].col
        ):
            del self.player_x.pieces["R"]
            if not testing:
                File.prompt("PlayerX rook captured")
                File.prompt("Stalemate")
                exit(0)

        self.state[piece.row][piece.col] = "*"
        piece.prev_coords = (piece.row, piece.col)
        piece.row = new_coords[0]
        piece.col = new_coords[1]
        self.state[0][0] = "player" + piece.player.upper()
        self.state[piece.row][piece.col] = piece.player + piece.type
        self.piece_positions = self.new_positions()
        player.turn += 1
        self.move_log = piece.player + piece.type + " to " + str(new_row) + "," + str(new_col)
예제 #9
0
파일: Ai.py 프로젝트: mittman/pycheckmate
	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)