diff --git a/08week/tic-tac-toe/script.js b/08week/tic-tac-toe/script.js index 0509eaee74..e654bc101b 100644 --- a/08week/tic-tac-toe/script.js +++ b/08week/tic-tac-toe/script.js @@ -1,5 +1,31 @@ 'use strict'; -$(document).ready(function() { - // Put app logic in here -}); +$(document).ready(function(){ + var turn = 'X'; + var count= 0; + $('[data-cell]').on('click', function(){ + if($(this).text() === ''){ + $(this).text(turn); + count++; + checkForWin(count); + if(turn === 'O'){ + turn = 'X'; + }else{ + turn = 'O'; + } + } + }) + + function checkForWin(tie){ + if(tie === 9){ + // console + $('#announce-winner').text(`Try again`); + } + if($('[data-cell="0"]').text() === turn && + $('[data-cell="1"]').text() === turn && + $('[data-cell="2"]').text() === turn){ + $('#announce-winner').text(`${turn} wins`); + } + + } +}) \ No newline at end of file