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
5f454e23
Commit
5f454e23
authored
1 month ago
by
Ulysse Durand
Browse files
Options
Downloads
Patches
Plain Diff
repaired
parent
9a2734b4
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
public/room.html
+36
-74
36 additions, 74 deletions
public/room.html
server.js
+2
-0
2 additions, 0 deletions
server.js
with
38 additions
and
74 deletions
public/room.html
+
36
−
74
View file @
5f454e23
...
@@ -27,10 +27,44 @@
...
@@ -27,10 +27,44 @@
const
playerName
=
params
.
get
(
'
name
'
);
const
playerName
=
params
.
get
(
'
name
'
);
const
roomId
=
window
.
location
.
pathname
.
split
(
'
/
'
).
pop
();
const
roomId
=
window
.
location
.
pathname
.
split
(
'
/
'
).
pop
();
const
COLS
=
7
;
const
ROWS
=
6
;
const
CELL_SIZE
=
100
;
const
RADIUS
=
CELL_SIZE
/
2
-
10
;
// Initialize game board
// Initialize game board
function
drawBoard
(
board
)
{
function
drawBoard
(
board
)
{
// Draw Connect4 grid based on board state
// Draw Connect4 grid based on board state
ctx
.
clearRect
(
0
,
0
,
canvas
.
width
,
canvas
.
height
);
for
(
let
row
=
0
;
row
<
ROWS
;
row
++
)
{
for
(
let
col
=
0
;
col
<
COLS
;
col
++
)
{
let
x
=
col
*
CELL_SIZE
+
CELL_SIZE
/
2
;
let
y
=
row
*
CELL_SIZE
+
CELL_SIZE
/
2
;
// Trou
ctx
.
beginPath
();
ctx
.
arc
(
x
,
y
,
RADIUS
,
0
,
Math
.
PI
*
2
);
ctx
.
fillStyle
=
'
white
'
;
ctx
.
fill
();
// Pion
if
(
board
[
row
][
col
]
!==
null
)
{
ctx
.
beginPath
();
ctx
.
arc
(
x
,
y
,
RADIUS
,
0
,
Math
.
PI
*
2
);
ctx
.
fillStyle
=
board
[
row
][
col
]
===
0
?
'
red
'
:
'
yellow
'
;
ctx
.
fill
();
}
}
}
}
}
canvas
.
addEventListener
(
'
click
'
,
(
e
)
=>
{
if
(
winner
!==
null
)
return
;
const
rect
=
canvas
.
getBoundingClientRect
();
const
x
=
e
.
clientX
-
rect
.
left
;
const
col
=
Math
.
floor
(
x
/
CELL_SIZE
);
socket
.
emit
(
'
move
'
,
col
);
});
socket
.
on
(
'
connect
'
,
()
=>
{
socket
.
on
(
'
connect
'
,
()
=>
{
socket
.
emit
(
'
joinRoom
'
,
{
socket
.
emit
(
'
joinRoom
'
,
{
...
@@ -74,79 +108,7 @@
...
@@ -74,79 +108,7 @@
messages
.
innerHTML
+=
`<div><strong>
${
name
}
:</strong>
${
message
}
</div>`
;
messages
.
innerHTML
+=
`<div><strong>
${
name
}
:</strong>
${
message
}
</div>`
;
});
});
//canvas game
<
script
src
=
"
/socket.io/socket.io.js
"
>
</script>
<script>
const
socket
=
io
();
const
urlParams
=
new
URLSearchParams
(
window
.
location
.
search
);
const
roomId
=
urlParams
.
get
(
'
room
'
)
||
'
room0
'
;
// par ex. /room.html?room=room2
const
name
=
prompt
(
"
Ton nom ?
"
)
||
"
Player
"
;
const
COLS
=
7
;
const
ROWS
=
6
;
const
CELL_SIZE
=
100
;
const
RADIUS
=
CELL_SIZE
/
2
-
10
;
let
currentPlayer
=
0
;
let
gameState
=
Array
(
ROWS
).
fill
().
map
(()
=>
Array
(
COLS
).
fill
(
null
));
let
winner
=
null
;
const
canvas
=
document
.
getElementById
(
'
gameCanvas
'
);
const
ctx
=
canvas
.
getContext
(
'
2d
'
);
socket
.
emit
(
'
joinRoom
'
,
{
name
,
roomId
});
socket
.
on
(
'
startGame
'
,
(
data
)
=>
{
gameState
=
data
.
board
;
currentPlayer
=
data
.
currentPlayer
;
drawBoard
();
});
socket
.
on
(
'
updateGame
'
,
(
data
)
=>
{
gameState
=
data
.
board
;
currentPlayer
=
data
.
currentPlayer
;
winner
=
data
.
winner
;
drawBoard
();
if
(
winner
!==
null
)
{
alert
(
`Le joueur
${
winner
}
a gagné !`
);
}
});
canvas
.
addEventListener
(
'
click
'
,
(
e
)
=>
{
if
(
winner
!==
null
)
return
;
const
rect
=
canvas
.
getBoundingClientRect
();
const
x
=
e
.
clientX
-
rect
.
left
;
const
col
=
Math
.
floor
(
x
/
CELL_SIZE
);
socket
.
emit
(
'
move
'
,
col
);
});
function
drawBoard
()
{
ctx
.
clearRect
(
0
,
0
,
canvas
.
width
,
canvas
.
height
);
for
(
let
row
=
0
;
row
<
ROWS
;
row
++
)
{
for
(
let
col
=
0
;
col
<
COLS
;
col
++
)
{
let
x
=
col
*
CELL_SIZE
+
CELL_SIZE
/
2
;
let
y
=
row
*
CELL_SIZE
+
CELL_SIZE
/
2
;
// Trou
ctx
.
beginPath
();
ctx
.
arc
(
x
,
y
,
RADIUS
,
0
,
Math
.
PI
*
2
);
ctx
.
fillStyle
=
'
white
'
;
ctx
.
fill
();
// Pion
if
(
gameState
[
row
][
col
]
!==
null
)
{
ctx
.
beginPath
();
ctx
.
arc
(
x
,
y
,
RADIUS
,
0
,
Math
.
PI
*
2
);
ctx
.
fillStyle
=
gameState
[
row
][
col
]
===
0
?
'
red
'
:
'
yellow
'
;
ctx
.
fill
();
}
}
}
}
drawBoard
();
// initial
drawBoard
();
// initial
</script>
</script>
</script>
</body>
</body>
</html>
</html>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
server.js
+
2
−
0
View file @
5f454e23
...
@@ -93,6 +93,7 @@ io.on('connection', (socket) => {
...
@@ -93,6 +93,7 @@ io.on('connection', (socket) => {
id
:
socket
.
id
,
id
:
socket
.
id
,
name
name
});
});
socket
.
join
(
roomId
);
io
.
emit
(
'
updateRooms
'
,
rooms
);
io
.
emit
(
'
updateRooms
'
,
rooms
);
io
.
to
(
roomId
).
emit
(
'
updatePlayers
'
,
room
.
players
);
io
.
to
(
roomId
).
emit
(
'
updatePlayers
'
,
room
.
players
);
...
@@ -128,6 +129,7 @@ io.on('connection', (socket) => {
...
@@ -128,6 +129,7 @@ io.on('connection', (socket) => {
}
}
currentRoom
.
currentPlayer
=
1
-
currentRoom
.
currentPlayer
;
currentRoom
.
currentPlayer
=
1
-
currentRoom
.
currentPlayer
;
socket
.
join
(
currentRoom
.
id
)
io
.
to
(
currentRoom
.
id
).
emit
(
'
updateGame
'
,
{
io
.
to
(
currentRoom
.
id
).
emit
(
'
updateGame
'
,
{
board
:
currentRoom
.
gameState
,
board
:
currentRoom
.
gameState
,
currentPlayer
:
currentRoom
.
currentPlayer
,
currentPlayer
:
currentRoom
.
currentPlayer
,
...
...
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