Skip to content

Instantly share code, notes, and snippets.

@clarkwinkelmann
Created April 29, 2017 13:32
Show Gist options
  • Save clarkwinkelmann/88a738e600350421c3786049edc6f8ce to your computer and use it in GitHub Desktop.
Save clarkwinkelmann/88a738e600350421c3786049edc6f8ce to your computer and use it in GitHub Desktop.
Morpion
class Morpion{constructor(){this.map=[null,null,null,null,null,null,null,null,null],this.joueur="x"}ajouterSymbole(a){if(!this.joueurActuel())throw"partie finie";if(a<0||a>8)throw"index case invalide";const b=Math.round(a);if(this.contenuCase(b))throw"case déjà utilisée";this.map[b]=this.joueur,this.joueur="x"===this.joueur?"o":"x"}joueurActuel(){return this.gagnant()?null:this.map.reduce(function(a,b){return a&&!!b},!0)?null:this.joueur}gagnant(){for(let a=0;a<9;a+=3){const b=this.map[a]===this.map[a+1]&&this.map[a+1]===this.map[a+2]&&this.map[a];if(b)return b}for(let a=0;a<3;a++){const b=this.map[a]===this.map[a+3]&&this.map[a+3]===this.map[a+6]&&this.map[a];if(b)return b}for(let a=-1;a<=1;a+=2){const b=this.map[4]===this.map[1+a]&&this.map[4]===this.map[7-a]&&this.map[4];if(b)return b}return null}contenuCase(a){return this.map[a]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment