Development of a Snake Game in Java with Unit Tests
VerifiedAdded on 2020/06/05
|12
|1657
|1917
Practical Assignment
AI Summary
This document presents a comprehensive Java implementation of the classic Snake game, providing a detailed look at its design and functionality. The assignment includes the Java code for the game engine, which manages snake movement, food consumption, and game over conditions. It also incorporates unit tests using JUnit to ensure the code's correctness and robustness. Furthermore, the assignment delves into the architecture of a custom JUnit test runner, designed to aid students in understanding their code and independently solving problems through hints and solutions. This is achieved by tracking student progress and adaptively revealing tests based on their performance. The document also describes the server component, or report collector, enabling a competitive environment by tracking student scores and providing instructor oversight of the class's progress. Various references on real-time heuristic search and AI are included to provide context to the project.

Snake game in Java
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Introduction
Snake is an older classic computer game. It was first created at the end of
70s. Later on it was brought to PCs. With this game the player controls the
snake. The aim is to eat as many pears as possible. Every time the snake
eats a f its body grows. The particular snake must avoid the wall space and
its own body.
The dimensions of each of the joints of a snake are 10 px. The particular
snake is controlled using the cursor keys. Initially, the particular snake has
three important joints. If the game is finished, the particular "Game Over"
message will be displayed in the middle of the table.
Java Code
package Snake_engin;
import javax.swing.JOptionPane;
import m_snake.MainPanel.snakeBody.Food;
import m_snake.MainPanel.snakeBody.snake;
/**
*
* @author SQLStudent
*/
public class engin {
/** Creates a new instance of engin */
public engin()
{
}
public void Move_mySnake(int newDirection,boolean
food_eaten)
{
Snake is an older classic computer game. It was first created at the end of
70s. Later on it was brought to PCs. With this game the player controls the
snake. The aim is to eat as many pears as possible. Every time the snake
eats a f its body grows. The particular snake must avoid the wall space and
its own body.
The dimensions of each of the joints of a snake are 10 px. The particular
snake is controlled using the cursor keys. Initially, the particular snake has
three important joints. If the game is finished, the particular "Game Over"
message will be displayed in the middle of the table.
Java Code
package Snake_engin;
import javax.swing.JOptionPane;
import m_snake.MainPanel.snakeBody.Food;
import m_snake.MainPanel.snakeBody.snake;
/**
*
* @author SQLStudent
*/
public class engin {
/** Creates a new instance of engin */
public engin()
{
}
public void Move_mySnake(int newDirection,boolean
food_eaten)
{

if(!Game_Over)
{
switch(newDirection)
{
case 1: if(direction!=3)
{
my_snake.put_Head_postion(0,-
step_to_move);
direction=1;
}
else move_as_last(direction);
break;//UP
case 2: if(direction!=4)
{
my_snake.put_Head_postion(step_to_move,0);
direction=2;
}
else move_as_last(direction);
break;//RIGHT
case 3:
if(direction!=1)
{
my_snake.put_Head_postion(0,step_to_move);
direction=3;
}
else move_as_last(direction);
break;//DOWN
case 4:
{
switch(newDirection)
{
case 1: if(direction!=3)
{
my_snake.put_Head_postion(0,-
step_to_move);
direction=1;
}
else move_as_last(direction);
break;//UP
case 2: if(direction!=4)
{
my_snake.put_Head_postion(step_to_move,0);
direction=2;
}
else move_as_last(direction);
break;//RIGHT
case 3:
if(direction!=1)
{
my_snake.put_Head_postion(0,step_to_move);
direction=3;
}
else move_as_last(direction);
break;//DOWN
case 4:
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

if(direction!=2)
{
my_snake.put_Head_postion(-step_to_move,0);
direction=4;
}
else move_as_last(direction);
break;//LEFT
}
if(food_eaten)
{
my_snake.Increase_lenght();// IF the Snake ate new
Food
}
my_snake.Move_snake();
if((my_snake.getHeadX()==snakeFood.get_int_X())&&(my_snake.getHea
dY()== snakeFood.get_int_Y()))
{
snakeFood.getFood();
Score+=20;
my_snake.Increase_lenght();
while(check_food_place())
{
snakeFood.getFood();
}
}
{
my_snake.put_Head_postion(-step_to_move,0);
direction=4;
}
else move_as_last(direction);
break;//LEFT
}
if(food_eaten)
{
my_snake.Increase_lenght();// IF the Snake ate new
Food
}
my_snake.Move_snake();
if((my_snake.getHeadX()==snakeFood.get_int_X())&&(my_snake.getHea
dY()== snakeFood.get_int_Y()))
{
snakeFood.getFood();
Score+=20;
my_snake.Increase_lenght();
while(check_food_place())
{
snakeFood.getFood();
}
}
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Game_Over=my_snake.check_game_over();
}
if(Game_Over)
{
JOptionPane.showConfirmDialog(null,"Score =
"+Integer.toString(Score),"Your
Score",JOptionPane.DEFAULT_OPTION);
// Thread.yield();
}
}
public void move_as_last(int diirec)
{
switch(direction)
{
case 1:
my_snake.put_Head_postion(0,-
step_to_move);
break;//UP
case 2:
my_snake.put_Head_postion(step_to_move,0);
break;//RIGHT
case 3:
}
if(Game_Over)
{
JOptionPane.showConfirmDialog(null,"Score =
"+Integer.toString(Score),"Your
Score",JOptionPane.DEFAULT_OPTION);
// Thread.yield();
}
}
public void move_as_last(int diirec)
{
switch(direction)
{
case 1:
my_snake.put_Head_postion(0,-
step_to_move);
break;//UP
case 2:
my_snake.put_Head_postion(step_to_move,0);
break;//RIGHT
case 3:

my_snake.put_Head_postion(0,step_to_move);
break;//DOWN
case 4:
my_snake.put_Head_postion(-step_to_move,0);
break;//LEFT
}
}
public int[] return_Snake_postionsX()
{
return my_snake.get_All_postionsX();
}
public int[] return_Snake_postionsY()
{
return my_snake.get_All_postionsY();
}
public int get_Oval_size()
{
return my_snake.getOval_Size();
}
public int get_my_snake_lenght()
{
return my_snake.get_lenght();
}
public void set_step_length()
break;//DOWN
case 4:
my_snake.put_Head_postion(-step_to_move,0);
break;//LEFT
}
}
public int[] return_Snake_postionsX()
{
return my_snake.get_All_postionsX();
}
public int[] return_Snake_postionsY()
{
return my_snake.get_All_postionsY();
}
public int get_Oval_size()
{
return my_snake.getOval_Size();
}
public int get_my_snake_lenght()
{
return my_snake.get_lenght();
}
public void set_step_length()
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

{
step_to_move=5;
}
public int get_Food_X()
{
return (int)snakeFood.getX();
}
public int get_Food_Y()
{
return (int)snakeFood.getY();
}
public boolean check_food_place()
{
int xfood=(int)snakeFood.getX();
int yfood=(int)snakeFood.getY();
if(xfood>260||yfood>260)return true;
else
return my_snake.compare_with_foodPostion(xfood,yfood);
}
public boolean get_Game_over()
{
return Game_Over;
}
private int direction=2;
private boolean Game_Over;
private boolean Moving_vertical=false;
private snake my_snake=new snake();
private final Food snakeFood=new Food();
private int step_to_move=20;
private int Score;
}
step_to_move=5;
}
public int get_Food_X()
{
return (int)snakeFood.getX();
}
public int get_Food_Y()
{
return (int)snakeFood.getY();
}
public boolean check_food_place()
{
int xfood=(int)snakeFood.getX();
int yfood=(int)snakeFood.getY();
if(xfood>260||yfood>260)return true;
else
return my_snake.compare_with_foodPostion(xfood,yfood);
}
public boolean get_Game_over()
{
return Game_Over;
}
private int direction=2;
private boolean Game_Over;
private boolean Moving_vertical=false;
private snake my_snake=new snake();
private final Food snakeFood=new Food();
private int step_to_move=20;
private int Score;
}
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Unit Test
The particular Snake Tester aims to help the students in their
understanding by enabling them to check their own code as they create it,
to check it works the required outcomes. In addition to this, the particular
students can also learn more independently by using the hints and options
that are attached to each job, and only shown to the pupil when one or more
of the assessments fail. Initially the Snake Tester was the selection of JUnit
tests created concurrently with the worksheets. The particular tests had
been packaged like a Coffee ‘ or Store. jar’ file, and put into the skeleton
code task as a dependency, making use of comparative coping with so that
the learners could copy and paste within new versions of the Specialist
program in future classes. The students could operate the tests by simply
managing a file called ‘SnakeGameTestRunner after that. java’ that was
included in the bones code also. Nonetheless, it had been observed in the
teaching periods that the students initially discovered the test results
confusing, because executing the selection gives 20 test results, without
sign of which task the given test was related to. This led to the development
of the custom JUnit test runner architecture that will tracks the progress of
every consumer, and adaptively unveils tests consistently with the
improvement of the user. It was at first considered whether to create a
check runner architecture from scratch, in order that it could be built from
the ground as much as support the project, such as the progress tracking
The particular Snake Tester aims to help the students in their
understanding by enabling them to check their own code as they create it,
to check it works the required outcomes. In addition to this, the particular
students can also learn more independently by using the hints and options
that are attached to each job, and only shown to the pupil when one or more
of the assessments fail. Initially the Snake Tester was the selection of JUnit
tests created concurrently with the worksheets. The particular tests had
been packaged like a Coffee ‘ or Store. jar’ file, and put into the skeleton
code task as a dependency, making use of comparative coping with so that
the learners could copy and paste within new versions of the Specialist
program in future classes. The students could operate the tests by simply
managing a file called ‘SnakeGameTestRunner after that. java’ that was
included in the bones code also. Nonetheless, it had been observed in the
teaching periods that the students initially discovered the test results
confusing, because executing the selection gives 20 test results, without
sign of which task the given test was related to. This led to the development
of the custom JUnit test runner architecture that will tracks the progress of
every consumer, and adaptively unveils tests consistently with the
improvement of the user. It was at first considered whether to create a
check runner architecture from scratch, in order that it could be built from
the ground as much as support the project, such as the progress tracking

and ideas and solutions aspect. Even so, after exploring the extensibility of
the existing JUnit structures, it was decided that this might be used instead,
meaning that a lot of the existing test runner construction could be reused,
and period spent instead on the improvement tracking and hints plus
solutions aspects that are particular to this project.
Progressively more independent when developing their particular software,
the particular further element of the Snake Tester has been implemented to
allow students in order to easily see which duties are associated with the
failed testing. A windowpane is then proven to the user that includes control
keys to access the particular hint for just about any unsuccessful tasks. If
that one test has failed through minimum three times, the particular button
to exhibit the full solution for this task is enabled. It was to ensure the
learning students produced the real effort to accomplish each task before
having the ability to view the solution.
The particular report collector is the particular server component for the
Snake Tester, allowing the machine to run on one machine upon the local
network, plus acknowledge connections from customers (other computers
on the network) which send the created report. In order to introduce an
component associated with competition, each report (identified uniquely by
the existing JUnit structures, it was decided that this might be used instead,
meaning that a lot of the existing test runner construction could be reused,
and period spent instead on the improvement tracking and hints plus
solutions aspects that are particular to this project.
Progressively more independent when developing their particular software,
the particular further element of the Snake Tester has been implemented to
allow students in order to easily see which duties are associated with the
failed testing. A windowpane is then proven to the user that includes control
keys to access the particular hint for just about any unsuccessful tasks. If
that one test has failed through minimum three times, the particular button
to exhibit the full solution for this task is enabled. It was to ensure the
learning students produced the real effort to accomplish each task before
having the ability to view the solution.
The particular report collector is the particular server component for the
Snake Tester, allowing the machine to run on one machine upon the local
network, plus acknowledge connections from customers (other computers
on the network) which send the created report. In order to introduce an
component associated with competition, each report (identified uniquely by
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

the username from the pupil who is logged in) also has a score, depending
upon how a lot of tests have handed, and whether or not the solution or
even hint was accessed, enabling the university students to get into friendly
competition to see who also can get the best score. Simply by collecting and
displaying the final results, this also allows the trainer to be able to track
the improvement of all the students in the course, and can compare scores
among college students, and see which workouts are causing the most
problems so that they know to focus training more on that area.
In early stages during the growth of this element, issues with concurrency
arose, since the Hint UI was shown on a separate thread that was getting
disposed when the check run ended. However, it was fixed by using a ‘flag’
adjustable with the volatile modifier to do something as a guard, blocking
performance until the guard is transformed. The volatile modifier helps to
ensure that changes to variables are usually visible across threads. The
particular flag was set to fake and the program enters a good infinite loop
initially, till the Hint UI window will be closed. When the UI can be closed, a
listener mounted on the window updates the significance of the flag
variable, permitting set up to proceed.
upon how a lot of tests have handed, and whether or not the solution or
even hint was accessed, enabling the university students to get into friendly
competition to see who also can get the best score. Simply by collecting and
displaying the final results, this also allows the trainer to be able to track
the improvement of all the students in the course, and can compare scores
among college students, and see which workouts are causing the most
problems so that they know to focus training more on that area.
In early stages during the growth of this element, issues with concurrency
arose, since the Hint UI was shown on a separate thread that was getting
disposed when the check run ended. However, it was fixed by using a ‘flag’
adjustable with the volatile modifier to do something as a guard, blocking
performance until the guard is transformed. The volatile modifier helps to
ensure that changes to variables are usually visible across threads. The
particular flag was set to fake and the program enters a good infinite loop
initially, till the Hint UI window will be closed. When the UI can be closed, a
listener mounted on the window updates the significance of the flag
variable, permitting set up to proceed.
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

To obtain a level of confidence the fact that computer software components
of the task were working as designed, the suite of unit testing were also
developed together with the project. In total, you can find tests that cover a
variety of functionality within the project forty-one, checking out areas such
as the results selection (from the custom JUnit framework), the particular
protocol responsible for handling communications between the client and
machine, plus testing that files are usually written correctly. Additionally, it
includes some nonfunctional testing, for example to test that all check
methods (i. e individuals with an ‘@Test’ annotation), also provide an
‘@Task’ annotation, to make sure that all test methods come with an
associated task
of the task were working as designed, the suite of unit testing were also
developed together with the project. In total, you can find tests that cover a
variety of functionality within the project forty-one, checking out areas such
as the results selection (from the custom JUnit framework), the particular
protocol responsible for handling communications between the client and
machine, plus testing that files are usually written correctly. Additionally, it
includes some nonfunctional testing, for example to test that all check
methods (i. e individuals with an ‘@Test’ annotation), also provide an
‘@Task’ annotation, to make sure that all test methods come with an
associated task

References
Koenig, Sven and Xiaoxun Sun. “Comparing Real-Time and Incremental Heuristic Search for Real-Time
Situated Agents”. Autonomous Agents and Multi-Agent Systems. Volume 18 Issue 3, 2009.
Korf, Richard E. “Real-Time Heuristic Search”. Artificial Intelligence. 1990:190-211 Likhachev, Maxim, et
al. “Anytime Search in Dynamic Graphs”. Artificial Intelligence. Volume 172 Issue 14, 2008.
Russell, Stuart and Peter Norvig. Artificial Intelligence: A Modern Approach. New Jersey: Pearson
Education, 2010.
Koenig, Sven and Xiaoxun Sun. “Comparing Real-Time and Incremental Heuristic Search for Real-Time
Situated Agents”. Autonomous Agents and Multi-Agent Systems. Volume 18 Issue 3, 2009.
Korf, Richard E. “Real-Time Heuristic Search”. Artificial Intelligence. 1990:190-211 Likhachev, Maxim, et
al. “Anytime Search in Dynamic Graphs”. Artificial Intelligence. Volume 172 Issue 14, 2008.
Russell, Stuart and Peter Norvig. Artificial Intelligence: A Modern Approach. New Jersey: Pearson
Education, 2010.
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 12
Related Documents

Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.