forked from AdaGold/jquery-tic-tac-toe
-
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
Kld/master #4
Open
kdefliese
wants to merge
12
commits into
Ada-C4:kld/master
Choose a base branch
from
kdefliese:kld/master
base: kld/master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kld/master #4
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4f434b4
added basic game structure
kdefliese dd30c47
added click function
kdefliese f7d504c
added turn logic
kdefliese 1016580
added on click handler to index page
kdefliese 12cdbf1
tiles working by player turns
kdefliese 8947c21
gameplay stops when a player gets 3 in a row
kdefliese 3526a94
added some css
kdefliese 17d53ba
fixed it so extra clicks don't count towards the turn total
kdefliese f2a9d4a
added new game button and function
kdefliese 611cfab
winner displayed after each game
kdefliese cd2c44f
draw also displayed
kdefliese b15dae4
simplified turn function
kdefliese File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,60 @@ | ||
html { | ||
|
||
body { | ||
background-color: #80d4ea; | ||
font-family: 'Oxygen', sans-serif; | ||
color: white; | ||
} | ||
|
||
h1 { | ||
margin: auto; | ||
text-align: center; | ||
padding-bottom: 20px; | ||
} | ||
|
||
table, td { | ||
border: 2px solid white; | ||
margin: auto; | ||
text-align: center; | ||
} | ||
|
||
table { | ||
border-collapse: collapse; | ||
} | ||
|
||
td { | ||
width: 100px; | ||
height: 100px; | ||
} | ||
|
||
button { | ||
margin: auto; | ||
display: block; | ||
margin-top: 20px; | ||
font-size: 22px; | ||
padding: 10px; | ||
border: 2px solid white; | ||
background-color: #80d4ea; | ||
color: white; | ||
} | ||
|
||
button:hover { | ||
background-color: #36A0BE; | ||
} | ||
|
||
.player_one { | ||
margin-left: 100px; | ||
float: left; | ||
font-size: 22px; | ||
} | ||
|
||
.player_two { | ||
margin-right: 100px; | ||
float: right; | ||
font-size: 22px; | ||
} | ||
|
||
.draw { | ||
margin: auto; | ||
text-align: center; | ||
font-size: 22px; | ||
margin-top: 20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,124 @@ | ||
function TicTacToe() { | ||
|
||
this.board = ["0","1","2","3","4","5","6","7","8"]; | ||
this.state = "new game"; | ||
this.turn_count = 0; | ||
this.player_one = "X"; | ||
this.player_two = "O"; | ||
this.whose_turn = this.player_one; | ||
} | ||
|
||
TicTacToe.prototype = { | ||
|
||
} | ||
TicTacToe.prototype.turn = function(cell_id) { | ||
if (this.checkGameOver() === true) { | ||
return; | ||
} | ||
else if (this.checkCellTaken(cell_id) === true) { | ||
return; | ||
} | ||
else { | ||
this.markCell(cell_id, this.whose_turn); | ||
this.incrementTurn(); | ||
var winner = this.checkWin()[1]; | ||
this.checkGameOver(); | ||
if (this.state == "game over") { | ||
this.displayWinner(winner); | ||
} | ||
} | ||
}; | ||
|
||
TicTacToe.prototype.checkGameOver = function() { | ||
if (this.state == "game over") { | ||
return true; | ||
} | ||
else if (this.turn_count == 9) { | ||
this.state = "game over"; | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
}; | ||
|
||
TicTacToe.prototype.checkCellTaken = function(cell_id) { | ||
if (this.board[cell_id] == this.player_one || this.board[cell_id] == this.player_two) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
|
||
TicTacToe.prototype.markCell = function(cell, player) { | ||
if (this.board[cell.toString()] === cell.toString()) { | ||
$("#" + cell).append(player); | ||
this.board[cell.toString()] = player; | ||
} | ||
}; | ||
|
||
TicTacToe.prototype.incrementTurn = function() { | ||
this.turn_count += 1; | ||
if (this.whose_turn == this.player_one) { | ||
this.whose_turn = this.player_two; | ||
} | ||
else { | ||
this.whose_turn = this.player_one; | ||
} | ||
}; | ||
|
||
TicTacToe.prototype.checkWin = function() { | ||
var winner; | ||
if (this.board[0] == this.board[1] && this.board[1] == this.board[2]) { | ||
this.state = "game over"; | ||
winner = this.board[0]; | ||
} | ||
else if (this.board[3] == this.board[4] && this.board[4] == this.board[5]) { | ||
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. Is there a way to DRY this up with function calls and iteration? |
||
this.state = "game over"; | ||
winner = this.board[3]; | ||
} | ||
else if (this.board[6] == this.board[7] && this.board[7] == this.board[8]) { | ||
this.state = "game over"; | ||
winner = this.board[6]; | ||
} | ||
else if (this.board[0] == this.board[3] && this.board[3] == this.board[6]) { | ||
this.state = "game over"; | ||
winner = this.board[0]; | ||
} | ||
else if (this.board[1] == this.board[4] && this.board[4] == this.board[7]) { | ||
this.state = "game over"; | ||
winner = this.board[1]; | ||
} | ||
else if (this.board[2] == this.board[5] && this.board[5] == this.board[8]) { | ||
this.state = "game over"; | ||
winner = this.board[2]; | ||
} | ||
else if (this.board[2] == this.board[4] && this.board[4] == this.board[6]) { | ||
this.state = "game over"; | ||
winner = this.board[2]; | ||
} | ||
else if (this.board[0] == this.board[4] && this.board[4] == this.board[8]) { | ||
this.state = "game over"; | ||
winner = this.board[0]; | ||
} | ||
var return_vals = [this.state, winner]; | ||
return return_vals; | ||
}; | ||
|
||
TicTacToe.prototype.displayWinner = function(winner) { | ||
if (winner == "X") { | ||
$(".player_one").append("Player One wins!"); | ||
} | ||
else if (winner == "O") { | ||
$(".player_two").append("Player Two wins!"); | ||
} | ||
else if (winner === undefined) { | ||
$(".draw").append("Draw!"); | ||
} | ||
}; | ||
|
||
TicTacToe.prototype.newGame = function() { | ||
game = new TicTacToe(); | ||
for (var i = 0; i < 9; i++) { | ||
$("#" + i.toString()).html(""); | ||
} | ||
$(".player_one").html(""); | ||
$(".player_two").html(""); | ||
$(".draw").html(""); | ||
return game; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure if this is just a preference thing, but I'd extract this javascript into its own file rather than including it in the html! 😆