forked from rolling-scopes-school/tic-tac-toe
-
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.
- Loading branch information
Andrei Volchenko
committed
Jan 30, 2017
0 parents
commit b532912
Showing
9 changed files
with
4,997 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,52 @@ | ||
### Tic Tac Toe | ||
|
||
--- | ||
⚠️ DO NOT SUBMIT PRS WITH SOLUTIONS TO THIS REPO ⚠️ | ||
|
||
### Description | ||
|
||
Your task is to implement `TicTacToe` class | ||
|
||
#### Methods: | ||
|
||
##### `getCurrentPlayerSymbol()` | ||
should return `x` or `o` | ||
|
||
##### `nextTurn(row, col)` | ||
should properly update class state (change current player, update marks storage etc.) | ||
|
||
##### `isFinished()` | ||
should return true if game is finished (e.g. there is a winner or it is a draw) | ||
|
||
##### `getWinner()` | ||
should return winner symbol (`x` or `o`) or null if there is no winner yet | ||
|
||
##### `noMoreTurns()` | ||
should return true if there is no more fields to place a `x` or `y` | ||
|
||
##### `isDraw()` | ||
should return true if there is no more turns and no winner | ||
|
||
##### `getFieldValue(row, col)` | ||
should return `matrix[row][col]` value (if any) or `null` | ||
|
||
### Prerequisites | ||
* Install [nodejs](https://nodejs.org/en/) (>= v6.9.4) | ||
* open bash in this folder | ||
* `npm install` | ||
|
||
### Run tests | ||
```sh | ||
npm test | ||
``` | ||
|
||
### Run in browser | ||
```sh | ||
npm start | ||
``` | ||
|
||
open http://localhost:8080 | ||
|
||
--- | ||
|
||
© [R1ZZU](https://github.com/R1ZZU) |
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,68 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Document</title> | ||
<style> | ||
html { | ||
height: 100%; | ||
} | ||
|
||
body { | ||
height: 100%; | ||
margin: 0; | ||
font-family: sans-serif; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: space-around; | ||
} | ||
|
||
#gameCanvas { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
|
||
.row { | ||
display: flex; | ||
flex-direction: row; | ||
} | ||
|
||
.row:first-child { | ||
border-top: 1px solid #000; | ||
} | ||
|
||
.row:last-child { | ||
border-bottom: 1px solid #000; | ||
} | ||
|
||
.column { | ||
width: 50px; | ||
height: 50px; | ||
border: 1px solid #000; | ||
cursor: pointer; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.column:first-child { | ||
border-left: 2px solid #000; | ||
} | ||
|
||
.column:last-child { | ||
border-right: 2px solid #000; | ||
} | ||
|
||
.column:hover { | ||
background: #eeeeee; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="gameCanvas"></div> | ||
<button id="resetBtn">Reset</button> | ||
<script src="./app.bundle.js"></script> | ||
</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,58 @@ | ||
const TicTacToe = require('./src/tic-tac-toe.js'); | ||
|
||
window.game = new TicTacToe(); | ||
|
||
resetBtn.addEventListener('click', () => { | ||
window.game = new TicTacToe(); | ||
render(); | ||
}); | ||
|
||
function render() { | ||
let html = ''; | ||
|
||
for (let i = 0; i < 3; i++) { | ||
html += '<div class="row">'; | ||
|
||
for (let j = 0; j < 3; j++) { | ||
html += `<div class="column">${game.getFieldValue(i, j) || ''}</div>`; | ||
} | ||
|
||
html += '</div>'; | ||
} | ||
|
||
gameCanvas.innerHTML = html; | ||
} | ||
|
||
render(); | ||
|
||
gameCanvas.addEventListener('click', e => { | ||
if (!e.target.classList.contains('column')) { | ||
return; | ||
} | ||
|
||
const rowIndex = Array.from(gameCanvas.children).indexOf(e.target.parentNode); | ||
const colIndex = Array.from(e.target.parentNode.children).indexOf(e.target); | ||
|
||
game.nextTurn(rowIndex, colIndex); | ||
|
||
const winner = game.getWinner(); | ||
const isDraw = game.isDraw(); | ||
|
||
render(); | ||
|
||
if (winner) { | ||
setTimeout(() => { | ||
alert(`${winner} won!`); | ||
window.game = new TicTacToe(); | ||
render(); | ||
}, 10) | ||
} | ||
|
||
if (isDraw) { | ||
setTimeout(() => { | ||
alert(`It's a draw`); | ||
window.game = new TicTacToe(); | ||
render(); | ||
}, 10); | ||
} | ||
}) |
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,19 @@ | ||
{ | ||
"name": "tic-tac-toe", | ||
"version": "1.0.0", | ||
"description": "", | ||
"scripts": { | ||
"test": "mocha -r ./test/setup-mocha.js", | ||
"start": "webpack-dev-server" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"mocha": "^3.0.2", | ||
"sinon": "^1.17.5", | ||
"sinon-chai": "^2.8.0", | ||
"webpack": "^1.13.1", | ||
"webpack-dev-server": "^1.14.1" | ||
} | ||
} |
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,14 @@ | ||
{ | ||
"TicTacToe": { | ||
"weight": 100, | ||
"suitesWeights": { | ||
"#getCurrentPlayerSymbol": 3, | ||
"#getFieldValue": 2, | ||
"#isDraw": 5, | ||
"#noMoreTurns": 5, | ||
"#getWinner": 40, | ||
"#isFinished": 5, | ||
"#nextTurn": 40, | ||
} | ||
} | ||
} |
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,35 @@ | ||
class TicTacToe { | ||
constructor() { | ||
|
||
} | ||
|
||
getCurrentPlayerSymbol() { | ||
|
||
} | ||
|
||
nextTurn(rowIndex, columnIndex) { | ||
|
||
} | ||
|
||
isFinished() { | ||
|
||
} | ||
|
||
getWinner() { | ||
|
||
} | ||
|
||
noMoreTurns() { | ||
|
||
} | ||
|
||
isDraw() { | ||
|
||
} | ||
|
||
getFieldValue(rowIndex, colIndex) { | ||
|
||
} | ||
} | ||
|
||
module.exports = TicTacToe; |
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,7 @@ | ||
const chai = require('chai'); | ||
const sinon = require('sinon'); | ||
const sinonChai = require('sinon-chai'); | ||
|
||
global.expect = chai.expect; | ||
global.sinon = sinon; | ||
chai.use(sinonChai); |
Oops, something went wrong.