Created
November 26, 2015 13:35
-
-
Save Swatto/cb13c81705ffde72a6b8 to your computer and use it in GitHub Desktop.
Jeu pierre-feuille-ciseau avec une bonne structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Jeu = function() { | |
var joueur_score = 0; | |
var ordinateur_score = 0; | |
this.initialize = function() { | |
update_score(); | |
document.querySelector('form').addEventListener('submit', fight); | |
}; | |
var fight = function(ev) { | |
ev.preventDefault(); | |
var joueur_coup = parseInt(this.elements.coup.value); | |
var ordinateur_coup = Math.floor((Math.random()*3)+1); | |
if(joueur_coup == ordinateur_coup) { | |
return; | |
} | |
if(joueur_coup > ordinateur_coup || (joueur_coup == 1 && ordinateur_coup == 3)) { | |
joueur_score++; | |
} else { | |
ordinateur_score++; | |
} | |
update_score(); | |
}; | |
var update_score = function() { | |
document.querySelector('#td_joueur').innerHTML = joueur_score; | |
document.querySelector('#td_ordinateur').innerHTML = ordinateur_score; | |
if(joueur_score >= 5 || ordinateur_score >= 5) { | |
final_action(); | |
} | |
}; | |
var final_action = function() { | |
if(joueur_score > ordinateur_score) { | |
alert('Vous avez gagné!'); | |
} else { | |
alert('Vous avez perdu...'); | |
} | |
joueur_score = ordinateur_score = 0; | |
update_score(); | |
}; | |
return this; | |
}; | |
new Jeu().initialize(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title>Ex16</title> | |
</head> | |
<body> | |
<table border="1"> | |
<tr> | |
<td width="50%">Joueur</td> | |
<td width="50%" id="td_joueur"></td> | |
</tr> | |
<tr> | |
<td>Ordinateur</td> | |
<td id="td_ordinateur"></td> | |
</tr> | |
</table> | |
<hr> | |
<form> | |
<label for="coup">Sélectionner un coup</label> | |
<select name="coup"> | |
<option value="1">Pierre</option> | |
<option value="2">Papier</option> | |
<option value="3">Ciseaux</option> | |
</select> | |
<input type="submit" value="Combat!"> | |
</form> | |
<script src="app.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment