s/eraseAt/remove/
[progcomp2012.git] / agents / basic_java / piece.java
1 /**
2  * Class to represent a Piece
3  * @author Sam Moore
4  * @website http://progcomp.ucc.asn.au
5  */
6
7 import java.lang.Exception;
8 import java.util.Vector;
9
10 class Piece
11 {
12         //Normally in the Java Way (TM) you would have to make these private, and add "Getters" and "Setters" and all sorts of crap.
13         // Disclaimer: The author is not responsible for the repercussions of not following the Java Way (TM)
14         public int x; //x coord
15         public int y; //y coord
16         public char rank; //Rank of the piece
17         public String colour; //The colour of the Piece
18         public Vector<int[]> positions; //All positions the piece has been at
19         public boolean beenRevealed; //True if the piece has been revealed in combat
20
21         public static char ranks[] = {'B','1','2','3','4','5','6','7','8','9','s','F', '?', '+'}; //List of all the possible ranks
22         //'?' is an unknown piece, '+' is an obstacle
23
24         /**
25          * Constructor
26          * @param c The new colour
27          * @param r The new rank
28          * @param xx The new x coord
29          * @param yy The new y coord
30          */
31         public Piece(String c, char r, int xx, int yy)
32         {
33                 
34                 this.colour = c;
35                 this.rank = r;
36                 this.x = xx;
37                 this.y = yy;
38                 this.positions = new Vector<int[]>();
39                 this.beenRevealed = false;
40                 
41                 positions.add(new int[2]);
42                 positions.elementAt(0)[0] = x;
43                 positions.elementAt(0)[1] = y;
44         }
45
46         /**
47          * @returns True if the piece can move, based on its rank
48          */
49         public boolean Mobile()
50         {
51                 return (rank != 'F' && rank != 'B' && rank != '+' && rank != '?');
52         }
53
54         /**
55          * @returns The value of the piece's rank
56          */
57         public int ValuedRank()
58         {
59                 for (int ii=0; ii < ranks.length; ++ii)
60                 {
61                         if (ranks[ii] == rank)
62                                 return (ranks.length - 2 - ii);
63                 }
64                 return 0;
65         }
66
67         /**
68          * @returns the index in ranks of a rank
69          * @throws Exception if the rank doesn't exist
70          */
71         public static int Index(char rank) throws Exception
72         {
73                 for (int ii=0; ii < ranks.length; ++ii)
74                 {
75                         if (ranks[ii] == rank)
76                                 return ii;
77                 }
78                 throw new Exception("Piece.Index - No such rank as " + rank);
79                 
80         }
81 }

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