diff --git a/index.css b/index.css index ae8b9a0..d6db74d 100644 --- a/index.css +++ b/index.css @@ -1,3 +1,23 @@ -html { - + + .square{ + width:100px; + height:100px; +} + +.v{ + border-left:1px solid #000; + border-right:1px solid #000; +} + +.h{ + border-top:1px solid #000; + border-bottom:1px solid #000; +} + +h1 { + margin-left: 60px; +} + +td { + text-align: center; } diff --git a/index.html b/index.html index de8d985..6919374 100644 --- a/index.html +++ b/index.html @@ -1,21 +1,34 @@ - - Tic Tac Toe! + +

Tic Tac Toe!

- +
+ + + + + + + + + + + + + + + + +
+
- -

Tic Tac Toe

-
- - --> - diff --git a/tic-tac-toe.js b/tic-tac-toe.js index 33c03a0..391a033 100644 --- a/tic-tac-toe.js +++ b/tic-tac-toe.js @@ -1,7 +1,226 @@ function TicTacToe() { + this.board = [["","",""], ["","",""], ["","",""]]; + this.count = 0; + } -} + var checkSpot = function(col, row, sign , board) { + if (board[col][row] === sign) { + return true; + } else { + return false; + } + } -TicTacToe.prototype = { - -} +TicTacToe.prototype.CheckRow = function(col, row, sign) { + countSign = 0; + for (var i = 0; i < 3 ; i++) { + if (checkSpot(col, i, sign, this.board)) { + countSign++; + } + } + if (countSign === 3) { + return true; + } else { + return false + } +}; + +TicTacToe.prototype.CheckCol = function(col, row, sign) { + countSign = 0; + for (var i = 0; i < 3 ; i++) { + if (checkSpot(i, row, sign, this.board)) { + countSign++; + } + } + if (countSign === 3) { + return true; + } else { + return false + } +}; + +TicTacToe.prototype.checkOblique = function(col, row, sign) { + if ((col === 0) && (row == 1) || (col == 1 && row === 0) || (col == 1 && row == 2) || (col == 2 && row == 1) ) { + return false; + } else if ((checkSpot(0,0,sign, this.board) && checkSpot(1,1,sign, this.board) && checkSpot(2,2, sign, this.board)) || (checkSpot(0,2,sign, this.board) && checkSpot(1,1,sign, this.board) && checkSpot(2,0,sign, this.board))){ + return true; + } +}; + +TicTacToe.prototype.checkAvai = function(col, row) { + if (this.board[col][row] === "") { + return true; + } else { return false;} +}; + + $(document).ready(function() { + var game = new TicTacToe(); + + $("#zz").click(function(){ + if (!game.checkAvai(0,0)) { + throw new Error ("This spot is already claimed!"); + } else if (game.count % 2 === 0) { + $(this).text("O"); + game.board[0][0]="O"; + if (game.CheckRow(0,0,"O") || game.CheckCol(0,0,"O") || game.checkOblique(0,0,"O") ) { + alert('Y wins'); + } else { + game.count ++;} + } else { + $(this).text("X"); + game.board[0][0]="X"; + if (game.CheckRow(0,0,"X") || game.CheckCol(0,0,"X") || game.checkOblique(0,0,"X") ) { + alert('X wins'); + } else { + game.count ++;} + } + }); + + $("#zo").click(function(){ + if (!game.checkAvai(0,1)) { + throw new Error ("This spot is already claimed!"); + } else if (game.count % 2 === 0) { + $(this).text("O"); + game.board[0][1] = "O"; + if (game.CheckRow(0,1,"O") ||game.CheckCol(0,1,"O") || game.checkOblique(0,1,"O") ) { + alert('Y wins'); } else { + game.count ++;} + } else { + $(this).text("X"); + game.board[0][1] = "X"; + if (game.CheckRow(0,1,"X") ||game.CheckCol(0,1,"X") || game.checkOblique(0,1,"X") ) { + alert('X wins'); } else { + game.count ++;} + } + }); + + $("#zt").click(function(){ + if (!game.checkAvai(0,2)) { + throw new Error ("This spot is already claimed!"); + } else if (game.count % 2 === 0) { + $(this).text("O"); + game.board[0][2] = "O"; + if (game.CheckRow(0,2,"O") ||game.CheckCol(0,2,"O") || game.checkOblique(0,2,"O") ) { + alert('Y wins'); } else { + game.count ++;} + } else { + $(this).text("X"); + game.board[0][2] = "X"; + if (game.CheckRow(0,2,"X") ||game.CheckCol(0,2,"X") || game.checkOblique(0,2,"X") ) { + alert('X wins'); } else { + game.count ++;} + } + }); + + $("#oz").click(function(){ + if (!game.checkAvai(1,0)) { + throw new Error ("This spot is already claimed!"); + } else if (game.count % 2 === 0) { + $(this).text("O"); + game.board[1][0] = "O"; + if (game.CheckRow(1,0,"O") ||game.CheckCol(1,0,"O") || game.checkOblique(1,0,"O") ) { + alert('Y wins'); } else { + game.count ++;} + } else { + $(this).text("X"); + game.board[1][0] = "X"; + if (game.CheckRow(1,0,"X") ||game.CheckCol(1,0,"X") || game.checkOblique(1,0,"X") ) { + alert('X wins'); } else { + game.count ++;} + } + }); + + $("#oo").click(function(){ + if (!game.checkAvai(1,1)) { + throw new Error ("This spot is already claimed!"); + } else if (game.count % 2 === 0) { + $(this).text("O"); + game.board[1][1] = "O"; + if (game.CheckRow(1,1,"O") ||game.CheckCol(1,1,"O") || game.checkOblique(1,1,"O") ) { + alert('Y wins'); } else { + game.count ++;} + } else { + $(this).text("X"); + game.board[1][1] = "X"; + if (game.CheckRow(1,1,"X") ||game.CheckCol(1,1,"X") || game.checkOblique(1,1,"X") ) { + alert('X wins'); } else { + game.count ++;} + } + }); + + $("#ot").click(function(){ + if (!game.checkAvai(1,2)) { + throw new Error ("This spot is already claimed!"); + } else if (game.count % 2 === 0) { + $(this).text("O"); + game.board[1][2] = "O"; + if (game.CheckRow(1,2,"O") ||game.CheckCol(1,2,"O") || game.checkOblique(1,2,"O") ) { + alert('Y wins'); } else { + game.count ++;} + } else { + $(this).text("X"); + game.board[1][2]= "X"; + if (game.CheckRow(1,2,"X") ||game.CheckCol(1,2,"X") || game.checkOblique(1,2,"X") ) { + alert('X wins'); } else { + game.count ++;} + } + }); + + $("#tz").click(function(){ + if (!game.checkAvai(2,0)) { + throw new Error ("This spot is already claimed!"); + } else if (game.count % 2 === 0) { + $(this).text("O"); + game.board[2][0] = "O"; + if (game.CheckRow(2,0,"O") ||game.CheckCol(2,0,"O") || game.checkOblique(2,0,"O") ) { + alert('Y wins'); } else { + game.count ++;} + } else { + $(this).text("X"); + game.board[2][0] = "X"; + if (game.CheckRow(2,0,"X") ||game.CheckCol(2,0,"X") || game.checkOblique(2,0,"X") ) { + alert('X wins'); } else { + game.count ++;} + } + }); + + $("#to").click(function(){ + if (!game.checkAvai(2,1)) { + throw new Error ("This spot is already claimed!"); + } else if (game.count % 2 === 0) { + $(this).text("O"); + game.board[2][1] = "O"; + if (game.CheckRow(2,1,"O") ||game.CheckCol(2,1,"O") || game.checkOblique(2,1,"O") ) { + alert('Y wins'); } else { + game.count ++;} + } else { + $(this).text("X"); + game.board[2][1] = "X"; + if (game.CheckRow(2,1,"X") ||game.CheckCol(2,1,"X") || game.checkOblique(2,1,"X") ) { + alert('X wins'); } else { + game.count ++;} + } + }); + + $("#tt").click(function(){ + if (!game.checkAvai(2,2)) { + throw new Error ("This spot is already claimed!"); + } else if (game.count % 2 === 0) { + $(this).text("O"); + game.board[2][2] = "O"; + if (game.CheckRow(2,2,"O") ||game.CheckCol(2,2,"O") || game.checkOblique(2,2,"O") ) { + alert('Y wins'); } else { + game.count ++;} + } else { + $(this).text("X"); + game.board[2][2] = "X"; + if (game.CheckRow(2,2,"X") ||game.CheckCol(2,2,"X") || game.checkOblique(2,2,"X") ) { + alert('X wins'); } else { + game.count ++;} + } + + }); + + if (game.count == 9) + {alert("it's a tie");} +});