-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up skeleton js for console vsn of game
- Loading branch information
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
function Gameboard() { | ||
const gameboard = [ | ||
[null, null, null], | ||
[null, null, null], | ||
[null, null, null] | ||
]; | ||
|
||
gameboard.forEach(row => { | ||
row.forEach(square => { | ||
square = SquareObject(); | ||
}); | ||
}); | ||
|
||
function SquareObject() { | ||
const square = {}; | ||
square.value = null; | ||
square.tokenCheck = false; | ||
|
||
const addToken = token => { | ||
if (!square.tokenCheck) { | ||
return null; | ||
} | ||
|
||
square.value = token; | ||
square.tokenCheck = true; | ||
} | ||
|
||
const getValue = () => square.value; | ||
|
||
return { addToken, getValue }; | ||
} | ||
|
||
const dropToken = (row, col, player) => { | ||
if (!gameboard[row][col].addToken(player.token)) { | ||
return null; | ||
} | ||
} | ||
|
||
const getBoard = () => gameboard; | ||
|
||
const printBoard = () => { | ||
currentBoard = gameboard.map(row => row.map(square => SquareObject.getValue())); | ||
} | ||
|
||
|
||
return { getBoard, printBoard, dropToken }; | ||
}; | ||
|
||
const Player = (name, token) => { | ||
const player = {}; | ||
player.name = name; | ||
player.token = token; | ||
|
||
return player; | ||
|
||
} | ||
|
||
|
||
const GameController = (function () { | ||
const PlayerOne = Player("PlayerOne", "x"); | ||
const PlayerTwo = Player("PlayerTwo", "o"); | ||
|
||
let activePlayer; | ||
|
||
const game = Gameboard(); | ||
|
||
const switchPlayer = () => { | ||
activePlayer = activePlayer !== PlayerTwo ? PlayerOne : PlayerTwo; | ||
} | ||
|
||
const getActivePlayer = () => activePlayer; | ||
|
||
const printNewRound = () => { | ||
game.printBoard(); | ||
console.log(`${getActivePlayer().name}'s turn.`) | ||
} | ||
|
||
const playRound = (row, col) => { | ||
game.dropToken(row, col, activePlayer.token); | ||
console.log(`Dropping ${getActivePlayer().token} onto square ${row}${col}.`) | ||
|
||
//win logic here | ||
|
||
switchPlayer(); | ||
printNewRound(); | ||
} | ||
|
||
printNewRound; | ||
return { playRound, getActivePlayer }; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<script src="./app.js" defer></script> | ||
<link rel="stylesheet" href="style.css"> | ||
<title>tic-tac-toe</title> | ||
</head> | ||
|
||
<body> | ||
<div class="board-container"> | ||
<div class="grid-row"> | ||
<div class="grid-square"></div> | ||
<div class="grid-square"></div> | ||
<div class="grid-square"></div> | ||
</div> | ||
<div class="grid-row"> | ||
<div class="grid-square"></div> | ||
<div class="grid-square"></div> | ||
<div class="grid-square"></div> | ||
</div> | ||
<div class="grid-row"> | ||
<div class="grid-square"></div> | ||
<div class="grid-square"></div> | ||
<div class="grid-square"></div> | ||
</div> | ||
</div> | ||
|
||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
body { | ||
--board-size: 800px; | ||
} | ||
|
||
div {} | ||
|
||
.board-container { | ||
height: var(--board-size); | ||
width: var(--board-size); | ||
|
||
display: flex; | ||
flex-direction: column; | ||
|
||
border: 3px solid black; | ||
} | ||
|
||
.grid-row { | ||
display: flex; | ||
flex: 0 0 1; | ||
} | ||
|
||
.grid-square { | ||
height: calc(var(--board-size)/3); | ||
width: calc(var(--board-size)/3); | ||
border: .5px solid black; | ||
} |