Skip to content

Commit c1a5a60

Browse files
first commit
1 parent 701c608 commit c1a5a60

18 files changed

+865
-272
lines changed

README.md

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,13 @@
1-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
1+
## Tic-tac-toe Game
22

3-
## Available Scripts
3+
Unbeatable Tic-tac-toe game using React, Create-react-app and Minimax algorithm for implementing AI.
44

5-
In the project directory, you can run:
5+
## Links
66

7-
### `npm start`
7+
* Demo: [Tic-tac-toe](https://about.phamvanlam.com/tic-tac-toe)
88

9-
Runs the app in the development mode.<br>
10-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
9+
## Visit me
1110

12-
The page will reload if you make edits.<br>
13-
You will also see any lint errors in the console.
14-
15-
### `npm test`
16-
17-
Launches the test runner in the interactive watch mode.<br>
18-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19-
20-
### `npm run build`
21-
22-
Builds the app for production to the `build` folder.<br>
23-
It correctly bundles React in production mode and optimizes the build for the best performance.
24-
25-
The build is minified and the filenames include the hashes.<br>
26-
Your app is ready to be deployed!
27-
28-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29-
30-
### `npm run eject`
31-
32-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33-
34-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35-
36-
Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37-
38-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39-
40-
## Learn More
41-
42-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43-
44-
To learn React, check out the [React documentation](https://reactjs.org/).
45-
46-
### Code Splitting
47-
48-
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49-
50-
### Analyzing the Bundle Size
51-
52-
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53-
54-
### Making a Progressive Web App
55-
56-
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57-
58-
### Advanced Configuration
59-
60-
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61-
62-
### Deployment
63-
64-
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65-
66-
### `npm run build` fails to minify
67-
68-
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
11+
* Blog: [Complete JavaScript](https://completejavascript.com/)
12+
* Fanpage: [Complete JavaScript](https://www.facebook.com/completejavascript/)
13+
* Portfolio: [Lam Pham](https://about.phamvanlam.com/)

public/favicon.ico

8.74 KB
Binary file not shown.

public/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
content="width=device-width, initial-scale=1, shrink-to-fit=no"
99
/>
1010
<meta name="theme-color" content="#000000" />
11+
<meta name="description" content="Unbeatable Tic-Tac-Toe game using React and Minimax Algorithm">
12+
<meta name="keywords" content="tic-tac-toe, react, minimax algorithm, completejavascript">
13+
1114
<!--
1215
manifest.json provides metadata used when your web app is added to the
1316
homescreen on Android. See https://developers.google.com/web/fundamentals/web-app-manifest/
@@ -22,7 +25,7 @@
2225
work correctly both with client-side routing and a non-root public URL.
2326
Learn how to configure a non-root public URL by running `npm run build`.
2427
-->
25-
<title>React App</title>
28+
<title>Unbeatable Tic-Tac-Toe Game - Lam Pham</title>
2629
</head>
2730
<body>
2831
<noscript>You need to enable JavaScript to run this app.</noscript>

src/App.css

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1-
.App {
2-
text-align: center;
3-
}
4-
5-
.App-logo {
6-
animation: App-logo-spin infinite 20s linear;
7-
height: 40vmin;
8-
}
9-
10-
.App-header {
11-
background-color: #282c34;
12-
min-height: 100vh;
13-
display: flex;
14-
flex-direction: column;
15-
align-items: center;
16-
justify-content: center;
17-
font-size: calc(10px + 2vmin);
18-
color: white;
19-
}
20-
21-
.App-link {
22-
color: #61dafb;
23-
}
24-
25-
@keyframes App-logo-spin {
26-
from {
27-
transform: rotate(0deg);
1+
.app {
2+
width: 100%;
3+
max-width: 640px;
4+
min-width: 360px;
5+
margin: 50px auto;
6+
box-shadow: rgba(0, 0, 0, 0.2) 0px 5px 30px 0px;
7+
}
8+
9+
@media (max-width: 1400px) {
10+
.app {
11+
max-width: 500px;
12+
margin: 20px auto;
2813
}
29-
to {
30-
transform: rotate(360deg);
31-
}
32-
}
14+
}

src/App.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
import React, { Component } from 'react';
2-
import logo from './logo.svg';
2+
import AppProvider from './AppProvider';
3+
import Header from './components/Header';
4+
import Main from './components/Main';
5+
import Footer from './components/Footer';
6+
37
import './App.css';
48

59
class App extends Component {
610
render() {
711
return (
8-
<div className="App">
9-
<header className="App-header">
10-
<img src={logo} className="App-logo" alt="logo" />
11-
<p>
12-
Edit <code>src/App.js</code> and save to reload.
13-
</p>
14-
<a
15-
className="App-link"
16-
href="https://reactjs.org"
17-
target="_blank"
18-
rel="noopener noreferrer"
19-
>
20-
Learn React
21-
</a>
22-
</header>
23-
</div>
12+
<AppProvider>
13+
<div className="app">
14+
<Header />
15+
<Main />
16+
<Footer />
17+
</div>
18+
</AppProvider>
2419
);
2520
}
2621
}

src/App.test.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/AppProvider.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import React, { Component } from 'react';
2+
import {
3+
GAME_TYPES, PLAYER_TURNS,
4+
checkGameState, getRandom, replace,
5+
findBestMove, findRandomMove
6+
} from './common';
7+
8+
const THINKING_TIME = 500;
9+
10+
export const AppContext = React.createContext();
11+
12+
export default class AppProvider extends Component {
13+
initState = {
14+
gameType: GAME_TYPES.TWO_PLAYERS,
15+
currentIcon: getRandom(0, 2),
16+
playerTurn: getRandom(0, 2),
17+
cells: new Array(9).fill(null),
18+
gameState: {
19+
position: "",
20+
iconType: null,
21+
isTie: null,
22+
},
23+
}
24+
25+
state = {
26+
gameType: this.initState.gameType,
27+
currentIcon: this.initState.currentIcon,
28+
playerTurn: this.initState.playerTurn,
29+
cells: this.initState.cells,
30+
gameState: this.initState.gameState,
31+
32+
changeType: (type) => {
33+
if (this.state.gameType !== type) {
34+
this.initNewGame(type);
35+
}
36+
},
37+
humanPlay: (index) => {
38+
this.humanPlay(index)
39+
},
40+
newGame: () => {
41+
this.initNewGame(this.state.gameType);
42+
}
43+
}
44+
45+
initGame = () => {
46+
if (this.state.gameType === GAME_TYPES.VERSUS_COMPUTER &&
47+
this.state.playerTurn === PLAYER_TURNS.COMPUTER) {
48+
49+
if (this.timeout) {
50+
clearTimeout(this.timeout);
51+
}
52+
53+
this.timeout = setTimeout(() => {
54+
const randomMove = findRandomMove(this.state.cells);
55+
this.computerPlay(randomMove);
56+
}, THINKING_TIME);
57+
}
58+
}
59+
60+
initNewGame = (type = this.initState.gameType) => {
61+
this.setState(() => {
62+
return {
63+
gameType: type,
64+
currentIcon: getRandom(0, 2),
65+
playerTurn: getRandom(0, 2),
66+
cells: this.initState.cells,
67+
gameState: this.initState.gameState,
68+
}
69+
}, () => {
70+
this.initGame();
71+
});
72+
}
73+
74+
applyState = (prevState, index) => {
75+
const cells = prevState.cells;
76+
const nextIcon = 1 - prevState.currentIcon;
77+
const nextPlayerTurn = 1 - prevState.playerTurn;
78+
const nextCells = replace(cells, index, prevState.currentIcon);
79+
const gameState = checkGameState(nextCells);
80+
81+
return {
82+
gameState: gameState,
83+
currentIcon: nextIcon,
84+
playerTurn: nextPlayerTurn,
85+
cells: nextCells
86+
}
87+
}
88+
89+
humanPlay = (index) => {
90+
if (this.state.gameState.position === "" && this.state.cells[index] === null &&
91+
(this.state.gameType === GAME_TYPES.TWO_PLAYERS || this.state.playerTurn === PLAYER_TURNS.HUMAN)) {
92+
93+
this.setState(prevState => {
94+
return this.applyState(prevState, index);
95+
}, () => {
96+
// Make a move for computer if the game is in 'versus computer' mode
97+
if (this.state.gameState.position === "" &&
98+
this.state.gameType === GAME_TYPES.VERSUS_COMPUTER &&
99+
this.state.playerTurn === PLAYER_TURNS.COMPUTER) {
100+
101+
setTimeout(() => {
102+
this.makeAIMove();
103+
}, THINKING_TIME);
104+
}
105+
});
106+
}
107+
}
108+
109+
computerPlay = (index) => {
110+
if (this.state.gameState.position === "" && this.state.cells[index] === null &&
111+
this.state.gameType === GAME_TYPES.VERSUS_COMPUTER &&
112+
this.state.playerTurn === PLAYER_TURNS.COMPUTER) {
113+
114+
this.setState(prevState => this.applyState(prevState, index));
115+
}
116+
}
117+
118+
makeAIMove = () => {
119+
const bestMove = findBestMove(this.state.cells, this.state.currentIcon);
120+
121+
if (bestMove !== null) {
122+
this.computerPlay(bestMove);
123+
}
124+
}
125+
126+
componentDidMount() {
127+
this.initGame();
128+
}
129+
130+
render() {
131+
return (
132+
<AppContext.Provider value={this.state}>
133+
{this.props.children}
134+
</AppContext.Provider>
135+
);
136+
}
137+
}

0 commit comments

Comments
 (0)