Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669

    Gioco degli Scacchi in puro JavaScript

    Ciao a tutt*,
    con questo topic vorrei invitarvi a postare i migliori programmi di scacchi scritti JavaScript. Io ho provato a cercarne un po' in giro, ma sono tutti abbastanza deludenti.
    Ho avuto però un'unica buona sorpresa. Si chiama Toledo JavaScript Chess Game, ed è la traduzione in puro JavaScript fatta dall'autore stesso del più piccolo e potente – in proporzione alle dimensioni – motore di scacchi che sia mai stato scritto in linguaggio C. La versione scritta in C ha vinto il premio alla diciottesima edizione dell'IOCCC (concorso dei più piccoli e potenti programmi scritti in codice offuscato – cioè fortemente compresso) come miglior gioco. L'autore ne ha anche scritto una versione in C ancora più ridotta, di soli 1257 caratteri.

    La versione scritta in JavaScript la potete trovare, in codice compresso, qui (versione grafica) e qui (versione testuale). Le dimensioni microscopiche e la potenza del calcolo fanno davvero impressione.

    Questo è quello che dichiara l'autore sul suo blog a proposito della traduzione dal linguaggio C:
    Around January 2007 I remembered the similarities between C and Javascript, and I was wondering if it would be possible to do a translation of my recent winning entry (19th IOCCC, Most Portable Chess Set), I did it on an afternoon and the result is the current world's smallest chess program in Javascript.
    Sperando di fare cosa gradita, posto di seguito il codice decompresso e reso leggibile della versione che non fa uso di immagini (versione solo testo):

    codice:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Toledo JavaScript Chess Game</title>
    </head>
    
    <body>
    <script type="text/javascript">
    // (c)2009 Oscar Toledo G.
    var B, i, y, u, b, I = [], G = 120, x = 10, z = 15, M = 1e4, l = [5, 3, 4, 6, 2, 4, 3, 5, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 13, 11, 12, 14, 10, 12, 11, 13, 0, 99, 0, 306, 297, 495, 846, - 1, 0, 1, 2, 2, 1, 0, - 1, - 1, 1, - 10, 10, - 11, - 9, 9, 11, 10, 20, - 9, - 11, - 10, - 20, - 21, - 19, - 12, - 8, 8, 12, 19, 21];
    
    function X(w, c, h, e, S, s) {
    	var t, o, L, E, d, O = e, N =- M * M, K = 78 - h << x, p, g, n, m, A, q, r, C, J, a = y ?- x : x;
    	y ^= 8;
    	G++;
    	d = w || s && s >= h && X(0, 0, 0, 21, 0, 0) > M;
    	do {
    		if (o = I[p = O]) {
    			q = o & z^y;
    			if (q < 7) {
    				A = q--& 2 ? 8 : 4;
    				C = o - 9 & z ? [53, 47, 61, 51, 47, 47][q] : 57;
    				do {
    					r = I[p += l[C]];
    					if (!w | p == w) {
    						g = q | p + a - S ? 0 : S;
    						if (!r & (!!q | A < 3 ||!!g) || (r + 1 & z^y) > 9 && q | A > 2) {
    							if (m =!(r - 2 & 7)) { return(y ^= 8, I[G--] = O, K); }
    							J = n = o & z;
    							E = I[p - a] & z;
    							t = q | E - 7 ? n : (n += 2, 6^y);
    							while (n <= t) {
    								L = r ? l[r & 7 | 32] - h - q : 0;
    								if (s) { L += (1 - q ? l[(p - p % x) / x + 37] - l[(O - O % x) / x + 37] + l[p % x + 38] * (q ? 1 : 2) - l[O % x + 38] + (o & 16) / 2 : !!m * 9) + (!q?!(I[p - 1]^n) + !(I[p + 1]^n) + l[n & 7 | 32] - 99 +!!g * 99 + (A < 2) : 0) +!(E^y^9); }
    								if (s > h || 1 < s & s == h && L > z | d) {
    									I[p] = n, I[O] = m ? (I[g] = I[m], I[m] = 0) : g ? I[g] = 0 : 0;
    									L -= X(s > h | d ? 0 : p, L - N, h + 1, I[G + 1], J = q | A > 1 ? 0 : p, s);
    									if (!(h || s - 1 | B - O | i - n | p - b | L <- M)) { return(G--, u = J); }
    									J = q - 1 | A < 7 || m ||!s | d | r | o < z || X(0, 0, 0, 21, 0, 0) > M;
    									I[O] = o;
    									I[p] = r;
    									m ? (I[m] = I[g], I[g] = 0) : g ? I[g] = 9^y : 0;
    								}
    								if (L > N) {
    									I[G] = O;
    									if (s > 1) {
    										if (h && c - L < 0) { return(y ^= 8, G--, L); }
    										if (!h) { i = n, B = O, b = p; }
    									}
    									N = L;
    								}
    								n += J || (g = p, m = p < O ? g - 3 : g + 2, I[m] < z | I[m + O - p] || I[p += p - O]) ? 1 : 0;
    							}
    						}
    					}
    				}
    				while (!r & q > 2 || (p = O, q | A > 2 | o > z &!r &&++C *--A));
    			}
    		}
    	}
    	while (++O > 98 ? O = 20 : e - O);
    	return(y ^= 8, G--, N + M * M && N >- K + 1924 | d ? N : 0);
    }
    
    B = i = y = u = 0;
    while (B++< 120) { I[B - 1] = B % x ? B / x % x < 2 | B % x < 2 ? 7 : B / x & 4 ? 0 : l[i++] | 16 : 7; }
    for (a = "<table align=center border=0>", B = 0; B < 8; B++) { for (a += "<tr>", i = 21; i < 29; i++) { a += "<td width=40 height=40 onclick=Y(" + (B * x + i) + ") id=o" + (B * x + i) + " style='border:2px solid transparent' bgcolor=#" + (i + B & 1 ? "c0c0ff" : "9090d0") + " align=center>"; } }
    a += "<tr><td colspan=8 align=center><select id=t><option>Q<option>R<option>B";
    document.write(a + "<option>N</select></table>");
    
    function W() {
    	for (p = 21; p < 99; p += p % x - 8 ? 1 : 3) { document.getElementById("o" + p).innerHTML = " pknbrq  PKNBRQ".charAt(I[p] & z); }
    	B = 0;
    }
    
    W();
    
    function Z(s, y) { document.getElementById("o" + s).style.borderColor = y ? "yellow" : "transparent"; }
    
    function Y(s) {
    	i = (I[s]^y) & z;
    	if (i > 8) {
    		if (B) { Z(B, 0); }
    		Z(B = s, 1);
    	}
    	else if (B && i < 9) {
    		Z(b = s, 1);
    		i = I[B] & z;
    		if ((i & 7) == 1 & (b < 29 | b > 90)) { i = 14 - document.getElementById("t").selectedIndex^y; }
    		X(0, 0, 0, 21, u, 1);
    		Z(B, 0);
    		Z(b, 0);
    		W();
    		if (y) { window.setTimeout("X(0,0,0,21,u,2/*ply*/),X(0,0,0,21,u,1),W()", 250); }
    	}
    }
    </script>
    
    
    
    If you want to play with black's pieces, click this
    <input type="button" value="button" onclick="setTimeout('X(0,0,0,21,u,2),X(0,0,0,21,u,1),W()',250);">
    to force a move. You will need to click every time after you do a move.</p>
    
    </body>
    </html>
    Se qualcun* ne conosce di altri si faccia pure avanti!

    P.S.: non spaventatevi per la mancanza della grafica... è un attimo renderlo very cool.

  2. #2
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Per gli amministratori della sezione scripts del sito html.it: non pensate valga la pena di aggiungerlo tra gli script di esempio, sotto la sezione “giochi”?

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    Originariamente inviato da carlomarx
    Per gli amministratori della sezione scripts del sito html.it
    puoi segnalarlo direttamente a
    redazione chiocciola html punto it

    ciao

  4. #4
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Lo dicevo io che non ci voleva niente a renderlo very cool...

    codice:
    <!doctype html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Toledo JavaScript Chess Game</title>
    <style type="text/css">
    #chessboard {
    	border-collapse: collapse;
    	width: 600px;
    	height: 600px;
    	border: 0;
    	margin-left: auto;
    	margin-right: auto;
    
    }
    #chessboard tr td.blackCell, #chessboard tr td.whiteCell {
    	width: 11%;
    	height: 11%;
    	font-size: 52px;
    	line-height: 42px;
    	text-align: center;
    	font-family: Times New Roman;
    }
    #chessboard tr th.topHeader {
    	width: 11%;
    	height: 6%;
    }
    #chessboard tr th.leftHeader {
    	width: 6%;
    	height: 11%;
    }
    .blackCell {
    	background-color: #cccccc;
    }
    #angle, #promotion {
    	height: 6%;
    }
    #promotion {
    	padding-left: 24px;
    }
    #angle, #rightBorder {
    	width: 6%;
    }
    #chessboard tr th, .angle, #rightBorder, #promotion{
    	overflow: visible;
    	background-color: #ce5c00;
    	color: #f9ac6d;
    	font-family: Arial;
    	font-size: 100%;
    }
    </style>
    </head>
    
    <body>
    <script type="text/javascript">
    // (c)2009 Oscar Toledo G.
    var B, i, y, u, b, I = [], G = 120, x = 10, z = 15, M = 1e4, l = [5, 3, 4, 6, 2, 4, 3, 5, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 13, 11, 12, 14, 10, 12, 11, 13, 0, 99, 0, 306, 297, 495, 846, - 1, 0, 1, 2, 2, 1, 0, - 1, - 1, 1, - 10, 10, - 11, - 9, 9, 11, 10, 20, - 9, - 11, - 10, - 20, - 21, - 19, - 12, - 8, 8, 12, 19, 21];
    
    function X(w, c, h, e, S, s) {
    	var t, o, L, E, d, O = e, N =- M * M, K = 78 - h << x, p, g, n, m, A, q, r, C, J, a = y ?- x : x;
    	y ^= 8;
    	G++;
    	d = w || s && s >= h && X(0, 0, 0, 21, 0, 0) > M;
    	do {
    		if (o = I[p = O]) {
    			q = o & z^y;
    			if (q < 7) {
    				A = q--& 2 ? 8 : 4;
    				C = o - 9 & z ? [53, 47, 61, 51, 47, 47][q] : 57;
    				do {
    					r = I[p += l[C]];
    					if (!w | p === w) {
    						g = q | p + a - S ? 0 : S;
    						if (!r & (!!q | A < 3 ||!!g) || (r + 1 & z^y) > 9 && q | A > 2) {
    							if (m =!(r - 2 & 7)) { return(y ^= 8, I[G--] = O, K); }
    							J = n = o & z;
    							E = I[p - a] & z;
    							t = q | E - 7 ? n : (n += 2, 6^y);
    							while (n <= t) {
    								L = r ? l[r & 7 | 32] - h - q : 0;
    								if (s) { L += (1 - q ? l[(p - p % x) / x + 37] - l[(O - O % x) / x + 37] + l[p % x + 38] * (q ? 1 : 2) - l[O % x + 38] + (o & 16) / 2 : !!m * 9) + (!q?!(I[p - 1]^n) + !(I[p + 1]^n) + l[n & 7 | 32] - 99 + !!g * 99 + (A < 2) : 0) + !(E^y^9); }
    								if (s > h || 1 < s & s === h && L > z | d) {
    									I[p] = n, I[O] = m ? (I[g] = I[m], I[m] = 0) : g ? I[g] = 0 : 0;
    									L -= X(s > h | d ? 0 : p, L - N, h + 1, I[G + 1], J = q | A > 1 ? 0 : p, s);
    									if (!(h || s - 1 | B - O | i - n | p - b | L <- M)) { return(G--, u = J); }
    									J = q - 1 | A < 7 || m ||!s | d | r | o < z || X(0, 0, 0, 21, 0, 0) > M;
    									I[O] = o;
    									I[p] = r;
    									m ? (I[m] = I[g], I[g] = 0) : g ? I[g] = 9^y : 0;
    								}
    								if (L > N) {
    									I[G] = O;
    									if (s > 1) {
    										if (h && c - L < 0) { return(y ^= 8, G--, L); }
    										if (!h) { i = n, B = O, b = p; }
    									}
    									N = L;
    								}
    								n += J || (g = p, m = p < O ? g - 3 : g + 2, I[m] < z | I[m + O - p] || I[p += p - O]) ? 1 : 0;
    							}
    						}
    					}
    				}
    				while (!r & q > 2 || (p = O, q | A > 2 | o > z & !r && ++C * --A));
    			}
    		}
    	}
    	while (++O > 98 ? O = 20 : e - O);
    	return(y ^= 8, G--, N + M * M && N >- K + 1924 | d ? N : 0);
    }
    
    B = i = y = u = 0;
    while (B++ < 120) { I[B - 1] = B % x ? B / x % x < 2 | B % x < 2 ? 7 : B / x & 4 ? 0 : l[i++] | 16 : 7; }
    a = "<table id=\"chessboard\"><tbody><tr><td class=\"angle\"><\/td><th class=\"topHeader\">a<\/th><th class=\"topHeader\">b<\/th><th class=\"topHeader\">c<\/th><th class=\"topHeader\">d<\/th><th class=\"topHeader\">e<\/th><th class=\"topHeader\">f<\/th><th class=\"topHeader\">g<\/th><th class=\"topHeader\">h<\/th><td id=\"rightBorder\" rowspan=\"9\"><\/th><\/tr>";
    for (B = 0; B < 8; B++) { for (a += "<tr><th class=\"leftHeader\">" + String(B + 1) + "<\/th>", i = 21; i < 29; i++) { a += "<td onclick=Y(" + (B * x + i) + ") id=o" + (B * x + i) + " class=\"" + (i + B & 1 ? "whiteCell" : "blackCell") + "\">"; } }
    a += "<tr><td id=\"promotion\" colspan=\"9\">Promote to: <select id=\"t\"><option value=\"Q\">Queen<\/option><option value=\"R\">Rook<\/option><option value=\"B\">Bishop<\/option><option value=\"N\">Knight<\/option><\/select><\/td><td class=\"angle\"> <\/td><\/tr><\/tbody><\/table>"
    
    document.write(a);
    
    function W() {
    	var pieceId;
    	for (p = 21; p < 99; p += p % x - 8 ? 1 : 3) { pieceId = (I[p] & z) - 1; document.getElementById("o" + p).innerHTML = I[p] === 0 ? "" : "&#98" + "231822212019171216151413".substr((parseInt(pieceId/8)*6+pieceId%8)*2, 2) + ";"; }
    	B = 0;
    }
    
    W();
    
    function Z(s, y) { document.getElementById("o" + s).style.backgroundColor = y ? "#4cff4c" : ""; }
    
    function Y(s) {
    	i = (I[s]^y) & z;
    	if (i > 8) {
    		if (B) { Z(B, 0); }
    		Z(B = s, 1);
    	}
    	else if (B && i < 9) {
    		Z(b = s, 1);
    		i = I[B] & z;
    		if ((i & 7) === 1 & (b < 29 | b > 90)) { i = 14 - document.getElementById("t").selectedIndex^y; }
    		X(0, 0, 0, 21, u, 1);
    		Z(B, 0);
    		Z(b, 0);
    		W();
    		if (y) { window.setTimeout("X(0,0,0,21,u,2/*ply*/),X(0,0,0,21,u,1),W()", 250); }
    	}
    }
    </script>
    
    
    
    If you want to play with black's pieces, click this
    <input type="button" value="button" onclick="window.setTimeout('X(0,0,0,21,u,2),X(0,0,0,21,u,1),W()',250);">
    to force a move. You will need to click every time after you do a move.</p>
    </body>
    </html>

  5. #5
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Originariamente inviato da Xinod
    puoi segnalarlo direttamente a
    redazione chiocciola html punto it

    ciao
    grazie, lo farò senz'altro

  6. #6
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    ciao a tutt*,
    son passati un paio di mesi da quando ho aperto questa discussione... Nel frattempo ho scoperto che un tipo aveva realizzato una fantastica scacchiera tridimensionale utilizzando l'elemento canvas (la potete trovare qui: http://www.nihilogic.dk/labs/chess/). Purtroppo era piena di bugs, la posizione dei pezzi non era totalmente a posto (potete vederlo da voi) e soprattutto si trattava di pura grafica, non era cioè in grado di calcolare alcun che...
    Allora mi sono chiesto: perché non farla interagire con lo script che avevo postato? Mi sono messo di buzzo buono e questo è quello che è venuto fuori:

    http://htmlchess.sourceforge.net/demo/example.html

    ...purtroppo IE non supporta la grafica tridimensionale quindi non usate quel browser per vedere di che si tratta....

    P.S.: la scacchiera 3D consente di fare lo zoom con i tasti + e - a destra della tastiera, e di utilizzare le i numeri del numpad per muoversi.

    Che ne pensate??? Mi date un parere?

    Grazie mille.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.