4 # Copy this file, change the agent as needed
6 from qchess import * # This is normally considered bad practice in python, but good practice in UCC::Progcomp
7 import random # For the example which makes random moves
9 # The first thing to do is pick a cool name...
10 class AgentSample(InternalAgent):
11 def __init__(self, name, colour):
12 InternalAgent.__init__(self, name, colour) # The InternalAgent class gives you some useful stuff
14 # You can access self.board to get a qchess.Board that stores the state as recorded by the agent
15 # This board is automatically updated by the InternalAgent base class
16 # As well as a grid of pieces, qchess.Board gives you lists of pieces and other useful functions; see qchess/src/board.py
19 #TODO: Any extra initialisation
21 # You should print debug messages like this:
22 sys.stderr.write(sys.argv[0] + " : " + str(self) + " : Initialised agent\n")
23 # I would appreciate it if you removed them before submitting an entry though.
25 # Must return [x,y] of selected piece
26 # Your agent will call select(), followed by get_move() and so on
30 sys.stderr.write(sys.argv[0] + " : " + str(self) + " : Selecting piece...\n")
33 # Here is a random choice algorithm to help you start
34 # It is a slight improvement on purely random; it will pick a piece that has at least one known possible move
35 # BUT it has a possibility to loop infinitely! You should fix that.
38 # Randomly pick a piece
39 # Use self.board.pieces[self.colour] to get a list of your pieces
40 # Use self.board.pieces[opponent(self.colour)] to get opponent pieces
41 # Use self.board.king[self.colour], vice versa, to get the king
43 choices = self.board.pieces[self.colour] # All the agent's pieces
44 choice_index = random.randint(0, len(choices)-1) # Get the index in the list of the chosen piece
45 self.choice = choices[choice_index] # Choose the piece, and remember it
47 # Find all known possible moves for the piece
48 # Use self.board.possible_moves(piece) to get a list of possible moves for a piece
49 # *BUT* Make sure the type of the piece is known (you can temporarily set it) first!
50 # Use Piece.current_type to get/set the current type of a piece
52 all_moves = [] # Will store all possible moves for the piece
53 tmp = self.choice.current_type # Remember the chosen piece's current type
55 if tmp == "unknown": # For pieces that are in a supperposition, try both types
56 for t in self.choice.types:
58 continue # Ignore unknown types
59 self.choice.current_type = t # Temporarily overwrite the piece's type
60 all_moves += self.board.possible_moves(self.choice) # Add the possible moves for that type
62 all_moves = self.board.possible_moves(self.choice) # The piece is in a classical state; add possible moves
63 self.choice.current_type = tmp # Reset the piece's current type
64 if len(all_moves) > 0:
65 break # If the piece had *any* possible moves, it is a good choice; leave the loop
66 # Otherwise the loop will try again
69 return [self.choice.x, self.choice.y] # Return the position of the selected piece
71 # Must return [x,y] of square to move the piece previously selected into
72 # Your agent will call select(), followed by get_move() and so on
73 # TODO: Implement this
76 sys.stderr.write(sys.argv[0] + " : " + str(self) + " : Moving piece ("+str(self.choice)+")\n")
77 # As an example we will just pick a random move for the piece previously chosen in select()
79 # Note that whichever piece was previously selected will have collapsed into a classical state
81 # self.board.possible_moves(piece) will return a list of [x,y] pairs for valid moves
83 moves = self.board.possible_moves(self.choice) # Get all moves for the selected piece
84 move_index = random.randint(0, len(moves)-1) # Get the index in the list of the chosen move
85 return moves[move_index] # This is a randomly chosen [x,y] pair for a valid move of the piece
89 # select will probably have to be more complicated than get_move, because by the time get_move is called, the piece's state is known
90 # If you want to see if a square is threatened/defended, you can call self.board.coverage([x,y]); see qchess/src/board.py
91 # A good approach is min/max. For each move, associate a score. Then subtract the scores for moves that the opponent could make. Then pick the move with the highest score.
92 # Look at qchess/src/agent_bishop.py for a more effective (but less explained) agent
94 if __name__ == "__main__":
95 colour = sys.stdin.readline().strip("\r\n")
96 agent = AgentSample(sys.argv[0], colour) # Change the class name here
97 run_agent(agent) # This is provided by qchess. It calls the functions of your agent as required during the game.
99 # You can run this as an external agent with the qchess program
100 # Just run ./qchess.py and apply common sense (or read the help file)
102 # If you are feeling adventurous you can add it to the qchess program as an internal agent
103 # This might give better performance... unless you use the --timeout switch, in which case there is absolutely no point
104 # 1. Delete the lines that run the agent (the block that starts with if __name__ == "__main__")
105 # 2. Copy the file to qchess/src/agent_sample.py (or whatever you want to call it)
106 # 3. Edit qchess/src/Makefile so that agent_sample.py appears as one of the files in COMPONENTS
107 # 4. Rebuild by running make in qchess
108 # Again, run ./qchess.py and apply common sense