-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
79 lines (73 loc) · 1.98 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const validerButton = document.querySelector("#button")
const champNombre = document.querySelector("#champ")
let player2Turn = false
let player2Number;
let player1Number;
let numberOfTries = 0
let minimalNumber = 0
let maximalNumber = 50
validerButton.addEventListener("click", () => {
gameplay()
})
function gameplay(){
console.log(player2Turn)
if (player2Turn){
console.log("P2 turn")
player2Number = champNombre.value
console.log("P2= " + player2Number)
didIWin(player2Number, player1Number)
}
else{
console.log("P1 turn")
player1Number = champNombre.value
console.log("P1= " + player1Number)
if (checkRange(player1Number)){
player2Turn = true
}
else{
alert("Entre 0 et 50 idiot!!!")
}
}
}
function didIWin(myNumber,secretNumber){
numberOfTries += 1
if (myNumber == secretNumber){
console.log("You Win")
draw(true)
return true
}
else{
if (myNumber < secretNumber){
console.log("Ton nombre est plus petit!")
if (minimalNumber < myNumber){
minimalNumber = myNumber
}
}
else{
console.log("Ton nombre est plus grand!")
if (maximalNumber > myNumber){
maximalNumber = myNumber
}
}
draw(false)
return false
}
}
function checkRange(myNumber){
if (myNumber < 0 || myNumber > 50){
return false
}
else{
return true
}
}
function draw(gameOver){
if (gameOver){
document.querySelector("h1").innerText = "Bravo tu as gagné, tu as trouvé le nombre: " + player1Number
document.querySelector("p").innerText = "Nombre d'essais: " + numberOfTries
}
else{
document.querySelector("p").innerText = "Nombre d'essais: " + numberOfTries
document.querySelector("h1").innerText = minimalNumber + " < ? < " + maximalNumber
}
}