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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CS375
GBS
Commits
da413344
Commit
da413344
authored
10 months ago
by
Jake Dreher
Browse files
Options
Downloads
Patches
Plain Diff
Write initial api for game code
parent
296fffd6
No related branches found
No related tags found
1 merge request
!8
game.js update (addFood & moveOneStep)
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
app.js
+9
-0
9 additions, 0 deletions
app.js
src/game/game.js
+66
-0
66 additions, 0 deletions
src/game/game.js
with
75 additions
and
0 deletions
app.js
+
9
−
0
View file @
da413344
const
express
=
require
(
"
express
"
);
const
{
WebSocketServer
}
=
require
(
"
ws
"
);
const
gameModule
=
require
(
"
./src/game/game.js
"
);
const
webserver
=
express
()
.
use
((
_
,
res
)
=>
res
.
sendFile
(
"
/index.html
"
,
{
root
:
`
${
__dirname
}
/public`
}))
...
...
@@ -11,6 +12,14 @@ const sockserver = new WebSocketServer({ port: 3001 });
// This will eventually map connections to lobby or game instances
let
connections
=
[];
let
games
=
{};
// TO BE REWRITTEN
games
[
"
game1
"
]
=
{
gameBoard
:
gameModule
.
createGameBoard
(
100
,
100
),
players
:
[],
};
let
gameBoard
=
[
[
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
],
[
0
,
1
,
0
,
0
,
0
,
0
,
0
,
0
,
0
],
...
...
This diff is collapsed.
Click to expand it.
src/game/game.js
0 → 100644
+
66
−
0
View file @
da413344
module
.
exports
=
{
cellTypes
:
{
EMPTY
:
0
,
PLAYERHEAD
:
1
,
PLAYERBODY
:
2
,
FOOD
:
3
,
},
createGameBoard
:
(
width
,
height
)
=>
{
let
gameBoard
=
[];
for
(
var
i
=
0
;
i
<
height
;
i
++
)
{
row
=
[];
for
(
var
j
=
0
;
j
<
width
;
j
++
)
{
row
.
push
(
createCell
(
j
,
i
,
cellTypes
.
EMPTY
));
}
gameBoard
.
push
(
row
);
}
return
gameBoard
;
},
addPlayer
:
(
gameBoard
,
pid
)
=>
{
let
x
=
getRandomInt
(
gameBoard
[
0
].
length
);
let
y
=
getRandomInt
(
gameBoard
.
length
);
gameBoard
[
y
][
x
].
type
=
cellTypes
.
PLAYERHEAD
;
gameBoard
[
y
][
x
].
pid
=
pid
;
gameBoard
[
y
][
x
].
direction
=
0
;
let
y2
=
y
-
1
;
gameBoard
[
y2
][
x
].
type
=
cellTypes
.
PLAYERBODY
;
gameBoard
[
y2
][
x
].
pid
=
pid
;
gameBoard
[
y2
][
x
].
direction
=
0
;
gameBoard
[
y
][
x
].
next
=
gameBoard
[
y2
][
x
];
return
gameBoard
;
},
moveOneStep
:
(
gameBoard
)
=>
{
// Loop through board until we find a PLAYERHEAD
// Move the head in the direction it is facing
// if the head has a next, move the next to the head's old position
// if the next has a next, move the next's next to the next's old position
// and so on
// return the new gameBoard
},
checkColissions
:
(
gameBoard
)
=>
{
// returns a list of players that have collided,
// and a new gameBoard with the players removed
},
};
let
createCell
=
(
x
,
y
,
type
)
=>
{
return
{
x
:
x
,
y
:
y
,
type
:
type
,
pid
:
0
,
direction
:
0
,
next
:
null
,
};
};
function
getRandomInt
(
max
)
{
return
Math
.
floor
(
Math
.
random
()
*
max
);
}
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