1 / 11

Game Extras

Game Extras. Pepper. Arrays Vs ArrayList (for our games). Array 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

maren
Download Presentation

Game Extras

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Game Extras Pepper

  2. Arrays Vs ArrayList (for our games) • Array • 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;

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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);

  10. 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.

  11. 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.

More Related