diff --git a/src/App.js b/src/App.js index 3784575..77b22ce 100644 --- a/src/App.js +++ b/src/App.js @@ -1,23 +1,12 @@ -import logo from './logo.svg'; import './App.css'; +import Board from './components/Board/Board'; + + function App() { return (
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
+
); } diff --git a/src/Component_and_state_framework_v1.png b/src/Component_and_state_framework_v1.png new file mode 100644 index 0000000..b0c3cf4 Binary files /dev/null and b/src/Component_and_state_framework_v1.png differ diff --git a/src/components/Board/Board.css b/src/components/Board/Board.css new file mode 100644 index 0000000..92b2645 --- /dev/null +++ b/src/components/Board/Board.css @@ -0,0 +1,20 @@ +.board { + display: flex; + flex-wrap: wrap; + flex-direction: column; + align-content: center; + height: 90vh; + background-image: url(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS7s9yhHPtcMcWRQFDm9tCBS2RhsUyyOye46A&usqp=CAU); + +} + +.row { + display: flex; + flex-wrap: wrap; + flex-direction: row; +} + +.message { + font-size: 2em; + margin: 10px; +} \ No newline at end of file diff --git a/src/components/Board/Board.js b/src/components/Board/Board.js new file mode 100644 index 0000000..58ce448 --- /dev/null +++ b/src/components/Board/Board.js @@ -0,0 +1,76 @@ +import './Board.css'; +import { React } from 'react'; +import Tile from '../../components/Tile/Tile'; +import { useContext } from 'react'; +import { GameContext } from '../../context/GameContext'; + +export default function Board() { + const { board, setBoard, setActive, currentPlayer, setCurrentPlayer } = useContext(GameContext); + + function ResetGame() { + setActive(true); + setCurrentPlayer('X'); + setBoard([ + { + place: 0, + value: '' + }, + { + place: 1, + value: '' + }, + { + place: 2, + value: '' + }, + { + place: 3, + value: '' + }, + { + place: 4, + value: '' + }, + { + place: 5, + value: '' + }, + { + place: 6, + value: '' + }, + { + place: 7, + value: '' + }, + { + place: 8, + value: '' + } + ]); + } + return ( +
+
WELCOME TO ALCHEMY TIC TAC-O
+
It is {currentPlayer}s turn
+ +
+
+ + + +
+
+ + + +
+
+ + + +
+
+
+ ); +} diff --git a/src/components/Tile/Tile.css b/src/components/Tile/Tile.css new file mode 100644 index 0000000..ace138b --- /dev/null +++ b/src/components/Tile/Tile.css @@ -0,0 +1,10 @@ +.tile { + width: 100px; + height: 100px; + margin: 5px; + border: 2px solid; + box-sizing: border-box; + background-color: aquamarine; + font-size: 3em; + text-align: center; +} \ No newline at end of file diff --git a/src/components/Tile/Tile.js b/src/components/Tile/Tile.js new file mode 100644 index 0000000..ce01145 --- /dev/null +++ b/src/components/Tile/Tile.js @@ -0,0 +1,37 @@ +import React from 'react'; +import { useContext } from 'react'; +import { GameContext } from '../../context/GameContext'; +import './Tile.css'; + + +export default function Tile({ location }) { + const { board, setBoard, active, currentPlayer, setCurrentPlayer } = useContext(GameContext); + + function handleTileClick() { + if (!active) { + return; + } else { + setBoard((prevState) => { + return prevState.map((space)=>{ + if (space.place === location && space.value === '') { + if (currentPlayer === 'X') { + setCurrentPlayer('O'); + } else { + setCurrentPlayer('X'); + } return { place: location, value: currentPlayer }; + } else { + return { ...space }; + } + }); + }); + + } + } + + + return ( +
+
{board[location].value}
+
+ ); +} \ No newline at end of file diff --git a/src/context/GameContext.js b/src/context/GameContext.js new file mode 100644 index 0000000..cf98959 --- /dev/null +++ b/src/context/GameContext.js @@ -0,0 +1,136 @@ +import { createContext, useState } from 'react'; +import initialBoard from '../initial-board-data'; +const GameContext = createContext(); + +const GameProvider = ({ children }) => { + const [board, setBoard] = useState(initialBoard); + const [playerX, setPlayerX] = useState(); + const [playerO, setPlayerO] = useState(); + const [tile, setTile] = useState(); + const [active, setActive] = useState(true); + const [currentPlayer, setCurrentPlayer] = useState('X'); + + const gameState = { + board, + setBoard, + tile, + setTile, + playerX, + setPlayerX, + playerO, + setPlayerO, + active, + setActive, + currentPlayer, + setCurrentPlayer + }; + + const checkWinner = () => { + if (!active) + return; + // conditions for player X to win + + if (board[0].value === 'X' && board[1].value === 'X' && board[2].value === 'X') { + setActive(false); + alert('X wins!'); + return; + } + if (board[3].value === 'X' && board[4].value === 'X' && board[5].value === 'X') { + setActive(false); + alert('X wins!'); + return; + } + if (board[6].value === 'X' && board[7].value === 'X' && board[8].value === 'X') { + setActive(false); + alert('X wins!'); + return; + } + if (board[0].value === 'X' && board[4].value === 'X' && board[8].value === 'X') { + setActive(false); + alert('X wins!'); + return; + } + if (board[6].value === 'X' && board[4].value === 'X' && board[2].value === 'X') { + setActive(false); + alert('X wins!'); + return; + } + if (board[0].value === 'X' && board[3].value === 'X' && board[6].value === 'X') { + setActive(false); + alert('X wins!'); + return; + } + if (board[1].value === 'X' && board[4].value === 'X' && board[7].value === 'X') { + setActive(false); + alert('X wins!'); + return; + } + if (board[2].value === 'X' && board[5].value === 'X' && board[8].value === 'X') { + setActive(false); + alert('X wins!'); + return; + } + + // conditions for player O to win + if (board[0].value === 'O' && board[1].value === 'O' && board[2].value === 'O') { + setActive(false); + alert('O wins!'); + return; + } + if (board[3].value === 'O' && board[4].value === 'O' && board[5].value === 'O') { + setActive(false); + alert('O wins!'); + return; + } + if (board[6].value === 'O' && board[7].value === 'O' && board[8].value === 'O') { + setActive(false); + alert('O wins!'); + return; + } + if (board[0].value === 'O' && board[4].value === 'O' && board[8].value === 'O') { + setActive(false); + alert('O wins!'); + return; + } + if (board[6].value === 'O' && board[4].value === 'O' && board[2].value === 'O') { + setActive(false); + alert('O wins!'); + return; + } + if (board[0].value === 'O' && board[3].value === 'O' && board[6].value === 'O') { + setActive(false); + alert('X wins!'); + return; + } + if (board[1].value === 'O' && board[4].value === 'O' && board[7].value === 'O') { + setActive(false); + alert('X wins!'); + return; + } + if (board[2].value === 'O' && board[5].value === 'O' && board[8].value === 'O') { + setActive(false); + alert('X wins!'); + return; + } + // check for cats game + else if (active && + board[0].value && + board[1].value && + board[2].value && + board[3].value && + board[4].value && + board[5].value && + board[6].value && + board[7].value && + board[8].value) { + setActive(false); + alert('cats game!'); + } + }; + checkWinner(); + + + return {children}; +}; + +export { GameProvider, GameContext }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index d563c0f..a2431e2 100644 --- a/src/index.js +++ b/src/index.js @@ -3,11 +3,14 @@ import ReactDOM from 'react-dom/client'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; +import { GameProvider } from './context/GameContext'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render( - + + + ); diff --git a/src/initial-board-data.js b/src/initial-board-data.js new file mode 100644 index 0000000..bfb7a64 --- /dev/null +++ b/src/initial-board-data.js @@ -0,0 +1,38 @@ +export default [ + { + place: 0, + value: '' + }, + { + place: 1, + value: '' + }, + { + place: 2, + value: '' + }, + { + place: 3, + value: '' + }, + { + place: 4, + value: '' + }, + { + place: 5, + value: '' + }, + { + place: 6, + value: '' + }, + { + place: 7, + value: '' + }, + { + place: 8, + value: '' + } +]; \ No newline at end of file