FreeCodeCamp - Tic Tac Toe Game

Developer
Size
2,820 Kb
Views
14,168

How do I make an freecodecamp - tic tac toe game?

What is a freecodecamp - tic tac toe game? How do you make a freecodecamp - tic tac toe game? This script and codes were developed by Jason Thomas on 14 September 2022, Wednesday.

FreeCodeCamp - Tic Tac Toe Game Previews

FreeCodeCamp - Tic Tac Toe Game - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>freeCodeCamp - Tic Tac Toe Game</title> <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="canvasDiv">
<canvas id="canvas1" width="100" height="100" onClick="loop(1)"></canvas>
<canvas id="canvas2" width="100" height="100" onClick="loop(2)"></canvas>
<canvas id="canvas3" width="100" height="100" onClick="loop(3)"></canvas><br />
<canvas id="canvas4" width="100" height="100" onClick="loop(4)"></canvas>
<canvas id="canvas5" width="100" height="100" onClick="loop(5)"></canvas>
<canvas id="canvas6" width="100" height="100" onClick="loop(6)"></canvas><br />
<canvas id="canvas7" width="100" height="100" onClick="loop(7)"></canvas>
<canvas id="canvas8" width="100" height="100" onClick="loop(8)"></canvas>
<canvas id="canvas9" width="100" height="100" onClick="loop(9)"></canvas>
</div>
<a href="https://codepen.io/samoht513/full/NjYaVj/" id ="reloadButton">Play Again</a> <script src="js/index.js"></script>
</body>
</html>

FreeCodeCamp - Tic Tac Toe Game - Script Codes CSS Codes

*{ padding: 0px; margin:0px;
}
body { text-align: center;
}
canvas { background: blue; box-shadow: 20px 20px 50px #1c1c1c;
-webkit-transition: -webkit-transform 1s;
}
canvas:hover { width: 101px; height: 99px; cursor: pointer;
}
#canvasDiv { margin:150px auto 40px;
}
#reloadButton { text-decoration: none; color:#1c1c1c; font-size:22px; text-shadow: 5px 5px 30px white;
}
#reloadButton:hover { font-size:22px; opacity:0.7;
}

FreeCodeCamp - Tic Tac Toe Game - Script Codes JS Codes

var button = [];
for (var i = 1; i < 10; i++) button[i] = document.getElementById('canvas' + i);
var ctx = [];
for (var i = 1; i < 10; i++) ctx[i] = button[i].getContext('2d');
var bDisabled = [];
for (var i = 1; i < 10; i++) bDisabled[i] = false;
var isResult = false;
var content = [];
function loop(x) { if (!bDisabled[x]){ bDisabled[x] = true; button[x].style.opacity = 0.7; content[x] = 'x'; button[x].style.webkitTransform = "rotateX(180deg)"; setTimeout(function() { ctx[x].lineWidth=3; ctx[x].beginPath(); ctx[x].moveTo(10,10); ctx[x].lineTo(90,90); ctx[x].moveTo(90,10); ctx[x].lineTo(10,90); ctx[x].stroke(); ctx[x].closePath(); computerTurn(); },300); setTimeout(checkWinner, 1000); }
}
function checkWinner(){ if(!isResult){ if(content[1] == 'x' && content[2] == 'x' && content[3] == 'x') showWinner('you win'); else if(content[4] == 'x' && content[5] == 'x' && content[6] == 'x') showWinner('you win'); else if(content[7] == 'x' && content[8] == 'x' && content[9] == 'x') showWinner('you win'); else if(content[1] == 'x' && content[4] == 'x' && content[7] == 'x') showWinner('you win'); else if(content[2] == 'x' && content[5] == 'x' && content[8] == 'x') showWinner('you win'); else if(content[3] == 'x' && content[6] == 'x' && content[9] == 'x') showWinner('you win'); else if(content[1] == 'x' && content[5] == 'x' && content[9] == 'x') showWinner('you win'); else if(content[3] == 'x' && content[5] == 'x' && content[7] == 'x') showWinner('you win'); else if(content[1] == '0' && content[2] == '0' && content[3] == '0') showWinner('you lose'); else if(content[4] == '0' && content[5] == '0' && content[6] == '0') showWinner('you lose'); else if(content[7] == '0' && content[8] == '0' && content[9] == '0') showWinner('you lose'); else if(content[1] == '0' && content[4] == '0' && content[7] == '0') showWinner('you lose'); else if(content[2] == '0' && content[5] == '0' && content[8] == '0') showWinner('you lose'); else if(content[3] == '0' && content[6] == '0' && content[9] == '0') showWinner('you lose'); else if(content[1] == '0' && content[5] == '0' && content[9] == '0') showWinner('you lose'); else if(content[3] == '0' && content[5] == '0' && content[7] == '0') showWinner('you lose'); else if( (content[1] == 'x' || content[1] == '0') && (content[2] == 'x' || content[2] == '0') && (content[3] == 'x' || content[3] == '0') && (content[4] == 'x' || content[4] == '0') && (content[5] == 'x' || content[5] == '0') && (content[6] == 'x' || content[6] == '0') && (content[7] == 'x' || content[7] == '0') && (content[8] == 'x' || content[8] == '0') && (content[9] == 'x' || content[9] == '0') ) showWinner('Game is a draw'); }
}
function showWinner(x){ setTimeout(function(){ alert(x);},900);
isResult=true;
}
function computerTurn(){ var r = Math.random(); if(r < 0.1 && !bDisabled[1]) draw0Steps(1); else if(r < 0.2 && !bDisabled[2]) draw0Steps(2); else if(r < 0.3 && !bDisabled[3]) draw0Steps(3); else if(r < 0.4 && !bDisabled[4]) draw0Steps(4); else if(r < 0.5 && !bDisabled[5]) draw0Steps(5); else if(r < 0.6 && !bDisabled[6]) draw0Steps(6); else if(r < 0.7 && !bDisabled[7]) draw0Steps(7); else if(r < 0.8 && !bDisabled[8]) draw0Steps(8); else if(r < 1 && !bDisabled[9]) draw0Steps(9); else computerTurn();
}
function draw0Steps(x){ bDisabled[x] = true; button[x].style.opacity = 0.7; content[x] = '0' button[x].style.webkitTransform = "rotateX(180deg)"; setTimeout(function(){ ctx[x].beginPath(); ctx[x].lineWidth = 3 ctx[x].arc(50,50,34,0,Math.PI*2, false); ctx[x].stroke(); ctx[x].closePath(); }, 300);
}
FreeCodeCamp - Tic Tac Toe Game - Script Codes
FreeCodeCamp - Tic Tac Toe Game - Script Codes
Home Page Home
Developer Jason Thomas
Username samoht513
Uploaded September 14, 2022
Rating 3
Size 2,820 Kb
Views 14,168
Do you need developer help for FreeCodeCamp - Tic Tac Toe Game?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Jason Thomas (samoht513) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!