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

added a few things, but wasn't able to refine it as much as I wanted to. #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
body {
text-align: center;
font-family: helvetica;
}

table {
Expand Down Expand Up @@ -34,3 +35,10 @@ table.board {
.board tr:last-child td {
border-bottom: none;
}

#reset-btn {
padding: 10px;
margin-top: 20px;
font-size: 14px;
background-color: white;
}
34 changes: 18 additions & 16 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</head>
<body>
<h1>Tic Tac Toe</h1>
<h2>Current Player: <span>1</span></h2>
<h2>Current Player: <span id="playerNumber">1</span></h2>

<table class="score">
<thead>
Expand All @@ -18,34 +18,36 @@ <h2>Current Player: <span>1</span></h2>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0</td>
<td id="player-1-score">0</td>
<td id="player-2-score">0</td>
</tr>
</tbody>
</table>

<table class="board">
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<tr id="rowA">
<td id="1" class="box"></td>
<td id="2" class="box"></td>
<td id="3" class="box"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<tr id="rowB">
<td id="4" class="box"></td>
<td id="5" class="box"></td>
<td id="6" class="box"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<tr id="rowC">
<td id="7" class="box"></td>
<td id="8" class="box"></td>
<td id="9" class="box"></td>
</tr>
</tbody>
</table>

<button id="reset-btn" type="button" name="Reset">Reset Board</button>

<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>

<script src="js/script.js" charset="utf-8"></script>
</body>
</html>
69 changes: 69 additions & 0 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
$(document).ready(function(){
// DOM has loaded

var playerOne = 'X';
var playerTwo = 'O';
var currentPlayer = playerOne;
var turns = 0, maxTurns = 9;
var playerScore = 0;

$('.box').on('click', function() {
// Set the current player's marker
$(this).text(currentPlayer);

// Increment the number of turns
turns++;

// Check if the user is a winner...
if (isWinner('#1','#2', '#3') || // row 1
isWinner('#4','#5', '#6') || // row 2
isWinner('#7','#8', '#9') || // row 3
isWinner('#1','#4', '#7') || // col 1
isWinner('#2','#5', '#8') || // col 2
isWinner('#3','#6', '#9') || // col 3
isWinner('#1','#5', '#9') || // diag 1
isWinner('#3','#5', '#7')) { // diag 2
alert("Tic-Tac-Toe Winner!");
} else {
// If the max number of turns were reached but no winner...
if (turns >= maxTurns) {
alert("Tie! It's a cat's game - mew!");
return;
}

// If game is in play, but no winner yet...
switchPlayer();
}

// Remove click event so each box can only be clicked once
$(this).off('click');
});

// Winner function
function isWinner(id1, id2, id3) {
return $(id1).text() && $(id1).text() === $(id2).text() && $(id2).text() === $(id3).text();
}

/* if ($(id1).text() === playerOne && $(id1).text() === $(id2).text() && $(id2).text() === $(id3).text()); {
playerScore++;
$('#player-1-score').text(playerScore);
};
} */

// Switch player function that is referenced above
function switchPlayer () {
if (currentPlayer === playerOne) {
currentPlayer = playerTwo;
$('#playerNumber').text(2);
} else {
currentPlayer = playerOne;
$('#playerNumber').text(1);
}
}

// Reload page
$('#reset-btn').on('click', function(){
window.location.reload(true);
// $('#board')location.reload(true);
});

}); //document ready end