Tic Tac Toe In Javascript With Source Code
The Tic Tac Toe In Javascript was developed using JavaScript, CSS and HTML, Only minimal logic and validation checks are used to build this Javascript game with source code on the front end. To creating this simple javascript project makes sure that you have basic understanding of front-end technologies such as HTML, CSS, and JavaScript.
A Tic Tac Toe Using Javascript, with a few simple validations and error checks, it’s pretty straightforward to create. The game is started by Player-1, and both players take turns making their movements. The player who creates a three-block chain in a row wins the game.
This HTML CSS Javascript Code also includes a downloadable Javascript Game With Source Code for free, just find the downloadable source code below and click to start downloading.
I have here a suggested list of Best JavaScript Projects with Source Code Free to download and I’m sure this can help you to improve your skills in JavaScript programming and web development as a whole.
To start executing a Tic Tac Toe In Javascript With Source Code, makes sure that you have any platform in creating a JavaScript, CSS, bootstrap, and HTML installed in your computer, in my case I will use Sublime Text.
Tic Tac Toe In Javascript With Source Code : Steps on how to create the project
Time needed: 5 minutes.
These are the steps on how to create Tic Tac Toe In Javascript With Source Code
- Step 1: Create a folder.
First, create a folder and named it.
- Step 2: Open the folder in “Sublime Text”.
Second ,after creating a folder, open it in “sublime text“.
- Step 3: Create a html file.
Third, create a “html” file and save it as “index.html“
The code given below is for the html module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<head> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> </head> <body> <div id="main"> <h1>TIC TAC TOE (IT SOURCECODE)</h1> <!-- Game Instructions --> <p id="ins">Game starts by just Tap on box<br><br>First Player starts as <b>Player X</b><br>And<br>Second Player as <b>Player 0</b> </p> <br><br> <!-- 3*3 grid of Boxes --> <input type="text" id="b1" onclick= "myfunc_3(); myfunc();" readonly> <input type="text" id="b2" onclick= "myfunc_4(); myfunc();" readonly> <input type="text" id="b3" onclick= "myfunc_5(); myfunc();" readonly> <br><br> <input type="text" id="b4" onclick= "myfunc_6(); myfunc();" readonly> <input type="text" id="b5" onclick= "myfunc_7(); myfunc();" readonly> <input type="text" id="b6" onclick= "myfunc_8(); myfunc();" readonly> <br><br> <input type="text" id="b7" onclick= "myfunc_9(); myfunc();" readonly> <input type="text" id="b8" onclick= "myfunc_10();myfunc();" readonly> <input type="text" id="b9" onclick= "myfunc_11();myfunc();" readonly> <!-- Grid end here --> <br><br><br> <!-- Button to reset game --> <button id="but" onclick="myfunc_2()"> RESET </button> <br><br> <!-- Space to show player turn --> <p id="print"></p> </div> </body> |
In this module which is the html module of the simple project.
The code given below is for the css module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<style> /* CSS Code */ /* Heading */ h1 { color: skyblue; font-size: 45px; } /* 3*3 Grid */ #b1, #b2, #b3, #b4, #b5, #b6, #b7, #b8, #b9 { width: 80px; height: 52px; margin: auto; border: 1px solid gray; border-radius: 6px; font-size: 30px; text-align: center; } /* Reset Button */ #but { box-sizing: border-box; width: 95px; height: 40px; border: 1px solid dodgerblue; margin: auto; border-radius: 4px; font-family: Verdana, Geneva, Tahoma, sans-serif; background-color: red; color: white; font-size: 20px; cursor: pointer; } /* Player turn space */ #print { font-family: Verdana, Geneva, Tahoma, sans-serif; color: dodgerblue; font-size: 20px; } /* Main Contianer */ #main { text-align: center; } /* Game Instruction Text */ #ins { font-family: Verdana, Geneva, Tahoma, sans-serif; color: dodgerblue; font-size: 17px; } </style> |
In this module which is the css module of the simple project.
The code given below is for the javascript module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
<script> // Function called whenever user tab on any box function myfunc() { // Setting DOM to all boxes or input field var b1, b1, b3, b4, b5, b6, b7, b8, b9; b1 = document.getElementById("b1").value; b2 = document.getElementById("b2").value; b3 = document.getElementById("b3").value; b4 = document.getElementById("b4").value; b5 = document.getElementById("b5").value; b6 = document.getElementById("b6").value; b7 = document.getElementById("b7").value; b8 = document.getElementById("b8").value; b9 = document.getElementById("b9").value; // Checking if Player X won or not and after // that disabled all the other fields if ((b1 == 'x' || b1 == 'X') && (b2 == 'x' || b2 == 'X') && (b3 == 'x' || b3 == 'X')) { document.getElementById('print') .innerHTML = "Player X won"; document.getElementById("b4").disabled = true; document.getElementById("b5").disabled = true; document.getElementById("b6").disabled = true; document.getElementById("b7").disabled = true; document.getElementById("b8").disabled = true; document.getElementById("b9").disabled = true; window.alert('Player X won'); } else if ((b1 == 'x' || b1 == 'X') && (b4 == 'x' || b4 == 'X') && (b7 == 'x' || b7 == 'X')) { document.getElementById('print') .innerHTML = "Player X won"; document.getElementById("b2").disabled = true; document.getElementById("b3").disabled = true; document.getElementById("b5").disabled = true; document.getElementById("b6").disabled = true; document.getElementById("b8").disabled = true; document.getElementById("b9").disabled = true; window.alert('Player X won'); } else if ((b7 == 'x' || b7 == 'X') && (b8 == 'x' || b8 == 'X') && (b9 == 'x' || b9 == 'X')) { document.getElementById('print') .innerHTML = "Player X won"; document.getElementById("b1").disabled = true; document.getElementById("b2").disabled = true; document.getElementById("b3").disabled = true; document.getElementById("b4").disabled = true; document.getElementById("b5").disabled = true; document.getElementById("b6").disabled = true; window.alert('Player X won'); } else if ((b3 == 'x' || b3 == 'X') && (b6 == 'x' || b6 == 'X') && (b9 == 'x' || b9 == 'X')) { document.getElementById('print') .innerHTML = "Player X won"; document.getElementById("b1").disabled = true; document.getElementById("b2").disabled = true; document.getElementById("b4").disabled = true; document.getElementById("b5").disabled = true; document.getElementById("b7").disabled = true; document.getElementById("b8").disabled = true; window.alert('Player X won'); } else if ((b1 == 'x' || b1 == 'X') && (b5 == 'x' || b5 == 'X') && (b9 == 'x' || b9 == 'X')) { document.getElementById('print') .innerHTML = "Player X won"; document.getElementById("b2").disabled = true; document.getElementById("b3").disabled = true; document.getElementById("b4").disabled = true; document.getElementById("b6").disabled = true; document.getElementById("b7").disabled = true; document.getElementById("b8").disabled = true; window.alert('Player X won'); } else if ((b3 == 'x' || b3 == 'X') && (b5 == 'x' || b5 == 'X') && (b7 == 'x' || b7 == 'X')) { document.getElementById('print') .innerHTML = "Player X won"; document.getElementById("b1").disabled = true; document.getElementById("b2").disabled = true; document.getElementById("b4").disabled = true; document.getElementById("b6").disabled = true; document.getElementById("b8").disabled = true; document.getElementById("b9").disabled = true; window.alert('Player X won'); } else if ((b2 == 'x' || b2 == 'X') && (b5 == 'x' || b5 == 'X') && (b8 == 'x' || b8 == 'X')) { document.getElementById('print') .innerHTML = "Player X won"; document.getElementById("b1").disabled = true; document.getElementById("b3").disabled = true; document.getElementById("b4").disabled = true; document.getElementById("b6").disabled = true; document.getElementById("b7").disabled = true; document.getElementById("b9").disabled = true; window.alert('Player X won'); } else if ((b4 == 'x' || b4 == 'X') && (b5 == 'x' || b5 == 'X') && (b6 == 'x' || b6 == 'X')) { document.getElementById('print') .innerHTML = "Player X won"; document.getElementById("b1").disabled = true; document.getElementById("b2").disabled = true; document.getElementById("b3").disabled = true; document.getElementById("b7").disabled = true; document.getElementById("b8").disabled = true; document.getElementById("b9").disabled = true; window.alert('Player X won'); } // Checking of Player X finsh // Checking for Player 0 starts, Is player 0 won or // not and after that disabled all the other fields else if ((b1 == '0' || b1 == '0') && (b2 == '0' || b2 == '0') && (b3 == '0' || b3 == '0')) { document.getElementById('print') .innerHTML = "Player 0 won"; document.getElementById("b4").disabled = true; document.getElementById("b5").disabled = true; document.getElementById("b6").disabled = true; document.getElementById("b7").disabled = true; document.getElementById("b8").disabled = true; document.getElementById("b9").disabled = true; window.alert('Player 0 won'); } else if ((b1 == '0' || b1 == '0') && (b4 == '0' || b4 == '0') && (b7 == '0' || b7 == '0')) { document.getElementById('print') .innerHTML = "Player 0 won"; document.getElementById("b2").disabled = true; document.getElementById("b3").disabled = true; document.getElementById("b5").disabled = true; document.getElementById("b6").disabled = true; document.getElementById("b8").disabled = true; document.getElementById("b9").disabled = true; window.alert('Player 0 won'); } else if ((b7 == '0' || b7 == '0') && (b8 == '0' || b8 == '0') && (b9 == '0' || b9 == '0')) { document.getElementById('print') .innerHTML = "Player 0 won"; document.getElementById("b1").disabled = true; document.getElementById("b2").disabled = true; document.getElementById("b3").disabled = true; document.getElementById("b4").disabled = true; document.getElementById("b5").disabled = true; document.getElementById("b6").disabled = true; window.alert('Player 0 won'); } else if ((b3 == '0' || b3 == '0') && (b6 == '0' || b6 == '0') && (b9 == '0' || b9 == '0')) { document.getElementById('print') .innerHTML = "Player 0 won"; document.getElementById("b1").disabled = true; document.getElementById("b2").disabled = true; document.getElementById("b4").disabled = true; document.getElementById("b5").disabled = true; document.getElementById("b7").disabled = true; document.getElementById("b8").disabled = true; window.alert('Player 0 won'); } else if ((b1 == '0' || b1 == '0') && (b5 == '0' || b5 == '0') && (b9 == '0' || b9 == '0')) { document.getElementById('print') .innerHTML = "Player 0 won"; document.getElementById("b2").disabled = true; document.getElementById("b3").disabled = true; document.getElementById("b4").disabled = true; document.getElementById("b6").disabled = true; document.getElementById("b7").disabled = true; document.getElementById("b8").disabled = true; window.alert('Player 0 won'); } else if ((b3 == '0' || b3 == '0') && (b5 == '0' || b5 == '0') && (b7 == '0' || b7 == '0')) { document.getElementById('print') .innerHTML = "Player 0 won"; document.getElementById("b1").disabled = true; document.getElementById("b2").disabled = true; document.getElementById("b4").disabled = true; document.getElementById("b6").disabled = true; document.getElementById("b8").disabled = true; document.getElementById("b9").disabled = true; window.alert('Player 0 won'); } else if ((b2 == '0' || b2 == '0') && (b5 == '0' || b5 == '0') && (b8 == '0' || b8 == '0')) { document.getElementById('print') .innerHTML = "Player 0 won"; document.getElementById("b1").disabled = true; document.getElementById("b3").disabled = true; document.getElementById("b4").disabled = true; document.getElementById("b6").disabled = true; document.getElementById("b7").disabled = true; document.getElementById("b9").disabled = true; window.alert('Player 0 won'); } else if ((b4 == '0' || b4 == '0') && (b5 == '0' || b5 == '0') && (b6 == '0' || b6 == '0')) { document.getElementById('print') .innerHTML = "Player 0 won"; document.getElementById("b1").disabled = true; document.getElementById("b2").disabled = true; document.getElementById("b3").disabled = true; document.getElementById("b7").disabled = true; document.getElementById("b8").disabled = true; document.getElementById("b9").disabled = true; window.alert('Player 0 won'); } // Checking of Player 0 finsh // Here, Checking about Tie else if ((b1 == 'X' || b1 == '0') && (b2 == 'X' || b2 == '0') && (b3 == 'X' || b3 == '0') && (b4 == 'X' || b4 == '0') && (b5 == 'X' || b5 == '0') && (b6 == 'X' || b6 == '0') && (b7 == 'X' || b7 == '0') && (b8 == 'X' || b8 == '0') && (b9 == 'X' || b9 == '0')) { document.getElementById('print') .innerHTML = "Match Tie"; window.alert('Match Tie'); } else { // Here, Printing Result if (flag == 1) { document.getElementById('print') .innerHTML = "Player X Turn"; } else { document.getElementById('print') .innerHTML = "Player 0 Turn"; } } } // Function to reset game function myfunc_2() { location.reload(); document.getElementById('b1').value = ''; document.getElementById("b2").value = ''; document.getElementById("b3").value = ''; document.getElementById("b4").value = ''; document.getElementById("b5").value = ''; document.getElementById("b6").value = ''; document.getElementById("b7").value = ''; document.getElementById("b8").value = ''; document.getElementById("b9").value = ''; } // Here onwards, functions check turn of the player // and put accordingly value X or 0 flag = 1; function myfunc_3() { if (flag == 1) { document.getElementById("b1").value = "X"; document.getElementById("b1").disabled = true; flag = 0; } else { document.getElementById("b1").value = "0"; document.getElementById("b1").disabled = true; flag = 1; } } function myfunc_4() { if (flag == 1) { document.getElementById("b2").value = "X"; document.getElementById("b2").disabled = true; flag = 0; } else { document.getElementById("b2").value = "0"; document.getElementById("b2").disabled = true; flag = 1; } } function myfunc_5() { if (flag == 1) { document.getElementById("b3").value = "X"; document.getElementById("b3").disabled = true; flag = 0; } else { document.getElementById("b3").value = "0"; document.getElementById("b3").disabled = true; flag = 1; } } function myfunc_6() { if (flag == 1) { document.getElementById("b4").value = "X"; document.getElementById("b4").disabled = true; flag = 0; } else { document.getElementById("b4").value = "0"; document.getElementById("b4").disabled = true; flag = 1; } } function myfunc_7() { if (flag == 1) { document.getElementById("b5").value = "X"; document.getElementById("b5").disabled = true; flag = 0; } else { document.getElementById("b5").value = "0"; document.getElementById("b5").disabled = true; flag = 1; } } function myfunc_8() { if (flag == 1) { document.getElementById("b6").value = "X"; document.getElementById("b6").disabled = true; flag = 0; } else { document.getElementById("b6").value = "0"; document.getElementById("b6").disabled = true; flag = 1; } } function myfunc_9() { if (flag == 1) { document.getElementById("b7").value = "X"; document.getElementById("b7").disabled = true; flag = 0; } else { document.getElementById("b7").value = "0"; document.getElementById("b7").disabled = true; flag = 1; } } function myfunc_10() { if (flag == 1) { document.getElementById("b8").value = "X"; document.getElementById("b8").disabled = true; flag = 0; } else { document.getElementById("b8").value = "0"; document.getElementById("b8").disabled = true; flag = 1; } } function myfunc_11() { if (flag == 1) { document.getElementById("b9").value = "X"; document.getElementById("b9").disabled = true; flag = 0; } else { document.getElementById("b9").value = "0"; document.getElementById("b9").disabled = true; flag = 1; } } </script> |
In this module which is the javascript module of the simple project.
Project Output

ABOUT PROJECT | PROJECT DETAILS |
---|---|
Project Name : | Tic Tac Toe In Javascript |
Project Platform : | JavaScript |
Programming Language Used: | php,javascript,html,css |
Developer Name : | itsourcecode.com |
IDE Tool (Recommended): | Sublime |
Project Type : | Web Application |
Database: | None |
Upload Date: | July 16, 2021 |
Run Quick Virus Scan for secure Download
Run Quick Scan for secure DownloadDownloadable Source Code
Summary
This JavaScript Project With Source Code is simply in HTML, CSS, and JavaScript. Play a few regular games to get in the mood. Mark your positions with Xs and Os or your place holders. The goal of the game is to be the first to cover three consecutive spots (horizontal, vertical, or diagonal).
Related Articles
- Todo List In JavaScript With Source Code
- Currency Converter Project in JavaScript with Source Code
- Calculator In JavaScript Source Code
- [SOLVED] How To Make A Live Chat In JavaScript With Source Code
Inquiries
If you have any questions or suggestions about Tic Tac Toe In Javascript With Source Code , please feel free to leave a comment below.