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
7fd45117
Commit
7fd45117
authored
2 months ago
by
Elarouti Adam
Browse files
Options
Downloads
Patches
Plain Diff
voilaaaaaaaaa
parent
d3e49d7b
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
htdocs/index.js
+57
-70
57 additions, 70 deletions
htdocs/index.js
with
57 additions
and
70 deletions
htdocs/index.js
+
57
−
70
View file @
7fd45117
...
...
@@ -10,7 +10,7 @@ const board = [
];
const
player
=
{
host
:
false
,
//
playedMove: board,
playedMove
:
board
,
roomId
:
null
,
username
:
""
,
socketId
:
""
,
...
...
@@ -18,7 +18,8 @@ const player = {
turn
:
false
,
win
:
false
,
isBlackPlayer
:
true
,
pieces
:
[
"
T
"
,
"
C
"
,
"
F
"
,
"
Q
"
,
"
K
"
,
"
P
"
]
pieces
:
[
"
T
"
,
"
C
"
,
"
F
"
,
"
Q
"
,
"
K
"
,
"
P
"
],
echec
:
false
};
...
...
@@ -108,13 +109,8 @@ socket.on('join room', (roomId) => {
socket
.
on
(
'
start game
'
,
(
players
)
=>
{
console
.
log
(
players
,
"
Le jeu commence
"
);
startGame
(
players
);
console
.
log
(
player
.
isBlackPlayer
)
if
(
player
.
isBlackPlayer
)
{
document
.
getElementById
(
'
chessboard
'
).
classList
.
add
(
'
black-perspective
'
);
console
.
log
(
"
échiquier inversé
"
)
}
});
...
...
@@ -178,7 +174,6 @@ function startGame(players) {
waitingArea
.
classList
.
add
(
'
d-none
'
);
turnMsg
.
classList
.
remove
(
'
d-none
'
);
const
ennemyPlayer
=
players
.
find
(
p
=>
p
.
socketId
!=
player
.
socketId
);
draw
();
ennemyUsername
=
ennemyPlayer
.
username
;
tour
=
{
currentPlayer
:
player
,
ennemyPlayer
:
ennemyPlayer
};
...
...
@@ -254,19 +249,6 @@ function drawPieces() {
if
(
piece
!==
""
)
{
let
img
=
new
Image
();
img
.
src
=
"
chess_pieces/
"
+
pieceImages
[
piece
];
if
(
player
.
isBlackPlayer
){
img
.
onload
=
function
()
{
ctx
.
save
();
ctx
.
translate
((
col
)
*
size
,
(
row
)
*
size
);
ctx
.
rotate
(
Math
.
PI
);
ctx
.
drawImage
(
img
,
-
size
,
-
size
,
size
,
size
);
ctx
.
restore
();
};
img
.
onerror
=
function
(
e
)
{
console
.
log
(
"
Erreur de chargement de l'image :
"
,
img
.
src
);
// En cas d'erreur de chargement
};
}
else
{
img
.
onload
=
function
()
{
ctx
.
drawImage
(
img
,
col
*
size
,
row
*
size
,
size
,
size
);
};
...
...
@@ -277,7 +259,6 @@ function drawPieces() {
}
}
}
}
// Fonction principale pour dessiner l'échiquier
function
draw
()
{
...
...
@@ -324,11 +305,13 @@ function selectPiece(row, col) {
}
function
highlightMoves
(
tour
,
piece
,
row
,
col
)
{
let
direction
=
0
;
let
ennemyPieces
=
[]
;
if
(
player
.
isBlackPlayer
===
false
){
ennemyPieces
=
[
"
T
"
,
"
C
"
,
"
F
"
,
"
Q
"
,
"
K
"
,
"
P
"
];
direction
=-
1
;
}
else
{
direction
=
1
;
ennemyPieces
=
[
"
t
"
,
"
c
"
,
"
f
"
,
"
q
"
,
"
k
"
,
"
p
"
];
}
let
moves
=
[];
console
.
log
(
piece
.
toLowerCase
()
===
"
p
"
);
...
...
@@ -346,10 +329,10 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
row
+
direction
]
&&
board
[
row
+
direction
][
col
]
===
""
)
{
moves
.
push
({
row
:
row
+
direction
,
col
});
}
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
row
+
direction
][
col
+
direction
])){
if
(
ennemyPieces
.
includes
(
board
[
row
+
direction
][
col
+
direction
])){
moves
.
push
({
row
:
row
+
direction
,
col
:
col
+
direction
})
}
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
row
+
direction
][
col
-
direction
])){
if
(
ennemyPieces
.
includes
(
board
[
row
+
direction
][
col
-
direction
])){
moves
.
push
({
row
:
row
+
direction
,
col
:
col
-
direction
})
}
}
...
...
@@ -360,7 +343,7 @@ function highlightMoves(tour,piece, row, col) {
moves
.
push
({
row
:
r
,
col
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
col
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
col
]))
{
moves
.
push
({
row
:
r
,
col
});
// Capture possible
}
break
// Bloqué par une pièce
...
...
@@ -372,7 +355,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
col
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
col
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
col
]))
{
moves
.
push
({
row
:
r
,
col
});
}
break
;
...
...
@@ -384,7 +367,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
row
][
c
]
===
""
)
{
moves
.
push
({
row
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
row
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
row
][
c
]))
{
moves
.
push
({
row
,
col
:
c
});
}
break
;
...
...
@@ -396,7 +379,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
row
][
c
]
===
""
)
{
moves
.
push
({
row
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
row
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
row
][
c
]))
{
moves
.
push
({
row
,
col
:
c
});
}
break
;
...
...
@@ -409,7 +392,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
// Bloqué par une pièce
...
...
@@ -421,7 +404,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -433,7 +416,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -445,7 +428,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -470,7 +453,7 @@ function highlightMoves(tour,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
]
===
""
||
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
newRow
][
newCol
]))
{
if
(
board
[
newRow
][
newCol
]
===
""
||
ennemyPieces
.
includes
(
board
[
newRow
][
newCol
]))
{
moves
.
push
({
row
:
newRow
,
col
:
newCol
});
}
}
...
...
@@ -490,7 +473,7 @@ function highlightMoves(tour,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
]
===
""
||
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
newRow
][
newCol
]))
{
if
(
board
[
newRow
][
newCol
]
===
""
||
ennemyPieces
.
includes
(
board
[
newRow
][
newCol
]))
{
moves
.
push
({
row
:
newRow
,
col
:
newCol
});
}
}
...
...
@@ -503,7 +486,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
col
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
col
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
col
]))
{
moves
.
push
({
row
:
r
,
col
});
// Capture possible
}
break
;
// Bloqué par une pièce
...
...
@@ -513,7 +496,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
col
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
col
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
col
]))
{
moves
.
push
({
row
:
r
,
col
});
}
break
;
...
...
@@ -525,7 +508,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
row
][
c
]
===
""
)
{
moves
.
push
({
row
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
row
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
row
][
c
]))
{
moves
.
push
({
row
,
col
:
c
});
}
break
;
...
...
@@ -535,7 +518,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
row
][
c
]
===
""
)
{
moves
.
push
({
row
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
row
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
row
][
c
]))
{
moves
.
push
({
row
,
col
:
c
});
}
break
;
...
...
@@ -547,7 +530,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -557,7 +540,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -567,7 +550,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -577,7 +560,7 @@ function highlightMoves(tour,piece, row, col) {
if
(
board
[
r
][
c
]
===
""
)
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
else
{
if
(
tour
.
ennemyP
layer
.
p
ieces
.
includes
(
board
[
r
][
c
]))
{
if
(
ennemyPieces
.
includes
(
board
[
r
][
c
]))
{
moves
.
push
({
row
:
r
,
col
:
c
});
}
break
;
...
...
@@ -601,36 +584,40 @@ function find_pieces(piece,board){
// function echec(tour, board) {
// // Trouve la position du roi ennemi
// if (tour.currentPlayer.isBlackPlayer === true){
// const [kingRow, kingCol] = find_pieces('k', board);
// }
// else{
// const [kingRow, kingCol] = find_pieces('K', board);
// let kingRow, kingCol;
// if (player.turn === true) {
// attackingPlayer = player;
// [kingRow, kingCol] = find_pieces('k', board); // Roi noir (adverse) si c'est au joueur blanc de jouer
// } else {
// attackingPlayer = ennemyPlayer;
// [kingRow, kingCol] = find_pieces('K', board); // Roi blanc (adverse) si c'est au joueur noir de jouer
// }
// let casesAttaquees = [];
// let allPositions = [];
// tour.currentPlayer.pieces.forEach(piece => {
// const piecePosition = find_pieces(piece, board); // Trouve la position de la pièce
// allPositions.push(piecePosition);
// });
// // Parcourt chaque pièce du joueur courant
// tour.currentPlayer.pieces.forEach(piece => {
// const pieceMoves = highlightMoves(piece, board, ); // On suppose que cette fonction renvoie un tableau de cases accessibles
// // Ajoute chaque case attaquée par la pièce aux cases attaquées
// casesAttaquees = casesAttaquees.concat(pieceMoves);
// });
// let allPositions = [];
// // Vérifie si le roi ennemi est attaqué
// return casesAttaquees.some(([row, col]) => row === kingRow && col === kingCol);
// for (let i = 0; i < board.length; i++) {
// for (let j = 0; j < board[i].length; j++) {
// const piece = board[i][j];
// if (piece) {
// const moves = highlightMoves(piece, board, i, j);
// allPositions.push({ piece, position: { i, j }, moves });
// // 🔍 Vérifie si un des mouvements possibles atteint le roi
// for (let move of moves) {
// if (move.i === kingRow && move.j === kingCol) {
// console.log(`Le roi est en échec par la pièce ${piece} en position (${i}, ${j})`);
// return true; // Roi est en échec
// }
// }
// }
// }
// }
// }
function
movePiece
(
piece
,
newRow
,
newCol
)
{
if
(
!
piece
)
return
;
// Vérifier si le mouvement est valide
const
validMoves
=
highlightMoves
(
tour
,
piece
,
selectedPosition
.
row
,
selectedPosition
.
col
);
console
.
log
(
validMoves
)
// Vérifier que validMoves est un tableau et qu'il contient des objets avec { row, col }
const
isValidMove
=
Array
.
isArray
(
validMoves
)
&&
validMoves
.
some
(
m
=>
m
.
row
===
newRow
&&
m
.
col
===
newCol
);
...
...
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