Skip to content

Instantly share code, notes, and snippets.

@jslegers
Last active January 16, 2020 21:38
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 jslegers/6054902 to your computer and use it in GitHub Desktop.
Save jslegers/6054902 to your computer and use it in GitHub Desktop.
Simple 2 player version of the "connect four" game. All code is MIT licenced.
<body>
<span id="a">red</span>
<span id="b">yellow</span>
<span id="n">Are you sure you want to start a new game?</span>
<span id="w">The %s player won.
Do you want to play a new game?</span>
<div id="game">It's the <span id="c"></span>&nbsp;player's move.
<table id="gameboard">
<tr>
<td id="c-1-1"></td>
<td id="c-1-2"></td>
<td id="c-1-3"></td>
<td id="c-1-4"></td>
<td id="c-1-5"></td>
<td id="c-1-6"></td>
<td id="c-1-7"></td>
</tr>
<tr>
<td id="c-2-1"></td>
<td id="c-2-2"></td>
<td id="c-2-3"></td>
<td id="c-2-4"></td>
<td id="c-2-5"></td>
<td id="c-2-6"></td>
<td id="c-2-7"></td>
</tr>
<tr>
<td id="c-3-1"></td>
<td id="c-3-2"></td>
<td id="c-3-3"></td>
<td id="c-3-4"></td>
<td id="c-3-5"></td>
<td id="c-3-6"></td>
<td id="c-3-7"></td>
</tr>
<tr>
<td id="c-4-1"></td>
<td id="c-4-2"></td>
<td id="c-4-3"></td>
<td id="c-4-4"></td>
<td id="c-4-5"></td>
<td id="c-4-6"></td>
<td id="c-4-7"></td>
</tr>
<tr>
<td id="c-5-1"></td>
<td id="c-5-2"></td>
<td id="c-5-3"></td>
<td id="c-5-4"></td>
<td id="c-5-5"></td>
<td id="c-5-6"></td>
<td id="c-5-7"></td>
</tr>
<tr>
<td id="c-6-1"></td>
<td id="c-6-2"></td>
<td id="c-6-3"></td>
<td id="c-6-4"></td>
<td id="c-6-5"></td>
<td id="c-6-6"></td>
<td id="c-6-7"></td>
</tr>
</table>
<div class="left leg"></div>
<div class="right leg"></div>
<button id="r">New game</button>
</div>
<script src="code.min.js"></script>
</body>
Copyright (c) 2013 John Slegers
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
(function(doc){
var
start = function(){
finished = false;
changePlayer();
},
newGame = function(message){
if (confirm(message)){
start();
forAllCells(emptyField);
}
},
element = function(id){
return doc.getElementById(id);
},
value = function(el){
return element(el).innerHTML;
},
cell = function(i,j){
return element("c-"+i+"-"+j);
},
forAllCells = function(action){
for (var t = 1;t<7;t++){
for (var counter2 = 1;counter2<8;counter2++){
action(t,counter2);
}
}
},
sameColor = function(i,j){
return testClass(i,j,players[current]);
},
changePlayer = function(){
element("c").innerHTML = players[current = (current + 1) % 2];
},
horizontalWon = function(i,j){
for(var min=j-1;min>0;min--)if(!sameColor(i,min))break;
for(var max=j+1;max<8;max++)if(!sameColor(i,max))break;
return max-min>4;
},
verticalWon = function(i,j){
for(var max=i+1;max<7;max++)if(!sameColor(max,j))break;
return max-i>3;
},
diagonalLtrWon = function(i,j){
for(var min=i-1,t=j-1;min>0;min--,t--)if(t<1||!sameColor(min,t))break;
for(var max=i+1,t=j+1;max<7;max++,t++)if(t>7||!sameColor(max,t))break;
return max-min>4;
},
diagonalRtlWon = function(i,j){
for(var min=i-1,t=j+1;min>0;min--,t++)if(t>7||!sameColor(min,t))break;
for(var max=i+1,t=j-1;max<7;max++,t--)if(t<1||!sameColor(max,t))break;
return max-min>4;
},
colorField = function(i,j,color){
cell(i,j).className = color;
},
emptyField = function(i,j){
colorField(i,j,'');
},
testClass = function(i,j,value){
return cell(i,j).className == value;
},
addCellBehavior = function(i,j){
cell(i,j).onclick = function(j){
return function(){
if(!finished){
for (var t = 6;t>0;t--){
if(testClass(t,j,'')){
colorField(t,j,players[current]);
if(horizontalWon(t,j) || verticalWon(t,j) || diagonalLtrWon(t,j) || diagonalRtlWon(t,j)){
finished = true;
newGame(wonMessage.replace("%s",players[current]));
} else {
changePlayer();
}
break;
}
}
}
}
}(j);
},
players = [value("a"),value("b")],
current = 0,
newGameMessage = value("n"),
wonMessage = value("w"),
finished;
start();
forAllCells(addCellBehavior);
element("r").onclick = function(){
newGame(newGameMessage)
};
})(document);
body {
text-align:center;
font-family: Helvetica, Arial, "Lucida Grande", sans-serif;
}
button,table,#game,body,td {
margin: 40px auto 0 auto;
background:#eee;
}
#game,#gameboard {
width:620px;
}
#gameboard,.leg {
background:#00f;
}
#gameboard,.leg,#gameboard td,#r {
border:2px solid #009;
}
#gameboard td {
width:76px;
height:76px;
border-radius:38px;
}
#gameboard {
padding:10px;
border-spacing:4px;
border-bottom-width:0;
border-radius:12px 12px 0 0;
}
#gameboard .red {
background:#f00;
}
#gameboard .yellow {
background:#ff0;
}
#r,#gameboard td {
border-color:#333;
}
.leg {
border-top-width:0;
height:100px;
width:20px;
border-radius:0 0 12px 12px;
}
.left {
float:left;
}
.right {
float:right;
}
.yellow,.red,#gameboard,.leg {
box-shadow: inset 0 0 20px rgba(100,100,100,0.6);
}
#n,#w,#a,#b {
display:none;
}
@jslegers
Copy link
Author

Huh?

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