-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.js
141 lines (121 loc) · 5.04 KB
/
Game.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
// It costs me a week to learn React step by step.
// I think [React tic-tac-toc tutorial](https://reactjs.org/tutorial/tutorial.html) logic is not so good,
// and i don't like class so that i refrator tutorial using [React hooks](https://reactjs.org/docs/hooks-intro.html)
import React, { useState, useEffect } from 'react';
import {
IonButton,
IonButtons,
IonCol,
IonContent,
IonGrid,
IonHeader,
IonItem,
IonLabel,
IonList,
IonListHeader,
IonMenuButton,
IonPage,
IonRow,
IonTitle,
IonToolbar
} from '@ionic/react';
import Prism from 'prismjs';
import 'prismjs/themes/prism-tomorrow.css';
const GamePage = () => {
const [snippet, setSnippet] = useState('');
const [history, setHistory] = useState([
['', '', '', '', '', '', '', '', '']
]);
const [player, setPlayer] = useState('X');
const [winner, setWinner] = useState(false);
const board = [...history[history.length - 1]];
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]
];
function handleSquare (index) {
board[index] = player;
setHistory(history.concat([board]));
for (let i = 0; i < lines.length; i++) {
const [a, b, c] = lines[i];
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return setWinner(true);
}
}
setPlayer(player => player === 'X' ? 'O' : 'X');
}
function handleUndo (index) {
setHistory(history.slice(0, index + 1))
setPlayer(index % 2 === 0 ? 'X' : 'O');
setWinner(false);
}
useEffect(() => {
fetch('Game.js')
.then(res => res.text())
.then(text => setSnippet(
Prism.highlight(text, Prism.languages.javascript, 'javascript')
));
}, [])
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonMenuButton />
</IonButtons>
<IonTitle>Tic Tac Toe</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonGrid>
<IonRow>
<IonCol className="ion-text-center" size="12" sizeMd="4">
<div>
<IonButton disabled={winner} size="large" onClick={() => handleSquare(0)}>{board[0]}</IonButton>
<IonButton disabled={winner} size="large" onClick={() => handleSquare(1)}>{board[1]}</IonButton>
<IonButton disabled={winner} size="large" onClick={() => handleSquare(2)}>{board[2]}</IonButton>
</div>
<div>
<IonButton disabled={winner} size="large" onClick={() => handleSquare(3)}>{board[3]}</IonButton>
<IonButton disabled={winner} size="large" onClick={() => handleSquare(4)}>{board[4]}</IonButton>
<IonButton disabled={winner} size="large" onClick={() => handleSquare(5)}>{board[5]}</IonButton>
</div>
<div>
<IonButton disabled={winner} size="large" onClick={() => handleSquare(6)}>{board[6]}</IonButton>
<IonButton disabled={winner} size="large" onClick={() => handleSquare(7)}>{board[7]}</IonButton>
<IonButton disabled={winner} size="large" onClick={() => handleSquare(8)}>{board[8]}</IonButton>
</div>
</IonCol>
<IonCol size="12" sizeMd="4">
<IonList lines="none">
<IonListHeader>
<IonLabel>{winner ? 'Winner ->' : 'Player ->'} {player}</IonLabel>
</IonListHeader>
{
winner ? <IonItem><IonButton onClick={() => handleUndo(0)}>Restart game</IonButton></IonItem>
: history.slice(0, -1).map((_, index) =>
<IonItem>
<IonButton key={index} onClick={() => handleUndo(index)}>undo step {index + 1}</IonButton>
</IonItem>
)
}
</IonList>
</IonCol>
</IonRow>
<IonRow>
<pre>
<code className="language-javascript" dangerouslySetInnerHTML={{ __html: snippet }}></code>
</pre>
</IonRow>
</IonGrid>
</IonContent>
</IonPage >
)
}
export default GamePage;