-
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 Toe #2
base: ked/master
Are you sure you want to change the base?
Tic Tac Toe #2
Changes from all commits
c13ff14
8775c22
1e6e86f
76a8626
1bd6f6b
80cc514
1b75085
5589ae7
29ed00e
0c1e257
11fbe44
4b9c154
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,51 @@ | ||
html { | ||
|
||
background: url(./jungle.jpg) no-repeat center center fixed; | ||
-webkit-background-size: cover; | ||
-moz-background-size: cover; | ||
-o-background-size: cover; | ||
background-size: cover; | ||
} | ||
|
||
#board_title { | ||
padding-bottom: 1%; | ||
padding-top: 10%; | ||
} | ||
|
||
h1 { | ||
text-align: center; | ||
font-family: arial; | ||
} | ||
|
||
p { | ||
text-align: center; | ||
} | ||
|
||
table { | ||
border-collapse: collapse; | ||
width: 30%; | ||
margin: auto; | ||
} | ||
|
||
table td { | ||
border: 1px solid black; | ||
} | ||
|
||
table tr:first-child td { | ||
border-top: 0; | ||
} | ||
|
||
table tr:last-child td { | ||
border-bottom: 0; | ||
} | ||
|
||
table tr td:first-child { | ||
border-left: 0; | ||
} | ||
|
||
table tr td:last-child { | ||
border-right: 0; | ||
} | ||
|
||
td { | ||
padding: 10%; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,86 @@ | ||
function TicTacToe() { | ||
|
||
this.board = [[0, 0, 0], | ||
[0, 0, 0], | ||
[0, 0, 0] | ||
]; | ||
this.player_1 = new Player(1, "Ham", "🐷"); | ||
this.player_2 = new Player(2, "Abu", "🐵"); | ||
this.current_player = this.player_1; | ||
this.winning_sequence = null; | ||
this.turnCount = 0; | ||
} | ||
|
||
TicTacToe.prototype = { | ||
|
||
function Player(number, name, marker) { | ||
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 function can be outside of the TicTacToe prototype since it represents a different type of object. |
||
this.number = number; | ||
this.name = name; | ||
this.marker = marker; | ||
} | ||
|
||
TicTacToe.prototype.playTurn = function(row, column) { | ||
this.board[row][column] = this.current_player.number; | ||
this.checkWinningBoard(); | ||
this.turnCount++; | ||
if (this.winning_sequence) { | ||
$("td").empty(); | ||
$(".board_footer").empty(); | ||
$(".row_" + this.winning_sequence[0][0] + ".column_" + this.winning_sequence[0][1]).text(this.current_player.marker); | ||
$(".row_" + this.winning_sequence[1][0] + ".column_" + this.winning_sequence[1][1]).text(this.current_player.marker); | ||
$(".row_" + this.winning_sequence[2][0] + ".column_" + this.winning_sequence[2][1]).text(this.current_player.marker); | ||
$( "table").after("<h1>" + this.current_player.name + " Wins!" + "</h1>"); | ||
} else if (this.turnCount === 9) { | ||
$(".board_footer").empty(); | ||
$( "table").after("<h1>Game Over</h1>"); | ||
} else { | ||
this.switch_player(); | ||
} | ||
}; | ||
|
||
TicTacToe.prototype.switch_player = function() { | ||
if (this.current_player === this.player_1) { | ||
this.current_player = this.player_2; | ||
} else { | ||
this.current_player = this.player_1; | ||
} | ||
}; | ||
|
||
TicTacToe.prototype.checkWinningBoard = function() { | ||
if ((this.board[0][0] === this.current_player.number) && (this.board[0][1] === this.current_player.number) && (this.board[0][2] === this.current_player.number)) { | ||
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 wonder if you could put all of these row/col values in an array and iterate over it to slightly more dynamically create this winning sequence instead of using this many conditionals. |
||
this.winning_sequence = [[0, 0], [0, 1], [0, 2]]; | ||
} else if ((this.board[1][0] === this.current_player.number) && (this.board[1][1] === this.current_player.number) && (this.board[1][2] === this.current_player.number)) { | ||
this.winning_sequence = [[1,0], [1,1], [1,2]]; | ||
} else if ((this.board[2][0] === this.current_player.number) && (this.board[2][1] === this.current_player.number) && (this.board[2][2] === this.current_player.number)) { | ||
this.winning_sequence = [[2,0], [2,1], [2,2]]; | ||
} else if ((this.board[0][0] === this.current_player.number) && (this.board[1][0] === this.current_player.number) && (this.board[2][0] === this.current_player.number)) { | ||
this.winning_sequence = [[0,0], [1,0], [2,0]]; | ||
} else if ((this.board[0][1] === this.current_player.number) && (this.board[1][1] === this.current_player.number) && (this.board[2][1] === this.current_player.number)) { | ||
this.winning_sequence = [[0,1], [1,1], [2,1]]; | ||
} else if ((this.board[0][2] === this.current_player.number) && (this.board[1][2] === this.current_player.number) && (this.board[2][2] === this.current_player.number)) { | ||
this.winning_sequence = [[0,2], [1,2], [2,2]]; | ||
} else if ((this.board[0][0] === this.current_player.number) && (this.board[1][1] === this.current_player.number) && (this.board[2][2] === this.current_player.number)) { | ||
this.winning_sequence = [[0,0], [1,1], [2,2]]; | ||
} else if ((this.board[0][2] === this.current_player.number) && (this.board[1][1] === this.current_player.number) && (this.board[2][0] === this.current_player.number)) { | ||
this.winning_sequence = [[0,2], [1,1], [2,0]]; | ||
} | ||
}; | ||
|
||
$(document).ready(function() { | ||
var game = new TicTacToe(); | ||
|
||
var make_clickable = function(row, column) { | ||
$(".row_" + row + ".column_" + column).click(function() { | ||
if (game.board[row][column] !== 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. What happens to the users experience when this error is thrown? |
||
} | ||
$(this).text(game.current_player.marker); | ||
game.playTurn(row,column); | ||
}); | ||
}; | ||
|
||
for (var i = 0; i < (game.board.length); i++) { | ||
var row = game.board[i]; | ||
for (var j = 0; j < (row.length); j++) { | ||
make_clickable(i, j); | ||
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 like the way you add this click handler to each row/col dynamically |
||
} | ||
} | ||
|
||
}); |
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.
I like the way you've encapsulated the player properties in a separate object