Game Extras Pepper.

Slides:



Advertisements
Similar presentations
Programming with Collections Collections in Java Using Arrays Week 9.
Advertisements

©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Building Java Programs
More on Classes Pepper With help from rs.html.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
AP Java Elevens Lab.
Java Arrays and ArrayLists COMP T1 #5
Chapter 7 – Arrays and Array Lists
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 5 of Computer Science II
Computing with C# and the .NET Framework
BlueJ Tester By Pepper.
Single Dimensional Arrays
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Foundations of Programming: Arrays
ARRAYLIST AND VECTOR.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Java Review: Reference Types
Welcome to CSE 143!.
Array List Pepper.
Peer Instruction 6 Java Arrays.
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
Lists in Python.
ArrayLists.
Organizing Memory in Java
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Chapter 8 Slides from GaddisText
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
int [] scores = new int [10];
<INSERT_WITTY_QUOTE_HERE>
Chapter 10 ArrayList reading: 10.1
ArrayLists.
Welcome to CSE 143!.
Lecture 1: ArrayList reading: 10.1
Can store many of the same kind of data together
Object Oriented Programming in java
Arrays .
Arrays.
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
int [] scores = new int [10];
What's wrong with Easter jokes? They crack you up
Arrays.
Array Lists CSE 1310 – Introduction to Computers and Programming
CSE 373: Data Structures and Algorithms
Functions continued.
Dr. Sampath Jayarathna Cal Poly Pomona
Review: libraries and packages
Java: Variables, Input and Arrays
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
Java String Class String is a class
Arrays and ArrayLists.
Week 7 - Monday CS 121.
Presentation transcript:

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.