ASIA PACIFIC INSTITUTE OF INFORMATION TECHNOLOGY Sudantha Sulochana Gunawardena (CB003198) Module Code : AAP004-3-2-JP INTAKE CODE : DF08A1ICT ASSIGNMENT TITLE : Java Programming SUBMITTED TO : Mr.Dilhan Thilakarathne HAND IN DATE : 10 th August 2009 HAND OUT DATE : 13 th October 2009
65
Embed
Java Programming - Creating a Snake and Ladders Game and Object oriented concepts
Creating a Snake and Ladders Game using Java AWT and Object oriented concepts
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
ASIA PACIFIC INSTITUTE OF INFORMATION
TECHNOLOGY
Sudantha Sulochana Gunawardena
(CB003198)
Module Code : AAP004-3-2-JP
INTAKE CODE : DF08A1ICT
ASSIGNMENT TITLE : Java Programming
SUBMITTED TO : Mr.Dilhan Thilakarathne
HAND IN DATE : 10th August 2009
HAND OUT DATE : 13th October 2009
Java Programming (AAP004-3-2-JP)
Acknowledgment
This was a great opportunity for me to put into practice which we learned in few lectures
and my own development knowledge on Java Programming.
First and foremost I would our sincere graduate to My Java Programming lecturer
Mr.Dilhan Thilakarathne for his great guidance in this project.
And special thanks goes to Mr.Balachandran Gnanasekaraiyer who thought me the
concepts on Java, object oriented methods and Harvard referencing which help on this
project as well.
Finally, I would like to thank each and every person who helped me in numerous ways
That two players are playing the game simultaneously and sharing the same
computer and input/output devices.
That player knows the aim of the game be the first player to reach the end by
moving across the board from First Square to the hundred square.
That player knows that when the disk lands in a square with base of the ladder
that he have to move up and when he lands on a square with a snake that he
have to return to a less valued square .
That when two players are playing that the both players have equal winning
probability.
That player has basic knowledge how to deal with computer based graphical
user interfaces.
That user who runs this application have installed Java run time (JRE)
environment on his running machine.
4
Java Programming (AAP004-3-2-JP)
Object Oriented Concepts
Classes
The following table shows the classes which used in this application and included
methods of them.
Class Name Purpose Included Methods/Other
Player Class To store and retrieve player information
and statistics.
getPlayerName()
setPlayerName()
setPlayerScore()
getPlayerScore()
Main Class Provide the main interface to the users
locate the snake board
Main() method
Changeplayer()
Specialvalues()
Control Class Store all the game controls ,Image
paths and button names
LoadMain()
Dice Class Generate the Dice random number and
provide it
RandomNumber()
Score Card
Class
Provide the interface for the score card Scorecard()
Welcome Class Provide interface to get the player
information
Welcome()
Music Class Store and provide sound facilities setMusic()
5
Java Programming (AAP004-3-2-JP)
Also common methods like paint method, Action performed method will be included in
most of the classes.
Objects
Most of these classes are used in the application are used as instances of them.
Especially the instances ‘control class’ will use in commonly because most of the
application control methods are located in that class.
There will be two instances of player class will be used as player 1 and player 2 for the
two players. Because both players have most common characteristics.
Packages
In this application mainly two packages used .Classes are related to game play is falls
under com.game package and ‘control classes’ will be falls under com.game.option
package.
Getters/Setters
Getters/Setters methods are specially used in player class to retrieve and change the player information, Location and score.
6
Java Programming (AAP004-3-2-JP)
Abstraction
According to Sun Microsystems (2009) abstraction is selecting needed characteristics
from a class.
For Example Dice may have endless characteristics like color, width, weight from them
for this application development only need few characteristics like current number, so
it’s necessary only to implement methods for needed characteristics.
Encapsulation
According to Kioskea.net(2009) it describes that encapsulation is restrict access to
certain data or in simple words hiding information .
In this application development encapsulation is used it certain matters like the current
value of a player. Using the access modifier ‘private’ the current value of a player is
hidden and given access only by its class.
Also in a another case the player count should be accessed by several classes in the
application so using access modifier ‘public ’ it given the access to the other classes .
7
Java Programming (AAP004-3-2-JP)
Inheritance
Meyer, Bertrand (1997). Object-Oriented Software Construction, describes that
Inheritance in object oriented programming is a relationship with a abstract classes.
Most of the classes which are visible for uses are inheritances from java.awt abstract
class for example following diagram shows the inheritance of the main class in the
application.
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
com.game.Main
Polymorphism
Sierra, Kathy; Bert Bates (2005). Head First Java describes that polymorphism is
means the ability to take more than one form. That means same methods can used in
different behaviors. In this application for example the paint method is used
polymorphism.
8
Java Programming (AAP004-3-2-JP)
Logical Design
To move the Disk though the snake board a 2-Dimentional array is used. There are 100
values in the 2-Dimentional array which is same as the snake board. Nested Loop can
travel trough the array and find the necessary location. According to java API Document
an object of a class ‘Dimension’ can store an X axis and the Y axis of a given array or
an value with having the X axis and the Y axis of the location we can relocate the disk at
a given location.
β = constant distance between frame and snake board, n/m= width, height of a box ,
a=Location retrieved by 2-Dimentional array.
The equation to get the x position location is:
x=β+n∗a
The equation to get the y position location is:
y=β+m∗a
9
Snake board
Box
0,0 0,10
n
β
m
Frame
Java Programming (AAP004-3-2-JP)
When a location with a snake or a ladder occurs there should be a proper way of
landing on the directed location. Logic for this when a number of a location with a snake
or ladder is occurred you have to redirect to the directed location .Using the same
equation above this can be achieved inside a conditional clauses like IF,ELSE or a
SWITCH .
For demonstration purposes if we think that the number occurred and it should be
redirected to the new location which have the array dimension of 4, 7
The equation to get the new X position location is:
x=β+n∗4
The equation to get the new Y position location is:
y=β+n∗7
The shifting of the players can be done though a simple Boolean logic and player
location and score can be easily retrieve using simple counting methods.
10
Java Programming (AAP004-3-2-JP)
Pseudo code
Generate the Random Number Between 1-6
Function RandomNumber
PASS IN : NOTHING
START
declare Random Number
set Integer MAX to 5
RandomNumber = GenerateRandam Number(MAX)
PASS OUT : Random Number
RETURN with Random Number
Endfunction
11
Java Programming (AAP004-3-2-JP)
Move the disk on the board
Function TravelLocation
PASS IN : Random Number
declare board(100,100) as integer
declare Die1
declare Die2
Set moveCount to 1
FOR each row on the board
FOR each column on the board
IF(board(row,colum) is occupied THEN
CALL swiftPlayers
INCREMENT moveCount
END IF
END FOR
END FOR
PASS OUT : Row,Colum
12
Java Programming (AAP004-3-2-JP)
Endfunction
Swift between the two players
Function SwiftPlayers
PASS IN : Boolean
declare PlayerFlag Boolean to true
IF PlayerFlag value is True THEN
Set PlayerFlag Value to False
ELSE IF
PlayerFlag value is False THEN
Set PlayerFlag Value to True
ELSE
END IF
PASS OUT : Boolean
Endfunction
13
Java Programming (AAP004-3-2-JP)
Travel though snake and ladders
IF currentValue==snakevalue THEN
Move the Disk to NewLocaton
ElSE IF currentValue==Laddervalue THEN
Move the Disk to NewLocation
ELSE
Disk ==NextLocation
END IF
Set/Get Player Name
Function SetPlayer
PASS IN : NOTHING
Declare PlayerName as a String value
PlayerName = Display ” Enter Player Name”
PASS OUT : Player Name
Endfuction
14
Java Programming (AAP004-3-2-JP)
Function GetPlayer
PASS IN : Nothing
Display ” Enter Player Name is : ”
PASS OUT Player Name
Endfuction
Winning Message
Function Winnermsg
PASS IN : UserLocation
IF UserLocation>=96 AND newDieValue >=4 THEN
Display “You Won ! “
END IF
PASS OUT : NOTHING
Endfunction
15
Java Programming (AAP004-3-2-JP)
Set Player Rank
IF userWinningcount ==0 THEN
Display “Noob”
ELSE IF userWinningcount <3 THEN
Display ”Beginner”
ELSE IF userWinningcount >3 THEN
Disply “Expert”
16
Java Programming (AAP004-3-2-JP)
Screen Design
Frame Name
Frame Welcome
Frame Title Frame Welcome
Image
Figure 1 – Welcome Screen
Description The above figure shows the welcome frame of the game which provides user registration .Players can enter their names and continue to the game.
Features User Registration Users can enter their information and register.
Get the Play instructions Users can get play instructions if they need.
17
Java Programming (AAP004-3-2-JP)
Quit the Game Can quit the application.
Frame Name
Frame main
Frame Title Snake and Ladders
Image
Figure 2 – Main Frame
Description As the figure above this frame the Snake board and the menu will be located, When click the Die button the die will be thrown and the number will be generated. New Game button will be load a new game. And the two disks for players will be located and as the number generation it will be moved in the snake board.
Features Snake Board The Snake board will be located on this Frame
Die Give Which the number to play.
18
Java Programming (AAP004-3-2-JP)
Disks The disks will be moved in the snake board.
Frame Name
Frame Instructions
Frame Title How to Play
Image
Figure 3 – How to Play Frame
Description This Frame will provide the game instructions to the player.
Features Play Instructions Provide Player instructions.
19
Java Programming (AAP004-3-2-JP)
Frame Name
Frame Score Card
Frame Title Score Card
Image
Figure 4 - Score card
Description Provide users their Play statistics for them
Features Save Player Statistics Statistics will be saved for each users.
Player Rank Show the player with his rank ‘Beginner’ or ‘Expert’.
20
Java Programming (AAP004-3-2-JP)
Test Plans
ID Test Description Expected Outcome
Test Data/Test Actions Actual outcome
Welcome Class
1 Test of User 1 Text Field
Accepted Player 1
2 Test of User 2 Text Field
Accepted Player 1
Test of Text Field Validations
Message Box Blank
3 Test of OK Button Show Main Frame
Mouse Click
4 Test of How to use Button
Show how to use Frame
Mouse Click
5 Quit button in welcome Frame
Quit the Application
Mouse Click
6 Test of Quit Button
Quit the Application
Mouse Click
7 Maximize Button in Frame
NOT Maximize the Frame
Mouse Click
Table 2 – Test Plan 1
21
Java Programming (AAP004-3-2-JP)
ID Test Description Expected Outcome
Test Data/Test Actions Actual outcome
Main Class
1 Test menu item ‘New Game’
Reset the Game board
Mouse Click
2 Test menu item ‘Instructions’
Show Instructions
Frame
Mouse Click
3 Test menu item ‘Exit’
Quit the Application
Mouse Click
4 Test menu item ‘Score Card’
Show the Score Card
Mouse Click
5 Test menu item ‘About’
Show the about Frame
Mouse Click
6 Test of Roll Die Button Move the Disks
Mouse Click
7 Test of New Game Button
Reset the Game board
Mouse Click
Table 2 – Test Plan 2
22
Java Programming (AAP004-3-2-JP)
Special Features
Audio Stream
Background music and event sounds are provided using java audio streams. All the
methods that provide sound to the application are located in the ‘music’ class and
instances of this class are created to get the sounds.
According to K.Sierra, B.Bates(2005) in their book Head first java describes that in this
audio stream method the file is taken from a input stream so File not found exception,
I/O exception should be handled.
For example the setMusic method in Music class:
public void setMusic(boolean status)
throws java.io.FileNotFoundException,
java.io.IOException
The above figure shows that the setMusic method is handling both File not found
exception and I/O Exception
Java Swing
To provide message boxes to users some components in java Swing library is
used .Specially JFrame component and JOptonPane component is used to deliver
message box dialogs.
23
Java Programming (AAP004-3-2-JP)
Figure 4 - Swing JOptionPane
Limitations
Two players have to play the game simultaneously and can NOT play only by a
single user.
The two players who have registered first have to play the game and player
names cannot be change in middle of a game.
Players have to share the same hardware resources and can NOT play using
networked environments.
When player finishes a game he has to press ‘new game’ button to start a new
game without resetting the board automatically.
24
Java Programming (AAP004-3-2-JP)
References
Sun Microsystems, Inc. (2009). Java 2 SDK SE Developer Documentation. Unite
States: Sun Microsystems, Inc.
Massachusetts Institute of Technology. Java Audio. In: Using
GALAXY/Communicator with Java. Unite States: Computer Science and Artificial
Intelligence Laboratory .
Marc Loy, Robert Eckstein (2003). Java Swing. 2nd ed. Unite States: O'Really
Media,Inc.
Kathy Sierra, Bert Bates (2005). Head first Java. 2nd ed. Unite States: O'Really
Media,Inc.
25
Java Programming (AAP004-3-2-JP)
Conclusion
Designing this game with object orientated concepts and java programming language is
a quiet challenge. After several weeks of coding and research I could come up with
efferent solutions which match with the assignment requirements.