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

finished game #3

Open
wants to merge 2 commits 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
34 changes: 21 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- bootstrap css -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">


<!-- custom styles -->
<link rel="stylesheet" type="text/css" href="main.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script type="text/javascript" src="main.js"></script>

<title>Tic Tac Toe</title>
</head>
<body>
<div class="container text-center">
<div class="row">
<div class="col-md-12">
<h1>Tic Tac Toe</h1>
<h2><button type="button" class="btn btn-danger">reset the game</button></h2>
<hr>
</div>
</div>
Expand All @@ -28,27 +36,27 @@ <h1>Tic Tac Toe</h1>

<!-- you can nest rows inside of columns! -->
<!-- our board has 3 rows, each with 3 columns inside -->
<div class="row">
<div class="col-xs-4 box"></div>
<div class="col-xs-4 box"></div>
<div class="col-xs-4 box"></div>
<div class="row" id="top_row">
<div class="col-xs-4 box" id="1"></div>
<div class="col-xs-4 box" id="2"></div>
<div class="col-xs-4 box" id="3"></div>
</div>

<div class="row">
<div class="col-xs-4 box"></div>
<div class="col-xs-4 box"></div>
<div class="col-xs-4 box"></div>
<div class="row" id="mid_row">
<div class="col-xs-4 box" id="4"></div>
<div class="col-xs-4 box" id="5"></div>
<div class="col-xs-4 box" id="6"></div>
</div>

<div class="row">
<div class="col-xs-4 box"></div>
<div class="col-xs-4 box"></div>
<div class="col-xs-4 box"></div>
<div class="row" id="bot_row">
<div class="col-xs-4 box" id="7"></div>
<div class="col-xs-4 box" id="8"></div>
<div class="col-xs-4 box" id="9"></div>
</div>
</div>
</div>
</div>

<!-- jquery -->

<!-- custom script -->
Expand Down
19 changes: 18 additions & 1 deletion main.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,21 @@
.box {
border: 1px solid black;
height: 150px;
}
}

.board {
border: 2px solid black;
}

.box {
font-size: 125px;

}

.xColor {
color: red;
}

.oColor {
color: blue;
}
93 changes: 89 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,91 @@
// wait for DOM to load before running JS
$(function() {

// your code here
$(document).ready(function() {
$('.btn').click(function clearBoard(){
$('.box').empty();
$('.box').removeClass('oColor');
$('.box').removeClass('xColor');
turn = 1;
moveCount= 0;

});


});

var moveCount = 0;
var playerX = 'x';
var playerO = 'o';
var turn = 1;
//click to place X or O.. also cannot overwrite
$('.box').click(function userInput() {
if($(this).text()=== "") {
if(turn===1) {
$(this).text(playerX);
$(this).addClass('xColor');
turn =2 ;
} else if (turn===2) {
$(this).text(playerO);
$(this).addClass('oColor');
turn =1 ;
}
moveCount++;
checkWin();
console.log(moveCount);
} else {
alert('That box is already taken!');
}
});


//check for winner
var checkWin = function () {
var topL = $('#1').text();
var topM = $('#2').text();
var topR = $('#3').text();
var midL = $('#4').text();
var midM = $('#5').text();
var midR = $('#6').text();
var botL = $('#7').text();
var botM = $('#8').text();
var botR = $('#9').text();

var xWins = 0;
var oWins = 0;
/* visualize the board
var board = [[topL, topM, topR],
[midL, midM, midR],
[botL, botM, botR]];
*/
var winningCombo= [[topL, topM, topR], //rows
[midL, midM, midR],
[botL, botM, botR],
[topL, midM, botR], //diagonal
[topR, midM, botL],
[topL, midL, botL],//columns
[topM, midM, botM],
[topR, midR, botR]];
for ( var i = 0; i< winningCombo.length; i++) {
if (winningCombo[i][0] !== "") {
if (winningCombo[i][0]===winningCombo[i][1] && winningCombo[i][1]===winningCombo[i][2]) {
if (winningCombo[i][0] === "x") {
xWins ++;
alert('we have a winner!! The winner is X');
}else if (winningCombo[i][0] === "o") {
oWins ++;
alert('we have a winnner!! The winner is O');
}
}
}
}




};



//I made variables to display move count, Xwins, and Owins, but did not have enough time to put them into be displayed on the html page.



});