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
03eb576e
Commit
03eb576e
authored
10 months ago
by
zm343
Browse files
Options
Downloads
Patches
Plain Diff
game.js update(addFood & moveOneStep)
parent
d48d16b6
No related branches found
No related tags found
1 merge request
!8
game.js update (addFood & moveOneStep)
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/game/game.js
+133
-46
133 additions, 46 deletions
src/game/game.js
with
133 additions
and
46 deletions
src/game/game.js
+
133
−
46
View file @
03eb576e
module
.
exports
=
{
cellTypes
:
{
const
cellTypes
=
{
EMPTY
:
0
,
PLAYERHEAD
:
1
,
PLAYERBODY
:
2
,
FOOD
:
3
,
},
};
function
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
);
}
function
addFood
(
gameBoard
)
{
//Find all the empty spaces on the grid
let
emptySpaces
=
[];
for
(
let
row
of
gameBoard
)
{
for
(
let
cell
of
row
)
{
if
(
cell
.
type
===
cellTypes
.
EMPTY
)
{
emptySpaces
.
push
(
cell
);
}
}
}
//Create food cells randomly
if
(
emptySpaces
.
length
>
0
)
{
let
randomCell
=
emptySpaces
[
getRandomInt
(
emptySpaces
.
length
)];
randomCell
.
type
=
cellTypes
.
FOOD
;
}
return
gameBoard
;
}
module
.
exports
=
{
cellTypes
,
createGameBoard
:
(
width
,
height
)
=>
{
let
gameBoard
=
[];
for
(
var
i
=
0
;
i
<
height
;
i
++
)
{
row
=
[];
let
row
=
[];
for
(
var
j
=
0
;
j
<
width
;
j
++
)
{
row
.
push
(
createCell
(
j
,
i
,
cellTypes
.
EMPTY
));
}
...
...
@@ -18,58 +55,108 @@ module.exports = {
for
(
var
i
=
0
;
i
<
10
;
i
++
)
{
gameBoard
=
addFood
(
gameBoard
);
}
return
gameBoard
;
},
addFood
:
(
gameBoard
)
=>
{
// select a random cell that is empty
// set the cell to FOOD
//console.log(gameBoard);
return
gameBoard
;
},
addPlayer
:
(
gameBoard
,
pid
)
=>
{
let
placedCell
=
false
;
while
(
!
placedCell
)
{
let
x
=
getRandomInt
(
gameBoard
[
0
].
length
);
let
y
=
getRandomInt
(
gameBoard
.
length
);
if
(
gameBoard
[
y
][
x
].
type
===
cellTypes
.
EMPTY
)
{
gameBoard
[
y
][
x
].
type
=
cellTypes
.
PLAYERHEAD
;
gameBoard
[
y
][
x
].
pid
=
pid
;
gameBoard
[
y
][
x
].
direction
=
0
;
let
y2
=
y
-
1
;
if
(
y2
>=
0
&&
gameBoard
[
y2
][
x
].
type
===
cellTypes
.
EMPTY
)
{
gameBoard
[
y2
][
x
].
type
=
cellTypes
.
PLAYERBODY
;
gameBoard
[
y2
][
x
].
pid
=
pid
;
gameBoard
[
y2
][
x
].
direction
=
0
;
gameBoard
[
y
][
x
].
next
=
gameBoard
[
y2
][
x
];
}
placedCell
=
true
;
}
}
//console.log(gameBoard);
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
},
for
(
var
i
=
0
;
i
<
gameBoard
.
length
;
i
++
)
{
for
(
var
j
=
0
;
j
<
gameBoard
[
i
].
length
;
j
++
)
{
let
cell
=
gameBoard
[
i
][
j
];
if
(
cell
.
type
===
cellTypes
.
PLAYERHEAD
)
{
let
oldX
=
cell
.
x
;
let
oldY
=
cell
.
y
;
let
newX
=
oldX
;
let
newY
=
oldY
;
checkColissions
:
(
gameBoard
)
=>
{
// returns a list of players that have collided,
// and a new gameBoard with the players removed
},
};
//New position based on direction
if
(
cell
.
direction
===
0
)
{
// Up
newY
-=
1
;
}
else
if
(
cell
.
direction
===
1
)
{
// Right
newX
+=
1
;
}
else
if
(
cell
.
direction
===
2
)
{
// Down
newY
+=
1
;
}
else
if
(
cell
.
direction
===
3
)
{
// Left
newX
-=
1
;
}
let
createCell
=
(
x
,
y
,
type
)
=>
{
return
{
x
:
x
,
y
:
y
,
type
:
type
,
pid
:
0
,
direction
:
0
,
next
:
null
,
//Continue if within bounds
if
(
newX
>=
0
&&
newX
<
gameBoard
[
0
].
length
&&
newY
>=
0
&&
newY
<
gameBoard
.
length
)
{
//Snake head to new position by updating cell object
gameBoard
[
newY
][
newX
]
=
{
...
cell
,
x
:
newX
,
y
:
newY
,
};
//Remove previous head position
gameBoard
[
oldY
][
oldX
].
type
=
cellTypes
.
EMPTY
;
gameBoard
[
oldY
][
oldX
].
pid
=
0
;
gameBoard
[
oldY
][
oldX
].
direction
=
0
;
gameBoard
[
oldY
][
oldX
].
next
=
null
;
//Snake body to previous locations
let
currentBody
=
gameBoard
[
newY
][
newX
].
next
;
while
(
currentBody
)
{
let
bodyOldX
=
currentBody
.
x
;
let
bodyOldY
=
currentBody
.
y
;
currentBody
.
x
=
oldX
;
currentBody
.
y
=
oldY
;
//Update cell body position
gameBoard
[
oldY
][
oldX
]
=
{
...
currentBody
,
type
:
cellTypes
.
PLAYERBODY
,
};
oldX
=
bodyOldX
;
oldY
=
bodyOldY
;
currentBody
=
currentBody
.
next
;
}
function
getRandomInt
(
max
)
{
return
Math
.
floor
(
Math
.
random
()
*
max
);
//Remove previous body position
gameBoard
[
oldY
][
oldX
].
type
=
cellTypes
.
EMPTY
;
gameBoard
[
oldY
][
oldX
].
pid
=
0
;
gameBoard
[
oldY
][
oldX
].
direction
=
0
;
gameBoard
[
oldY
][
oldX
].
next
=
null
;
//console.log(gameBoard);
return
gameBoard
;
}
}
}
}
return
gameBoard
;
},
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.
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