Skip to content
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
101 changes: 95 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Board from './components/Board';

const PLAYER_1 = 'X';
const PLAYER_2 = 'O';
let TURN = true;


const generateSquares = () => {
const squares = [];
Expand All @@ -25,15 +27,38 @@ const generateSquares = () => {
return squares;
};


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

const [turn, setTurn] = useState(TURN);
const [winner, setWinner] = useState('');
let string = null;

// 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 updateSquares = (id) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is a small logical bug here. There is no check here to see if there is a winner, so it is possible to continue to play. This can result in unexpected behavior. For example, if the board looks like this:

| | | |
| |o|o|
|x|x|x|

the game will display 'Winner is x'. However, 'o' can still move, so if 'o' clicks in the middle left square:

| | | |
|o|o|o|
|x|x|x|

the game will display "Winner is ". This continues until the entire board is filled, at which point the game will display "Winner is Tie".

let newSquares = [];
let strVal = '';
strVal = turn==true? 'x':'o';
for (let row of squares){
let newRow = row.map((square)=>{
if ((square.id===id) && (!square.value)) {
square.value = strVal;
return square;
}
return square;
});
newSquares.push(newRow);
}
let newTurn = !turn;
setTurn(newTurn);
setSquares(newSquares);
checkForWinner();
};

const checkForWinner = () => {
// Complete in Wave 3
Expand All @@ -45,21 +70,85 @@ const App = () => {
// 3 squares in each column match
// 3. Go across each diagonal to see if
// all three squares have the same value.

const winning = [ new Set([0,3,6]), new Set([1,4,7]), new Set([2,5,8]), new Set ([0,1,2]),
new Set([3,4,5]), new Set([6,7,8]),new Set([0,8,4]),new Set([2,4,6])];

let count = 0;

const xSet=new Set();
const oSet= new Set();
for(let row of squares){
for (let element of row){
// console.log('element=', element);
if (element.value === 'x'){
xSet.add(count);
} else if (element.value === 'o') {
oSet.add(count);
}
count ++;
}
}
const myX = [];

console.log('xSet', xSet);
console.log('oSet', oSet);

// Roslyn is done

for (let combo of winning){
if (isSuperset(xSet,combo)){
myX.push(combo);
}
}
Comment on lines +99 to +103

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Interesting approach!

// const myO=winning.filter((val)=>{
// isSuperset(oSet,val);
// });
const myO = [];
for (let combo of winning){
if (isSuperset(oSet,combo)){
myO.push(combo);
}
}

console.log('myX', myX);
console.log('myO', myO);

if (myX.length > myO.length) {
string = 'x';
} else if (myO.length > myX.length){
string = 'o';
} else if ((oSet.size + xSet.size) === 9){
string = 'Tie';
} else {
string = '';
}
setWinner(string);
return string;
};

const resetGame = () => {
// Complete in Wave 4
const isSuperset = (set, subset) => {
for (let elem of subset) {
if (!set.has(elem)) {
return false;
}
}
return true;
};
Comment on lines +130 to +137

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

👍


const resetGame = () => {
setSquares(generateSquares());
};

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>
<h2>Winner is {winner}</h2>
<button onClick={()=>{resetGame();}}>Reset Game</button>
</header>
<main>
<Board squares={squares} />
<Board squares={squares} onClickCallback={updateSquares}/>
</main>
</div>
);
Expand Down
6 changes: 3 additions & 3 deletions src/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('App', () => {
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
1 change: 1 addition & 0 deletions src/components/Board.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ div.grid {
display: grid;
grid-template: repeat(3, 1fr) / repeat(3, 1fr);
}

28 changes: 23 additions & 5 deletions src/components/Board.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,34 @@ const generateSquareComponents = (squares, onClickCallback) => {
// squares is a 2D Array, but
// you need to return a 1D array
// of square components
console.log('We are about to iterate through squares');
let oneD =[];
// let classVal='';
for (let row of squares){
for (let square of row) {
// if (square.value==='x'){
// classVal='jasmine';
// }else if (square.value==='o'){
// classVal='chris';
// }
oneD.push(<Square key={square.id} id={square.id} value={square.value} onClickCallback={onClickCallback} />);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice!

}
}
return oneD;

}
};

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(
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
12 changes: 11 additions & 1 deletion src/components/Square.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@
justify-content: center;
align-items: center;
margin: 2px;
}
}

.jasmine {
background-image: url("https://ca.slack-edge.com/T024N8K3W8N-U029WFRD8HW-807f2572ed86-512");
Background-size: 200px 200px;
}

.chris {
background-image: url("https://ca.slack-edge.com/T024N8K3W8N-U0283SDM62H-f8f25fdaa3da-512");
Background-size: 200px 200px;
}
Comment on lines +14 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

😂

22 changes: 15 additions & 7 deletions src/components/Square.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
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.
let classVal='';

if (props.value==='x'){
classVal='jasmine';
}else if (props.value==='o'){
classVal='chris';
}
return (
<button className={`square ${classVal}`} onClick = {() => {props.onClickCallback(props.id);}}>
{props.value}
</button>
);
};


return <button
className="square"
>
{props.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