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

C16 - Spruce - Vange Spracklin #70

Open
wants to merge 10 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
5 changes: 2 additions & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
"eslint:recommended",
"plugin:react/recommended",
"plugin:jsx-a11y/recommended",
"plugin:react-hooks/recommended",
"plugin:jest/recommended",
"plugin:testing-library/react"
"plugin:react-hooks/recommended"
// "plugin:testing-library/react"
Comment on lines +14 to +15
Copy link

@audreyandoy audreyandoy Jan 12, 2022

Choose a reason for hiding this comment

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

I assume jest was removed from VS Code because it was running tests with every change right?

We actually turn off auto run by adding the key-value pair to our settings.json in VS Code:

"jest.autoRun": "off"

],
"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
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"react-dom": "^17.0.1",
"react-scripts": "4.0.3"
},
"homepage": "http://adagold.github.io/react-tic-tac-toe",
"homepage": "https://houseofvange.github.io/react-tic-tac-toe/",
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
Expand All @@ -21,8 +21,7 @@
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
"react-app"
]
},
"browserslist": {
Expand Down
107 changes: 91 additions & 16 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import './App.css';

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 @@ -30,36 +30,111 @@ const App = () => {
// empty value and unique ids.
const [squares, setSquares] = useState(generateSquares());

const [currentPlayer, setCurrentPlayer] = useState(PLAYER_1);

// 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
const onClickCallback = (squareID) => {
if (winner){
return;
}

let madeMove = false;

const newSquares = squares.map(row => row.map(position => {
if (position.id != squareID){
return position;
}
if (position.value !== ''){
return position;
}
Comment on lines +47 to +52

Choose a reason for hiding this comment

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

Because both conditionals have the same logic when evaluated to true, we can combine them into one using the || (or) operator:

Suggested change
if (position.id != squareID){
return position;
}
if (position.value !== ''){
return position;
}
if (position.id != squareID || position.value !== '' ) {
return position;
}

madeMove = true;
const newSquare = {...position, value: currentPlayer};

Choose a reason for hiding this comment

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

Great use of the spread operator to copy the key-value pairs and assign value to the current player!

return newSquare;
}));

if (madeMove) {
//update the board
setSquares(newSquares);

// change the player with each click
let nextPlayer = (currentPlayer === PLAYER_1) ? PLAYER_2 : PLAYER_1;
setCurrentPlayer(nextPlayer);
Comment on lines +63 to +64

Choose a reason for hiding this comment

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

Nice ternary!

}
};

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 resetGame = () => {
// Complete in Wave 4
setSquares(generateSquares());
setCurrentPlayer(PLAYER_1);
};

const winner = checkForWinner();

const showStatus = () => {
if (winner){
// alert(`You did amazing ${winner}!`)
return `Winner is ${winner}`;
}else{
return `Your turn ${currentPlayer}`;
}
};

return (
<div className="App">
<header className="App-header">
<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>
<h2> {showStatus()} </h2>

Choose a reason for hiding this comment

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

Nice helper function! Another approach to displaying winner text is by using a ternary operator:

Suggested change
<h2> {showStatus()} </h2>
<h2> { winner ? Winner is ${winner} : `Your turn ${currentPlayer}` } </h2>

<button onClick={() => resetGame()}>Reset Game</button>
</header>
<main>
<Board squares={squares} />
<Board
squares={squares}
onClickCallback={onClickCallback}
/>
</main>
</div>
);
Expand Down
8 changes: 4 additions & 4 deletions src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ describe('App', () => {

buttons = container.querySelectorAll('.grid button');
expect(buttons[buttonIndex].innerHTML).toEqual(expectedResult);
}
};

describe.skip('Wave 2: clicking on squares and rendering App', () => {
describe('Wave 2: clicking on squares and rendering App', () => {

test('App renders with a board of 9 empty buttons', () => {
// Arrange-Act - Render the app
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('App', () => {
});


describe.skip('Wave 3: Winner tests', () => {
describe('Wave 3: Winner tests', () => {
describe('Prints "Winner is x" when x wins', () => {
test('that a winner will be identified when 3 Xs get in a row across the top', () => {
// Arrange
Expand Down Expand Up @@ -364,7 +364,7 @@ describe('App', () => {
});
});

describe.skip('Wave 4: reset game button', () => {
describe('Wave 4: reset game button', () => {
test('App has a "Reset Game" button', () => {
// Arrange-Act
render(<App />);
Expand Down
28 changes: 16 additions & 12 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,33 @@ import './Board.css';
import Square from './Square';
import PropTypes from 'prop-types';


// This turns the 2D array into a 1D array
const generateSquareComponents = (squares, onClickCallback) => {
// Complete this for Wave 1
// squares is a 2D Array, but
// you need to return a 1D array
// of square components

}
const singleArraySquares = [].concat(...squares);
return singleArraySquares.map((square) => {
return (
<Square
value={square.value}
id={square.id}
onClickCallback={onClickCallback}
key={square.id}
/>
);
});
};

const Board = ({ squares, onClickCallback }) => {
const squareList = generateSquareComponents(squares, onClickCallback);
console.log(squareList);
return <div className="grid" >
{squareList}
</div>
}
return <div className='grid'>{squareList}</div>;
};

Board.propTypes = {
squares: PropTypes.arrayOf(
PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
value: PropTypes.string.isRequired
value: PropTypes.string.isRequired,
})
)
),
Expand Down
4 changes: 2 additions & 2 deletions src/components/Board.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('Wave 1: Board', () => {
});
describe('Wave 2: Board', () => {
describe('button click callbacks', () => {
test.skip('that the callback is called for the 1st button', () => {
test('that the callback is called for the 1st button', () => {
// Arrange
const callback = jest.fn();
const { container } = render(<Board squares={SAMPLE_BOARD} onClickCallback={callback} />);
Expand All @@ -97,7 +97,7 @@ describe('Wave 2: Board', () => {
expect(callback).toHaveBeenCalled();
});

test.skip('that the callback is called for the last button', () => {
test('that the callback is called for the last button', () => {
// Arrange
const callback = jest.fn();
const { container } = render(<Board squares={SAMPLE_BOARD} onClickCallback={callback} />);
Expand Down
23 changes: 16 additions & 7 deletions src/components/Square.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import React from 'react';
import PropTypes from 'prop-types';

import './Square.css'
import './Square.css';

const Square = (props) => {
// For Wave 1 enable this
// Component to alert a parent
// component when it's clicked on.

return <button
className="square"
>
{props.value}
</button>
}
const handleOnClickSquareButton = () => {
// if (props.value){
// console.log('you cant play that square!');
// }else{
props.onClickCallback(props.id);
// }
};

return <button
className='square'
onClick={handleOnClickSquareButton}
>
{props.value}
</button>;
};
Comment on lines 6 to +25
Copy link

@audreyandoy audreyandoy Jan 12, 2022

Choose a reason for hiding this comment

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

We could use object destructuring to reference props by their key name instead.

Suggested change
const Square = (props) => {
// For Wave 1 enable this
// Component to alert a parent
// component when it's clicked on.
return <button
className="square"
>
{props.value}
</button>
}
const handleOnClickSquareButton = () => {
// if (props.value){
// console.log('you cant play that square!');
// }else{
props.onClickCallback(props.id);
// }
};
return <button
className='square'
onClick={handleOnClickSquareButton}
>
{props.value}
</button>;
};
const Square = ( {value, id, onClickCallback, key} ) => {
const handleOnClickSquareButton = () => {
onClickCallback(id);
};
return <button
className='square'
onClick={handleOnClickSquareButton}
>
{value}
</button>;
};


Square.propTypes = {
value: PropTypes.string.isRequired,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Square.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Wave 1: Square', () => {
});

describe('Wave 2: Square', () => {
test.skip('when clicked on it calls the callback function', async () => {
test('when clicked on it calls the callback function', async () => {
const callback = jest.fn();

render(<Square value="X" id={1} onClickCallback={callback} />);
Expand Down
Loading