Final Commit
[progcomp2013.git] / agents / python / sample.py
1 #!/usr/bin/python -u
2
3 # Sample agent
4 # Copy this file, change the agent as needed
5
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
8
9 debug = False
10
11 # The first thing to do is pick a cool name...
12 class AgentSample(InternalAgent): 
13         def __init__(self, name, colour):
14                 InternalAgent.__init__(self, name, colour) # The InternalAgent class gives you some useful stuff
15
16                 # You can access self.board to get a qchess.Board that stores the state as recorded by the agent
17                 # This board is automatically updated by the InternalAgent base class
18                 # As well as a grid of pieces, qchess.Board gives you lists of pieces and other useful functions; see qchess/src/board.py
19                 
20
21                 #TODO: Any extra initialisation
22                 
23                 # You should print debug messages like this:
24                 if debug:
25                         sys.stderr.write(sys.argv[0] + " : Initialised agent\n")
26                 
27
28         # Must return [x,y] of selected piece
29         # Your agent will call select(), followed by get_move() and so on
30         # TODO: Implement
31         def select(self):
32                 # debug message
33                 if debug:
34                         sys.stderr.write(sys.argv[0] + " : Selecting piece...\n")
35                 
36
37                 # Here is a random choice algorithm to help you start
38                 # It is a slight improvement on purely random; it will pick a piece that has at least one known possible move
39                 # BUT it has a possibility to loop infinitely! You should fix that.
40
41                 while True:
42                         # Randomly pick a piece
43                         # Use self.board.pieces[self.colour] to get a list of your pieces
44                         # Use self.board.pieces[opponent(self.colour)] to get opponent pieces
45                         # Use self.board.king[self.colour], vice versa, to get the king
46
47                         choices = self.board.pieces[self.colour] # All the agent's pieces
48                         choice_index = random.randint(0, len(choices)-1) # Get the index in the list of the chosen piece
49                         self.choice = choices[choice_index] # Choose the piece, and remember it
50                         
51                         # Find all known possible moves for the piece
52                         # Use self.board.possible_moves(piece) to get a list of possible moves for a piece
53                         # *BUT* Make sure the type of the piece is known (you can temporarily set it) first!
54                         # Use Piece.current_type to get/set the current type of a piece
55
56                         all_moves = [] # Will store all possible moves for the piece
57                         tmp = self.choice.current_type # Remember the chosen piece's current type
58
59                         if tmp == "unknown": # For pieces that are in a supperposition, try both types
60                                 for t in self.choice.types:
61                                         if t == "unknown":
62                                                 continue # Ignore unknown types
63                                         self.choice.current_type = t # Temporarily overwrite the piece's type
64                                         all_moves += self.board.possible_moves(self.choice) # Add the possible moves for that type
65                         else:
66                                 all_moves = self.board.possible_moves(self.choice) # The piece is in a classical state; add possible moves
67                         self.choice.current_type = tmp # Reset the piece's current type
68                         if len(all_moves) > 0:
69                                 break # If the piece had *any* possible moves, it is a good choice; leave the loop
70                         # Otherwise the loop will try again
71                 # End while loop
72         
73                 return [self.choice.x, self.choice.y] # Return the position of the selected piece
74
75         # Must return [x,y] of square to move the piece previously selected into
76         # Your agent will call select(), followed by get_move() and so on
77         # TODO: Implement this
78         def get_move(self):     
79                 # debug message
80                 if debug:
81                         sys.stderr.write(sys.argv[0] + " : Moving piece ("+str(self.choice)+")\n")
82                 # As an example we will just pick a random move for the piece previously chosen in select()
83
84                 # Note that whichever piece was previously selected will have collapsed into a classical state
85
86                 # self.board.possible_moves(piece) will return a list of [x,y] pairs for valid moves
87
88                 moves = self.board.possible_moves(self.choice) # Get all moves for the selected piece
89                 move_index = random.randint(0, len(moves)-1) # Get the index in the list of the chosen move
90                 return moves[move_index] # This is a randomly chosen [x,y] pair for a valid move of the piece
91
92
93 # Hints:
94 # 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
95 # If you want to see if a square is threatened/defended, you can call self.board.coverage([x,y]); see qchess/src/board.py
96 # 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.
97 # Look at qchess/src/agent_bishop.py for a more effective (but less explained) agent
98
99 if __name__ == "__main__":
100
101         # Parse arguments here
102         for i in range(len(sys.argv)):
103                 if sys.argv[i] == "--debug":
104                         debug = True
105                 elif sys.argv[i] == "--no-debug":
106                         debug = False
107
108         colour = sys.stdin.readline().strip("\r\n")
109         agent = AgentSample(sys.argv[0], colour) # Change the class name here
110         run_agent(agent) # This is provided by qchess. It calls the functions of your agent as required during the game.
111
112 # You can run this as an external agent with the qchess program
113 # Just run ./qchess.py and apply common sense (or read the help file)
114
115 # If you are feeling adventurous you can add it to the qchess program as an internal agent
116 # This might give better performance... unless you use the --timeout switch, in which case there is absolutely no point
117 # 1. Delete the lines that run the agent (the block that starts with if __name__ == "__main__")
118 # 2. Copy the file to qchess/src/agent_sample.py (or whatever you want to call it)
119 # 3. Edit qchess/src/Makefile so that agent_sample.py appears as one of the files in COMPONENTS
120 # 4. Rebuild by running make in qchess
121 # Again, run ./qchess.py and apply common sense
122
123         
124

UCC git Repository :: git.ucc.asn.au