Snake Game

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

“SNAKE GAME ”

A
Minor Project

On

Submitted In Partial Fulfilment For The Award Of

Bachelor of Technology In

Computer Science &

Engineering Submitted To

RAJIV GANDHI PROUDYOGIKI VISHWAVIDYALAYA BHOPAL (M.P.)

Submitted By

Narendra Vishwakarma – 0614CS191014

Sandeep Brar – 0614CS191019

Under the Guidance of

Prof. SUJAL AGARWAL (Ass. Prof)

PT. DEVPRABHAKAR SHASTRI COLLAGE OF TECHNOLOGY CHHATARPUR (M.P.)


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
PT. DEVPRABHAKAR SHASTRI COLLAGE OF TECHNOLOGY
CHHATARPUR (M.P.)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

CERTIFICATE

This is to Certify That Major Project Project Entitled “SNAKE GAME ” Submitted to Rajiv
Gandhi Proudyogiki Vishwavidyalaya, Bhopal (M.P.) by “Narendra Vishwakarma Sandeep Brar
” for Partial Fulfilment for the Award of the Degree of the Bachelor of Technology in Computer
Science & Engineering.

Narendra Vishwakarma – 0614CS191014

Sandeep Brar – 0614CS191019

Sandeep Sharma
HOD (CSE)
PDSCT, CHHATARPUR(M.P.)
ACKNOWLEDGEMENT

This is one of the best moments of my B.Tech. program to publicly acknowledgment those who
have contributed in many different ways to make my success a part of their own. The completion
of the Minor Project depends upon the co-operation, coordination and combined effects of
several resources of knowledge energy.

I heartily thanks to Prof SUJAL AGARWAL ,who have supported in miner project, faculties
of Department of Computer Science & Engineering, for accepting me to work under their
Valuable Guidance, Closely Supervised this work over the past few months and offering many
innovative ideas and helpful suggestions, which led to the successful completion of this
dissertation work.

I am especially thankful DR. J. SONI , Director, PDSCT, Chhatarpur for his kind co-
operation and rendering me all possible facilities.

I am thankful to Mr. Sandeep Sharma and all staff members of the CSE department and my friends
for their timely help co-operation and suggestion during my project work. Lastly but not the least, i
must express thanks to my family, without their moral support it was impossible for me to complete this
minor project work.

Narendra Vishwakarma – 0614CS191014

Sandeep Brar – 0614CS191019

Sandeep Sharma
HOD (CSE)
PDSCT, CHHATARPUR(M.P.)
Table of Contents

1 Acknowledgment.......1
2 Abstract......................2
3 Objectives.......................................................................................................................3
4 Programming Environment............................................................................................4
5 Methodology..................................................................................................................5
5.1 iSnake Client Application.........................................................................................6
5.1.1 Client Encoder/Decoder....................................................................................6
5.1.2 Client Network Interface...................................................................................6
5.1.3 Input Handler.....................................................................................................6
5.1.4 Game Field Matrix.............................................................................................7
5.1.5 Game Field Canvas.............................................................................................7
5.1.6 User Interface Components..............................................................................7
5.1.7 Game Controller................................................................................................7
5.2 iSnake Game Server................................................................................................8
5.2.1 Server Encoder/Decoder...................................................................................8
5.2.2 Server Network Interface..................................................................................8
5.2.3 Virtual Game Field.............................................................................................9
5.2.4 Player Information Manager.............................................................................9
5.2.5 Random Number Pool.......................................................................................9
5.2.6 Status Server......................................................................................................9
5.2.7 Server Core......................................................................................................10
5.3 Intelligent Autonomous Opponent Snakes...........................................................10
5.4 iSnake Game Server Manager (GSM) @ SF.net....................................................13
6 Project Management...................................................................................................14
7 Documentation............................................................................................................16
8 Limitations....................................................................................................................16
9 Future Enhancements..................................................................................................17
10 Conclusion..................................................................................................................17
11 References..................................................................................................................18
1 Acknowledgment
We offer gratitude to project supervisor Asst. Prof. Jayaram Timsina (Deputy HOD, Dept. Of
Electronics & Computer Engineering, IOE Pulchowk Campus) for guiding us throughout the
project.

We would like to thank all those forum members at JavaGaming.org [http://www.javagaming.org]


and Sun Java Forum [http://forum.java.sun.com] who replied to our endless queries without any
complain.

We acknowledge the creators of Quantum Random Bit Generator Service [QRBG –


http://random.irb.hr], which provides our game server with high quality random numbers.

1
2 Abstract
This project aims to bring the fun and simplicity of snake game with some new features. It will include
computer controlled intelligent opponents whose aim will be to challenge the human players. It will
also have the multiplayer feature that will allow more than one players to play the game over a
network.

This project explores a new dimension in the traditional snake game to make it more interesting and
challenging. The simplicity of this game makes it an ideal candidate for a minor project as we can
focus on advanced topics like multiplayer functionality and implementation of computer controlled
intelligent opponents.
3 Objectives
This game aims to change the way people think of traditional snake game. It will offer the

experience of commercial multilayer games to the player retaining the simplicity of traditional

snake game.

The major objectives of this project are:

● Create a snake game that will have all the functionality of traditional snake games.

● Introduce multilayer functionality in the game that will allow several players to play a
game simultaneously. It should be able to give the experience of a real time
multiplayer game to the players.

● Introduce computer controlled intelligent opponent (unique feature of this game) to


make the game more challenging and interesting. The movement and action of these
intelligent opponents will be controlled by computer whose aim will be to eat the food
before human players capture it.

3
4 Programming Environment
We used several open source tools to develop this project:

● Netbeans 5.5 IDE

All the developers of iSnake team used Netbeans IDE for the development of this project.

● Inkscape 0.45 and Gimp 2.2

These graphics development tools were extensively used for the development of User Interface
components. The illustrations presented in this report have also been prepared using these open
source tools.

● Gnuplot

The data obtained during profiling of two path finding algorithms viz. Blackmamba and Viper
was plotted using gnuplot.

● OpenOffice Writer 2.2

All the project documents and this report were prepared using OpenOffice Writer 2.2.

● Wireshark Network Traffic Analyzer 0.99.6

During the development of iSnake Game Server Manager (GSM) @ SF.net, Wireshark tool
was used to analyze the network traffic.
5 Methodology
iSnake is a multiplayer version of traditional snake game (popular among cell phone gamers) with
computer controlled intelligent opponents that challenges the human players.

The player who hosts the game server is called “local player”. The iSnake Client Application and
Game Server run in separate execution domains. The iSnake Client App. for local player communicates
with the game server through network layer, just like other remote players, as shown in illustration 1.

Illustration 1: I Snake block diagram

The complete iSnake application is divided into four major components:

● iSnake Client Application

● iSnake Game Server

● Intelligent Autonomous Opponent Snakes

● iSnake Game Server Manager (GSM) @ SF.net

5
5.1 iSnake Client Application
iSnake client application refers to the application used to play snake game. A player joins an
already existing iSnake game server using this application.

Illustration 2: Block diagram of iSnake Client Application


The main components of iSnake Client application are:

5.1.1 Client Encoder/Decoder


This module performs the encoding and decoding of messages leaving/arriving the client
network interface module using the protocol standard described in ANNEX D: Inter Snake
Communication Protocol.

5.1.2 Client Network Interface


It provides an interface to the Game Controller module for communication with the game
server hosted at local/remote computer. It is responsible for triggering of appropriate methods of
Game Controller when message from Game Server is received.

5.1.3 Input Handler


It manages the task of sampling key strokes from local player and forwarding it to Game
Controller when requested. It maintains a queue of size 2 so that quick keystrokes are not
lost. It is active only when the game is in running mode.
5.1.4 Game Field Matrix
Game Controller maintains the complete state of the game using game field matrix. It
is a 2D array of size 58x58 (equal to the game field dimensions). Each game field object has a
unique identifier in the game field. Game Controller updates the cells of this matrix in each
cycle to register the changes that occur in the game.

5.1.5 Game Field Canvas


It represents the game field as seen by the player. Game Controller analyzes the game
field matrix in each cycle and updates the game field canvas to represent the state of game in that
game cycle. Double Buffering [using java.awt.Canvas.createBufferStrategy()] has been
implemented to avoid flickering of game field. Each block in the game field has dimension 10x10
pixels.

The update of game field canvas occurs in the way similar to the refreshing technique of
a cathode ray monitor. The game field matrix and game field canvas are updated in separate
thread. The update of game field canvas starts by scanning each column of the 1st row in the
game field matrix, then 2nd row and so on upto the 58th row. The game field is refreshed twice
during each game cycle (two refresh cycle for game field canvas for each game cycle). This is
done make the movements in the game field smoother.

5.1.6 User Interface Components


This module includes all the components, except game field, visible to the player. The
look and feel of default swing components have been overridden to give the feel of a game to the
players. MIT OCW's course1 “[6.831] User Interface Design and Implementation” was very
helpful during design of most of the user interface components of this game.

5.1.7 Game Controller


It is the most important component of the iSnake client application. It coordinates the
working of all the other modules in the application and handles all the messages

1 See References [Website]

7
received from the game server. Game Controller maintains the game cycle when the game is
running. This game cycle is synchronized with the game cycle of the game server. All the updates
to game field matrix are done during this cycle time. After expiry of each game cycle the
game field is repainted to reflect the changes in the game field.

5.2 iSnake Game Server


iSnake game server handles the multiplayer feature of this game and allows multiple iSnake
client applications to play the game hosted by that particular game server.

Illustration 3: Block diagram of iSnake Game Server


The main components of iSnake Game Server are:

5.2.1 Server Encoder/Decoder


This module performs the encoding and decoding of messages leaving/arriving the server
network interface module using the protocol standard described in ANNEX D: Inter Snake
Communication Protocol.

5.2.2 Server Network Interface


It provides an interface to the Server Core module for communication with the remote/local
players of the game being hosted. It is responsible for triggering of appropriate methods of
Server Core when message from remote/local players is received.
5.2.3 Virtual Game Field
Game server maintains the state of the game using a 2D array of size 58x58 (similar to
that used by Game Controller – refer to 5.1.4).

It is maintained by the game server to check whether the food has been eaten and whether
any player has collide with the wall. It maintains the head coordinate (not the coordinates for tails)
of each player. The head coordinate of players is moved in each game cycle and checked for the
presence of wall/food in that coordinate position. Server Core generates corresponding event
(collide or food eaten) and all the active players are informed about the event in the same cycle.

5.2.4 Player Information Manager


It manages the information about the players involved in the game. Player information like
name, location, score, snake's starting position, snake color, etc are maintained. A new entry is
added whenever a new player joins the game. Similarly, when the player leaves the game, it is
removed.

5.2.5 Random Number Pool


Game server maintains a buffer of random numbers obtained from Quantum Random Bit
Generator service [QRBG http://random.irb.hr ]. This gives the game server access to true random
number. These random data is used to generate the position of food and the starting coordinate of
each player's snake.

If the random number service is unreachable pseudo random numbers are generated
using java.util.Random class provided by Java.

5.2.6 Status Server


Status server maintains all the information required to reply the current status of the
game server. The service of status server is utilized by iSnake Game Server Manager(iSnake
GSM)2 hosted at http://isnake.sf.net. The response of status server is a well formed XML
document that is parsed by iSnake – GSM to display information about the game server in the
website.

2 Refer to “5.4 iSnake Game Server Manager (GSM) @ SF.net”

9
A typical response of Status Server is given below:

<?xml version=”1.0” encoding=”UTF8”?>


<iSnake>
<GameServerData>
<GameServerAddress>124.41.228.219</GameServerAddress>
<GameServerPort>9669</GameServerPort>
<NoOfPlayersOnline>5</GameServerLocation>
<GameServerStatus>Waiting</GameServerStatus>
</GameServerData>
</iSnake>

5.2.7 Server Core


It is the most important component of the iSnake Game Server. It coordinates the working
of all the other modules in the application and handles all the messages received from the remote
players.

When the game is in "Waiting" mode, Server Core provides the facility of chat messaging
to the game players. During this state new players can join the game.

When all the players have sent signal to start the game, the Server Core changes
state to “Running” mode. If a new player tries to join the game, it receives a “NAK” response.
In this mode, Server Core maintains a game cycle time during which it receives the movement
coordinates (in terms of deltaX and deltaY) from the players. If a player does not send any
packet during this cycle time, server considers the movement coordinate sent in last cycle for the
current game cycle. After expiry of cycle time, game server broadcasts a packet containing
movement coordinates of each player to all the players active in the game. The game server also
checks if any player has eaten the food or if any players have collided to the wall in each cycle.

5.3 Intelligent Autonomous Opponent Snakes


These are computer controlled snakes, in the game, whose aim is to challenge the human
players. We have two implementations of path finding algorithms to create intelligent
autonomous opponent snakes. These algorithms return the shortest possible
path from given source (S) and target (T) coordinate pair considering the obstacles (if any)
present in the game field. The code name3 for these two implementations are:

● Blackmamba (refer to ANNEX A)

● Viper (refer to ANNEX B)

A detailed paper describing the algorithm used by these two implementation is present in
ANNEX A and ANNEX B. The module implementing these two path finding algorithm easily fits
into the existing design of iSnake Client Application as shown in Illustration 4

Illustration 4: Module implementing two path finding algorithms replaces the Input Handler
module of standard iSnake Client application.

To know which of the two implementations perform better, we profiled them using a simple
JUnit test and the results were plotted using gnuplot:

3 Named after two popular species of venomous snakes

11
Illustration 5: Turn around time for Illustration 6: Length of path returned
two path finding algorithms by the two path finding algorithms

Turn around time plot

The time elapsed between the instant of supplying the (source,target) coordinate pair
(S,T) to the algorithm and the instant when it returns a path for supplied (S,T) pair is called turn
around time. From the plot of illustration 5, it is clear that the turn around time for Viper is
always smaller as compared to Blackmamba.

Path Length plot

The length of path (computed by counting the number of game field coordinates in the
path) returned by the two path finding algorithms is depicted by the plot of illustration 6. It is
clear from the plot that Viper implementation results in smaller paths (and hence efficient) as
compared to Blackmamba implementation.

NOTE: The value of turn around time and path length for the coordinate pairs 5,7,8 have
negative values for Blackmamba. This suggests that the algorithm was not able to compute a path
for given (S,T) pair in given timeout period (250 ms for this test).
5.4 iSnake Game Server Manager (GSM) @ SF.net
iSnake Game Server Manager (iSnake – GSM) hosted at http://isnake.sf.net is used to manage all
the information about iSnake game servers being hosted over the Internet.

Illustration 7: Block diagram of iSnake Game Server Manager at http://isnake.sf.net


The iSnake – GSM has been developed using PHP. Java Web Start technology has been used to
deploy iSnake application at our website which automatically downloads/installs iSnake
application and its library dependencies. This provides the gamers with the facility of “one
click launch” of the iSnake application. It also make the distribution of updates of the iSnake
application to the end users very convenient.

The iSnake application deployed at our website has been digitally signed by the iSnake
team to address the security issues related to launch of Internet applications.

13
6 Project Management
The first thing we did before starting the work on iSnake was to register a project at Sourceforge4.
Apart from hosting services, it provided us several code and project management services.

Illustration 8: Commit statistics for iSnake subversion repository at


http://isnake.svn.sourceforge.net

Illustration 8 shows the svn commit statistics for the isnake code repository at sourceforge. We
collaborated on project documents (including prototype designs, project plan, TODO list, etc) using
WIKI (http://isnake.wiki.sourceforge.net).

JUnit tests were developed to independently test some of the modules before integration. The
integration of modules developed by the three developers was performed in three phases:

● Phase 1 Integration (Sep. 28, 2007) chat functionality of the game was tested
successfully

4 http://www.sourceforge.net
● Phase 2 Integration (Oct. 06, 2007) successful testing of basic version of
multiplayer snake game

● Phase 3 Integration (Feb 17 26, 2008) – integration of all the modules for iSnake
0.1 Beta release.

7 Documentation
Documentation of every task being done in the project was a priority for all the team members. Almost
every portion of the source code contains full code documentation conforming to Javadoc standards.

Illustration 9: Screen shot of a snippet of source code showing method comments conforming to
javadoc standards.
There exists two path finding algorithms that implements intelligent opponent in the game viz
Blackmamba and Viper. These two algorithms have been fully documented with illustrations5.

The protocol devised for communication between game server and clients has been documented in
ANNEXD.

8 Limitations
The limitations of present implementation of iSnake are:

● The present implementation of iSnake can only be played in LAN. Due to large
latency time and bandwidth limitation, it cannot be played over the Internet.

● Path finding algorithms (Blackmamba and Viper) implemented in this game have
their own computation limitations which has been describe in ANNEX A,B.
5 Refer to ANNEX – A,B
● Full stress test of the application has not been done yet. Hence, the response of game
server in unpredictable situations cannot be handled properly.

● iSnake's Game Server Manager (iSnake – GSM) located at http://isnake.sf.net is


still in its early development phase. There are some unresolved security issues.

9 Future Enhancements
● Port iSnake to cell phone platform and One Laptop Per Child – OLPC (which uses
Sugar Desktop environment). The presence of several connectivity options(Bluetooth,
WIFI, GPRS, CDMA) in cell phones makes it a very attractive platform for a
multiplayer game like iSnake. Local WIFI network formed by kids using OLPC laptops
can be used as a platform for iSnake's deployment.

● As iSnake game server communicates with remote playing using a well defined and
very simple protocol (Refer to ANNEX D), iSnake clients programmed in other
programming platform like Flash, Python, etc can be developed.

10. Coding / snapshots

Gaming instraction :-

Play game :-
11.Conclusion
We were successful in creating a multiplayer version of traditional snake game. The computer
controlled intelligent opponents have been successfully tested in the game is a unique feature of
iSnake.

We learned several project management techniques used by professionals to develop large scale
project. The experience of working in team and integration of modules developed independently, with
just requirement specifications, is a very important achievement for the iSnake team.
12. References
Books

1. The Java Programming Language, by Ken Arnold, James Gosling, David

Holmes ADDISON-WESLEY, 2000, ISBN 81-7808-148-2

2. Java Threads, Second Edition, by Scott Oaks, Henry

Wong O'REILLY, 1997, ISBN 81-7366-057-3

3. Core Java – Volume I/II, Seventh Edition, by Cay S. Horstmann, Gary

Cornell PEARSON EDUCATION, 2005, ISBN 81-297-0932-5

4. Introduction to Algorithms, Second Edition, by Thomas H. Cormen, Charles


E. Leiserson, Ronald L. Rivest, Clifford Stein

MIT PRESS, 2001, ISBN 81-203-2141-3

5. Head First Java, Second Edition, by Kathy Sierra, Bert

Bates O'REILLY, May 2003, ISBN 81-7366-602-4

Websites

1. Netbeans Community Docs

http://wiki.netbeans.org/wiki/view/CommunityDocs

2. The Inkscape Tutorials Blog

http://inkscapetutorials.wordpress.com/

3. Smashing Magazine – On line magazine for designers

http://www.smashingmagazine.com/

4. The Java Tutorials

http://java.sun.com/docs/books/tutorial/

5. Timing Framework

https://timingframework.dev.java.net/
6. JavaGaming.org's Forum

http://www.javagaming.org/forums/index.php

7. MIT Open Course Ware - 6.831 User Interface Design and Implementation

http://ocw.mit.edu/OcwWeb/ElectricalEngineeringandComputerScience/6831Fall
2004/CourseHome/index.htm

8. Amit's A* Page – Tutorial on A* Path finding algorithms

http://theory.stanford.edu/~amitp/GameProgramming/

9. Arianne - a multiplayer online games framework

http://arianne.sourceforge.net/

10. JavaDesktop Community Blog

http://community.java.net/javadesktop/

11. Java Forum

http://forum.java.sun.com

12. Blog of Romain Guy – coauthor of Filthy Rich Clients

http://www.curiouscreature.org/

13. Filthy Rich Clients - Examples

http://www.filthyrichclients.com/

14. A Distributed Multiplayer Game Server System

http://warriors.eecs.umich.edu/games/papers/quakefinal.pdf

15. The Java Developers Almanac 1.4

http://www.exampledepot.com/

16. Apache MINA Sample programs and tutorials

http://mina.apache.org/

17. PHP Documentation for XML

library http://www.php.net/
18. Quantum Random Bit Generator Service (QRBG)

http://random.irb.hr/
ANNEX – D2
D 1

You might also like