25
Game Programming in Java Dr. Jeyakesavan Veerasamy CS faculty, The University of Texas at Dallas Email: [email protected] Website: www.utdallas.edu/~jeyv

Game Programming in Java

  • Upload
    cormac

  • View
    59

  • Download
    2

Embed Size (px)

DESCRIPTION

Game Programming in Java. Dr. Jeyakesavan Veerasamy CS faculty , The University of Texas at Dallas Email: [email protected] Website: www.utdallas.edu/~jeyv. Goals. Understand C++ vs. Java concepts Strengthen recursive coding skills See algorithm efficiency in action - PowerPoint PPT Presentation

Citation preview

Page 1: Game Programming in Java

Game Programming in Java

Dr. Jeyakesavan VeerasamyCS faculty, The University of Texas at Dallas

Email: [email protected] Website: www.utdallas.edu/~jeyv

Page 2: Game Programming in Java

Goals• Understand C++ vs. Java concepts• Strengthen recursive coding skills• See algorithm efficiency in action• Strengthen logical thinking skills• Strengthen coding skills in Java• Event driven programming

This is NOT a “serious” game programming workshop!

Page 3: Game Programming in Java

C++ vs. Java: Similarities

• Both support OOP. Most OOP library contents are similar, however Java continues to grow faster.

• Syntax is very close – Java has strong influence of C/C++. Easy to learn the other language when you know one of these.

Page 4: Game Programming in Java

C++ Compiler & Linker expectations

file1.cpp file2.cpp filen.cpp….

file1.o file2.o filen.o….

Linker

application (executable)

Compiler Compiler Compiler

C++ compiler does notcare about filenames.

Page 5: Game Programming in Java

Java?

…file1.java file2.java file3.java filen.java

Page 6: Game Programming in Java

C++ Concepts: Operator overloading

• Operator overloading: ComplexNumber x, y, z;z = x + y;• In Java?

Page 7: Game Programming in Java

C++ Concepts: Method overloading

• Method overloading – similar to Java– use different argument types to differentiate

Page 8: Game Programming in Java

C++ Concept: Friend

• friend designation - breaks OOP philosophy!– specific functions/methods outside the class can

access private data Why?

Page 9: Game Programming in Java

Concepts: Objects

• Objects can be created as local variables just like any basic data types.

ComplexNumber num1;

Page 10: Game Programming in Java

Arrays

• Basic data types and classes are treated the same way in C++, unlike Java.

ComplexNumber numbers[5];

Page 11: Game Programming in Java

C++ Array version #2

ComplexNumber *numbers;numbers = new ComplexNumber[5];

Page 12: Game Programming in Java

C++ Array version #3

ComplexNumber **numbers;numbers = new ComplexNumber*[5];for( index i = 0 ; i < 5 ; i++)numbers[i] = new ComplexNumber(…);

Page 13: Game Programming in Java

Java

ComplexNumber numbers[];numbers = new ComplexNumber[5];for( index i = 0 ; i < 5 ; i++)numbers[i] = new ComplexNumber(…);

Page 14: Game Programming in Java

C++ Pointers vs Java References

• Explicit in C++: ComplexType *cnump;• Pointer arithmetic• Dynamic memory allocation requires pointers

(just like references in Java)

Page 15: Game Programming in Java

Dynamic memory allocation

• No automatic garbage collection in C++• # of new invocations should match # of delete

invocations.• If a class constructor allocates memory (i.e.

uses “new …”), it needs a destructor method too – it should use “delete …” to release allocated memory.

Page 16: Game Programming in Java

C++ vs. Java: differencesC++ Java

Write once, compile everywhere unique executable for each target

Write once, run anywhere same class files will run above all target-specific JREs.

No strict relationship between class names and filenames. Typically, a header file and implementation file are used for each class.

Strict relationship is enforced, e.g. source code for class PayRoll has to be in PayRoll.java

I/O statements use cin and cout, e.g.cin >> x;cout << y;

I/O input mechanism is bit more complex, since default mechanism reads one byte at a time (System.in). Output is easy, e.g.System.out.println(x);

Pointers, References, and pass by value are supported. No array bound checking.

Primitive data types always passed by value. Objects are passed by reference. Array bounds are always checked.

Explicit memory management. Supports destructors.

Automatic Garbage Collection.

Supports operator overloading. Specifically operator overloading was left out.

Page 17: Game Programming in Java

Example: Guessing game

• Think of a number between 1 and 100 in your mind. Then, the computer (program) will ask you a series of questions and determine that number based on your answers.

Series of interactions:Program: Is it NN?Your response: <, =, or >

Page 18: Game Programming in Java

Sample run #1

Guess a number between 1 and 100 (both inclusive)and get ready to answer a few questions.How about 50 (<,=,>)? <How about 25 (<,=,>)? <How about 12 (<,=,>)? >How about 18 (<,=,>)? >How about 21 (<,=,>)? <How about 19 (<,=,>)? >Your guess is 20.

Page 19: Game Programming in Java

Sample run #2

Guess a number between 1 and 100 (both inclusive)and get ready to answer a few questions.How about 50 (<,=,>)? >How about 75 (<,=,>)? <How about 62 (<,=,>)? >How about 68 (<,=,>)? >How about 71 (<,=,>)? =Your guess is 71.

Page 20: Game Programming in Java

Example: Lotto game

• Generate 6 unique numbers from 1 to 50.• Output should NOT have repeating numbers.

Random ran = new Random();int x = ran.nextInt(50); //returns 0 to 49

Page 21: Game Programming in Java

Example: Hangman game

Page 22: Game Programming in Java

Programming Competitions:usaco.org

Create a new account in usaco.orgUse graduation year 9999, country code INDThen go to your email to find the password.Login first, then go to www.usaco.org/index.php?page=nov12problemslook into Bronze level problems

Page 23: Game Programming in Java

USA Computing Olympiad training site

ace.delos.com/usacogateYou need to create an account (use KITE email address) to gain access.

100s or 1000s of programming problems!A few solution files are @www.utdallas.edu/~jeyv/compete

Page 24: Game Programming in Java

Example: Knapsack problem

int weights[] = { 5, 9, 23, 12, 45, 10, 4, 19, 53 };int target = 100;Goal is to find 2 distinct weights, when added, come to close to target, but do NOT exceed it.For this problem, answer is 45 and 53.Now, let us write generic algorithm so that we can find it for any user-specified target value.

Page 25: Game Programming in Java

Memory game

• GUI setup code• Image files