Skip to content
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
17,155 changes: 17,155 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 6 additions & 13 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import React, { Component } from 'react';
import logo from './logo.svg';
import Jeu from './Jeu';


import './App.css';

class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>

<Jeu />

</header>
</div>
);
Expand Down
12 changes: 12 additions & 0 deletions src/Case.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React, { Component } from 'react';


export default class Case extends React.Component {
render() {
return (
<button className="cases" onClick={this.props.onClick}>
{this.props.value}
</button>
);
}
}
82 changes: 82 additions & 0 deletions src/Jeu.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React, { Component } from 'react';
import Plateau from './Plateau';
import calculateWinner from './history';


export default class Jeu extends Component {

constructor(props) {
super(props);
this.state = {
history: [{
cases: Array(9).fill(null),
}],
stepNumber: 0,
xIsNext: true
};
}
handleClick(i) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const cases = current.cases.slice();
if (calculateWinner(cases) || cases[i]) {
return;
}
cases[i] = this.state.xIsNext ? "X" : "O";
this.setState({
history: history.concat([
{
cases: cases
}
]),
stepNumber: history.length,
xIsNext: !this.state.xIsNext
});
}

jumpTo(step) {
this.setState({
stepNumber: step,
xIsNext: (step % 2) === 0
});
}

render() {
const history = this.state.history;
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.cases);

const moves = history.map((step, move) => {
const desc = move ?
'Mouvement #' + move :
'Nouvelle partie';
return (
<li key={move}>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});

let status;
if (winner) {
status = "Vainqueur: " + winner;
} else {
status = "Joueur Suivant: " + (this.state.xIsNext ? "X" : "O");
}

return (
<div className="jeu">
<div className="jeu-board">
<Plateau
cases={current.cases}
onClick={i => this.handleClick(i)}
/>
</div>
<div className="jeu-info">
<div>{status}</div>
<ol>{moves}</ol>
</div>
</div>
);
}
}
79 changes: 79 additions & 0 deletions src/Plateau.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, { Component } from 'react';
import Case from './Case';
import index from './index';
import calculateWinner from './history';

export default class Plateau extends React.Component {

constructor(props) {
super(props);
this.state = {
cases: Array(9).fill(null),
xIsNext: true,
};
}

handleClick(i) {
const history = this.state.history;
const current = history[history.length - 1];
const cases = current.cases.slice();
if (calculateWinner(cases) || cases[i]) {
return;
}

cases[i] = this.state.xIsNext ? 'X' : 'O';
document.getElementById.style.cursor = "default";



this.setState({
history: history.concat([{
casees: cases,
}]),
xIsNext: !this.state.xIsNext,
});
}


renderCase(i) {
return (
<Case
value={this.props.cases[i]}
onClick={() => this.props.onClick(i)}
/>
);
}



render() {
const winner = calculateWinner(this.state.cases);
let status;
if (winner) {
status = 'Vainqueur: ' + winner;
} else {
status = 'Prochain Joueur: ' + (this.state.xIsNext ? 'X' : 'O');
}

return (
<div>
<div className="status">{status}</div>
<div className="plateau-row">
{this.renderCase(0)}
{this.renderCase(1)}
{this.renderCase(2)}
</div>
<div className="plateau-row">
{this.renderCase(3)}
{this.renderCase(4)}
{this.renderCase(5)}
</div>
<div className="plateau-row">
{this.renderCase(6)}
{this.renderCase(7)}
{this.renderCase(8)}
</div>
</div>
);
}
}
47 changes: 47 additions & 0 deletions src/history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// history = [
// // Avant le premier mouvement
// {
// cases: [
// null, null, null,
// null, null, null,
// null, null, null,
// ]
// },
// // Après le premier mouvement
// {
// cases: [
// null, null, null,
// null, 'X', null,
// null, null, null,
// ]
// },
// // Apres le second mouvement
// {
// cases: [
// null, null, null,
// null, 'X', null,
// null, null, 'O',
// ]
// },
// // ...
// ]

export default function calculateWinner(cases) {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6],
];
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (cases[a] && cases[a] === cases[b] && cases[a] === cases[c]) {
return cases[a];
}
}
return null;
}
20 changes: 20 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,23 @@ code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}

.case{
background: #fff;
border: 1px solid #999;
float: left;
font-size: 24px;
font-weight: bold;
line-height: 34px;
margin-right: -1px;
margin-top: -1px;
padding: 0;
text-align: center;
height: 70px;
width: 70px;
cursor: pointer;
}

.inactive{
background-color: red;
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as serviceWorker from './serviceWorker';

ReactDOM.render(<App />, document.getElementById('root'));


// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: http://bit.ly/CRA-PWA
Expand Down