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

Spruce: Kelemen - Tic Tac Toe Digital starter #68

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
"eslint:recommended",
"plugin:react/recommended",
"plugin:jsx-a11y/recommended",
"plugin:react-hooks/recommended",
"plugin:jest/recommended",
"plugin:testing-library/react"
"plugin:react-hooks/recommended"
],
"parserOptions": {
"ecmaFeatures": {
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# React Tic-Tac-Toe

## Please Read First!

The digital campus' version of Tic-Tac-Toe differs from the original in the following ways:
- We will *not* be using `main` branch. Follow step 6 in the **One-Time Project Setup** to change branches.
- Wave 1 has been completed for you; however, it would help you understand the flow of data by reviewing the code written for Wave 1.
- Wave 3's `checkForWinner` function has been created for you; however, read through Wave 3 instructions to figure out how and where to use it.

## Skills Assessed

- Following directions and reading comprehension
Expand Down Expand Up @@ -81,9 +88,15 @@ We can run `yarn install` multiple times safely, but we only need to do this onc

The file `package.json` contains details about our project, the scripts available, and the dependencies needed. We can inspect this file when we are curious about the details of our dependencies.

6. Follow the directions in the "Getting Started" section.
6. We will not being using `main` branch. Make sure you are working from `digital-starter` by running:

```bash
$ git checkout digital-starter
```

7. Follow the directions in the "Getting Started" section.

7. Follow the directions in the "Project Requirements" section.
8. Follow the directions in the "Project Requirements" section.

## Project Development Workflow

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
"react-app"
]
},
"browserslist": {
Expand Down
7 changes: 7 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@
margin-bottom: 2em;
}

.green {
color: rgb(199, 238, 199);
}

.red {
color: rgb(187, 133, 133);
}
124 changes: 102 additions & 22 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { useState } from 'react';
import './App.css';

import Login from './components/Login';
import Board from './components/Board';

const PLAYER_1 = 'X';
const PLAYER_2 = 'O';
const PLAYER_1 = 'x';
const PLAYER_2 = 'o';

const generateSquares = () => {
const squares = [];
Expand All @@ -26,40 +26,120 @@ const generateSquares = () => {
};

const App = () => {
// This starts state off as a 2D array of JS objects with
// empty value and unique ids.
// useState
const [squares, setSquares] = useState(generateSquares());
const [player, setPlayer] = useState(PLAYER_1);

const onClickCallback = (id) => {
if (winner) {
return;
}

// Wave 2
// You will need to create a method to change the square
// When it is clicked on.
// Then pass it into the squares as a callback
let madeMove = false;
const newState = squares.map((row) =>
row.map((pos) => {
if (pos.id !== id) {
return pos;
}
if (pos.value !== '') {
return pos;
}
madeMove = true;
return { ...pos, value: player };
})
);

if (madeMove) {
setSquares(newState);
setPlayer(player === PLAYER_1 ? PLAYER_2 : PLAYER_1);
}
Comment on lines +52 to +55

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

We only want to switch players if there was a valid move made.

};

const checkForWinner = () => {
// Complete in Wave 3
// You will need to:
// 1. Go accross each row to see if
// 3 squares in the same row match
// i.e. same value
// 2. Go down each column to see if
// 3 squares in each column match
// 3. Go across each diagonal to see if
// all three squares have the same value.
let i = 0;

// Check all the rows and columns for a winner
while (i < 3) {
if (
squares[i][0].value === squares[i][1].value &&
squares[i][2].value === squares[i][1].value &&
squares[i][0].value !== ''
) {
return squares[i][0].value;
} else if (
squares[0][i].value === squares[1][i].value &&
squares[2][i].value === squares[1][i].value &&
squares[0][i].value !== ''
) {
return squares[0][i].value;
}
i += 1;
}
// Check Top-Left to bottom-right diagonal
if (
squares[0][0].value === squares[1][1].value &&
squares[2][2].value === squares[1][1].value &&
squares[1][1].value !== ''
) {
return squares[0][0].value;
}

// Check Top-right to bottom-left diagonal
if (
squares[0][2].value === squares[1][1].value &&
squares[2][0].value === squares[1][1].value &&
squares[1][1].value !== ''
) {
return squares[0][2].value;
}

return null;
};
const nameColor = player === PLAYER_1 ? 'green' : 'red';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice calculated value to get the class to apply to the status line. No state required! We can simply do the calculation in the body of our function before we reach the return. Components are functions, and we we can run whatever code we need to generate our tag hierarchy before we get to the return statement!


// const playersList = () => (
// <div>
// <h5>Players: {player1Name} vs. {player2Name}</h5>
// </
// div>
// );

{
/* const addPlayerNames = (newPlayers) => {
newPlayersList(() => (
userName1: userName1,
userName2: userName2,
));
setFormFields(newPlayers);
}; */
}

const winner = checkForWinner();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. We can't make use of the checkForWinner (at least as written) during the click handler, since the state variable that it checks won't be updated until the subsequent render. But we can call it in the function body (which happens on each render) to see whether there is a winner, and then make adjustments to the tag structure we return based on the calculated winner (if any).

const getStatus = () => {
if (winner) {
return `Winner is ${winner}`;
}
return `It is now ${player}'s turn`;
};

const resetGame = () => {
// Complete in Wave 4
setSquares(generateSquares());
setPlayer(PLAYER_1);
};
Comment on lines 125 to 128

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Resetting the game simply involves setting each of our state values back to their initial values.


// ========= App rendered ===========

return (
<div className="App">
<header className="App-header">
<h1>React Tic Tac Toe</h1>
<h2>The winner is ... -- Fill in for wave 3 </h2>
<button>Reset Game</button>
<Login />
<h2 className={nameColor}>{getStatus()}</h2>

<button onClick={() => resetGame()}>Reset Game</button>
</header>
<main>
<Board squares={squares} />
<Board squares={squares} onClickCallback={onClickCallback} />
</main>
</div>
);
Expand Down
Loading