Commit 57c895a9 authored by Chaiprasit Khamsi's avatar Chaiprasit Khamsi

Initial commit

parent 6c2d7b84
Pipeline #1030 failed with stages
...@@ -4,7 +4,7 @@ This project was bootstrapped with [Create React App](https://github.com/faceboo ...@@ -4,7 +4,7 @@ This project was bootstrapped with [Create React App](https://github.com/faceboo
In the project directory, you can run: In the project directory, you can run:
### `npm start` ### `yarn start`
Runs the app in the development mode.<br /> Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser. Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
...@@ -12,12 +12,12 @@ Open [http://localhost:3000](http://localhost:3000) to view it in the browser. ...@@ -12,12 +12,12 @@ Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.<br /> The page will reload if you make edits.<br />
You will also see any lint errors in the console. You will also see any lint errors in the console.
### `npm test` ### `yarn test`
Launches the test runner in the interactive watch mode.<br /> Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `npm run build` ### `yarn build`
Builds the app for production to the `build` folder.<br /> Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance. It correctly bundles React in production mode and optimizes the build for the best performance.
...@@ -27,7 +27,7 @@ Your app is ready to be deployed! ...@@ -27,7 +27,7 @@ Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `npm run eject` ### `yarn eject`
**Note: this is a one-way operation. Once you `eject`, you can’t go back!** **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
...@@ -63,6 +63,6 @@ This section has moved here: https://facebook.github.io/create-react-app/docs/ad ...@@ -63,6 +63,6 @@ This section has moved here: https://facebook.github.io/create-react-app/docs/ad
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
### `npm run build` fails to minify ### `yarn build` fails to minify
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
{ {
"name": "tictactoe", "name": "my-app",
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"dependencies": { "dependencies": {
"@testing-library/jest-dom": "^4.2.4", "@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0", "@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.2.1", "@testing-library/user-event": "^7.1.2",
"react": "^16.13.1", "react": "^16.13.1",
"react-dom": "^16.13.1", "react-dom": "^16.13.1",
"react-scripts": "3.4.1" "react-scripts": "3.4.1"
......
body { body {
margin: 0; font: 14px "Century Gothic", Futura, sans-serif;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', margin: 20px;
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', }
sans-serif;
-webkit-font-smoothing: antialiased; ol, ul {
-moz-osx-font-smoothing: grayscale; padding-left: 30px;
} }
code { .board-row:after {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', clear: both;
monospace; content: "";
} display: table;
}
.status {
margin-bottom: 10px;
}
.square {
background: #fff;
border: 1px solid #999;
float: left;
font-size: 24px;
font-weight: bold;
line-height: 34px;
height: 34px;
margin-right: -1px;
margin-top: -1px;
padding: 0;
text-align: center;
width: 34px;
}
.square:focus {
outline: none;
}
.kbd-navigation .square:focus {
background: #ddd;
}
.game {
display: flex;
flex-direction: row;
}
.game-info {
margin-left: 20px;
}
\ No newline at end of file
import React from 'react'; import React from "react";
import ReactDOM from 'react-dom'; import ReactDOM from "react-dom";
import './index.css'; import "./index.css";
import App from './App';
import * as serviceWorker from './serviceWorker'; function Square(props) {
return (
ReactDOM.render( <button className="square" onClick={props.onClick}>
<React.StrictMode> {props.value}
<App /> </button>
</React.StrictMode>, );
document.getElementById('root') }
);
class Board extends React.Component {
// If you want your app to work offline and load faster, you can change renderSquare(i) {
// unregister() to register() below. Note this comes with some pitfalls. return (
// Learn more about service workers: https://bit.ly/CRA-PWA <Square
serviceWorker.unregister(); value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}
render() {
return (
<div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
{this.renderSquare(2)}
</div>
<div className="board-row">
{this.renderSquare(3)}
{this.renderSquare(4)}
{this.renderSquare(5)}
</div>
<div className="board-row">
{this.renderSquare(6)}
{this.renderSquare(7)}
{this.renderSquare(8)}
</div>
</div>
);
}
}
class Game extends React.Component {
constructor(props) {
super(props);
this.state = {
history: [
{
squares: Array(9).fill(null)
}
],
stepNumber: 0,
xIsNext: true
};
}
handleClick(i) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? "X" : "O";
this.setState({
history: history.concat([
{
squares: squares
}
]),
stepNumber: history.length,
xIsNext: !this.state.xIsNext
});
}
jumpTo(step) {
this.setState({
stepNumber: step,
xIsNext: (step % 2) === 0
});
}
render() {
const history = this.state.history;
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
const moves = history.map((step, move) => {
const desc = move ?
'Go to move #' + move :
'Go to game start';
return (
<li key={move}>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});
let status;
if (winner) {
status = "Winner: " + winner;
} else {
status = "ผู้เล่นคนถัดไป: " + (this.state.xIsNext ? "X" : "O");
}
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={i => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div>
<ol>{moves}</ol>
</div>
</div>
);
}
}
// ========================================
ReactDOM.render(<Game />, document.getElementById("root"));
function calculateWinner(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[a] === squares[c]) {
return squares[a];
}
}
return null;
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment