Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Echecs
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
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Elarouti Adam
Echecs
Commits
cdcc59f4
Commit
cdcc59f4
authored
4 months ago
by
Elarouti Adam
Browse files
Options
Downloads
Patches
Plain Diff
client dialogue
parent
9ffd249a
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
app.js
+3
-3
3 additions, 3 deletions
app.js
htdocs/index.js
+75
-83
75 additions, 83 deletions
htdocs/index.js
with
78 additions
and
86 deletions
app.js
+
3
−
3
View file @
cdcc59f4
...
...
@@ -62,9 +62,9 @@ io.on('connection', (socket) => {
io
.
to
(
socket
.
id
).
emit
(
'
list rooms
'
,
rooms
);
});
socket
.
on
(
'
play
'
,
(
player
)
=>
{
console
.
log
(
`[play]
${
player
.
username
}
`
);
io
.
to
(
player
.
roomId
).
emit
(
'
play
'
,
player
);
socket
.
on
(
'
play
'
,
(
g
)
=>
{
console
.
log
(
g
);
socket
.
broadcast
.
emit
(
'
play
'
,
g
);
// On renvoie l'objet complet aux autres joueurs
});
socket
.
on
(
'
play again
'
,
(
roomId
)
=>
{
...
...
This diff is collapsed.
Click to expand it.
htdocs/index.js
+
75
−
83
View file @
cdcc59f4
const
board
=
[
[
"
T
"
,
"
C
"
,
"
F
"
,
"
Q
"
,
"
K
"
,
"
F
"
,
"
C
"
,
"
T
"
],
[
"
P
"
,
"
P
"
,
"
P
"
,
"
P
"
,
"
P
"
,
"
P
"
,
"
P
"
,
"
P
"
],
[
""
,
""
,
""
,
""
,
""
,
""
,
""
,
""
],
[
""
,
""
,
""
,
""
,
""
,
""
,
""
,
""
],
[
""
,
""
,
""
,
""
,
""
,
""
,
""
,
""
],
[
""
,
""
,
""
,
""
,
""
,
""
,
""
,
""
],
[
"
p
"
,
"
p
"
,
"
p
"
,
"
p
"
,
"
p
"
,
"
p
"
,
"
p
"
,
"
p
"
],
[
"
t
"
,
"
c
"
,
"
f
"
,
"
q
"
,
"
k
"
,
"
f
"
,
"
c
"
,
"
t
"
]
];
const
player
=
{
host
:
false
,
played
Cell
:
""
,
played
Move
:
board
,
roomId
:
null
,
username
:
""
,
socketId
:
""
,
symbol
:
"
X
"
,
turn
:
false
,
win
:
false
win
:
false
,
isBlackPlayer
:
false
};
const
socket
=
io
();
...
...
@@ -81,21 +92,6 @@ $("#form").on('submit', function (e) {
socket
.
emit
(
'
playerData
'
,
player
);
});
$
(
"
.cell
"
).
on
(
"
click
"
,
function
(
e
)
{
const
playedCell
=
this
.
getAttribute
(
'
id
'
);
if
(
this
.
innerText
===
""
&&
player
.
turn
)
{
player
.
playedCell
=
playedCell
;
this
.
innerText
=
player
.
symbol
;
player
.
win
=
calculateWin
(
playedCell
);
player
.
turn
=
false
;
socket
.
emit
(
'
play
'
,
player
);
}
});
$
(
"
#restart
"
).
on
(
'
click
'
,
function
()
{
restartGame
();
})
...
...
@@ -106,48 +102,54 @@ socket.on('join room', (roomId) => {
});
socket
.
on
(
'
start game
'
,
(
players
)
=>
{
console
.
log
(
players
)
console
.
log
(
players
)
;
startGame
(
players
);
});
socket
.
on
(
'
play
'
,
(
ennemyPlayer
)
=>
{
if
(
ennemyPlayer
.
socketId
!==
player
.
socketId
&&
!
ennemyPlayer
.
turn
)
{
const
playedCell
=
document
.
getElementById
(
`
${
ennemyPlayer
.
playedCell
}
`
);
socket
.
on
(
'
play
'
,
(
f
)
=>
{
console
.
log
(
f
);
console
.
log
(
board
);
playedCell
.
classList
.
add
(
'
text-danger
'
);
playedCell
.
innerHTML
=
'
O
'
;
player
.
playedMove
=
f
;
console
.
log
(
player
.
playedMove
);
draw
(
player
);
if
(
ennemyPlayer
.
win
)
{
setTurnMessage
(
'
alert-info
'
,
'
alert-danger
'
,
`C'est perdu ! <b>
${
ennemyPlayer
.
username
}
</b> a gagné !`
);
calculateWin
(
ennemyPlayer
.
playedCell
,
'
O
'
);
showRestartArea
();
return
;
}
if
(
calculateEquality
())
{
setTurnMessage
(
'
alert-info
'
,
'
alert-warning
'
,
"
C'est une egalité !
"
);
return
;
}
// if (ennemyPlayer.socketId !== player.socketId && !ennemyPlayer.turn) {
setTurnMessage
(
'
alert-info
'
,
'
alert-success
'
,
"
C'est ton tour de jouer
"
);
player
.
turn
=
true
;
}
else
{
if
(
player
.
win
)
{
$
(
"
#turn-message
"
).
addClass
(
'
alert-success
'
).
html
(
"
Félicitations, tu as gagné la partie !
"
);
showRestartArea
();
return
;
}
// player.playedMove = ennemyPlayer.playedMove;
if
(
calculateEquality
())
{
setTurnMessage
(
'
alert-info
'
,
'
alert-warning
'
,
"
C'est une egalité !
"
);
showRestartArea
();
return
;
}
// if (ennemyPlayer.win) {
// setTurnMessage('alert-info', 'alert-danger', `C'est perdu ! <b>${ennemyPlayer.username}</b> a gagné !`);
// //calculateWin(ennemyPlayer.playedCell, 'O');
// showRestartArea();
// return;
// }
setTurnMessage
(
'
alert-success
'
,
'
alert-info
'
,
`C'est au tour de <b>
${
ennemyUsername
}
</b> de jouer`
)
player
.
turn
=
false
;
}
// if (calculateEquality()) {
// setTurnMessage('alert-info', 'alert-warning', "C'est une egalité !");
// return;
// }
// setTurnMessage('alert-info', 'alert-success', "C'est ton tour de jouer");
// player.turn = true;
// } else {
// if (player.win) {
// $("#turn-message").addClass('alert-success').html("Félicitations, tu as gagné la partie !");
// showRestartArea();
// return;
// }
// if (calculateEquality()) {
// setTurnMessage('alert-info', 'alert-warning', "C'est une egalité !");
// showRestartArea();
// return;
// }
// setTurnMessage('alert-success', 'alert-info', `C'est au tour de <b>${ennemyUsername}</b> de jouer`)
// player.turn = false;
// }
});
socket
.
on
(
'
play again
'
,
(
players
)
=>
{
...
...
@@ -158,8 +160,8 @@ function startGame(players) {
restartArea
.
classList
.
add
(
'
d-none
'
);
waitingArea
.
classList
.
add
(
'
d-none
'
);
turnMsg
.
classList
.
remove
(
'
d-none
'
);
draw
();
const
ennemyPlayer
=
players
.
find
(
p
=>
p
.
socketId
!=
player
.
socketId
);
draw
(
ennemyPlayer
);
ennemyUsername
=
ennemyPlayer
.
username
;
if
(
player
.
host
&&
player
.
turn
)
{
...
...
@@ -174,6 +176,7 @@ function setTurnMessage(classToRemove, classToAdd, html) {
turnMsg
.
classList
.
add
(
classToAdd
);
turnMsg
.
innerHTML
=
html
;
}
const
joinRoom
=
function
()
{
if
(
usernameInput
.
value
!==
""
)
{
player
.
username
=
usernameInput
.
value
;
...
...
@@ -193,16 +196,6 @@ const ctx = canvas.getContext("2d");
const
size
=
60
;
// Taille des cases
currentPlayer
=
0
;
// Disposition initiale de l'échiquier
const
board
=
[
[
"
T
"
,
"
C
"
,
"
F
"
,
"
Q
"
,
"
K
"
,
"
F
"
,
"
C
"
,
"
T
"
],
[
"
P
"
,
"
P
"
,
"
P
"
,
"
P
"
,
"
P
"
,
"
P
"
,
"
P
"
,
"
P
"
],
[
""
,
""
,
""
,
""
,
""
,
""
,
""
,
""
],
[
""
,
""
,
""
,
""
,
""
,
""
,
""
,
""
],
[
""
,
""
,
""
,
""
,
""
,
""
,
""
,
""
],
[
""
,
""
,
""
,
""
,
""
,
""
,
""
,
""
],
[
"
p
"
,
"
p
"
,
"
p
"
,
"
p
"
,
"
p
"
,
"
p
"
,
"
p
"
,
"
p
"
],
[
"
t
"
,
"
c
"
,
"
f
"
,
"
q
"
,
"
k
"
,
"
f
"
,
"
c
"
,
"
t
"
]
];
// Charger les images des pièces
...
...
@@ -237,10 +230,10 @@ function drawBoard() {
}
// Dessiner les pièces sur l'échiquier
function
drawPieces
()
{
function
drawPieces
(
player
)
{
for
(
let
row
=
0
;
row
<
8
;
row
++
)
{
for
(
let
col
=
0
;
col
<
8
;
col
++
)
{
const
piece
=
board
[
row
][
col
];
const
piece
=
player
.
playedMove
[
row
][
col
];
if
(
piece
!==
""
)
{
let
img
=
new
Image
();
img
.
src
=
"
chess_pieces/
"
+
pieceImages
[
piece
];
...
...
@@ -256,13 +249,13 @@ function drawPieces() {
}
// Fonction principale pour dessiner l'échiquier
function
draw
()
{
function
draw
(
player
)
{
drawBoard
();
drawPieces
();
drawPieces
(
player
);
}
// Appeler la fonction pour afficher l'échiquier
draw
();
draw
(
player
);
//Mouvement des pièces sur l'échiquier
...
...
@@ -275,7 +268,7 @@ canvas.addEventListener("click", function(event) {
const
row
=
Math
.
floor
(
event
.
offsetY
/
size
);
if
(
selectedPiece
)
{
movePiece
(
row
,
col
);
movePiece
(
player
,
row
,
col
);
}
else
{
selectPiece
(
row
,
col
);
}
...
...
@@ -561,16 +554,7 @@ function highlightMoves(player,piece, row, col) {
function
drawHighlightedMoves
(
moves
)
{
ctx
.
fillStyle
=
"
rgba(0, 255, 0, 0.5)
"
;
// Vert transparent
for
(
let
move
of
moves
)
{
ctx
.
beginPath
();
ctx
.
arc
(
move
.
col
*
size
+
size
/
2
,
move
.
row
*
size
+
size
/
2
,
size
/
4
,
0
,
2
*
Math
.
PI
);
ctx
.
fill
();
}
}
function
movePiece
(
newRow
,
newCol
)
{
function
movePiece
(
player
,
newRow
,
newCol
)
{
if
(
!
selectedPiece
)
return
;
// Vérifier si le mouvement est valide
...
...
@@ -591,10 +575,18 @@ function movePiece(newRow, newCol) {
currentPlayer
=
(
currentPlayer
+
1
)
%
2
;
// Redessiner le plateau
drawBoard
();
drawPieces
();
drawPieces
(
player
);
player
.
playedMove
=
board
;
console
.
log
(
"
[Client] Envoi des données :
"
,
{
socketId
:
player
.
socketId
,
playedMove
:
board
// ou ce qui est nécessaire pour l'échiquier
});
socket
.
emit
(
'
play
'
,
player
.
playedMove
// ou quel que soit le nom de ta variable représentant l'échiquier
);
}
else
{
// Si le mouvement est invalide, tu pourrais afficher un message
console
.
log
(
"
Mouvement invalide !
"
);
selectedPiece
=
null
;
}
}
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