Exploring Java Programming Computation and Modeling 2010 Troy O’Neal

Exploring Java Programming
Computation and Modeling
Troy O’Neal
Alan Wu
Athens Drive High School
Enloe High School
2010
Overview
 Three ideas were brainstormed. All programming was
done from scratch in the Java 6.0 language.
 1) Sudoku Solver
Quick and efficient.
 2) Networked Chat Server/Client
Talk with your friends; anywhere, anytime.
 3) Racing Simulator
A virtual gaming world with multiplayer capabilities.
Sudoku Solver: Challenges
 Logic of Sudoku rules must be taken into account
 Algorithm must be designed to run efficiently and quickly
 Techniques to construct and display an appropriate GUI
must be created.
 An appropriate method of user input must be designed
 Sudoku: The objective is to fill a 9×9 grid with digits so that
each column, each row, and each of the nine 3×3 sub-grids
that compose the grid (also called "boxes", "blocks",
"regions", or "sub-squares") contains all of the digits from 1
to 9. The puzzle setter provides a partially completed grid,
which typically has a unique solution. – (Wikipedia)
Sudoku Solver: Solving the puzzle
 When the user clicks “Attempt solve,” a solving
algorithm begins. This is a simplified representation:
START. Set “B”
equal to “null”.
No
Is the puzzle
complete?
Find an empty cell (“A”) .
Set its source to “B”.
Is the puzzle
now
impossible
to complete?
Fill in “A” with a
feasible value
No
NO SOLUTION EXISTS.
EXIT.
Yes
Yes
Yes
No
Yes
Is “A”
null?
SUCCESS. EXIT.
No
Clear “A.” Set “A” equal
to the source of “A”.
Set “B” to “A”
Can “A” be
any other
number?
Example – This is part of a Sudoku
puzzle
3
5
6
7,8
9
2
1,4
Control Class
This is the “main” class which begins the
entire program. It initializes and displays
the various buttons and panels.
SudokuPuzzle Class
This class contains methods and fields to
manage the “logic” of the Sudoku puzzle.
It does none of the display work.
The solve() method
Located within the SudokuPuzzle
Class, does as the name implies.
Various Display and
Control Methods
Update the display and receive
keyboard events from the user.
Display the Sudoku puzzle onscreen.
Display the user’s selected square as a
blue square.
Chat System: Challenges
 Creating a working server with a multithreaded execution




environment that can accept multiple clients for
networking
Creating the server’s protocol and procedures to deal with
events such as account creation and logging in or out.
Using high-level Java networking libraries to make an
application portable across multiple computing
environments
Creating a client system that will successfully be able to
connect to the server’s protocol.
Distributing the client system so that multiple users can
connect to the same server.
Server Console Window
Creating the Server
 To initialize the server, a ServerSocket was created. The socket
was bound to a specified port on the computer, and port 12001
was chosen(for no real reason). It is unlikely that any other
application would use port 12001 and conflict.
 To listen for incoming connections, the ServerSocket’s accept()
method was called. This method waits for a connection to be
established, then proceeds.
 The server maintains an active list of which computers are
currently connected to it, and can manipulate this list at any
time. An example of this functionality is the ability to ban
computers which are causing trouble (e.g. spammers).
 The computer on which the server is running displays everything
which is going through the server’s system, including usernames
and passwords! We can see everything!
Creating the Chat Client
 Since most users use the client-side application instead of
the server-side, the client was designed with practicality,
aesthetics, and intuitiveness in mind.
 When the client initializes, it attempts to establish a
connection to the Summer Ventures Chat Server(previous
slide).
 If a connection is successfully established, other options
become available, such as logging in, logging out, creating
a new account, and chatting.
 These functions must adhere strictly to the server’s
command protocol, or else the server will not recognize
them as valid.
Client
Server
This is the “main” class which begins the
client program. It initializes and displays
the panel and text field. It attempts to
establish a connection to the server
This is the “main” class which begins the
server. It initializes the ServerSocket on
port 12001 and begins listening for
connections.
ListenToServer Class
Connection Class
Represents a “connection” the server has to a
client. For each client connected to the server,
there will be one connection object. The
This class is “Runnable” which means its
thread method can be executed
independently of other system processes
Connections are stored and can be manipulated
Client Methods and Processes
ServerProtocol Class
Contain ActionListeners and other
Listeners for dealing with user input and
sending that input to the server.
Contains lower-level string parsing
methods for determining server
commands and server output.
.
Result?
Lost
productivity!
To further decrease productivity…
Racing Simulator: Challenges
 Creating the behavior of the cars, keeping track of
angle, position, etc.
 “Collision checking,” which, as the name implies,
involves checking for collisions between certain game
objects, and if a collision is found, modifying those
game objects in some way.
 Visual design: constructing an engaging yet functional
racing “world.”
 Interface: Menu screens, character choosers, and other
UI.
GUI (graphical user interface)
Powerup (Collidable Object)
Background Tiles
Active Character
Projectiles (Moving Objects)
Checkpoint
View Class
Control Class
This is a window that represents a
rectangular portion of the visual world.
It contains methods for drawing game
objects on screen. It accesses the Map to
glean information about those objects.
Controls the flow of the entire
program, and contains methods
for drawing menu screens and
other interfaces.
Map Class
Data representation methods and
fields. Everything in the game
world can be accessed.
Creating new
game objects
This can be done from any
existing object, or the Map
itself.
GameObj Class
Stored in the current Map, this generalized
class represents any object in the game,
from cars, to background images. It
contains methods for basic processing and
collision checking.
More Specific Classes
For object behavior more specific to
individual objects, multiple specific classes,
like “Player,” and “TrackTile,”, and methods,
like turnLeft() and explode() are needed.
Destroying game
objects
This can be done from any
existing object, or the Map
itself.
Screenshot Gallery
Summary and Conclusion
 1) Sudoku Solver
Quick and efficient.
 2) Networked Chat Server/Client
Talk with your friends; anywhere, anytime.
 3) Racing Simulator
A virtual gaming world with multiplayer capabilities.
 Creating Java programs from scratch is difficult work, but the
payoffs include:
 Flexibility – Make your program do EXACTLY what you want it to do
 Speed – Optimize your program to perform quickly
 Experience – Gain valuable knowledge of computer programming
principles
Demonstrations Now!
 Thank you for your time!
- Troy O’Neal and Alan Wu
Questions? Comments?
 Thanks again!