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
fdb40dd0
Commit
fdb40dd0
authored
1 month ago
by
De Brettes Thomas
Browse files
Options
Downloads
Patches
Plain Diff
Tour à tour qui fonctionne ENFINNNNNNNN
parent
1c129233
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
app.js
+1
-1
1 addition, 1 deletion
app.js
htdocs/index.js
+43
-18
43 additions, 18 deletions
htdocs/index.js
with
44 additions
and
19 deletions
app.js
+
1
−
1
View file @
fdb40dd0
...
...
@@ -5,7 +5,7 @@ const express = require('express');
const
app
=
express
();
const
http
=
require
(
'
http
'
).
createServer
(
app
);
const
path
=
require
(
'
path
'
);
const
port
=
80
6
0
;
const
port
=
80
8
0
;
const
io
=
require
(
'
socket.io
'
)(
http
);
...
...
This diff is collapsed.
Click to expand it.
htdocs/index.js
+
43
−
18
View file @
fdb40dd0
...
...
@@ -17,8 +17,8 @@ const player = {
symbol
:
"
X
"
,
turn
:
false
,
win
:
false
,
isBlackPlayer
:
fals
e
,
pieces
:
[
"
t
"
,
"
c
"
,
"
f
"
,
"
q
"
,
"
k
"
,
"
p
"
]
isBlackPlayer
:
tru
e
,
pieces
:
[
"
T
"
,
"
C
"
,
"
F
"
,
"
Q
"
,
"
K
"
,
"
P
"
]
};
...
...
@@ -83,6 +83,8 @@ $("#form").on('submit', function (e) {
player
.
roomId
=
roomId
;
}
else
{
player
.
host
=
true
;
player
.
isBlackPlayer
=
false
;
player
.
pieces
=
[
"
t
"
,
"
c
"
,
"
f
"
,
"
q
"
,
"
k
"
,
"
p
"
];
player
.
turn
=
true
;
}
...
...
@@ -107,6 +109,7 @@ socket.on('join room', (roomId) => {
socket
.
on
(
'
start game
'
,
(
players
)
=>
{
console
.
log
(
players
,
"
Le jeu commence
"
);
startGame
(
players
);
console
.
log
(
player
.
isBlackPlayer
)
});
...
...
@@ -118,8 +121,17 @@ socket.on('play', (moveData) => {
newRow
=
moveData
.
move
.
to
.
x
;
newCol
=
moveData
.
move
.
to
.
y
;
selectedPosition
=
{
row
,
col
};
board
[
newRow
][
newCol
]
=
moveData
.
move
.
piece
;
board
[
selectedPosition
.
row
][
selectedPosition
.
col
]
=
""
;
// Réinitialiser la pièce sélectionnée
selectedPiece
=
null
;
selectedPosition
=
null
;
draw
()
// Ne remplace PAS l'échiquier complet, mais applique le coup reçu
movePiece
(
moveData
.
move
.
piece
,
newRow
,
newCol
);
// Applique le coup reçu sur l'échiquier local et redessine l'échiquier
//
movePiece(moveData.move.piece, newRow, newCol); // Applique le coup reçu sur l'échiquier local et redessine l'échiquier
// if (ennemyPlayer.win) {
// setTurnMessage('alert-info', 'alert-danger', `C'est perdu ! <b>${ennemyPlayer.username}</b> a gagné !`);
...
...
@@ -148,8 +160,6 @@ socket.on('play', (moveData) => {
// return;
// }
setTurnMessage
(
'
alert-success
'
,
'
alert-info
'
,
`C'est au tour de <b>
${
ennemyUsername
}
</b> de jouer`
)
player
.
turn
=
false
;
// }
});
...
...
@@ -157,13 +167,14 @@ socket.on('play again', (players) => {
restartGame
(
players
);
})
function
startGame
(
players
)
{
restartArea
.
classList
.
add
(
'
d-none
'
);
waitingArea
.
classList
.
add
(
'
d-none
'
);
turnMsg
.
classList
.
remove
(
'
d-none
'
);
const
ennemyPlayer
=
players
.
find
(
p
=>
p
.
socketId
!=
player
.
socketId
);
ennemyPlayer
.
isBlackPlayer
=
true
;
ennemyPlayer
.
pieces
=
[
"
P
"
,
"
T
"
,
"
C
"
,
"
F
"
,
"
Q
"
,
"
K
"
];
draw
();
ennemyUsername
=
ennemyPlayer
.
username
;
tour
=
{
currentPlayer
:
player
,
ennemyPlayer
:
ennemyPlayer
};
...
...
@@ -257,7 +268,7 @@ function draw() {
}
// Appeler la fonction pour afficher l'échiquier
draw
();
//Mouvement des pièces sur l'échiquier
...
...
@@ -269,14 +280,14 @@ canvas.addEventListener("click", function(event) {
const
row
=
Math
.
floor
(
event
.
offsetY
/
size
);
if
(
selectedPiece
)
{
socket
.
emit
(
'
play
'
,
{
/*
socket.emit('play', {
move: {
from:{ x:selectedPosition.row, y:selectedPosition.col
},
to: { x: row, y: col }, // Position d'arrivée
piece: selectedPiece // Nom de la pièce (ex: "pion", "tour", etc.)
}
});
});
*/
movePiece
(
selectedPiece
,
row
,
col
);
}
else
{
selectPiece
(
row
,
col
);
...
...
@@ -284,16 +295,18 @@ canvas.addEventListener("click", function(event) {
});
function
selectPiece
(
row
,
col
)
{
if
(
player
.
turn
)
{
const
current_piece
=
board
[
row
][
col
];
selectedPosition
=
{
row
,
col
};
if
(
current_piece
!==
""
&&
tour
.
currentP
layer
.
pieces
.
includes
(
current_piece
))
{
// Vérifier que ce n'est pas une case vide
if
(
current_piece
!==
""
&&
p
layer
.
pieces
.
includes
(
current_piece
))
{
// Vérifier que ce n'est pas une case vide
selectedPiece
=
current_piece
;
highlightMoves
(
tour
,
current_piece
,
row
,
col
);
}
}
}
function
highlightMoves
(
playe
r
,
piece
,
row
,
col
)
{
if
(
tour
.
currentP
layer
.
isBlackPlayer
===
false
){
function
highlightMoves
(
tou
r
,
piece
,
row
,
col
)
{
if
(
p
layer
.
isBlackPlayer
===
false
){
direction
=-
1
;
}
else
{
direction
=
1
;
...
...
@@ -301,12 +314,12 @@ function highlightMoves(player,piece, row, col) {
let
moves
=
[];
console
.
log
(
piece
.
toLowerCase
()
===
"
p
"
);
if
(
piece
.
toLowerCase
()
===
"
p
"
)
{
if
(
row
===
6
&&
tour
.
currentP
layer
.
isBlackPlayer
===
false
){
if
(
row
===
6
&&
p
layer
.
isBlackPlayer
===
false
){
if
(
board
[
row
+
2
*
direction
][
col
]
===
""
)
{
moves
.
push
({
row
:
row
+
2
*
direction
,
col
});
}
}
if
(
row
===
1
&&
tour
.
currentP
layer
.
isBlackPlayer
===
true
){
if
(
row
===
1
&&
p
layer
.
isBlackPlayer
===
true
){
if
(
board
[
row
+
2
*
direction
][
col
]
===
""
)
{
moves
.
push
({
row
:
row
+
2
*
direction
,
col
});
}
...
...
@@ -608,10 +621,22 @@ function movePiece(piece, newRow, newCol) {
board
[
selectedPosition
.
row
][
selectedPosition
.
col
]
=
""
;
// Réinitialiser la pièce sélectionnée
console
.
log
(
selectedPosition
.
row
,
selectedPosition
.
col
)
//currentPlayer=(currentPlayer+1)%2;
socket
.
emit
(
'
play
'
,
{
move
:
{
from
:{
x
:
selectedPosition
.
row
,
y
:
selectedPosition
.
col
},
to
:
{
x
:
newRow
,
y
:
newCol
},
// Position d'arrivée
piece
:
selectedPiece
// Nom de la pièce (ex: "pion", "tour", etc.)
}
});
setTurnMessage
(
'
alert-success
'
,
'
alert-info
'
,
`C'est au tour de <b>
${
ennemyUsername
}
</b> de jouer`
)
player
.
turn
=
false
;
selectedPiece
=
null
;
selectedPosition
=
null
;
//currentPlayer=(currentPlayer+1)%2;
[
tour
.
currentPlayer
,
tour
.
ennemyPlayer
]
=
[
tour
.
ennemyPlayer
,
tour
.
currentPlayer
];
// if (echec(tour,board)===true){
// console.log('echeeeeeeeeecs')
...
...
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