Skip to content

Commit

Permalink
adding tictactoe challenge, almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
immber committed Apr 23, 2015
1 parent 5c88f06 commit 0a7cedb
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
158 changes: 158 additions & 0 deletions javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
$(document).ready(function() {

currentGame = new Board();
gameType = setGameType();
xMoves = [];
oMoves = [];
placeFirstX();
bindEvents();

});

function bindEvents(){
$(".corner, .edge, .center").on('click', function(event) {
if (currentGame.legalMove(this.id)){
currentGame.playerMove(this.id);
} else {
alert("You can't do that, that spot is already taken")
}
});
}

function setGameType(){
var rand = Math.random() >= .5;
if (rand == 0) {
gameType = "CENTER"
}else {
gameType = "CORNER"
};
return gameType
}

function placeFirstX(){
if (gameType == "CENTER") {
currentGame.computerMove(5)
} else if (gameType == "CORNER") {
var corners = [1,3,7,9];
var corner = corners[(Math.floor(Math.random() * 4))];
currentGame.computerMove(corner);
}
};



function Board(){
this.gameBoard = ["-","-","-","-","-","-","-","-","-"];
this.magicSquare = [2, 7, 6, 9, 5, 1, 4, 3, 8]
}

Board.prototype.legalMove = function(space) {
return this.gameBoard[space-1] == "-";
}

Board.prototype.playerMove = function(space) {
this.gameBoard[space -1] = "o";
$("#" + space).html("o");
updatePlayerMoves(space);
findPossibleMoves();
};

Board.prototype.computerMove = function(space) {
this.gameBoard[space -1] = "x";
$("#" + space).html("x");
updateComputerMoves(space);
this.keepPlaying()
};

function updatePlayerMoves(space) {
var value = currentGame.magicSquare[space-1];
oMoves.push(value);
// console.log("o moves " + oMoves.toString());

}

function updateComputerMoves(space) {
var value = currentGame.magicSquare[space-1];
xMoves.push(value);
// console.log("x moves " + xMoves.toString());
}


Board.prototype.keepPlaying = function(){
//check for tictactoe
for (i=0; i < this.gameBoard.length; i++) {
console.log('divided ' + (Math.floor(i / 3)))
console.log('mod ' + (Math.floor(i % 3)))
}

// var row1 = this.gameBoard.slice(0, 3)
// console.log(row1)
// var row2 = this.gameBoard.slice(3, 6)
// console.log(row2)
// var row3 = this.gameBoard.slice(6, 9)
// console.log(row3)

// var xTotal = 0
// var oTotal = 0
// for (i = 0; i < this.gameBoard.length; i++) {
// if (this.gameBoard[i] == "x") {
// xTotal += (i + 1);
// } else if (this.gameBoard[i] == "o") {
// oTotal += (i + 1);
// }
// }
// if ((xScore == 15) || (oScore == 15)) {
// alert("There is a winner! Refresh the page to play again")
// } else {
return true
// }
}

function pickNextLargestNum() {
var oneToNine = [1,2,3,4,5,6,7,8,9]
var taken = (xMoves.concat(oMoves)).join("")
// console.log('taken are ' + taken)
for (i = 0; i < taken.length; i++) {
// console.log(taken[i])
var index = oneToNine.indexOf(parseInt(taken[i]))
oneToNine.splice(index, 1)
}
// console.log('heres whats left ' + oneToNine)
var nextMove = oneToNine.pop()
// console.log('next move should be magic ' + nextMove)
var space = (currentGame.magicSquare.indexOf(nextMove) + 1)
// console.log('thats in space ' + space)
return space
}

function findPossibleMoves () {
// console.log(xMoves.length)
if (xMoves.length == 1) {
//find the next highest number in the magic square that is available and use that
// console.log("first move")
var space = pickNextLargestNum()
currentGame.computerMove(space)
//put the win spot into an array for later
// var xScore = parseInt(xMoves[0]) + parseInt(xMoves[1])

} else if (xMoves.length >= 2) {
// console.log('second move')
//find possible moves by blocking or winning
//check for win option first, block if can't win
var xScore = parseInt(xMoves[0]) + parseInt(xMoves[1])
var winSpot = currentGame.magicSquare.indexOf(15-xScore) + 1
if (currentGame.legalMove(winSpot)) {
currentGame.computerMove(winSpot)
} else {
var oScore = parseInt(oMoves[0]) + parseInt(oMoves[1])
var blockSpace = currentGame.magicSquare.indexOf(15 - oScore) + 1
if (currentGame.legalMove(blockSpace)) {
currentGame.computerMove(blockSpace)
} else {
var space = pickNextLargestNum();
currentGame.computerMove(space);
}
}
}
}

7 changes: 7 additions & 0 deletions stylesheet.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
td {
border: 1px solid black;
height: 100px;
width: 100px;
text-align: center;

}
31 changes: 31 additions & 0 deletions tictactoe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<html>
<head>
<title>tictactoe</title>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
</head>
<body>
<h3>Welcome to tic-tac-toe</h3>
<p>you are player o, go ahead just try to beat me...</p>
<table>
<tr id="row1">
<td class="corner" id="1"></td>
<td class="edge" id="2"></td>
<td class="corner" id="3"></td>
</tr>
<tr id="row2" >
<td class="edge" id="4"></td>
<td class="center" id="5"></td>
<td class="edge" id="6"></td>
</tr>
<tr id="row3">
<td class="corner" id="7"></td>
<td class="edge" id="8"></td>
<td class="corner" id="9"></td>
</tr>
</table>
</table>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="javascript.js"></script>

</body>
</html>

0 comments on commit 0a7cedb

Please sign in to comment.