-
Notifications
You must be signed in to change notification settings - Fork 23
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
sailor moon tic tac toe #3
base: master
Are you sure you want to change the base?
Changes from all commits
b100f9b
fbb1874
8c3d065
5e278fa
0fd7369
bacfd0f
283630e
2984bd7
1bd1e93
cd368e5
b031693
cc50be6
a917c79
70f46ae
9a57acc
fae115e
8e96921
5bce54b
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,88 @@ | ||
html { | ||
|
||
text-align: center; | ||
font-family: 'Delius Unicase', cursive; | ||
} | ||
|
||
body { | ||
background-image: url("starsbg.png"); | ||
} | ||
|
||
/*** | ||
TITLE AND MESSAGE | ||
***/ | ||
|
||
h1 { | ||
font-size: 4rem; | ||
margin-top: 0rem; | ||
margin-bottom: 0rem; | ||
color: #B345B9; | ||
} | ||
|
||
#message { | ||
font-size: 1.5rem; | ||
margin-top: 0rem; | ||
margin-bottom: .5rem; | ||
color: #F04DB5; | ||
font-weight: bold; | ||
} | ||
|
||
/*** | ||
GAME BOARD | ||
***/ | ||
|
||
/*entire table*/ | ||
#game-board { | ||
width: 40%; | ||
font-size: 6rem; | ||
border-collapse: collapse; | ||
border-spacing: 0; | ||
margin-top: 3rem; | ||
margin-left: auto; | ||
margin-right: auto; | ||
} | ||
|
||
/*size of each space*/ | ||
td { | ||
padding: 5rem; | ||
cursor: pointer; | ||
} | ||
|
||
/*hover img over space*/ | ||
.space_hover { | ||
background-image: url("moon.png"); | ||
background-size: 40%; | ||
background-repeat: no-repeat; | ||
background-position: center; | ||
} | ||
|
||
/*horizontal lines*/ | ||
#top-row, #middle-row { | ||
border-bottom: .5rem solid #A908E2; | ||
} | ||
|
||
/*vertical lines*/ | ||
#space-1, #space-4, #space-7, #space-2, #space-5, #space-8 { | ||
border-right: .5rem solid #BB29EF; | ||
} | ||
|
||
/*** | ||
SPACE MARKERS | ||
***/ | ||
|
||
/*player 1 space marker*/ | ||
.player_1 { | ||
background-image: url("locket.png"); | ||
background-size: 85%; | ||
background-repeat: no-repeat; | ||
background-position: center; | ||
transition: all .3s ease-in; | ||
} | ||
|
||
/*player 2 space marker*/ | ||
.player_2 { | ||
background-image: url("locket2.png"); | ||
background-size: 85%; | ||
background-repeat: no-repeat; | ||
background-position: center; | ||
transition: all .3s ease-in; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,121 @@ | ||
$(document).on('ready', function() { | ||
|
||
var game = new TicTacToe(); | ||
|
||
function updateMessage(message) { | ||
$('#message').text(message) | ||
} | ||
|
||
$('td').hover( | ||
function(){ $(this).addClass('space_hover') }, | ||
function(){ $(this).removeClass('space_hover') } | ||
) | ||
|
||
$('td').click(function() { | ||
var space = $(this) | ||
var board = $('#game-board') | ||
|
||
if (game.isSpaceEmpty(space)) { | ||
if (game._current_player === "Player 2") { | ||
space.addClass("player_2") | ||
if (game.checkIfWon(board,"player_2")) { | ||
alert(game._current_player + " won!") | ||
game.reset(board,game) | ||
updateMessage("Your turn, " + game._current_player) | ||
} else { | ||
if (game.checkIfDraw(board)) { | ||
alert("It's a draw!") | ||
game.reset(board,game) | ||
updateMessage("Your turn, " + game._current_player) | ||
} | ||
game.changePlayer() | ||
updateMessage("Your turn, " + game._current_player) | ||
} | ||
} else { | ||
space.addClass("player_1") | ||
if (game.checkIfWon(board,"player_1")) { | ||
alert(game._current_player + " won!") | ||
game.reset(board,game) | ||
updateMessage("Your turn, " + game._current_player) | ||
} else { | ||
if (game.checkIfDraw(board)) { | ||
alert("It's a draw!") | ||
game.reset(board,game) | ||
updateMessage("Your turn, " + game._current_player) | ||
} | ||
game.changePlayer() | ||
updateMessage("Your turn, " + game._current_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. I noticed from line 18 to here there is some repetition depending on whose turn it is. I wonder if a helper method could have been created to reduce the repetition. Like a |
||
} | ||
} | ||
}) | ||
}) | ||
|
||
|
||
function TicTacToe() { | ||
this._current_player = "Player 1" | ||
this._message = $('#message').text("Your turn, " + this._current_player) | ||
|
||
} | ||
this.changePlayer = function() { | ||
if (this._current_player === "Player 1") { | ||
this._current_player = "Player 2" | ||
} else { | ||
this._current_player = "Player 1" | ||
} | ||
} | ||
|
||
this.isSpaceEmpty = function(td) { | ||
if (td.hasClass('player_2') || td.hasClass('player_1')) { | ||
return false | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
this.checkIfWon = function(board, player) { | ||
var won = false | ||
if(board.find('#space-1').hasClass(player) && board.find('#space-2').hasClass(player) && board.find('#space-3').hasClass(player)) { | ||
won = true | ||
} else if (board.find('#space-1').hasClass(player) && board.find('#space-4').hasClass(player) && board.find('#space-7').hasClass(player)) { | ||
won = true | ||
} else if (board.find('#space-1').hasClass(player) && board.find('#space-5').hasClass(player) && board.find('#space-9').hasClass(player)) { | ||
won = true | ||
} else if (board.find('#space-4').hasClass(player) && board.find('#space-5').hasClass(player) && board.find('#space-6').hasClass(player)) { | ||
won = true | ||
} else if (board.find('#space-7').hasClass(player) && board.find('#space-8').hasClass(player) && board.find('#space-9').hasClass(player)) { | ||
won = true | ||
} else if (board.find('#space-2').hasClass(player) && board.find('#space-5').hasClass(player) && board.find('#space-8').hasClass(player)) { | ||
won = true | ||
} else if (board.find('#space-3').hasClass(player) && board.find('#space-6').hasClass(player) && board.find('#space-9').hasClass(player)) { | ||
won = true | ||
} else if (board.find('#space-3').hasClass(player) && board.find('#space-5').hasClass(player) && board.find('#space-7').hasClass(player)) { | ||
won = true | ||
} | ||
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.
|
||
return won; | ||
} | ||
|
||
this.checkIfDraw = function(board) { | ||
var draw = false | ||
if(board.find('#space-1').is(".player_1,.player_2") && board.find('#space-2').is(".player_1,.player_2") && board.find('#space-3').is(".player_1,.player_2")) { | ||
if(board.find('#space-4').is(".player_1,.player_2") && board.find('#space-5').is(".player_1,.player_2") && board.find('#space-6').is(".player_1,.player_2")) { | ||
if(board.find('#space-7').is(".player_1,.player_2") && board.find('#space-8').is(".player_1,.player_2") && board.find('#space-9').is(".player_1,.player_2")) { | ||
var draw = true | ||
} | ||
} | ||
} | ||
return draw | ||
} | ||
|
||
this.reset = function(board,game) { | ||
if (game._current_player === "Player 1") { | ||
game._current_player = "Player 2" | ||
} else { | ||
game._current_player = "Player 1" | ||
} | ||
|
||
board.find('td').each(function() { | ||
$(this).removeClass('player_1').removeClass('player_2'); | ||
}); | ||
} | ||
|
||
TicTacToe.prototype = { | ||
|
||
} |
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.
This commenting style looks helpful! Especially if you've got a long file of css!