-
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
tic tac toes #15
base: grangerdanger
Are you sure you want to change the base?
tic tac toes #15
Changes from all 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,25 @@ | ||
html { | ||
|
||
|
||
} | ||
|
||
h1 { | ||
text-align: center; | ||
} | ||
|
||
#tictactoe { | ||
text-align: center; | ||
} | ||
|
||
div.square { | ||
width: 100px; | ||
height: 100px; | ||
display:inline-block; | ||
border-width: thin; | ||
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. lines 17-19 could be condensed to |
||
border-style: solid; | ||
border-color: black; | ||
text-align: center; | ||
vertical-align: top; | ||
margin-bottom: 5px; | ||
} | ||
|
||
} | ||
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. this looks like an extra closing bracket |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,15 +7,76 @@ | |
</head> | ||
|
||
<body> | ||
<h1>Tic Tac Toe</h1> | ||
<div id="tic-tac-toe"></div> | ||
<h1>Let's Play!</h1> | ||
<div id="tic-tac-toe"> | ||
<div class="row"> | ||
<div class="square" id="one"></div> | ||
<div class="square" id="two"></div> | ||
<div class="square" id="three"></div> | ||
</div> | ||
<div class="row"> | ||
<div class="square" id="four"></div> | ||
<div class="square" id="five"></div> | ||
<div class="square" id="six"></div> | ||
</div> | ||
<div class="row"> | ||
<div class="square" id="seven"></div> | ||
<div class="square" id="eight"></div> | ||
<div class="square" id="nine"></div> | ||
</div> | ||
</div> | ||
<a href="file:///Users/lgranger/Documents/ADA/git/ProjectForks/js-tic-tac-toe/index.html" class="button">reset</a> | ||
</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"> | ||
$(document).on('ready', function() { | ||
console.log('create and begin the game here!'); | ||
var game = new TicTacToe(); | ||
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 would pull this logic out and put it with the other JS logic in your tic-tac-toe.js file so that all of the logic is located in the same place rather than jumping back and forth to find it in both locations. |
||
$("div.square").one("click", function() { | ||
// insert the X or O into the div | ||
var pickSquare = this; | ||
game.mark(pickSquare); | ||
// make the div not able to be clickable again | ||
|
||
// case statement to determine which squaresArray to pass to the score method | ||
switch (pickSquare.id) { | ||
case "one": | ||
var squaresArray = ["A", "G", "D"] | ||
break; | ||
case "two": | ||
var squaresArray = ["A", "F"] | ||
break; | ||
case "three": | ||
var squaresArray = ["A", "E", "H"] | ||
break; | ||
|
||
case "four": | ||
var squaresArray = ["B", "G"] | ||
break; | ||
case "five": | ||
var squaresArray = ["B", "F", "H", "D"] | ||
break; | ||
case "six": | ||
var squaresArray = ["B", "E"] | ||
break; | ||
|
||
case "seven": | ||
var squaresArray = ["C", "G", "H"] | ||
break; | ||
case "eight": | ||
var squaresArray = ["C", "F", "H", "D"] | ||
break; | ||
case "nine": | ||
var squaresArray = ["C", "E", "D"] | ||
break; | ||
}; | ||
// increment the count for the score | ||
game.score(squaresArray); | ||
// populate div at top of page to show who has won | ||
$("h1").html(game.winner); | ||
}); | ||
|
||
}) | ||
|
||
</script> | ||
</html> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,36 @@ | ||
function TicTacToe() { | ||
var TicTacToe = function() { | ||
this.markers = ["X", "O", "X", "O", "X", "O", "X", "O", "X", "O"]; | ||
this.i = 0; | ||
this.xScore = {A: 0, B: 0, C: 0, D: 0, E: 0, F: 0, G: 0, H: 0}; | ||
this.oScore = {A: 0, B: 0, C: 0, D: 0, E: 0, F: 0, G: 0, H: 0}; | ||
this.winner = "Tic Tac Toe"; | ||
}; | ||
|
||
} | ||
// prototype that iterated through the markers | ||
TicTacToe.prototype.mark = function (changeSquare) { | ||
var symbol = this.markers[this.i]; | ||
$(changeSquare).html(symbol); | ||
}; | ||
|
||
TicTacToe.prototype = { | ||
|
||
} | ||
TicTacToe.prototype.score = function (squaresArray) { | ||
// use the appropriate score hash | ||
if (this.i % 2 == 0) { | ||
var scoreChart = this.xScore; | ||
} else { | ||
var scoreChart = this.oScore; | ||
} | ||
// in the score hash add one to the value of the key that corresponds to the letter coming in from the squaresArray | ||
var gameThis = this; | ||
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. Compliment time: All of your variables have been clearly and concisely named. gameThis is a little awkward and could be more clearly named. |
||
squaresArray.forEach(function(letter) { | ||
if (scoreChart[letter] == 2) { | ||
// the game is over | ||
gameThis.winner = gameThis.markers[gameThis.i] + " wins!"; | ||
} | ||
scoreChart[letter] += 1; | ||
}) | ||
this.i++ | ||
if (this.i == 9){ | ||
// the game is over | ||
this.winner = "CAT: No Winner!"; | ||
} | ||
}; | ||
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. Beautiful precise logic here, well done! |
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.
lines 1-3 are unused, you could remove those