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
30 changes: 30 additions & 0 deletions my-app/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.App {
text-align: center;
}

.container{
display: flex;
justify-content: center;
text-align: center;

}
.boardSquare{
background-color: #d5bef3;
border-style:solid;
height:200px;
width:200px;
};

.board{
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
}

.boardRow{
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
};
27 changes: 27 additions & 0 deletions my-app/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import logo from './logo.svg';
import Board from './Board';
import './App.css';

export const BoardSquareContext = React.createContext();
export const BoardSetSquareContext = React.createContext();

function App() {
const [boardSquares, setBoardSquares] = React.useState(
new Array(9).fill(null)
);

return (
<div className="App">
<h1>A tic-tac-toe game</h1>
<BoardSquareContext.Provider value={boardSquares}>
<BoardSetSquareContext.Provider value={setBoardSquares}>
<Board />
</BoardSetSquareContext.Provider>
</BoardSquareContext.Provider>

</div>
);
}

export default App;
9 changes: 9 additions & 0 deletions my-app/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import { render } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
const { getByText } = render(<App />);
const linkElement = getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
94 changes: 94 additions & 0 deletions my-app/src/Board.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import React from 'react';
import logo from './logo.svg';
import Square from './Square';
import {BoardSquareContext, BoardSetSquareContext} from './App.js';

// main board component
function Board() {

const boardSquares = React.useContext(BoardSquareContext);
const setBoardSquares = React.useContext(BoardSetSquareContext);

// state for the next turn
const [isXPlayer, setXPlayer] = React.useState(true);

// fucntion for when a sqaure is clicked
const whenClicked = index => {
// spread the items in boardSquare to new array to make a copy
const squares = [...boardSquares];
// check if square has a value
if (squares[index]) return;
// add next turn
squares[index] = isXPlayer ? 'X' : 'O';

// set the new state of the board squares
setBoardSquares(squares);

// change the state for the next turn
setXPlayer(!isXPlayer);
};

// rendering the squares using square component
const renderSquare = index => {
return (
<Square id={index} value={boardSquares[index]} onClick={() => whenClicked(index)} />
);
};

// create the gameStatus to determine next player and winner
let gameStatus;
const winner = findWinner(boardSquares);
gameStatus = winner
? `Winner is ${winner}!`
: `It is${isXPlayer ? 'X' : 'O'}'s Turn...'`;

return (
<div className="board">
<h2 className={gameStatus}>{gameStatus}</h2>
<div className="boardRow">
{renderSquare(0)}
{renderSquare(1)}
{renderSquare(2)}
</div>
<div className="boardRow">
{renderSquare(3)}
{renderSquare(4)}
{renderSquare(5)}
</div>
<div className="boardRow">
{renderSquare(6)}
{renderSquare(7)}
{renderSquare(8)}
</div>
</div>
)
}

// function to determine winner
const findWinner = (squares) => {
// winning combinations

const winningCombos = [
[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 < winningCombos.length; i++) {
// destructure winningCombos[i] array
const [a, b, c] = winningCombos[i];
// returning x or o if squares match
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
// else return null
return null;
}


export default Board;
11 changes: 11 additions & 0 deletions my-app/src/Square.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';

function Square(props) {
return (
<div className="boardSquare" key={props.id} onClick={props.onClick}>
{props.value}
</div>
);
}

export default Square
13 changes: 13 additions & 0 deletions my-app/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
15 changes: 15 additions & 0 deletions my-app/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
<App />,
document.getElementById('root')
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
7 changes: 7 additions & 0 deletions my-app/src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
141 changes: 141 additions & 0 deletions my-app/src/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// This optional code is used to register a service worker.
// register() is not called by default.

// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.

// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://bit.ly/CRA-PWA

const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.0/8 are considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);

export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
return;
}

window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;

if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
checkValidServiceWorker(swUrl, config);

// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://bit.ly/CRA-PWA'
);
});
} else {
// Is not localhost. Just register service worker
registerValidSW(swUrl, config);
}
});
}
}

function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
);

// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');

// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}

function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl, {
headers: { 'Service-Worker': 'script' },
})
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl, config);
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
);
});
}

export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready
.then(registration => {
registration.unregister();
})
.catch(error => {
console.error(error.message);
});
}
}
5 changes: 5 additions & 0 deletions my-app/src/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';