-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
186 lines (181 loc) · 5.52 KB
/
game.js
File metadata and controls
186 lines (181 loc) · 5.52 KB
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
180
181
182
183
184
185
186
'use strict';
class Square extends React.Component {
render() {
return (
<button className={`square ${this.props.isLastPosition ? ' last-position' : ''}` +
`${this.props.isWinPos ? ' win-position' : ''}` +
`${this.props.isTipPos ? ' tip-position' : ''}`} onClick={this.props.onClick}>
{this.props.value}
</button>
);
}
}
class Board extends React.Component {
renderSquare(i, j){
let isLastPosition = false;
if(this.props.lastPosition
&& this.props.lastPosition.r == i
&& this.props.lastPosition.c == j){
isLastPosition = true;
}
let isWinPos = false;
if(this.props.winPos){
for(let k = 0; k < this.props.winPos.length; k++){
if(i == this.props.winPos[k].r
&& j == this.props.winPos[k].c){
isWinPos = true;
break;
}
}
}
let isTipPos = false;
if(this.props.tipPosition){
if(i == this.props.tipPosition.r
&& j == this.props.tipPosition.c){
isTipPos = true;
}
}
return <Square
value={this.props.squares[i][j]}
onClick={() => this.props.onClick(i, j)}
isLastPosition={isLastPosition}
isWinPos={isWinPos}
isTipPos={isTipPos}
key={`${i}-${j}`}
/>;
}
render() {
const squareRows = [];
for(let i = 0; i < this.props.squares.length; i++){
let row = [];
for(let j = 0; j < this.props.squares[i].length; j++){
row.push(this.renderSquare(i, j));
}
squareRows.push(<div key={i} className="board-row">{row}</div>);
}
return (
<div>
{squareRows}
</div>
);
}
}
class Game extends React.Component {
winner = null;
constructor(props){
super(props);
this.state = this.genInitState();
}
genInitState(){
let squares = [];
for(let i = 0; i < this.props.rowSize; i++){
squares.push(Array(this.props.colSize).fill(null));
}
return {
squares: squares,
xIsNext: true,
xIscomputer: randomBoolean(),
lastStep: null,
lastPosition: null,
tipPosition: null
};
}
initGame(){
this.setState(this.genInitState());
this.winner = null;
}
handleClick(i, j){
this.check(i, j);
}
//落子
check(i, j){
if(this.state.squares[i][j]){
return;
}
const squares = deepClone2DimArray(this.state.squares);
const lastStep = !this.isComputerStep() ? deepClone2DimArray(squares) : this.state.lastStep;
squares[i][j] = this.state.xIsNext ? 'X' : 'O';
this.setState({
squares: squares,
xIsNext: !this.state.xIsNext,
lastStep: lastStep,
lastPosition: {r:i, c:j},
tipPosition: null
});
}
//悔棋
toLastStep(){
this.setState({
squares: this.state.lastStep,
lastStep: null,
tipPosition: null
});
}
//提示
tip(){
let checkedPoint = getBestEvaluatePoint(this.state.squares, this.props.rowSize, this.props.colSize, this.props.targetCount, this.state.xIscomputer ? 'X' : 'O');
this.setState({
tipPosition: {r:checkedPoint.r, c:checkedPoint.c}
});
}
//当前是否电脑步骤
isComputerStep(){
return ((this.state.xIsNext && this.state.xIscomputer) || (!this.state.xIsNext && !this.state.xIscomputer));
}
//电脑行动
computerAction(){
let checkedPoint = getBestEvaluatePoint(this.state.squares, this.props.rowSize, this.props.colSize, this.props.targetCount, this.state.xIscomputer ? 'X' : 'O');
this.check(checkedPoint.r, checkedPoint.c);
}
componentDidMount(){
if(this.isComputerStep()){
this.computerAction();
}
}
componentDidUpdate(){
if(!this.winner && this.isComputerStep()){
this.computerAction();
}
}
render(){
let winObj = calculateWinner(this.state.squares, this.props.rowSize, this.props.colSize, this.props.targetCount);
let winPos = null;
if(winObj){
this.winner = winObj.winner;
winPos = winObj.winPos;
}else{
this.winner = null;
}
return (
<div className="game">
<div className="game-board-ctn">
<div className={`game-board cursor-${this.state.xIsNext ? 'X' : 'O'}`}>
<Board squares={this.state.squares} onClick={(i, j) => this.handleClick(i, j)}
lastPosition={this.state.lastPosition} winPos={winPos} tipPosition={this.state.tipPosition}/>
</div>
{this.winner ? <div className="mask-panel"><span>{this.winner}胜利!</span></div> : null}
{this.isComputerStep() && !this.winner ? <div className="mask-panel"><span>{this.winner}对方正在思考...</span></div> : null}
</div>
<div className="game-info">
<div> 我:{this.state.xIscomputer ? 'O' : 'X'}</div>
<div>电脑:{this.state.xIscomputer ? 'X' : 'O'}</div>
<br/>
{this.state.lastStep && !this.winner && !this.isComputerStep() ? <div className='oper-btn'><button onClick={() => this.toLastStep()}>悔棋</button></div> : null}
{!this.winner && !this.isComputerStep() ? <div className='oper-btn'><button onClick={() => this.tip()}>提示</button></div> : null}
{this.winner ? <div className='oper-btn'><button onClick={() => this.initGame()}>再来一局</button></div> : null}
</div>
</div>
);
}
}
function deepClone2DimArray(array){
let newArray = [];
for(let i = 0; i < array.length; i++){
let [...newRow] = array[i];
newArray.push(newRow);
}
return newArray;
}
function randomBoolean(){
return Math.random() >= 0.5;
}