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
JLN js-tic-tac-toe #19
Open
noglows
wants to merge
21
commits into
Ada-C4:jln/master
Choose a base branch
from
noglows:jln/master
base: jln/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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4535498
Basic HTML and CSS for tic tac toe board
noglows 961b752
Clicking the boxes changes their color
noglows 66aee0f
Two different player colors
noglows b937422
Boxes can not be changed once they are picked
noglows a4186bb
Reset button restores the initial color to all squares
noglows 2149d0d
Moved things into the prototype
noglows 2bc4463
Working game over logic
noglows d3a50f3
Cleaned up the code a little bit
noglows a8809b9
Win/Lose logic sort of working
noglows 3b1b6d6
More win/loss logic
noglows 6aedb98
Working scoring logic
noglows b50af31
Deleting accidentally uploaded node modules
noglows e3bc1c2
Fixed the color switching issue
noglows 0658598
Keeping track of number of wins for each player
noglows 0fd6433
Small performance improvement
noglows 80e5b1a
DRYed up scoring logic
noglows 13a67c6
Further DRYing of code
noglows ec62df1
CSS fixes
noglows e93c0cd
Additional CSS styling
noglows b157dc1
CSS
noglows 1542679
More refactoring
noglows 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,70 @@ | ||
html { | ||
|
||
font-family: 'Poiret One', cursive; | ||
} | ||
|
||
h1 { | ||
text-align: center; | ||
font-size: 80px; | ||
margin: 0; | ||
} | ||
|
||
#tictactoe { | ||
width: 60%; | ||
margin-left: auto; | ||
margin-right: auto; | ||
} | ||
|
||
.box { | ||
width: 32%; | ||
height: 100%; | ||
display:inline-block; | ||
background-color: blue; | ||
} | ||
|
||
#row1, #row2, #row3 { | ||
margin-top: 5px; | ||
width: 100%; | ||
height: 200px; | ||
} | ||
|
||
.c1 { | ||
margin-left: 10px; | ||
} | ||
|
||
#player-1-score, #player-2-score { | ||
display: inline-block; | ||
text-align: center; | ||
} | ||
|
||
#player-1-score { | ||
margin-right: 40px; | ||
} | ||
|
||
#player-2-score { | ||
margin-left: 40px; | ||
} | ||
|
||
#player-scores { | ||
width: 600px; | ||
margin: auto; | ||
text-align: center; | ||
font-size: 30px; | ||
} | ||
|
||
#reset-button { | ||
font-family: 'Poiret One', cursive; | ||
font-size: 30px; | ||
width: 200px; | ||
height:50px; | ||
text-align: center; | ||
margin:0 auto; | ||
padding: 10px; | ||
border-radius: 10px; | ||
-moz-border-radius: 10px; | ||
-webkit-border-radius: 10px; | ||
} | ||
|
||
#button-holder { | ||
text-align: center; | ||
margin-bottom: 30px; | ||
} |
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,114 @@ | ||
function TicTacToe() { | ||
|
||
function TicTacToe() { | ||
this.pickSpaces(); | ||
this.resetButton(); | ||
} | ||
|
||
TicTacToe.prototype = { | ||
|
||
} | ||
board_spaces: ["r1c1", "r1c2", "r1c3", "r2c1", "r2c2", "r2c3", "r3c1", "r3c2", "r3c3"], | ||
player_1_score: [ 0, 0, 0, 0, 0, 0, 0, 0 ], | ||
player_2_score: [ 0, 0, 0, 0, 0, 0, 0, 0 ], | ||
player_1_wins: 0, | ||
player_2_wins: 0, | ||
moves: 0, | ||
player: 1, | ||
|
||
pickSpaces: function(){ | ||
var self = this; | ||
$("#player-1-wins").html(this.player_1_wins); | ||
$("#player-2-wins").html(this.player_2_wins); | ||
self.board_spaces.forEach(function(box) { | ||
$("." + box).click(function() { | ||
if ($("." + box).css('background-color') == 'rgb(0, 0, 255)') { | ||
self.markSpaces(self.player, box); | ||
} | ||
if (self.moves == 9) { | ||
alert("It's a tie!"); | ||
self.resetBoard(); | ||
} | ||
}); | ||
}); | ||
}, | ||
|
||
markSpaces: function(player, box) { | ||
var color = (player == 1 ? 'red' : 'yellow'); | ||
$("." + box).css('background-color', color); | ||
this.moves++; | ||
this.updateScore(box, player); | ||
this.player = (player == 1 ? 2 : 1); | ||
}, | ||
|
||
resetBoard: function(){ | ||
this.board_spaces.forEach(function(box) { | ||
$("." + box).css('background-color', 'blue'); | ||
$("." + box).off('click'); | ||
}); | ||
this.pickSpaces(); | ||
this.player_1_score = [ 0, 0, 0, 0, 0, 0, 0, 0 ]; | ||
this.player_2_score = [ 0, 0, 0, 0, 0, 0, 0, 0 ]; | ||
this.moves = 0; | ||
}, | ||
|
||
resetButton: function(){ | ||
var self = this; | ||
$('#reset-button').click(function() { | ||
self.resetBoard(); | ||
}); | ||
}, | ||
|
||
incrementScore: function(index, player) { | ||
var self = this; | ||
if (player == 1) { | ||
self.player_1_score[index]++; | ||
if (self.player_1_score[index] == 3) { | ||
self.playerWin(1); | ||
} | ||
} else { | ||
self.player_2_score[index]++; | ||
if (self.player_2_score[index] == 3) { | ||
self.playerWin(2); | ||
} | ||
} | ||
}, | ||
|
||
playerWin: function(player) { | ||
alert("Player " + player + " wins!"); | ||
this.resetBoard(); | ||
this.trackWins(player); | ||
}, | ||
|
||
updateScore: function(space, player) { | ||
var track_space = space.toString(); | ||
var self = this; | ||
if(track_space.substring(0,2) == "r1") { | ||
self.incrementScore(0, player); | ||
} else if (track_space.substring(0,2) == "r2") { | ||
self.incrementScore(1, player); | ||
} else if (track_space.substring(0,2) == "r3") { | ||
self.incrementScore(2, player); | ||
} | ||
if (track_space.substring(2,4) == "c1") { | ||
self.incrementScore(3, player); | ||
} else if (track_space.substring(2,4) == "c2") { | ||
self.incrementScore(4, player); | ||
} else if (track_space.substring(2,4) == "c3") { | ||
self.incrementScore(5, player); | ||
} | ||
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. To clean up these else if statements, you could use JavaScript's switch statement. It's very similar to Ruby's case statement! |
||
if (["r1c1", "r2c2", "r3c3"].includes(track_space)) { | ||
self.incrementScore(6, player); | ||
} | ||
if (["r1c3", "r2c2", "r3c1"].includes(track_space)) { | ||
self.incrementScore(7, player); | ||
} | ||
}, | ||
|
||
trackWins: function(player) { | ||
if (player == 1) { | ||
this.player_1_wins++; | ||
$("#player-1-wins").html(this.player_1_wins); | ||
} else { | ||
this.player_2_wins++; | ||
$("#player-2-wins").html(this.player_2_wins); | ||
} | ||
}, | ||
}; |
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.
Ternary! 👍