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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| "use strict";
// -----------------
// JEU : Devinez le nombre
// -----------------
// Définition de l objet
var GuessTheNumber = {
// -----------------
// Initialisation
game_Init:function()
{
var thisGame = this;
// ---------
this.numberToFind_Max = 100;
// ---------
// Nombre d essais
this.nbre_Essais_Max = 10;
// ---------
this.html_Generate();
// ---------
this.game_Start();
// ---------
},
// -----------------
// Start / ou Replay
game_Start:function()
{
// ---------
// Le nombre à trouver
this.numberToFind = Math.floor(Math.random()*(this.numberToFind_Max+1));
// ---------
// Probabilité
this.Proba_Min = 1;
this.Proba_Max = this.numberToFind_Max;
// ---------
this.You_Win = false;
this.nbre_Essais_Count = 0;
// ---------
this.html_Msg_Error.textContent = "";
this.html_Show_Proba.textContent = "";
this.html_Show_Result.textContent = "";
// ---------
this.show_Probabilite();
// ---------
this.html_Button_Replay.style.display = "none";
// ---------
},
// -----------------
// HTML - GENERER le code HTML
html_Generate:function()
{
var thisGame = this;
var elt, div;
// ---------
// <div id="GuessTheNumber_TheGame"></div> : à mettre dans le HTML au départ
this.html_Frame = document.querySelector('#GuessTheNumber_TheGame');
// ---------
// TITRE
elt = document.createElement('h4');
elt.textContent = "Trouvez le nombre entre 1 et "+this.numberToFind_Max+" : ";
this.html_Frame.append( elt );
// ---------
// INPUT CHOIX + BUTTON PLAY
div = document.createElement('form');
this.html_Frame.append( div );
// ----
elt = document.createElement('label');
elt.textContent = "Votre choix : ";
div.append( elt );
// ----
elt = document.createElement('input');
elt.id = 'GuessTheNumber-Input-Choice';
elt.type = 'number';
elt.min = 1;
elt.max = this.numberToFind_Max;
elt.value = '';
div.append( elt );
this.html_Input_Choice = document.querySelector('#'+elt.id);
// ----
elt = document.createElement('button');
elt.id = 'GuessTheNumber-Play';
elt.type = 'submit';
elt.textContent = "OK";
div.append( elt );
this.html_Button_Play = document.querySelector('#'+elt.id);
// ----
elt = document.createElement('span');
elt.id = 'GuessTheNumber-Error';
div.append( elt );
this.html_Msg_Error = document.querySelector('#'+elt.id);
// ---------
// PROBABILITE
div = document.createElement('p');
this.html_Frame.append( div );
// ----
elt = document.createElement('label');
elt.textContent = "Probabilité : ";
div.append( elt );
// ----
elt = document.createElement('span');
elt.id = 'GuessTheNumber-Proba';
div.append( elt );
this.html_Show_Proba = document.querySelector('#'+elt.id);
// ---------
// RESULTATS
div = document.createElement('div');
this.html_Frame.append( div );
// ----
elt = document.createElement('label');
elt.innerHTML = "Résultats : "+"<em>(vous avez droit à "+this.nbre_Essais_Max+" essais)</em>";
div.append( elt );
// ----
elt = document.createElement('ul');
elt.id = 'GuessTheNumber-Result';
div.append( elt );
this.html_Show_Result = document.querySelector('#'+elt.id);
// ---------
// BUTTON REPLAY
div = document.createElement('p');
this.html_Frame.append( div );
// ----
elt = document.createElement('button');
elt.id = 'GuessTheNumber-Replay';
elt.textContent = "Rejouer une partie ?";
div.append( elt );
this.html_Button_Replay = document.querySelector('#'+elt.id);
// ---------
// ACTIONS
this.html_Action_Load();
// ---------
},
// -------------------
// ACTIONS
html_Action_Load:function()
{
var thisGame = this;
// ---------
// PLAY
this.html_Button_Play.addEventListener('click', function(event) {
event.preventDefault();
// ---------
var val_user = Number(thisGame.html_Input_Choice.value);
thisGame.html_Input_Choice.value = "";
thisGame.html_Msg_Error.textContent = "";
// ---------
if( val_user < thisGame.Proba_Min || val_user > thisGame.Proba_Max )
{
thisGame.html_Msg_Error.textContent = " Choix incorrect";
} else {
if( !thisGame.You_Win ) {
// -----
thisGame.nbre_Essais_Count++;
// -----
if( !thisGame.You_Win && thisGame.nbre_Essais_Count > thisGame.nbre_Essais_Max )
{
thisGame.append_Result( "Vous avez épuisé vos "+thisGame.nbre_Essais_Max+" essais. <b>Vous avez perdu !</b>" );
// -----
thisGame.html_Button_Replay.style.display = "block";
} else {
// -----
if( val_user < thisGame.numberToFind )
{
thisGame.Proba_Min = val_user;
thisGame.append_Result( "Essai n°"+thisGame.nbre_Essais_Count+" : "+val_user+" est trop petit" );
thisGame.show_Probabilite();
} else if ( val_user > thisGame.numberToFind )
{
thisGame.Proba_Max = val_user;
thisGame.append_Result( "Essai n°"+thisGame.nbre_Essais_Count+" : "+val_user+" est trop grand" );
thisGame.show_Probabilite();
} else {
thisGame.You_Win = true;
thisGame.append_Result( "Essai n°"+thisGame.nbre_Essais_Count+" : <b>"+val_user+"</b> est bien le nombre à trouver. <b>Vous avez gagné !</b>" );
// -----
thisGame.html_Button_Replay.style.display = "block";
}
// -----
}
} else {
thisGame.html_Show_Result.textContent = "";
// -----
thisGame.html_Button_Replay.style.display = "block";
}
}
});
// ---------
// REPLAY
this.html_Button_Replay.addEventListener('click', function() {
thisGame.game_Start();
});
},
// -----------------
// RESULTAT
append_Result:function( msg )
{
var elt = document.createElement('li');
elt.innerHTML = msg;
this.html_Show_Result.append( elt );
},
// -----------------
// PROBABILITE
show_Probabilite:function()
{
this.html_Show_Proba.textContent = "Vous avez 1 chance sur "+(this.Proba_Max - this.Proba_Min + 1);
}
};
// -------------------
// au chargement de page
window.addEventListener('DOMContentLoaded', function(){
GuessTheNumber.game_Init();
});
// ------------------- |