Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CPSC219Snake-Game
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ethan Stirling
CPSC219Snake-Game
Commits
e62ccaae
Commit
e62ccaae
authored
10 months ago
by
amrib
Browse files
Options
Downloads
Patches
Plain Diff
Added movement, and increming snake length upon consumption of food, and game end on walls and self
parent
bb6b737d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
SnakeGame.java
+144
-70
144 additions, 70 deletions
SnakeGame.java
with
144 additions
and
70 deletions
SnakeGame.java
+
144
−
70
View file @
e62ccaae
// Objective: Implent a snake game in java
import
java.util.Random
;
import
java.util.Scanner
;
public
class
SnakeGame
{
// Constants declared
public
static
final
int
GRID_SIZE_X
=
20
;
// Width of row
public
static
final
int
GRID_SIZE_Y
=
10
;
// Height of column
public
static
final
int
MAX_SNAKE_LENGTH
=
100
;
public
static
final
char
SNAKE_CHAR
=
'S'
;
// Snake on the grid
public
static
final
char
FOOD_CHAR
=
'@'
;
// F
O
od on the grid
public
static
final
char
FOOD_CHAR
=
'@'
;
// F
o
od on the grid
public
static
final
char
EMPTY_CHAR
=
'.'
;
// Empty space of the grid
public
static
final
char
WALL_CHAR
=
'#'
;
// Wall of the grid
// Variables declared
public
static
char
[][]
grid
;
// The game space grid
public
static
int
snakeLength
;
// Current snake length tracker
public
static
int
snake
[][];
// Body of snake
public
static
int
[][]
snake
;
// Body of snake
public
static
int
foodX
,
foodY
;
// Position of food
public
static
char
direction
=
'd'
;
// Current direction of the snake
public
static
boolean
isGameOver
=
false
;
// Game over flag
// Initialize the grid
public
static
void
initializeGame
()
{
grid
=
new
char
[
GRID_SIZE_Y
][
GRID_SIZE_X
];
Random
random
=
new
Random
();
...
...
@@ -27,9 +29,9 @@ public static void initializeGame() {
for
(
int
y
=
0
;
y
<
GRID_SIZE_Y
;
y
++)
{
for
(
int
x
=
0
;
x
<
GRID_SIZE_X
;
x
++)
{
if
(
y
==
0
||
y
==
GRID_SIZE_Y
-
1
||
x
==
0
||
x
==
GRID_SIZE_X
-
1
)
{
grid
[
y
][
x
]
=
WALL_CHAR
;
//
c
reate walls
grid
[
y
][
x
]
=
WALL_CHAR
;
//
C
reate walls
}
else
{
grid
[
y
][
x
]
=
EMPTY_CHAR
;
//
c
reate empty space
grid
[
y
][
x
]
=
EMPTY_CHAR
;
//
C
reate empty space
}
}
}
...
...
@@ -37,41 +39,97 @@ public static void initializeGame() {
generateFood
();
// Generation of Food
// Initialize the snake
int
snakeLength
=
3
;
snakeLength
=
3
;
snake
=
new
int
[
MAX_SNAKE_LENGTH
][
2
];
// Space for snake
int
startX
=
0
,
startY
=
0
;
int
startX
,
startY
;
// Generate a valid random coordinate start
do
{
// Randomized position of snake within valid grid boundaries (not walls)
startX
=
random
.
nextInt
(
GRID_SIZE_X
-
2
)
+
1
;
// Random value between 1 and GRID_SIZE_X-2
startY
=
random
.
nextInt
(
GRID_SIZE_Y
-
2
)
+
1
;
// Random value between 1 and GRID_SIZE_Y-2
}
while
(
grid
[
startY
][
startX
]
!=
EMPTY_CHAR
);
// Loop runs until an empty space is found
// Place the snake on the grid
for
(
int
i
=
0
;
i
<
snakeLength
;
i
++)
{
snake
[
i
][
0
]
=
startY
+
i
;
// Y-coordinate of the snake segment
snake
[
i
][
1
]
=
startX
;
// X-coordinate of the snake segment
if
(
i
==
0
)
{
grid
[
snake
[
i
][
0
]][
snake
[
i
][
1
]]
=
SNAKE_CHAR
;
// Add the snake head
}
snake
[
i
][
0
]
=
startY
;
// Y-coordinate of the snake segment
snake
[
i
][
1
]
=
startX
-
i
;
// X-coordinate of the snake segment
grid
[
snake
[
i
][
0
]][
snake
[
i
][
1
]]
=
SNAKE_CHAR
;
// Place snake on the grid
}
}
// Generate food at a random location
public
static
void
generateFood
()
{
Random
rand
=
new
Random
();
int
foodPosX
=
rand
.
nextInt
(
19
)
+
1
;
// X-coordinate (1-19)
int
foodPosY
=
rand
.
nextInt
(
9
)
+
1
;
// Y-coordinate (1-9)
do
{
foodX
=
rand
.
nextInt
(
GRID_SIZE_X
-
2
)
+
1
;
// X-coordinate (1-18)
foodY
=
rand
.
nextInt
(
GRID_SIZE_Y
-
2
)
+
1
;
// Y-coordinate (1-8)
}
while
(
grid
[
foodY
][
foodX
]
!=
EMPTY_CHAR
);
// Ensure food is not placed on the snake
grid
[
foodY
][
foodX
]
=
FOOD_CHAR
;
// Place food on the grid
}
// Update the snake's position and check for collisions
public
static
void
moveSnake
(
char
direction
)
{
// Calculate new head position
int
newHeadX
=
snake
[
0
][
1
];
int
newHeadY
=
snake
[
0
][
0
];
switch
(
direction
)
{
case
'w'
:
newHeadY
--;
break
;
// Move up
case
's'
:
newHeadY
++;
break
;
// Move down
case
'a'
:
newHeadX
--;
break
;
// Move left
case
'd'
:
newHeadX
++;
break
;
// Move right
}
// Check for collision with walls or self
if
(!
isValidMove
(
newHeadX
,
newHeadY
))
{
isGameOver
=
true
;
return
;
}
if
(
grid
[
foodPosY
][
foodPosX
]
==
EMPTY_CHAR
)
{
// Check if the position is empty
grid
[
foodPosY
][
foodPosX
]
=
FOOD_CHAR
;
// Add food to grid
// Check if the snake has eaten the food
if
(
newHeadX
==
foodX
&&
newHeadY
==
foodY
)
{
// Grow the snake
snakeLength
++;
generateFood
();
// Place new food
}
else
{
generateFood
();
// New coordinates if position in snake
// Clear the tail from the grid
int
tailX
=
snake
[
snakeLength
-
1
][
1
];
int
tailY
=
snake
[
snakeLength
-
1
][
0
];
grid
[
tailY
][
tailX
]
=
EMPTY_CHAR
;
}
// Move the snake's body
for
(
int
i
=
snakeLength
-
1
;
i
>
0
;
i
--)
{
snake
[
i
][
0
]
=
snake
[
i
-
1
][
0
];
snake
[
i
][
1
]
=
snake
[
i
-
1
][
1
];
}
// Update the snake's head position
snake
[
0
][
0
]
=
newHeadY
;
snake
[
0
][
1
]
=
newHeadX
;
// Place the new head on the grid
grid
[
newHeadY
][
newHeadX
]
=
SNAKE_CHAR
;
}
// Check if the move is valid (i.e., within boundaries and not hitting itself)
public
static
boolean
isValidMove
(
int
x
,
int
y
)
{
// Check if the new position is outside the grid or on the wall
if
(
grid
[
y
][
x
]
==
WALL_CHAR
)
{
return
false
;
}
// Check if the new position collides with itself
for
(
int
i
=
0
;
i
<
snakeLength
;
i
++)
{
if
(
snake
[
i
][
0
]
==
y
&&
snake
[
i
][
1
]
==
x
)
{
return
false
;
}
}
return
true
;
}
// Display the grid
public
static
void
displayGrid
()
{
for
(
int
y
=
0
;
y
<
GRID_SIZE_Y
;
y
++)
{
for
(
int
x
=
0
;
x
<
GRID_SIZE_X
;
x
++)
{
...
...
@@ -81,15 +139,31 @@ public static void displayGrid() {
}
}
// Main game loop
public
static
void
main
(
String
[]
args
)
{
initializeGame
();
displayGrid
();
}
Scanner
scanner
=
new
Scanner
(
System
.
in
);
while
(!
isGameOver
)
{
char
direction
=
' '
;
boolean
validInput
=
false
;
while
(!
validInput
)
{
System
.
out
.
println
(
"Enter a direction ('w', 'a', 's', 'd'): "
);
direction
=
scanner
.
next
().
charAt
(
0
);
if
(
direction
==
'w'
||
direction
==
's'
||
direction
==
'a'
||
direction
==
'd'
)
{
validInput
=
true
;
}
else
{
System
.
out
.
println
(
"Invalid input. Please enter 'w', 's', 'a', or 'd'."
);
}
}
moveSnake
(
direction
);
displayGrid
();
}
// End of game loop
scanner
.
close
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment