Le compte est bon

Description

Intégrée à une simple page html, cette version javascript, permet de s'adonner au jeu télévisé bien connu, sans aucun logiciel ni applet.
Fonctionne sur tous les navigateurs récents.

Source / Exemple :


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="fr">
<head>
	<title>Le compte est bon</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
body,input,select {font-family:georgia;font-size:9pt;color:#000066}
body {margin:0px;padding:20px}
h1 {margin:13px;padding:0;margin-top:0;font-size:20pt;text-align:center;font-weight:bold;color:#990099}
hr{width:300px}
div{margin:0;padding:0;border:0}
p {margin:7px;padding:0;text-align:center}
input {text-align:center;border:1px solid gray}
#jeu{margin-bottom:17px}
#jeu p {text-align:center}
#jeu span{font-size:9pt;font-weight:normal}
#idc {width:100px;margin:13px auto;padding:3px 3px 7px;font-size:36px;background-color:#cccccc}
#ipl {width:436px;margin:0 auto;border:0px solid red}
#ipl input {display:block;float:left;width:44px;margin:0 10px;padding:3px 3px 5px;font-size:24px;background-color:#eeeeee}
#mdp {clear:both;margin-top:60px}
#idt,#isl {font-size:18px;font-weight:bold}
a {color:#990099}
#pdp{text-align:center}
-->
</style>
<script type="text/javascript">
<!--
function $(i){return document.getElementById(i)}
var _p=[1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,25,50,75,100];
var _c,_t=new Array(6),_m,_e={"which":13,"target":null};
var Tgn={
	c:null,// valeur cible initiale
	b:null,// valeur cible bis (initiale ou modifiée)
	n:null,// nombre de plaques valides
	m:null,// message compte, approximation ou erreur
	r:null,// réponse littérale
	s:null,// spectre des valeurs
	t:new Array(42),// tableau des plaques selon les niveaux
	z:null,// temps de calcul

	sol:function(c,p,mode){var i,j,k,o=[],q=[],l=p.length,x;
		this.z=-(new Date().getTime());
	  if (c=='') c=0;c=parseInt(c);
		if (typeof(c)!='number' || c<1) {this.m='Valeur cible erronée';
			this.r='<br>toutes les valeurs entières positives sont ici admises !';this.z=0;return}
	  this.c=parseInt(c);this.b=this.c;this.s={};
	  j=0;for (i=0;i<l;i++) {if (isNaN(k=parseInt(p[i]))) continue;q[j++]=k;}
    if (j<3) {this.m='Nombre de plaques insuffisant';
			this.r='<br>trois plaques vierges au plus sont acceptées !';this.z=0;return}
    this.m='Le compte est bon';
	  // Classement décroissant 
	  this.n=j;for (i=0;i<j;i++) {k=i;while(0<k && o[k-1]<q[i]) o[k]=o[--k];o[k]=q[i];}
	  // Enregitrement plaques ordonnées (le max dégrade o) et test si une plaque convient
		for(i=0;i<j;i++) {Tgn.t[6*j+i]=o[i];if (this.c==o[i]) {this.r='<br>'+o[i];this.z=0;return}}
		// Calcul du max modifié 2010.09.19
		x=1;
		while (o[k=j-1]==1) if (5<j && o[j-2]==1 && o[j-3]==1 && o[j-4]==2 && o[j-5]==2 && o[j-6]==2) {x=27;j=j-6;}
			else if (3<j && o[j-2]==1 && o[j-3]==2 && o[j-4]==2) {x=9;j=j-4;}
			else {v=o[--k]+1;while(0<k && o[k-1]<v) o[k]=o[k--];o[k]=v;j=j-1;}
		k=j-1;while(-1<k) x*=o[k--];
		
		// Cible modifiée si supérieure à max
		if (x<this.c) {this.b=x;this.m=''+x;}
    while (!this.dcp(this.n)) {
   		i=this.b+1;while(typeof(this.s[i])=='undefined') i++;
  		j=this.b-1;while(0<j && typeof(this.s[j])=='undefined') j--;
  		if ((this.b-j)<=(i-this.b)) this.b=j;else this.b=i;
  		this.m=''+this.b;this.s={};}
    	this.z+=new Date().getTime();
		},
  // Modifié 2010.09.19 plus de tri (mais un test initial) pour ne reprendre qu'une fois les valeurs
	// Modifié le 2010.09.21 seules les opérations utiles sont reprises
	dcp:function(a){var i,j,k,v=(a<<3)-a,u=v-a,x,y,w;
  	for (i=u;i<v;i++)	for (j=u;j<v;j++){
		  if (i==j || (x=this.t[i])<(y=this.t[j])) continue;

		  // addition
		  if ((w=x+y)==this.b) {this.r='<br> '+x+' + '+y+' = '+w;return 1;}
			// seule définition des plaques à reprendre
			k=u-5;for (var l=u;l<v;l++) {if (l==i || l==j) continue;
        this.t[k++]=this.t[l];}
      k=u-6;
			this.s[w]=1;this.t[k]=w;
			if (this.dcp(a-1)==1) {
				if (-1<this.r.search(new RegExp(' '+w+' '))) this.r='<br> '+this.t[i]+' + '+this.t[j]+' = '+w+this.r;
				return 1;}
			// soustraction
			if (x!=y && x!=y<<1) {
		  if ((w=x-y)==this.b) {this.r='<br> '+x+' - '+y+' = '+w;return 1;}
     	this.s[w]=1;this.t[k]=w;
			if (this.dcp(a-1)==1){
				if (-1<this.r.search(new RegExp(' '+w+' ')))	this.r='<br> '+this.t[i]+' - '+this.t[j]+' = '+w+this.r;
				return 1;}}
			// multiplication
			if (1<x && 1<y) {
		  if ((w=x*y)==this.b) {this.r='<br> '+x+' x '+y+' = '+w;return 1;}
     	this.s[w]=1;this.t[k]=w;
			if (this.dcp(a-1)==1){
				if (-1<this.r.search(new RegExp(' '+w+' ')))	this.r='<br> '+this.t[i]+' x '+this.t[j]+' = '+w+this.r;
				return 1;}}
			// division
			if (1<y && x%y==0 && x!=y*y) {
		  if ((w=x/y)==this.b) {this.r='<br> '+x+' / '+y+' = '+w;return 1;}
     	this.s[w]=1;this.t[k]=w;
			if (this.dcp(a-1)==1){
				if (-1<this.r.search(new RegExp(' '+w+' '))) this.r='<br> '+this.t[i]+' / '+this.t[j]+' = '+w+this.r;
				return 1;}}
	}
	return 0}
};

function ini(){var i,j,p={},t;
	do {_c=100+Math.floor(900*Math.random())} while (_c==1000);
  for (i=0;i<6;i++) {
    do {j=Math.floor(24*Math.random())} while (j==24 || typeof(p[j])=='number');
		_t[i]=_p[j];p[j]=1;}
	$('idc').value=_c;$('isl').innerHTML='';
	t="";i=0;while(i<6) t+='<input id="idp'+i+'" type="text" value="'+_t[i++]+'">';
	$('ipl').innerHTML=t;
	setTimeout('sol()',7);
}
function ntr(e){var o=!e?window.event.keyCode:e.which,t=!e?window.event.srcElement:e.target;
	if (t.id.substr(0,2)=='id' && t.value!='' && (t.value==0 || isNaN(t.value))) {t.value='';$('isl').innerHTML='';return}
	if (t.id.substr(0,2)=='id' && t.value<0) {t.value=-t.value;$('isl').innerHTML='';return}
	if (o!=13 || t.id.substr(0,2)!='id') return true;
	_c=$('idc').value;
	for (i=0;i<6;i++) _t[i]=$('idp'+i).value;
	$('isl').innerHTML='';setTimeout('sol()',7);
}
function sol(){
	Tgn.sol(_c,_t);_m='<a id="idt" href="javascript:edt()" title="En savoir plus...">'+Tgn.m+' !</a>';
	$('isl').innerHTML=_m;
}
function edt(){var t=(Tgn.z<2)?'<span>immédiat</span>':'<span>(en '+Tgn.z.toString()+' millisecondes)</span>';
	$('isl').innerHTML=Tgn.m+' '+t+Tgn.r;
}
document.onkeyup=ntr;
// -->
</script>
</head>
<body onload="ini()">
	<div><h1>Le compte est bon</h1>
	  <p>Le <a href="http://programmes.france3.fr/des-chiffres-et-des-lettres/23806420-fr.php" title="Voir Des chiffres et des lettres sur France3">jeu</a> consiste à obtenir ou approcher un nombre de trois chiffres<br>
		au moyen des opérations élémentaires appliquées au plus à six nombres entiers</p>
		<div id="jeu"><p><input id="idc" type="text" value=""></p><p id="ipl"></p>
		<p id="mdp"><a href="javascript:(_e.target=$('idc'),ntr(_e))" title="Lancer la résolution du jeu affiché">Ok</a> ou <b><i>Entrée</i></b> après modifications des valeurs ou <a href="javascript:ini()" title="Nouveau jeu">tirage au sort</a></p>
  	<p id="isl"></p>
  	</div>
	</div>

	<div id="pdp">L&#65533;utilisation de FireFox, Google Chrome, Opera ou Safari permet d&#65533;éviter les avertissements d&#65533;Internet Explorer
	<hr><i>Page entoilée par Julien de Prabère - septembre 2010</i></div>
</body>
</html>

Conclusion :


Qui saura améliorer ce script pour trouver non pas la première mais la meilleure solution ?

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.