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

Jillian's AstroPup-tastic Tic Tac Toe Game #9

Open
wants to merge 5 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
Binary file added images/cat.jpeg
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 images/pup1.jpeg
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 images/pup2.jpeg
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 images/space.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 47 additions & 1 deletion index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
html {

background-image: url(images/space.jpeg);
margin: auto;
width: 60%;
padding: 10px;
text-align: center;
color: white;
}

button {
height: 100px;
width: 100px;
padding: 10px;
border-style: hidden;
opacity: .5;
}

.middle {
margin: .2em;
}

button:hover {
opacity: 0;
}

.X {
transition: .5s;
background-size: 100px 100px;
background-image: url(images/pup1.jpeg);
opacity: 1;
}

.O {
transition: .5s;
background-size: 100px 100px;
background-image: url(images/pup2.jpeg);
opacity: 1;
}

.C {
transition: .5s;
background-size: 100px 100px;
background-image: url(images/cat.jpeg);
opacity: 1;
}

.X:hover, .O:hover, .C:hover {
opacity: 1;
}
27 changes: 21 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,28 @@
<body>
<h1>Tic Tac Toe</h1>
<div id="tic-tac-toe"></div>
<div class="game-control">
<!-- <button class="new-game">New Game!</button> -->
</div>
<div class="gameboard">
<div class="top">
<button class="play" data-grid="1"></button>
<button class="play" data-grid="2"></button>
<button class="play" data-grid="3"></button>
</div>
<div class="middle">
<button class="play" data-grid="4"></button>
<button class="play" data-grid="5"></button>
<button class="play" data-grid="6"></button>
</div>
<div class="bottom">
<button class="play" data-grid="7"></button>
<button class="play" data-grid="8"></button>
<button class="play" data-grid="9"></button>
</div>
</div>
</body>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.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!');
})
</script>
</html>

88 changes: 85 additions & 3 deletions tic-tac-toe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,89 @@
function TicTacToe() {
function TicTacToe(callback) {
this._player_X = 'X'
this._player_O = 'O'
this._plays = 0
this._current_player = this._player_X
this._gameboard = {
1: 'a',
2: 'b',
3: 'c',
4: 'd',
5: 'e',
6: 'f',
7: 'g',
8: 'h',
9: 'i'
}
}

TicTacToe.prototype.makePlay = function(spot) {
this._plays++

if ((this._gameboard[spot] !== 'X') && (this._gameboard[spot] !== 'O')) {
this._gameboard[spot] = this._current_player
} else {
return alert("This space has already been played!")
}

var result = this.hasWon()

if (result === true) {
var winner = this._current_player
this.turnSwitch()
var loser = this._current_player

$('.' + loser).switchClass(loser, winner)
$('.play').switchClass('play', winner)

} else if ((result === false) && (this._plays === 9)) {
$('.X').switchClass('X', 'C')
$('.O').switchClass('O', 'C')
} else {
this.turnSwitch()
}
}

TicTacToe.prototype.hasWon = function() {
var board = this._gameboard

if ((board[1] === board[2]) && (board[1] === board[3])) {
return true
} else if ((board[4] === board[5]) && (board[4] === board[6])) {
return true
} else if ((board[7] === board[8]) && (board[7] === board[9])) {
return true
} else if ((board[1] === board[4]) && (board[1] === board[7])) {
return true
} else if ((board[2] === board[5]) && (board[2] === board[8])) {
return true
} else if ((board[3] === board[6]) && (board[3] === board[9])) {
return true
} else if ((board[1] === board[5]) && (board[1] === board[9])) {
return true
} else if ((board[3] === board[5]) && (board[3] === board[7])) {
return true
} else {
return false
}
}

TicTacToe.prototype = {

TicTacToe.prototype.turnSwitch = function () {
if (this._current_player === this._player_X) {
this._current_player = this._player_O
} else {
this._current_player = this._player_X
}
}

$(document).ready(function() {

var ttt = new TicTacToe

$('.play').mousedown(function(event) {
var button = $(this).data('grid')
$(this).switchClass('play', ttt._current_player)

ttt.makePlay(button, ttt._current_player)

})
})