Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed tic tac toe #11

Open
wants to merge 4 commits into
base: th/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions index.css
Original file line number Diff line number Diff line change
@@ -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;
}
33 changes: 23 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<title>Tic Tac Toe!</title>

<h1 >Tic Tac Toe! </h1>
<meta charset="utf-8">
<link href="index.css" media="screen" rel="stylesheet" type="text/css">
</head>
<div id="board">
<table>
<tr id="row1">
<td class="square", id="zz"></td>
<td class="square v", id="zo"></td>
<td class="square", id="zt"> </td>
</tr>
<tr id="row2">
<td class="square h" id="oz"></td>
<td class="square v h", id="oo"></td>
<td class="square h", id="ot"></td>
</tr>
<tr id="row3">
<td class="square", id="tz"></td>
<td class="square v", id="to"></td>
<td class="square", id="tt"></td>
</tr>
</table>
</div>

<body>
<h1>Tic Tac Toe</h1>
<div id="tic-tac-toe"></div>
</body>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="tic-tac-toe.js"></script>
<script type="text/javascript">
<!-- <script type="text/javascript">
$(document).on('ready', function() {
console.log('create and begin the game here!');
})
</script>
</script> -->
</html>

227 changes: 223 additions & 4 deletions tic-tac-toe.js
Original file line number Diff line number Diff line change
@@ -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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment with regards to generalizing this using the variable, I think in this case you can use col instead of row like above.

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(){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this duplicate logic only works because there are 9 spots, but if you added even one additional row/col it would be even more difficult. I would really like to see you try to refactor this to use a single click event handler rather that an individual one for each space.

if (!game.checkAvai(0,0)) {
throw new Error ("This spot is already claimed!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will throwing an error affect the user's gameplay? What will happen for the user?

} else if (game.count % 2 === 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way that this game.count variable name is defined, it seems to imply that this variable is tracking the count of the games. I would suggest maybe re-naming this to turn or turnCount for clarity.

$(this).text("O");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this conditional could be setting a variable equal to either "O" or "X". Then the rest of the logic that is in this conditional and the next one would be exactly the same.

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");}
});