Skip to content

Commit 646a628

Browse files
author
Chris Schuhmacher
committed
Created React Tic Tac Toe game
1 parent b0c793e commit 646a628

8 files changed

+9332
-2
lines changed

README.md

+2,229-2
Large diffs are not rendered by default.

package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "tic-tac-toe",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"node-sass-chokidar": "^0.0.3",
7+
"react": "^16.1.1",
8+
"react-dom": "^16.1.1",
9+
"react-scripts": "1.0.17"
10+
},
11+
"scripts": {
12+
"build-css": "node-sass-chokidar src/ -o src/",
13+
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
14+
"start": "react-scripts start",
15+
"build": "react-scripts build",
16+
"test": "react-scripts test --env=jsdom",
17+
"eject": "react-scripts eject"
18+
}
19+
}

public/favicon.ico

3.78 KB
Binary file not shown.

public/index.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
<meta name="theme-color" content="#000000">
7+
<!--
8+
manifest.json provides metadata used when your web app is added to the
9+
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
10+
-->
11+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
12+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
13+
<!--
14+
Notice the use of %PUBLIC_URL% in the tags above.
15+
It will be replaced with the URL of the `public` folder during the build.
16+
Only files inside the `public` folder can be referenced from the HTML.
17+
18+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
19+
work correctly both with client-side routing and a non-root public URL.
20+
Learn how to configure a non-root public URL by running `npm run build`.
21+
-->
22+
<title>React Tac Toe</title>
23+
</head>
24+
<body>
25+
<noscript>
26+
You need to enable JavaScript to run this app.
27+
</noscript>
28+
<div id="root"></div>
29+
<!--
30+
This HTML file is a template.
31+
If you open it directly in the browser, you will see an empty page.
32+
33+
You can add webfonts, meta tags, or analytics to this file.
34+
The build step will place the bundled scripts into the <body> tag.
35+
36+
To begin the development, run `npm start` or `yarn start`.
37+
To create a production bundle, use `npm run build` or `yarn build`.
38+
-->
39+
</body>
40+
</html>

public/manifest.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
}
10+
],
11+
"start_url": "./index.html",
12+
"display": "standalone",
13+
"theme_color": "#000000",
14+
"background_color": "#ffffff"
15+
}

src/index.css

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
body {
2+
font: 14px "Century Gothic", Futura, sans-serif;
3+
margin: 20px;
4+
}
5+
6+
ol, ul {
7+
padding-left: 30px;
8+
}
9+
10+
.board-row:after {
11+
clear: both;
12+
content: "";
13+
display: table;
14+
}
15+
16+
.status {
17+
margin-bottom: 10px;
18+
}
19+
20+
.square {
21+
background: #fff;
22+
border: 1px solid #999;
23+
float: left;
24+
font-size: 24px;
25+
font-weight: bold;
26+
line-height: 34px;
27+
height: 34px;
28+
margin-right: -1px;
29+
margin-top: -1px;
30+
padding: 0;
31+
text-align: center;
32+
width: 34px;
33+
}
34+
35+
.square:focus {
36+
outline: none;
37+
}
38+
39+
.kbd-navigation .square:focus {
40+
background: #ddd;
41+
}
42+
43+
.game {
44+
display: flex;
45+
flex-direction: row;
46+
}
47+
48+
.game-info {
49+
margin-left: 20px;
50+
}

src/index.js

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import './index.css';
4+
5+
function Square(props) {
6+
return (
7+
<button className="square" onClick={props.onClick}>
8+
{props.value}
9+
</button>
10+
);
11+
}
12+
13+
class Board extends React.Component {
14+
renderSquare(i) {
15+
return (
16+
<Square
17+
value={this.props.squares[i]}
18+
onClick={() => this.props.onClick(i)}
19+
/>
20+
);
21+
}
22+
23+
render() {
24+
return (
25+
<div>
26+
<div className="board-row">
27+
{this.renderSquare(0)}
28+
{this.renderSquare(1)}
29+
{this.renderSquare(2)}
30+
</div>
31+
<div className="board-row">
32+
{this.renderSquare(3)}
33+
{this.renderSquare(4)}
34+
{this.renderSquare(5)}
35+
</div>
36+
<div className="board-row">
37+
{this.renderSquare(6)}
38+
{this.renderSquare(7)}
39+
{this.renderSquare(8)}
40+
</div>
41+
</div>
42+
);
43+
}
44+
}
45+
46+
class Game extends React.Component {
47+
constructor() {
48+
super();
49+
this.state = {
50+
history: [{
51+
squares: Array(9).fill(null)
52+
}],
53+
stepNumber: 0,
54+
xIsNext: true
55+
}
56+
}
57+
58+
handleClick(i) {
59+
const history = this.state.history.slice(0, this.state.stepNumber + 1);
60+
const current = history[history.length - 1];
61+
const squares = current.squares.slice();
62+
if(calculateWinner(squares) || squares[i]) {
63+
return;
64+
}
65+
squares[i] = this.state.xIsNext ? 'X' : 'O';
66+
this.setState({
67+
history: history.concat([{
68+
squares: squares
69+
}]),
70+
stepNumber: history.length,
71+
xIsNext: !this.state.xIsNext
72+
});
73+
}
74+
75+
jumpTo(step) {
76+
this.setState({
77+
stepNumber: step,
78+
xIsNext: (step % 2) === 0
79+
});
80+
}
81+
82+
render() {
83+
const history = this.state.history;
84+
const current = history[this.state.stepNumber];
85+
const winner = calculateWinner(current.squares);
86+
87+
const moves = history.map((step, move) => {
88+
const desc = move ?
89+
'Go to move #' + move :
90+
'Go to game start';
91+
return (
92+
<li key={move}>
93+
<button onClick={() => this.jumpTo(move)}>{desc}</button>
94+
</li>
95+
);
96+
});
97+
98+
let status;
99+
if(winner) {
100+
status = 'Winner: ' + winner;
101+
} else {
102+
status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
103+
}
104+
105+
return (
106+
<div className="game">
107+
<div className="game-board">
108+
<Board
109+
squares={current.squares}
110+
onClick={(i) => this.handleClick(i)}
111+
/>
112+
</div>
113+
<div className="game-info">
114+
<div>{status}</div>
115+
<ol>{moves}</ol>
116+
</div>
117+
</div>
118+
);
119+
}
120+
}
121+
122+
// ========================================
123+
124+
ReactDOM.render(
125+
<Game />,
126+
document.getElementById('root')
127+
);
128+
129+
function calculateWinner(squares) {
130+
const lines = [
131+
[0, 1, 2],
132+
[3, 4, 5],
133+
[6, 7, 8],
134+
[0, 3, 6],
135+
[1, 4, 7],
136+
[2, 5, 8],
137+
[0, 4, 8],
138+
[2, 4, 6]
139+
];
140+
141+
for(let i = 0; i < lines.length; i++) {
142+
const [a, b, c] = lines[i];
143+
if(squares[a] && squares[a] === squares[b] && squares[a] === squares[c]){
144+
return squares[a];
145+
}
146+
}
147+
return null;
148+
}

0 commit comments

Comments
 (0)