Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Connect4
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Durand Ulysse
Connect4
Commits
f26770a5
Commit
f26770a5
authored
2 months ago
by
oumaima laklouch
Browse files
Options
Downloads
Patches
Plain Diff
Update server.js
parent
9a2734b4
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
server.js
+46
-90
46 additions, 90 deletions
server.js
with
46 additions
and
90 deletions
server.js
+
46
−
90
View file @
f26770a5
...
@@ -121,65 +121,25 @@ io.on('connection', (socket) => {
...
@@ -121,65 +121,25 @@ io.on('connection', (socket) => {
col
col
});
});
const
winner
=
checkWin
(
currentRoom
.
gameState
,
row
,
col
);
if
(
winner
!==
null
)
{
hallOfFame
[
playerName
]
=
(
hallOfFame
[
playerName
]
||
0
)
+
1
;
fs
.
writeFileSync
(
'
halloffame.json
'
,
JSON
.
stringify
(
hallOfFame
));
}
currentRoom
.
currentPlayer
=
1
-
currentRoom
.
currentPlayer
;
io
.
to
(
currentRoom
.
id
).
emit
(
'
updateGame
'
,
{
board
:
currentRoom
.
gameState
,
currentPlayer
:
currentRoom
.
currentPlayer
,
winner
});
});
socket
.
on
(
'
chatMessage
'
,
(
message
)
=>
{
if
(
!
currentRoom
)
{
return
;
}
io
.
to
(
currentRoom
.
id
).
emit
(
'
message
'
,
{
name
:
playerName
,
message
});
});
socket
.
on
(
'
disconnect
'
,
()
=>
{
leaveRooms
();
});
});
function
findAvailableRow
(
board
,
col
)
{
for
(
let
row
=
5
;
row
>=
0
;
row
--
)
{
if
(
!
board
[
row
][
col
])
return
row
;
}
return
-
1
;
}
function
checkWin
(
board
,
row
,
col
)
{
const
player
=
board
[
row
][
col
];
function
checkWin
(
board
,
row
,
col
)
{
function
checkWin
(
board
,
row
,
col
)
{
const
player
=
board
[
row
][
col
];
const
player
=
board
[
row
][
col
];
if
(
player
===
null
)
return
null
;
if
(
player
===
null
)
return
null
;
const
ROWS
=
board
.
length
;
const
ROWS
=
board
.
length
;
const
COLS
=
board
[
0
].
length
;
const
COLS
=
board
[
0
].
length
;
const
WINNING_LENGTH
=
4
;
const
WINNING_LENGTH
=
4
;
const
directions
=
[
const
directions
=
[
[
0
,
1
],
[
0
,
1
],
// horizontal
[
1
,
0
],
[
1
,
0
],
// vertical
[
1
,
1
],
[
1
,
1
],
// diagonale descendante
[
1
,
-
1
]
[
1
,
-
1
]
// diagonale montante
];
];
for
(
const
[
dx
,
dy
]
of
directions
)
{
for
(
const
[
dx
,
dy
]
of
directions
)
{
let
count
=
1
;
let
count
=
1
;
// Dans une direction
for
(
let
step
=
1
;
step
<
WINNING_LENGTH
;
step
++
)
{
for
(
let
step
=
1
;
step
<
WINNING_LENGTH
;
step
++
)
{
const
newRow
=
row
+
step
*
dx
;
const
newRow
=
row
+
step
*
dx
;
const
newCol
=
col
+
step
*
dy
;
const
newCol
=
col
+
step
*
dy
;
...
@@ -194,7 +154,7 @@ function checkWin(board, row, col) {
...
@@ -194,7 +154,7 @@ function checkWin(board, row, col) {
}
}
}
}
// Dans l’autre direction
for
(
let
step
=
1
;
step
<
WINNING_LENGTH
;
step
++
)
{
for
(
let
step
=
1
;
step
<
WINNING_LENGTH
;
step
++
)
{
const
newRow
=
row
-
step
*
dx
;
const
newRow
=
row
-
step
*
dx
;
const
newCol
=
col
-
step
*
dy
;
const
newCol
=
col
-
step
*
dy
;
...
@@ -209,14 +169,10 @@ function checkWin(board, row, col) {
...
@@ -209,14 +169,10 @@ function checkWin(board, row, col) {
}
}
}
}
if
(
count
>=
WINNING_LENGTH
)
{
if
(
count
>=
WINNING_LENGTH
)
{
return
player
;
return
player
;
}
}
}
return
null
;
}
return
null
;
return
null
;
}
}
\ No newline at end of file
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