Skip to content
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

Audrey's tic-tac-toe #10

Open
wants to merge 23 commits into
base: ald/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c546346
Adding images and classes for stormtrooper and vader
Dreedle Jan 28, 2016
9e787c7
Can make a new TicTacToe
Dreedle Jan 28, 2016
b522868
Can begin a game
Dreedle Jan 28, 2016
fbb228b
Fixing how prototype is laid out
Dreedle Jan 28, 2016
46d6ee5
On clicking a square, can get to the board
Dreedle Jan 28, 2016
6626598
Working on using jquery alone to identify which box was clicked and w…
Dreedle Jan 28, 2016
60568db
Clicking a square returns the coordinates of it's spot in the table
Dreedle Jan 28, 2016
79e39a8
Returning coordinates as a hash now
Dreedle Jan 28, 2016
670c852
In the middle of figuring out player toggling
Dreedle Jan 29, 2016
828295c
Able to play out game taking turns.
Dreedle Jan 29, 2016
34df349
Separated some logic into a function to for completeing a turn
Dreedle Jan 29, 2016
a705b0c
Able to mark array and update board with player names
Dreedle Jan 29, 2016
3f95461
Moving a bit of code around for organization
Dreedle Jan 29, 2016
1ca07ba
Adding logic to end the game
Dreedle Jan 29, 2016
15ebfbe
Players cannot click a square more than once
Dreedle Jan 29, 2016
54408ee
Can recognize a horizonal win
Dreedle Jan 29, 2016
fbc175e
I think checking for a game win is working
Dreedle Jan 29, 2016
a6831a0
Game appears to work, and shows a message saying winner when there is…
Dreedle Jan 29, 2016
60313c7
Restarting a game is working
Dreedle Jan 29, 2016
2c41d16
Adding a tiny bit of css (background image)
Dreedle Jan 29, 2016
f8d3d1c
Working on making a tie. Winners are coming up opposite
Dreedle Jan 29, 2016
d1dcbd0
shows correct winner, except for tie
Dreedle Jan 30, 2016
0f9fd0c
The tie case works, and able to restart game on clicking the board.
Dreedle Jan 30, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
html {

}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to remove these lines if not needed.


body {
background-image: url("stars.jpg");
}

h1 {
text-align: center;
color: white;
}

h2 {
text-align: center;
color: white;
}

.hide {
display: none;
}

table {
background-color: white;
border: 3px solid black;
margin: 0 auto;
}

table tbody tr td {
width: 100px;
height: 100px;
background-size: cover;
background-position: center;
}

.trooper {
background-image: url("stormtrooper.jpg");
}

.vader {
background-image: url("vader.jpg");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSS is easy to read and well formatted! ⭐

44 changes: 40 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,51 @@
</head>

<body>
<h1>Tic Tac Toe</h1>
<div id="tic-tac-toe"></div>
<h1>Dark Side Tic Tac Toe</h1>
<div id="tic-tac-toe">
<table>
<tbody>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</tbody>
</table>
<h2 class="gameover hide">
<p class="vader-win hide">VADER WINS!</p>
<p class="trooper-win hide">TROOPER WINS!</p>
<p class="tie hide">NO ONE WINS!</p>
</h2>
</div>
</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!');
tictac = new TicTacToe();
tictac.begin();
})
</script>
</html>


</html>
Binary file added stars.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added stormtrooper.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
156 changes: 153 additions & 3 deletions tic-tac-toe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,157 @@
function TicTacToe() {
//click handler
//if a game has just ended, there will be a winner. Will start a new game.
//if a game is in progress, check to see that square is empty
//if I do apply more styling to tds, would need to change this logic
$("td").click(function () {
if (tictac.currentWinner != false) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In JS it's more common to use the !== operator.

tictac.begin();
} else if (this.classList.length == 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When checking for equality, === is used more often in JS.

tictac.takeTurn(this);
}
});


////////////////////////////////

//game logic
function TicTacToe () {
};



//start game
//if previous games have been played, this will erase all images
TicTacToe.prototype.begin = function(){
this.updateBoard([[0,0,0], [0,0,0],[0,0,0]]);
this.updatePlayer("trooper");
this.updateWinner(false);
$(".gameover").addClass("hide");
$(".gameover").children("p").addClass("hide");
$("td").removeClass("vader trooper");
};


//logic for a single turn
TicTacToe.prototype.takeTurn = function(clickedSquare){
var coords = this.getCoords(clickedSquare);
var nextBoard = this.markArray(coords);
this.updateBoard(nextBoard);
this.markSquare(clickedSquare);
this.checkGame(); //checks to see if game is won
if (this.currentWinner !== false){
this.endGame(this.currentWinner);
} else {
this.changePlayer();
}
};


//defines who the current player is
TicTacToe.prototype.updatePlayer = function(nextPlayer){
this.currentPlayer = nextPlayer;
};

//defines who the current winner is
TicTacToe.prototype.updateWinner = function(nextWinner){
this.currentWinner = nextWinner;
}

TicTacToe.prototype = {

//defines what the current board is
TicTacToe.prototype.updateBoard = function(nextBoard) {
this.currentBoard = nextBoard;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job using SRP and making your functions do one specific thing.


//gets the coordinates from which square was clicked
TicTacToe.prototype.getCoords = function(square) {
var rowInd = (square.parentNode).rowIndex;
var colInd = square.cellIndex;
var coordinates = {row: rowInd, col: colInd};
return coordinates
};

//checks to see if game is finished
TicTacToe.prototype.checkGame = function () {
var board = this.currentBoard;
var win = false;

//identify combos that need to be checked
//situation is simply enough that we don't really need to iterate
var row1 = board[0];
var row2 = board[1];
var row3 = board[2];
var col1 = [row1[0], row2[0], row3[0]];
var col2 = [row1[1], row2[1], row3[1]];
var col3 = [row1[2], row2[2], row3[2]];
var diag1 = [row1[0], row2[1], row3[2]];
var diag2 = [row1[2], row2[1], row3[0]];

//initialize an array that will store combos to be checked
var combos = [row1, row2, row3, col1, col2, col3, diag1, diag2]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This array logic is pretty cool. It's a little difficult to follow but gets the job done. Are there ways you can simplify this?


//get rid of any combo that has a zero in it
var nonzeroCombos = new Array;
combos.forEach(function(combo) {
if (combo[0] != 0 && combo[1] != 0 && combo[2] != 0) {
nonzeroCombos.push(combo);
}
return nonzeroCombos;
});

//check to see if any combos contain all the same value
nonzeroCombos.forEach(function(combo) {
if (combo[0] === combo[1] && combo[1] === combo[2]) {
win = true;
}
return win;
});

if (win == true) {
this.updateWinner(this.currentPlayer);
}

//if there are no combos with zero, and no winner, the board must be full
if (nonzeroCombos.length == 8 && this.currentWinner == false) {
this.updateWinner("tie");
}
};

//changes display when the game is over
//new game isn't started until the next click on a box
TicTacToe.prototype.endGame = function (winner) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

winner is passed in as an argument but is never used. Did you intend to use it?

if (this.currentWinner == "vader") {
$(".vader-win").removeClass("hide");
} else if (this.currentWinner == "trooper") {
$(".trooper-win").removeClass("hide");
} else {
$(".tie").removeClass("hide");
};
$(".gameover").removeClass("hide");
};

//checks to see if that spot is occupied
//changes the array contents based on square that was clicked
//returns array that can be used to update the board
//board is marked with player name
TicTacToe.prototype.markArray = function(coords) {
var board = this.currentBoard;
var row = coords["row"];
var col = coords["col"];
if (board[row][col] == 0) {
board[row][col] = this.currentPlayer;
}
return board;
};

//alternates player
TicTacToe.prototype.changePlayer = function() {
if (this.currentPlayer == "trooper") {
this.updatePlayer("vader");
} else if (this.currentPlayer == "vader") {
this.updatePlayer("trooper");
}
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.currentPlayer === "trooper" ? this.updatePlayer("vader") : this.updatePlayer("trooper")

This can be written as a one liner if you'd like.


//actually marks the square in the browser
TicTacToe.prototype.markSquare = function(square) {
square.setAttribute("class", this.currentPlayer);
};
Binary file added vader.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.