oui
This commit is contained in:
parent
2c5888d64c
commit
76af0d7849
2 changed files with 282 additions and 87 deletions
169
Puissance4.html
169
Puissance4.html
|
@ -1,84 +1,85 @@
|
|||
<!DOCTYPE HTML>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="Puissance4.css" type="text/css">
|
||||
<script type="text/javascript" src="Puissance4.js" defer></script>
|
||||
<html>
|
||||
<head>
|
||||
<title>Puissance 4</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<nav>
|
||||
<a href="Page1.html">Home</a>
|
||||
<a href="Puissance4.html">Puissance 4</a>
|
||||
<a href="Morpion.html">Morpion</a>
|
||||
<a href="Snake.html">Snake</a>
|
||||
<a href="JustePrix.html">Juste Prix</a>
|
||||
</nav>
|
||||
</header>
|
||||
<main>
|
||||
<section>
|
||||
<table id="Board">
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</table>
|
||||
</section>
|
||||
</main>
|
||||
<footer>© Copyright 2022 by Sahel Olivan. All Rights Reserved.</footer>
|
||||
</body>
|
||||
</html>
|
||||
''' <!DOCTYPE HTML>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="Puissance4.css" type="text/css">
|
||||
<script type="text/javascript" src="Puissance4.js" defer></script>
|
||||
<html>
|
||||
<head>
|
||||
<title>Puissance 4</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<nav>
|
||||
<a href="Page1.html">Home</a>
|
||||
<a href="Puissance4.html">Puissance 4</a>
|
||||
<a href="Morpion.html">Morpion</a>
|
||||
<a href="Snake.html">Snake</a>
|
||||
<a href="JustePrix.html">Juste Prix</a>
|
||||
</nav>
|
||||
</header>
|
||||
<main>
|
||||
<section>
|
||||
<table id="Board">
|
||||
<tr>
|
||||
<td id="td00"></td>
|
||||
<td id="td01"></td>
|
||||
<td id="td02"></td>
|
||||
<td id="td03"></td>
|
||||
<td id="td04"></td>
|
||||
<td id="td05"></td>
|
||||
<td id="td06"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td id="td10"></td>
|
||||
<td id="td11"></td>
|
||||
<td id="td12"></td>
|
||||
<td id="td13"></td>
|
||||
<td id="td14"></td>
|
||||
<td id="td15"></td>
|
||||
<td id="td16"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td id="td20"></td>
|
||||
<td id="td21"></td>
|
||||
<td id="td22"></td>
|
||||
<td id="td23"></td>
|
||||
<td id="td24"></td>
|
||||
<td id="td25"></td>
|
||||
<td id="td26"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td id="td30"></td>
|
||||
<td id="td31"></td>
|
||||
<td id="td32"></td>
|
||||
<td id="td33"></td>
|
||||
<td id="td34"></td>
|
||||
<td id="td35"></td>
|
||||
<td id="td36"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td id="td40"></td>
|
||||
<td id="td41"></td>
|
||||
<td id="td42"></td>
|
||||
<td id="td43"></td>
|
||||
<td id="td44"></td>
|
||||
<td id="td45"></td>
|
||||
<td id="td46"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td id="td50"></td>
|
||||
<td id="td51"></td>
|
||||
<td id="td52"></td>
|
||||
<td id="td53"></td>
|
||||
<td id="td54"></td>
|
||||
<td id="td55"></td>
|
||||
<td id="td56"></td>
|
||||
</tr>
|
||||
</table>
|
||||
</section>
|
||||
</main>
|
||||
<footer>© Copyright 2022 by Sahel Olivan. All Rights Reserved.</footer>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
200
Puissance4.js
200
Puissance4.js
|
@ -1,17 +1,211 @@
|
|||
|
||||
var currentRow = 0;
|
||||
var currentColumn = 0;
|
||||
var nbOfPlay = 0;
|
||||
var columnFill0 = 5;
|
||||
var columnFill1 = 5;
|
||||
var columnFill2 = 5;
|
||||
var columnFill3 = 5;
|
||||
var columnFill4 = 5;
|
||||
var columnFill5 = 5;
|
||||
var columnFill6 = 5;
|
||||
var currentPosition = [0,0];
|
||||
var countYellow = 0;
|
||||
var countRed = 0;
|
||||
|
||||
const tbody = document.querySelector('#Board');
|
||||
//update currentRow, currentColumn, nbOfPlay and calls for placeCoin, everytime we press a cell
|
||||
const tbody = document.querySelector('#Board');
|
||||
tbody.addEventListener('click', function (e){
|
||||
const cell = e.target.closest('td');
|
||||
if (!cell) {return;}
|
||||
const row = cell.parentElement;
|
||||
currentRow = row.rowIndex;
|
||||
currentColumn = cell.cellIndex;
|
||||
test();
|
||||
nbOfPlay++;
|
||||
PlaceCoin();
|
||||
CheckIfWin();
|
||||
});
|
||||
|
||||
//to remove
|
||||
function test(){
|
||||
console.log(currentRow, currentColumn);
|
||||
console.log(currentRow, currentColumn, nbOfPlay, currentPosition);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//place a coin on the board accordingly to the rules
|
||||
//variable columnFill = nb of cell free of a coin in this column (max = 0(start at 5))
|
||||
//place coin at colomnFill + 1 and update columnFill
|
||||
//place coin : change background-color : pair nbOfPlay : red, odd nbOfPlay : yellow
|
||||
//if columnFill > 5 then catch error and alert(column is already filled) and don't count the play
|
||||
//also calls for CheckIfWin after each play
|
||||
function PlaceCoin(){
|
||||
try{
|
||||
switch (currentColumn) {
|
||||
case 0:
|
||||
currentPosition = [columnFill0,currentColumn];
|
||||
if (nbOfPlay % 2 == 0){
|
||||
document.getElementById('td'+columnFill0+currentColumn).style.backgroundColor = "red";
|
||||
columnFill0--;
|
||||
}
|
||||
else{
|
||||
document.getElementById('td'+columnFill0+currentColumn).style.backgroundColor = "yellow";
|
||||
columnFill0--;
|
||||
}
|
||||
break;
|
||||
|
||||
case 1:
|
||||
currentPosition = [columnFill1,currentColumn];
|
||||
if (nbOfPlay % 2 == 0){
|
||||
document.getElementById('td'+columnFill1+currentColumn).style.backgroundColor = "red";
|
||||
columnFill1--;
|
||||
}
|
||||
else{
|
||||
document.getElementById('td'+columnFill1+currentColumn).style.backgroundColor = "yellow";
|
||||
columnFill1--;
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
currentPosition = [columnFill2,currentColumn];
|
||||
if (nbOfPlay % 2 == 0){
|
||||
document.getElementById('td'+columnFill2+currentColumn).style.backgroundColor = "red";
|
||||
columnFill2--;
|
||||
}
|
||||
else{
|
||||
document.getElementById('td'+columnFill2+currentColumn).style.backgroundColor = "yellow";
|
||||
columnFill2--;
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
currentPosition = [columnFill3,currentColumn];
|
||||
if (nbOfPlay % 2 == 0){
|
||||
document.getElementById('td'+columnFill3+currentColumn).style.backgroundColor = "red";
|
||||
columnFill3--;
|
||||
}
|
||||
else{
|
||||
document.getElementById('td'+columnFill3+currentColumn).style.backgroundColor = "yellow";
|
||||
columnFill3--;
|
||||
}
|
||||
break;
|
||||
|
||||
case 4:
|
||||
currentPosition = [columnFill4,currentColumn];
|
||||
if (nbOfPlay % 2 == 0){
|
||||
document.getElementById('td'+columnFill4+currentColumn).style.backgroundColor = "red";
|
||||
columnFill4--;
|
||||
}
|
||||
else{
|
||||
document.getElementById('td'+columnFill4+currentColumn).style.backgroundColor = "yellow";
|
||||
columnFill4--;
|
||||
}
|
||||
break;
|
||||
|
||||
case 5:
|
||||
currentPosition = [columnFill5,currentColumn];
|
||||
if (nbOfPlay % 2 == 0){
|
||||
document.getElementById('td'+columnFill5+currentColumn).style.backgroundColor = "red";
|
||||
columnFill5--;
|
||||
}
|
||||
else{
|
||||
document.getElementById('td'+columnFill5+currentColumn).style.backgroundColor = "yellow";
|
||||
columnFill5--;
|
||||
}
|
||||
break;
|
||||
|
||||
case 6:
|
||||
currentPosition = [columnFill6,currentColumn];
|
||||
if (nbOfPlay % 2 == 0){
|
||||
document.getElementById('td'+columnFill6+currentColumn).style.backgroundColor = "red";
|
||||
columnFill6--;
|
||||
}
|
||||
else{
|
||||
document.getElementById('td'+columnFill6+currentColumn).style.backgroundColor = "yellow";
|
||||
columnFill6--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch{
|
||||
nbOfPlay--;
|
||||
currentPosition[0] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
let moves = 0;
|
||||
|
||||
function CheckIfWin() {
|
||||
moves++
|
||||
if (moves < 7){
|
||||
|
||||
|
||||
// Check if the current player has won
|
||||
|
||||
// Check for horizontal win
|
||||
for (let row = 0; row < 6; row++) {
|
||||
for (let col = 0; col < 4; col++) {
|
||||
const cell1 = getComputedStyle(document.getElementById(`td${row}${col}`)).backgroundColor;
|
||||
const cell2 = getComputedStyle(document.getElementById(`td${row}${col + 1}`)).backgroundColor;
|
||||
const cell3 = getComputedStyle(document.getElementById(`td${row}${col + 2}`)).backgroundColor;
|
||||
const cell4 = getComputedStyle(document.getElementById(`td${row}${col + 3}`)).backgroundColor;
|
||||
if (cell1 === cell2 && cell2 === cell3 && cell3 === cell4 && cell1 !== "rgb(255, 255, 255)") {
|
||||
// Display winning message
|
||||
alert(`Player ${cell1} wins!`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for vertical win
|
||||
for (let row = 0; row < 3; row++) {
|
||||
for (let col = 0; col < 7; col++) {
|
||||
const cell1 = getComputedStyle(document.getElementById(`td${row}${col}`)).backgroundColor;
|
||||
const cell2 = getComputedStyle(document.getElementById(`td${row + 1}${col}`)).backgroundColor;
|
||||
const cell3 = getComputedStyle(document.getElementById(`td${row + 2}${col}`)).backgroundColor;
|
||||
const cell4 = getComputedStyle(document.getElementById(`td${row + 3}${col}`)).backgroundColor;
|
||||
if (cell1 === cell2 && cell2 === cell3 && cell3 === cell4 && cell1 !== "rgb(255, 255, 255)") {
|
||||
// Display winning message
|
||||
alert(`Player ${cell1} wins!`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for diagonal win (top left to bottom right)
|
||||
for (let row = 0; row < 3; row++) {
|
||||
for (let col = 0; col < 4; col++) {
|
||||
const cell1 = getComputedStyle(document.getElementById(`td${row}${col}`)).backgroundColor;
|
||||
const cell2 = getComputedStyle(document.getElementById(`td${row + 1}${col + 1}`)).backgroundColor;
|
||||
const cell3 = getComputedStyle(document.getElementById(`td${row + 2}${col + 2}`)).backgroundColor;
|
||||
const cell4 = getComputedStyle(document.getElementById(`td${row + 3}${col + 3}`)).backgroundColor;
|
||||
if (cell1 === cell2 && cell2 === cell3 && cell3 === cell4 && cell1 !== "rgb(255, 255, 255)") {
|
||||
// Display winning message
|
||||
alert(`Player ${cell1} wins!`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for diagonal win (top right to bottom left)
|
||||
for (let row = 0; row < 3; row++) {
|
||||
for (let col = 3; col < 7; col++) {
|
||||
const cell1 = getComputedStyle(document.getElementById(`td${row}${col}`)).backgroundColor;
|
||||
const cell2 = getComputedStyle(document.getElementById(`td${row + 1}${col - 1}`)).backgroundColor;
|
||||
const cell3 = getComputedStyle(document.getElementById(`td${row + 2}${col - 2}`)).backgroundColor;
|
||||
const cell4 = getComputedStyle(document.getElementById(`td${row + 3}${col - 3}`)).backgroundColor;
|
||||
if (cell1 === cell2 && cell2 === cell3 && cell3 === cell4 && cell1 !== "rgb(255, 255, 255)") {
|
||||
// Display winning message
|
||||
alert(`Player ${cell1} wins!`);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (moves > 42){
|
||||
alert("it's a tie!")
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//il faudrait que je mette un autre message d'érreur que leode rgb de la couleur.
|
Loading…
Reference in a new issue