Skip to content

Instantly share code, notes, and snippets.

@padawin
Forked from 140bytes/LICENSE.txt
Last active December 17, 2015 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save padawin/5635620 to your computer and use it in GitHub Desktop.
Save padawin/5635620 to your computer and use it in GitHub Desktop.

Tic-Tac-Toe - 140byt.es

A simple version of the game Tic-Tac-Toe. It is a 2 human players version.

Adapted from My c version: https://github.com/padawin/Tic-tac-toe.

Try it.

Source

Not minified:

function p(a,board,playedcell,t)
{
	if (a >= 0 & a <9) {
		a = board | (playedcell = 1 << a);
		for (t = 7; ~t && (w[--t] & (c=c|playedcell)) ^ w[t];);
		return (a==511||a==board)?a:~t;
	}
}

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

Acknowledgments

Thanks to malko and adriengibrat for their tips and help during the minifying process.

function p(
f, // Selected cell
n, // Game board
r, // Played cell - placeholder
i // Increment - placeholder
){
if(f>=0&9>f){ // Continue only if the selected cell is
// between 0 and 9
for(
f=n|(r=1<<f), // r=1<<f: played cell's corresponding
// byte set to 1,
// n|(r=1<<f): board updated with the
// selected cell
i=7; // and i initialized
~i // Loop while
// ~i is not null (= while
// i > -1)
&&(w[--i]&(c|=r))^w[i]; // and the current player's game matches
// no combination to win
// c = current player, global variable
// w = array containing the combinations
// to win, global variable
); //
return 511==f||f==n?f:~i // Return the board if the board is full
// => draw
// or equal to the board before the turn
// => already played cell
// else return ~i (if ~i != 0, the
// player wins, else the game continues)
//
}
}
function p(f,n,r,i,w){w=[7,56,448,73,146,292,273,84];if(f>=0&9>f){for(f=n|(r=1<<f),i=7;~i&&(w[--i]&(c|=r))^w[i];);return 511==f||f==n?f:~i}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "TicTacToe",
"description": "Small implementation of the game Tic-tac-toe using bits operations.",
"keywords": [
"tie tac toe",
"bits"
]
}
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
td {
width: 70px;
height: 70px;
border: 1px black solid;
text-align: center;
}
</style>
</head>
<body>
<table id="game">
<tr>
<td id="cell8"></td>
<td id="cell7"></td>
<td id="cell6"></td>
</tr>
<tr>
<td id="cell5"></td>
<td id="cell4"></td>
<td id="cell3"></td>
</tr>
<tr>
<td id="cell2"></td>
<td id="cell1"></td>
<td id="cell0"></td>
</tr>
</table>
<script type="text/javascript">
var player1 = 0;
var player2 = 0;
var c = player1;
var result;
/**
Null => invalid input
board => Already played cell
511 => Draw
!= 0 => win
0 => Continue
*/
function p(f,n,r,i,w){w=[7,56,448,73,146,292,273,84];if(f>=0&9>f){for(f=n|(r=1<<f),i=7;~i&&(w[--i]&(c|=r))^w[i];);return 511==f||f==n?f:~i}}
function display()
{
var disp = 1 << 8;
var i = 8;
while (disp > 0) {
if ((disp & player1) == disp)
document.getElementById('cell' + i).innerHTML = 'X';
else if ((disp & player2) == disp)
document.getElementById('cell' + i).innerHTML = 'O';
else
document.getElementById('cell' + i).innerHTML = '';
disp = disp >> 1;
i--;
}
}
document.getElementById('game').onclick = function(e)
{
var target = e.target;
if (target.tagName != 'TD') {
return;
}
// Get typed value
var value = target.id.substr(4);
// Get the board
var board = player1|player2;
result = p(value,board);
if (result === null || result != 511 && result == (c|player1|player2)) {
return;
}
// Update player, because reference do not work for int in js
if (c == (player1 | (1 << value))) {
player1 = c;
c = player2;
}
else {
player2 = c;
c = player1;
}
// Refresh the grid
display();
// If the game is over, display the result
if (result == 511) {
document.getElementById('game').innerHTML = "Draw";
return;
}
else if (result != 0) {
document.getElementById('game').innerHTML = "And the winner is " + (c == player2 ? 'player1' : 'player2');
return;
}
}
</script>
</body>
</html>
@tsaniel
Copy link

tsaniel commented May 25, 2013

How about this?
function p(f,n,r,i){if(f>=0&9>f){for(f=n|(r=1<<f),i=7;~i&&(w[--i]&(c|=r))^w[i];);return 511==f||f==n?f:~i?3:4}}

@atk
Copy link

atk commented May 27, 2013

If we already have w, we can use it to shorten the initial comparison:

function p(f,n,r,i){if(f in w){for(f=n|(r=1<<f),i=7;~i&&(w[--i]&(c|=r))^w[i];);return 511==f||f==n?f:~i?3:4}}

@atk
Copy link

atk commented May 27, 2013

We can shorten it even further using binary logic in the last part:

function p(f,n,r,i){if(f in w){for(f=n|(r=1<<f),i=7;~i&&(w[--i]&(c|=r))^w[i];);return 511^f&&f^n?~i?3:4:f}}

We'd have now enough space to put w inside the function:

function p(f,n,r,i,w){w=[7,56,448,73,146,292,273,84];if(f in w){for(f=n|(r=1<<f),i=7;~i&&(w[--i]&(c|=r))^w[i];);return 511^f&&f^n?~i?3:4:f}}

@padawin
Copy link
Author

padawin commented Oct 16, 2013

nice, thanks!

@padawin
Copy link
Author

padawin commented Oct 16, 2013

will take time to update it

@padawin
Copy link
Author

padawin commented Dec 31, 2013

Ok it's updated, I didn't change the if, because the test was not good, I test if the typed value is a cell of the board (1 to 9), not a winning move.

The part ?3:4 was a remaining, not working, it's been removed as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment