-
Notifications
You must be signed in to change notification settings - Fork 21
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
base: th/master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} |
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> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,221 @@ | ||
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) { | ||
if (row === 0) { | ||
if (checkSpot(col, 1, sign, this.board) && (checkSpot(col,2, sign, this.board))) { | ||
return true; | ||
} else if ( row === 1) { | ||
if (checkSpot(col, 0, sign, this.board) && (checkSpot(col,2, sign, this.board))) | ||
return true; | ||
} else { | ||
if (checkSpot(col, 0, sign, this.board) && (checkSpot(col,1, sign, this.board))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like the logic for these three conditionals could more easily be done one if using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kariabancroft Thank you for the comments! I refactored this method- so now I call only one time to the "checkSopt" method. |
||
return true; | ||
} | ||
} | ||
}; | ||
|
||
TicTacToe.prototype.CheckCol = function(col, row, sign) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
if (col === 0) { | ||
if (checkSpot(1,row, sign, this.board) && (checkSpot(2,row, sign, this.board))) | ||
return true; | ||
} else if ( col === 1) { | ||
if (checkSpot(0, row, sign, this.board) && (checkSpot(2,row, sign, this.board))) | ||
return true; | ||
} else { | ||
if (checkSpot(0, row, sign, this.board) && (checkSpot(1,row, sign, this.board))) | ||
return true; | ||
} | ||
}; | ||
|
||
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(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way that this |
||
$(this).text("O"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Watch the formatting here, this |
||
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");} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation of this conditional vs the
{}
that are associated with each condition do not seem to match up. Watch out for creating conditionals without the{}
because this can cause unexpected behavior.