Skip to content

Commit

Permalink
🐣 I'm born
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Volchenko committed Jan 30, 2017
0 parents commit b532912
Show file tree
Hide file tree
Showing 9 changed files with 4,997 additions and 0 deletions.
52 changes: 52 additions & 0 deletions README.md
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)
68 changes: 68 additions & 0 deletions index.html
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>
58 changes: 58 additions & 0 deletions index.js
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);
}
})
19 changes: 19 additions & 0 deletions package.json
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"
}
}
14 changes: 14 additions & 0 deletions score-weight.json
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,
}
}
}
35 changes: 35 additions & 0 deletions src/tic-tac-toe.js
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;
7 changes: 7 additions & 0 deletions test/setup-mocha.js
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);
Loading

0 comments on commit b532912

Please sign in to comment.