Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding player names #9

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 48 additions & 48 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState } from "react";

function Square({ value, onSquareClick }) {
return (
Expand All @@ -8,26 +8,46 @@ function Square({ value, onSquareClick }) {
);
}

function Board({ xIsNext, squares, onPlay }) {
export default function Board() {
const [xIsNext, setXIsNext] = useState(true);
const [squares, setSquares] = useState(Array(9).fill(null));

const [xname, setXName] = useState("");
const [enteredNameX, setenteredNameX] = useState("");
const [oname, setOName] = useState("");
const [enteredNameO, setEnteredNameO] = useState("");

function handleClicknameX(e) {
setenteredNameX(xname);
setXName("");
}
function handleClicknameO(e) {
setEnteredNameO(oname);
setOName("");
}

function handleClick(i) {
if (calculateWinner(squares) || squares[i]) {
return;
}
const nextSquares = squares.slice();
if (xIsNext) {
nextSquares[i] = 'X';
nextSquares[i] = "X";
} else {
nextSquares[i] = 'O';
nextSquares[i] = "O";
}
onPlay(nextSquares);
setSquares(nextSquares);
setXIsNext(!xIsNext);
}

const winner = calculateWinner(squares);
let status;
if (winner) {
status = 'Winner: ' + winner;
if (winner == "O") {
status = "Winner: " + enteredNameO;
} else if (winner == "X") {
status = "Winner: " + enteredNameX;
} else {
status = 'Next player: ' + (xIsNext ? 'X' : 'O');
status = "Next player: " + (xIsNext ? enteredNameX : enteredNameO);
}

return (
Expand All @@ -48,49 +68,29 @@ function Board({ xIsNext, squares, onPlay }) {
<Square value={squares[7]} onSquareClick={() => handleClick(7)} />
<Square value={squares[8]} onSquareClick={() => handleClick(8)} />
</div>
</>
);
}

export default function Game() {
const [history, setHistory] = useState([Array(9).fill(null)]);
const [currentMove, setCurrentMove] = useState(0);
const xIsNext = currentMove % 2 === 0;
const currentSquares = history[currentMove];

function handlePlay(nextSquares) {
const nextHistory = [...history.slice(0, currentMove + 1), nextSquares];
setHistory(nextHistory);
setCurrentMove(nextHistory.length - 1);
}

function jumpTo(nextMove) {
setCurrentMove(nextMove);
}
<div>
<p>
<small>X Player: {enteredNameX}</small>
</p>
<input
type="text"
onChange={(e) => setXName(e.target.value)}
value={xname}
/>
<button onClick={handleClicknameX}>Enter</button>

const moves = history.map((squares, move) => {
let description;
if (move > 0) {
description = 'Go to move #' + move;
} else {
description = 'Go to game start';
}
return (
<li key={move}>
<button onClick={() => jumpTo(move)}>{description}</button>
</li>
);
});

return (
<div className="game">
<div className="game-board">
<Board xIsNext={xIsNext} squares={currentSquares} onPlay={handlePlay} />
<p>
<small>O Player: {enteredNameO}</small>
</p>
<input
type="text"
onChange={(e) => setOName(e.target.value)}
value={oname}
/>
<button onClick={handleClicknameO}>Enter</button>
</div>
<div className="game-info">
<ol>{moves}</ol>
</div>
</div>
</>
);
}

Expand Down