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
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Elarouti Adam
Echecs
Commits
79aa3270
Commit
79aa3270
authored
2 months ago
by
Elarouti Adam
Browse files
Options
Downloads
Patches
Plain Diff
mouvement noir
parent
c4b06008
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
htdocs/chess.js
+43
-34
43 additions, 34 deletions
htdocs/chess.js
htdocs/page_acceuil.html
+1
-1
1 addition, 1 deletion
htdocs/page_acceuil.html
htdocs/page_jeu.html
+3
-3
3 additions, 3 deletions
htdocs/page_jeu.html
server.js
+5
-4
5 additions, 4 deletions
server.js
with
52 additions
and
42 deletions
htdocs/chess.js
+
43
−
34
View file @
79aa3270
const
canvas
=
document
.
getElementById
(
"
chessboard
"
);
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
"
],
...
...
@@ -16,8 +16,6 @@ const board = [
// Charger les images des pièces
const
pieceImages
=
{
"
p
"
:
"
pion_blanc.png
"
,
"
P
"
:
"
pion_noir.png
"
,
...
...
@@ -33,8 +31,8 @@ const pieceImages = {
"
K
"
:
"
roi_noir.png
"
};
const
noirs
=
[
"
P
"
,
"
T
"
,
"
C
"
,
"
F
"
,
"
Q
"
,
"
K
"
];
const
blancs
=
[
"
p
"
,
"
t
"
,
"
c
"
,
"
f
"
,
"
q
"
,
"
k
"
];
const
pieces_joueurs
=
{
0
:
[
"
p
"
,
"
t
"
,
"
c
"
,
"
f
"
,
"
q
"
,
"
k
"
],
1
:
[
"
P
"
,
"
T
"
,
"
C
"
,
"
F
"
,
"
Q
"
,
"
K
"
]
}
;
...
...
@@ -97,29 +95,40 @@ canvas.addEventListener("click", function(event) {
function
selectPiece
(
row
,
col
)
{
const
piece
=
board
[
row
][
col
];
if
(
piece
!==
""
)
{
// Vérifier que ce n'est pas une case vide
if
(
piece
!==
""
&&
pieces_joueurs
[
currentPlayer
].
includes
(
piece
)
)
{
// Vérifier que ce n'est pas une case vide
selectedPiece
=
piece
;
selectedPosition
=
{
row
,
col
};
highlightMoves
(
piece
,
row
,
col
);
highlightMoves
(
currentPlayer
,
piece
,
row
,
col
);
}
}
function
highlightMoves
(
piece
,
row
,
col
)
{
function
highlightMoves
(
player
,
piece
,
row
,
col
)
{
if
(
player
===
0
){
direction
=-
1
;
opponent
=
1
;
}
else
{
direction
=
1
;
opponent
=
0
;
}
let
moves
=
[];
if
(
piece
.
toLowerCase
()
===
"
p
"
)
{
if
(
row
===
6
){
if
(
board
[
row
-
2
][
col
]
===
""
)
{
moves
.
push
({
row
:
row
-
2
,
col
});
if
(
row
===
6
&&
player
===
0
){
if
(
board
[
row
+
2
*
direction
][
col
]
===
""
)
{
moves
.
push
({
row
:
row
+
2
*
direction
,
col
});
}
}
if
(
row
===
1
&&
player
===
1
){
if
(
board
[
row
+
2
*
direction
][
col
]
===
""
)
{
moves
.
push
({
row
:
row
+
2
*
direction
,
col
});
}
}
let
direction
=
-
1
;
// Pion blanc monte
if
(
board
[
row
+
direction
]
&&
board
[
row
+
direction
][
col
]
===
""
)
{
moves
.
push
({
row
:
row
+
direction
,
col
});
}
if
(
noirs
.
includes
(
board
[
row
+
direction
][
col
+
direction
])){
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
row
+
direction
][
col
+
direction
])){
moves
.
push
({
row
:
row
+
direction
,
col
:
col
+
direction
})
}
if
(
noirs
.
includes
(
board
[
row
+
direction
][
col
-
direction
])){
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
row
+
direction
][
col
-
direction
])){
moves
.
push
({
row
:
row
+
direction
,
col
:
col
-
direction
})
}
}
...
...
@@ -130,7 +139,7 @@ function highlightMoves(piece, row, col) {
moves
.
push
({
row
:
r
,
col
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
col
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
col
]))
{
moves
.
push
({
row
:
r
,
col
});
// Capture possible
}
break
// Bloqué par une pièce
...
...
@@ -142,7 +151,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
col
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
col
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
col
]))
{
moves
.
push
({
row
:
r
,
col
});
}
break
;
...
...
@@ -154,7 +163,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
row
][
c
]
===
""
)
{
moves
.
push
({
row
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
row
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
row
][
c
]))
{
moves
.
push
({
row
,
col
:
c
});
}
break
;
...
...
@@ -166,7 +175,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
row
][
c
]
===
""
)
{
moves
.
push
({
row
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
row
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
row
][
c
]))
{
moves
.
push
({
row
,
col
:
c
});
}
break
;
...
...
@@ -179,7 +188,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
// Bloqué par une pièce
...
...
@@ -191,7 +200,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -203,7 +212,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -215,7 +224,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -240,7 +249,7 @@ function highlightMoves(piece, row, col) {
// Vérifie que la nouvelle position est sur l'échiquier
if
(
newRow
>=
0
&&
newRow
<
8
&&
newCol
>=
0
&&
newCol
<
8
)
{
if
(
board
[
newRow
][
newCol
]
===
""
||
noirs
.
includes
(
board
[
newRow
][
newCol
]))
{
if
(
board
[
newRow
][
newCol
]
===
""
||
pieces_joueurs
[
opponent
]
.
includes
(
board
[
newRow
][
newCol
]))
{
moves
.
push
({
row
:
newRow
,
col
:
newCol
});
}
}
...
...
@@ -260,7 +269,7 @@ function highlightMoves(piece, row, col) {
// Vérifie que la nouvelle position est sur l'échiquier
if
(
newRow
>=
0
&&
newRow
<
8
&&
newCol
>=
0
&&
newCol
<
8
)
{
if
(
board
[
newRow
][
newCol
]
===
""
||
noirs
.
includes
(
board
[
newRow
][
newCol
]))
{
if
(
board
[
newRow
][
newCol
]
===
""
||
pieces_joueurs
[
opponent
]
.
includes
(
board
[
newRow
][
newCol
]))
{
moves
.
push
({
row
:
newRow
,
col
:
newCol
});
}
}
...
...
@@ -273,7 +282,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
col
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
col
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
col
]))
{
moves
.
push
({
row
:
r
,
col
});
// Capture possible
}
break
;
// Bloqué par une pièce
...
...
@@ -283,7 +292,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
col
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
col
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
col
]))
{
moves
.
push
({
row
:
r
,
col
});
}
break
;
...
...
@@ -295,7 +304,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
row
][
c
]
===
""
)
{
moves
.
push
({
row
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
row
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
row
][
c
]))
{
moves
.
push
({
row
,
col
:
c
});
}
break
;
...
...
@@ -305,7 +314,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
row
][
c
]
===
""
)
{
moves
.
push
({
row
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
row
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
row
][
c
]))
{
moves
.
push
({
row
,
col
:
c
});
}
break
;
...
...
@@ -317,7 +326,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -327,7 +336,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -337,7 +346,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -347,7 +356,7 @@ function highlightMoves(piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
noirs
.
includes
(
board
[
r
][
c
]))
{
if
(
pieces_joueurs
[
opponent
]
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -376,7 +385,7 @@ function movePiece(newRow, newCol) {
if
(
!
selectedPiece
)
return
;
// Vérifier si le mouvement est valide
const
validMoves
=
highlightMoves
(
selectedPiece
,
selectedPosition
.
row
,
selectedPosition
.
col
);
const
validMoves
=
highlightMoves
(
currentPlayer
,
selectedPiece
,
selectedPosition
.
row
,
selectedPosition
.
col
);
console
.
log
(
validMoves
);
// Vérifie la structure des validMoves
// Vérifier que validMoves est un tableau et qu'il contient des objets avec { row, col }
...
...
@@ -390,7 +399,7 @@ function movePiece(newRow, newCol) {
// Réinitialiser la pièce sélectionnée
selectedPiece
=
null
;
selectedPosition
=
null
;
currentPlayer
=
(
currentPlayer
+
1
)
%
2
;
// Redessiner le plateau
drawBoard
();
drawPieces
();
...
...
This diff is collapsed.
Click to expand it.
htdocs/page_acceuil.html
+
1
−
1
View file @
79aa3270
This diff is collapsed.
Click to expand it.
htdocs/page_jeu.html
+
3
−
3
View file @
79aa3270
...
...
@@ -23,7 +23,7 @@
// Envoi d'un message vers le serveur
socket
.
emit
(
'
message
'
,
{
'
msg
'
:
'
Hello, serveur !
'
});
socket
.
emit
(
'
disconnect
'
,{
'
msg
'
:
'
Le client est deco
'
});
// Optionnel : écouter d'autres événements, par exemple pour les erreurs
socket
.
on
(
'
connect_error
'
,
function
(
err
)
{
console
.
error
(
"
Erreur de connexion :
"
,
err
);
...
...
This diff is collapsed.
Click to expand it.
server.js
+
5
−
4
View file @
79aa3270
...
...
@@ -70,16 +70,17 @@ io.on('connection', function (socket) {
// Envoyer un message "hello" au client
socket
.
emit
(
'
hello
'
,
{
'
this
'
:
'
is my data
'
});
// Écouter un message du client (optionnel)
socket
.
on
(
'
message
'
,
function
(
data
)
{
console
.
log
(
"
Message reçu du client :
"
,
data
);
socket
.
on
(
'
disconnect
'
,
()
=>
{
console
.
log
(
'
Le client est déconnecté
'
);
});
});
});
// Lancer le serveur sur le port 8080
server
.
listen
(
8080
,
()
=>
{
console
.
log
(
"
Serveur WebSocket démarré sur http://localhost:8080
"
);
});
\ 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