Game Extras Pepper
Arrays Vs ArrayList (for our games) Fixed size: Cannot change size once it is created Has length property, but no methods on the array as a whole Can be used by Arrays class methods ArrayList Variable size: can add or remove items to change array length An Object: Has helpful methods available such as add and remove Can be used by Collections class methods (shuffle) Cannot hold primitives (int, char, double) Import java.util.ArrayList;
Review Array syntax Card[] c : creates variable to hold an array of Cards new Card[5] : constructs object - 5 empty array spots for Cards c[0] = new Card() : constructs a card object and places it into c[0] c.length : array c's size
ArrayList Syntax (for game) ArrayList<Card> cAL: creates variable to hold an array list of Cards new ArrayList<Card>() : constructs object - unlimited empty Array list of Cards cAL.add(new Card()): constructs a card object and places it at the end of the Array List of cards cAL.set(1,newCard()) : constructs a card object and insert it inside the existing Array, pushing every following item after it cAL.size() : array list c's size cAL.get(0) : gets the array list's first item See http://max.cs.kzoo.edu/AP/Workshops/Java/ArraysArrayLists.html
Converting between the two ArrayList to Array: Ask the ArrayList to give you its elements as an array: cAL.toArray(new Card[0] ) returns an array Array to ArrayList: Ask the static Arrays class to give you an array's elements as an ArrayList Arrayd.asList(c) returns an arrayList of the array's generic object type
Some Helpful ArrayList Methods Collections.shuffle(cAL) : This method takes in an array list and shuffles it directly. cAL.remove(0): This asks the arrayList to remove its first item. The array will be a smaller size
Shuffling an array Many methods available One choice (though not perfect) Loop through your array getting a random number within the size of your array int index = rnd.nextInt(a.length); Swap the cell of your counter with the cell of the random number int hold = a[index] // save the card you will swap a[index] = a[counter] // your loop counter value a[counter] = hold // finish the swap
Player Class Use and Testing Unit testing - Use tester to test methods before using in your game See the value? Code toString Method for easy printing You can pass a player into any static method to reduce copied lines of code for each player
Tester Right click class to add test case Remove extends junit.framework.TestCase Set up Players to use for your test in testSomething, or as Class variables: Player t1 = new Player("John"); t1.place= 5; Player t2 = new Player("Ann"); Add test cases by calling those players: t.checkExpect(t1.movePlayer(3), 8); t.checkExpect(t2.movePlayer(3), 4);
Test using toString System.out.println(player1) prints garbage with special characters Create a toString method for your player to return a String describing the player nicely public String toString(){return "name is " + this.name + " and his location is " + this.loc;} Make a nice string to display all your variables Then you can easily add many System.out.println(player1) statements to help you test.
Pass a player to a Turn method Extract everything that happens for one player into one method: Find the place one player's turn starts and the place it ends - cut that code Replace it with a takeTurn() Create a new takeTurn(Player p) method below main and paste the cut code into this method Adjust the turn to use the player named p. It will directly effect the player created in main.