-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (156 loc) · 3.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
function calulateWinner(squares) {
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 (squares[a] && squares[a] === squares[b] && squares[b] === squares[c]) {
return {winner: squares[a],line:[a,b,c]};
}
}
return {winner:null, line:[]};
}
class Square extends React.Component {
render() {
if(this.props.highlight){
return (
<button className="square" onClick={() => this.props.onClick()} style={{color:"red"}}>
{this.props.value}
</button>
)
}else{
return (
<button className="square" onClick={() => this.props.onClick()}>
{this.props.value}
</button>
);
}
}
}
class Board extends React.Component {
renderSquare(i) {
return (<Square
key={i}
onClick={() => this.props.onClick(i)}
value={this.props.squares[i]}
highlight={this.props.winnerLine.includes(i)}
/>
);
}
render() {
let rows = [];
for (let i = 0; i < 3; i++) {
let row = [];
for (var j = 3 * i; j < 3 * i + 3; j++) {
row.push(this.renderSquare(j));
}
rows.push(<div className="board-row" key={i}> {row} </div>);
}
return (
<div>
{rows}
</div>
);
}
}
class Game extends React.Component {
constructor() {
super();
this.state = {
history: [{
squares: Array(9).fill(null),
lastStep: 'Game start'
}],
xIsNext: true,
stepNumber: 0,
sort: false
}
}
jumpTo(step) {
this.setState({
stepNumber: step,
xIsNext: (step % 2) ? false : true
})
}
handClick(i) {
const history = this.state.history;
const current = history[this.state.stepNumber];
const squares = current.squares.slice();
if (calulateWinner(squares).winner || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? 'X' : 'O';
const location = '(' + (Math.floor(i / 3) + 1) + ',' + ((i % 3) + 1) + ')';
const desc = squares[i] + ' movedTo' + location;
this.setState({
history: history.concat([{
squares: squares,
lastStep: desc
}]),
stepNumber: history.length,
xIsNext: !this.state.xIsNext
});
}
toggleSort(){
this.setState({sort: !this.state.sort});
}
render() {
let history = this.state.history;
const current = history[this.state.stepNumber];
const winner = calulateWinner(current.squares).winner;
const winnerLine = calulateWinner(current.squares).line;
let status;
if (winner) {
status = 'Winner: ' + winner;
} else {
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
}
if(this.state.sort){
history = this.state.history.slice();
history.reverse();
}
const moves = history.map((step, move) => {
const desc = step.lastStep;
if(move === this.state.stepNumber){
return (
<li key={move}>
<a href="#" onClick={() => this.jumpTo(move)}><strong>{desc}</strong></a>
</li>
);
}else{
return (
<li key={move}>
<a href="#" onClick={() => this.jumpTo(move)}>{desc}</a>
</li>
);
}
});
return (
<div className="game">
<div className="game-board">
<Board squares={current.squares} onClick={(i) => this.handClick(i)} winnerLine={winnerLine} />
</div>
<div className="game-info">
<div>{status}</div>
<button onClick = {()=> this.toggleSort()}>Sort</button>
<ol>{moves}</ol>
</div>
</div>
);
}
}
// ========================================
ReactDOM.render(
<Game />,
document.getElementById('root')
);