Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GBS
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
CS375
GBS
Commits
f9a5fcba
Commit
f9a5fcba
authored
Sep 4, 2024
by
Jake Dreher
Browse files
Options
Downloads
Patches
Plain Diff
Rebase working changes into new branch
parent
444f763a
No related branches found
No related tags found
1 merge request
!13
8 deadplayers
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/game/game.js
+75
-69
75 additions, 69 deletions
src/game/game.js
src/models/WebSocketModel.js
+46
-31
46 additions, 31 deletions
src/models/WebSocketModel.js
with
121 additions
and
100 deletions
src/game/game.js
+
75
−
69
View file @
f9a5fcba
...
...
@@ -7,6 +7,14 @@ const cellTypes = {
BORDER
:
5
,
};
const
COLLISIONTYPES
=
{
NONE
:
0
,
FOOD
:
1
,
BANANA
:
2
,
BORDER
:
3
,
SNAKE
:
4
,
};
function
createCell
(
x
,
y
,
type
)
{
return
{
x
:
x
,
...
...
@@ -51,8 +59,39 @@ function addFood(gameBoard) {
return
gameBoard
;
}
function
checkCollisions
(
newY
,
newX
,
gameBoard
)
{
// returns a list of players that have collided,
// and a new gameBoard with the players removed
if
(
newX
<
0
||
newX
>=
gameBoard
[
0
].
length
||
newY
<
0
||
newY
>=
gameBoard
.
length
)
{
console
.
log
(
`Player has collided with the wall!`
);
return
COLLISIONTYPES
.
BORDER
;
}
//Handle collision with food
if
(
gameBoard
[
newY
][
newX
].
type
===
cellTypes
.
FOOD
)
{
console
.
log
(
`Player has eaten the food!`
);
return
COLLISIONTYPES
.
FOOD
;
}
//Handle collision with banana
if
(
gameBoard
[
newY
][
newX
].
type
==
cellTypes
.
BANANA
)
{
console
.
log
(
"
Slip
"
);
return
COLLISIONTYPES
.
BANANA
;
}
//Handle collision with border
if
(
gameBoard
[
newY
][
newX
].
type
==
cellTypes
.
BORDER
)
{
console
.
log
(
"
Void
"
);
return
COLLISIONTYPES
.
BORDER
;
}
return
COLLISIONTYPES
.
NONE
;
}
module
.
exports
=
{
cellTypes
,
COLLISIONTYPES
,
createGameBoard
:
(
width
,
height
)
=>
{
let
gameBoard
=
[];
...
...
@@ -103,6 +142,7 @@ module.exports = {
moveOneStep
:
(
gameBoard
)
=>
{
//Save board state to allow multiple snake movements
let
updatedBoard
=
gameBoard
.
map
((
row
)
=>
row
.
map
((
cell
)
=>
({
...
cell
})));
let
deadPlayers
=
[];
//Loop through board until we find a PLAYERHEAD
for
(
var
i
=
0
;
i
<
gameBoard
.
length
;
i
++
)
{
...
...
@@ -131,57 +171,28 @@ module.exports = {
newX
-=
1
;
}
//Check for collisions with walls
let
collision
=
checkCollisions
(
newY
,
newX
,
gameBoard
);
if
(
newX
<
0
||
newX
>=
gameBoard
[
0
].
length
||
newY
<
0
||
newY
>=
gameBoard
.
length
collision
===
COLLISIONTYPES
.
BANANA
||
collision
===
COLLISIONTYPES
.
FOOD
)
{
console
.
log
(
`Player
${
cell
.
pid
}
has collided with the wall!`
);
//Remove the player from the game (indicating death)
updatedBoard
=
addFood
(
updatedBoard
);
}
if
(
collision
>
COLLISIONTYPES
.
FOOD
)
{
updatedBoard
=
updatedBoard
.
map
((
row
)
=>
row
.
map
((
c
)
=>
c
.
pid
===
cell
.
pid
?
createCell
(
c
.
x
,
c
.
y
,
cellTypes
.
EMPTY
)
:
c
,
),
);
deadPlayers
.
push
(
cell
.
pid
);
continue
;
}
//Handle collision with food
if
(
gameBoard
[
newY
][
newX
].
type
===
cellTypes
.
FOOD
)
{
console
.
log
(
`Player
${
cell
.
pid
}
has eaten the food!`
);
//Add new body segment at the current head's position
if
(
collision
===
COLLISIONTYPES
.
FOOD
)
{
let
newBodySegment
=
createCell
(
oldX
,
oldY
,
cellTypes
.
PLAYERBODY
);
newBodySegment
.
pid
=
cell
.
pid
;
newBodySegment
.
next
=
cell
.
next
;
cell
.
next
=
newBodySegment
;
//Place new food somewhere else
updatedBoard
=
addFood
(
updatedBoard
);
}
//Handle collision with banana
if
(
gameBoard
[
newY
][
newX
].
type
==
cellTypes
.
BANANA
)
{
console
.
log
(
"
Slip
"
);
updatedBoard
[
newY
][
newX
]
=
createCell
(
newX
,
newY
,
cellTypes
.
EMPTY
);
updatedBoard
=
updatedBoard
.
map
((
row
)
=>
row
.
map
((
c
)
=>
c
.
pid
===
cell
.
pid
?
createCell
(
c
.
x
,
c
.
y
,
cellTypes
.
EMPTY
)
:
c
,
),
);
updatedBoard
=
addFood
(
updatedBoard
);
continue
;
}
//Handle collision with border
if
(
gameBoard
[
newY
][
newX
].
type
==
cellTypes
.
BORDER
)
{
console
.
log
(
"
Void
"
);
updatedBoard
=
updatedBoard
.
map
((
row
)
=>
row
.
map
((
c
)
=>
c
.
pid
===
cell
.
pid
?
createCell
(
c
.
x
,
c
.
y
,
cellTypes
.
EMPTY
)
:
c
,
),
);
continue
;
}
//Snake head to new position by updating cell object
...
...
@@ -219,7 +230,7 @@ module.exports = {
}
}
}
return
updatedBoard
;
return
[
updatedBoard
,
deadPlayers
]
;
},
applyBorders
:
(
gameBoard
,
borderCounter
)
=>
{
...
...
@@ -229,7 +240,7 @@ module.exports = {
//console.log(borderCounter);
//Return gameBoard when it is 2x2
if
(
borderCounter
*
2
>=
(
Math
.
min
(
height
,
width
)
)
-
2
)
{
if
(
borderCounter
*
2
>=
Math
.
min
(
height
,
width
)
-
2
)
{
return
gameBoard
;
}
...
...
@@ -273,9 +284,4 @@ module.exports = {
// YANG: Find head with pid = pid
// return [x][y] around head
},
checkCollisions
:
(
gameBoard
)
=>
{
// returns a list of players that have collided,
// and a new gameBoard with the players removed
},
};
This diff is collapsed.
Click to expand it.
src/models/WebSocketModel.js
+
46
−
31
View file @
f9a5fcba
...
...
@@ -2,6 +2,50 @@ const { WebSocketServer } = require("ws");
const
gameModule
=
require
(
"
../game/game.js
"
);
class
WebSocketModel
{
createInterval
(
game
)
{
setInterval
(()
=>
{
if
(
!
this
.
games
[
game
].
started
)
return
;
let
deadPlayers
=
[];
[
this
.
games
[
game
].
gameBoard
,
deadPlayers
]
=
gameModule
.
moveOneStep
(
this
.
games
[
game
].
gameBoard
,
);
if
(
!
this
.
games
[
game
].
elapsedTime
)
{
this
.
games
[
game
].
elapsedTime
=
0
;
}
this
.
games
[
game
].
elapsedTime
++
;
//Every 10 seconds, generate borders
if
(
this
.
games
[
game
].
elapsedTime
%
10
===
0
)
{
this
.
games
[
game
].
gameBoard
=
gameModule
.
applyBorders
(
this
.
games
[
game
].
gameBoard
,
this
.
games
[
game
].
borderCounter
,
);
this
.
games
[
game
].
borderCounter
++
;
}
if
(
deadPlayers
.
length
>
0
)
{
for
(
let
conn
of
this
.
games
[
game
].
players
)
{
conn
.
send
(
JSON
.
stringify
({
type
:
"
deadPlayers
"
,
data
:
deadPlayers
,
}),
);
}
}
for
(
let
conn
of
this
.
games
[
game
].
players
)
{
conn
.
send
(
JSON
.
stringify
({
type
:
"
gameBoard
"
,
data
:
this
.
games
[
game
].
gameBoard
,
// YANG: gameModule.getPlayerView()...
}),
);
}
},
1000
);
}
constructor
()
{
this
.
connections
=
[];
this
.
games
=
{};
...
...
@@ -34,37 +78,7 @@ class WebSocketModel {
this
.
onConnection
();
for
(
let
game
in
this
.
games
)
{
setInterval
(()
=>
{
if
(
!
this
.
games
[
game
].
started
)
return
;
this
.
games
[
game
].
gameBoard
=
gameModule
.
moveOneStep
(
this
.
games
[
game
].
gameBoard
,
);
if
(
!
this
.
games
[
game
].
elapsedTime
)
{
this
.
games
[
game
].
elapsedTime
=
0
;
}
this
.
games
[
game
].
elapsedTime
++
;
//Every 10 seconds, generate borders
if
(
this
.
games
[
game
].
elapsedTime
%
10
===
0
)
{
this
.
games
[
game
].
gameBoard
=
gameModule
.
applyBorders
(
this
.
games
[
game
].
gameBoard
,
this
.
games
[
game
].
borderCounter
);
this
.
games
[
game
].
borderCounter
++
;
}
for
(
let
conn
of
this
.
games
[
game
].
players
)
{
conn
.
send
(
JSON
.
stringify
({
type
:
"
gameBoard
"
,
data
:
this
.
games
[
game
].
gameBoard
,
// YANG: gameModule.getPlayerView()...
}),
);
}
},
1000
);
this
.
createInterval
(
game
);
}
}
...
...
@@ -89,6 +103,7 @@ class WebSocketModel {
readyPlayers
:
0
,
borderCounter
:
0
,
};
this
.
createInterval
(
gameId
);
}
moveHandler
(
connection
,
message
)
{
...
...
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
sign in
to comment