Skip to content

Commit ece8d58

Browse files
Abhijeet JainAbhijeet Jain
authored andcommitted
Feat: TicTacToe
1 parent 5a50297 commit ece8d58

File tree

10 files changed

+840
-17
lines changed

10 files changed

+840
-17
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@types/node": "^16.7.13",
1111
"@types/react": "^18.0.0",
1212
"@types/react-dom": "^18.0.0",
13+
"antd": "^5.20.6",
1314
"react": "^18.3.1",
1415
"react-dom": "^18.3.1",
1516
"react-scripts": "5.0.1",

src/App.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
import React from 'react';
22
import logo from './logo.svg';
33
import './App.css';
4+
import {Card, DatePicker} from 'antd';
5+
import MyButton from './components/MyButton';
6+
import ProfileCard from './components/ProfileCard';
7+
import TicTacToe from './components/TicTacToe';
8+
import Board from './components/TicTacToe/Board';
49

510
function App() {
11+
const [count, setCount] = React.useState(0);
12+
13+
function handleClick(){
14+
setCount(count+1);
15+
console.log(`Button clicked ${count} times`);
16+
}
617
return (
718
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.tsx</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
19+
{/* <h1>Welcome to my App!</h1>
20+
<MyButton count={count} OnClick={handleClick}/>
21+
<br/>
22+
<MyButton count={count} OnClick={handleClick}/>
23+
<ProfileCard />
24+
<br/> */}
25+
{/* <TicTacToe /> */}
26+
<Board />
2227
</div>
2328
);
2429
}

src/components/MyButton/index.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Button} from 'antd';
2+
import { MouseEventHandler, useState } from 'react';
3+
4+
// function handleClick(count: number, setCount: Function){
5+
// alert("Button Clicked");
6+
// setCount(count+1);
7+
// }
8+
9+
export default function MyButton({count, OnClick}: {count: number, OnClick: MouseEventHandler<HTMLButtonElement>}){
10+
// const [count, setCount] = useState(0);
11+
console.log("Rendering MyButton"); // why is this being called twice?
12+
13+
// function handleClick(){
14+
// setCount(count+1);
15+
// console.log(`Button clicked ${count} times`);
16+
// }
17+
return(
18+
<Button onClick={OnClick}>Clicked {count} times</Button>
19+
)
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// import { Card } from "antd";
2+
3+
4+
const user = {
5+
name: "Iron Man",
6+
imgSrc: "https://www.shutterstock.com/shutterstock/photos/2061533240/display_1500/stock-photo-mountain-view-california-october-iron-man-cardstock-mask-of-marvel-book-character-2061533240.jpg",
7+
imageSize: 90,
8+
altDesc: "Iron Man profile photo"
9+
}
10+
11+
interface IProduct {
12+
title: string;
13+
id: number;
14+
isFruit: boolean;
15+
}
16+
17+
const products: IProduct[] = [
18+
{ title: 'Cabbage', isFruit: false, id: 1 },
19+
{ title: 'Garlic', isFruit: false, id: 2 },
20+
{ title: 'Apple', isFruit: true, id: 3 },
21+
];
22+
23+
export default function ProfileCard() {
24+
let title: JSX.Element | null;
25+
let image: JSX.Element | null;
26+
let productsList: JSX.Element[] = products.map((product) => <li key={product.id} style={{ color: product.isFruit ? 'red' : 'green' }}>{product.title}</li>);
27+
if (user.name.length != 0) {
28+
title = <h1>{user.name}</h1>;
29+
image = <img
30+
className="avatar"
31+
src={user.imgSrc}
32+
alt={user.altDesc}
33+
style={{ height: user.imageSize, width: user.imageSize }} />;
34+
35+
}
36+
else {
37+
title = <h1>John Doe</h1>;
38+
image = <img src="https://www.shutterstock.com/shutterstock/photos/2061533240/display_1500/stock-photo-mountain-view-california-oct" />;
39+
}
40+
return (
41+
<div>
42+
{title}
43+
{image}
44+
<ul>{productsList}</ul>
45+
</div>
46+
);
47+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { useState } from "react";
2+
import Square from "../Square";
3+
import { nextTick } from "process";
4+
5+
export default function Board(){
6+
const [squares, setSquares] = useState<Array<String | null>>(Array(9).fill(null));
7+
const [isXNext, setIsXNext] = useState(true);
8+
9+
const calculateWinner = () => {
10+
const lines = [
11+
[0, 1, 2],
12+
[3, 4, 5],
13+
[6, 7, 8],
14+
[0, 3, 6],
15+
[1, 4, 7],
16+
[2, 5, 8],
17+
[0, 4, 8],
18+
[2, 4, 6]
19+
];
20+
21+
for(let i = 0; i<lines.length; i++){
22+
console.log("called");
23+
const [a, b, c] = lines[i];
24+
25+
if(squares[a] && squares[a] == squares[b] && squares[a] == squares[c]){
26+
return squares[a];
27+
}
28+
29+
}
30+
return null;
31+
32+
}
33+
34+
const handleClick = (i: number) => {
35+
const squaresCopy = squares.slice();
36+
if(squaresCopy[i] || calculateWinner()){
37+
return;
38+
}
39+
squaresCopy[i] = isXNext ? 'X' : 'O';
40+
setSquares(squaresCopy);
41+
setIsXNext(!isXNext);
42+
}
43+
44+
const status = () => {
45+
const winner = calculateWinner();
46+
if(winner){
47+
return `Winner is ${winner}`;
48+
}
49+
}
50+
51+
return (
52+
<>
53+
<div className='board-row'>
54+
<Square value={squares[0]} OnSquareClicked={() => handleClick(0)} />
55+
<Square value={squares[1]} OnSquareClicked={() => handleClick(1)} />
56+
<Square value={squares[2]} OnSquareClicked={() => handleClick(2)} />
57+
</div>
58+
<div className='board-row'>
59+
<Square value={squares[3]} OnSquareClicked={() => handleClick(3)} />
60+
<Square value={squares[4]} OnSquareClicked={() => handleClick(4)} />
61+
<Square value={squares[5]} OnSquareClicked={() => handleClick(5)} />
62+
</div>
63+
<div className='board-row'>
64+
<Square value={squares[6]} OnSquareClicked={() => handleClick(6)} />
65+
<Square value={squares[7]} OnSquareClicked={() => handleClick(7)} />
66+
<Square value={squares[8]} OnSquareClicked={() => handleClick(8)} />
67+
</div>
68+
<div>{status()}</div>
69+
</>
70+
)
71+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Button } from 'antd';
2+
import { MouseEventHandler, useState } from 'react';
3+
4+
export default function Square({value, OnSquareClicked}: {value: String | null, OnSquareClicked: MouseEventHandler<HTMLButtonElement>}){
5+
6+
return (
7+
<Button className='square' onClick={OnSquareClicked}>{value || ""}</Button>
8+
)
9+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.square {
2+
background: #fff;
3+
border: 1px solid #999;
4+
float: left;
5+
font-size: 24px;
6+
font-weight: bold;
7+
line-height: 34px;
8+
height: 34px;
9+
margin-right: -1px;
10+
margin-top: -1px;
11+
padding: 0;
12+
text-align: center;
13+
width: 34px;
14+
}
15+
16+
.board-row:after {
17+
clear: both;
18+
content: '';
19+
display: table;
20+
}
21+
22+
.board-row{
23+
display: flex;
24+
flex-direction: row;
25+
justify-content: center;
26+
}

src/components/TicTacToe/index.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Square from "./Square";
2+
3+
export default function TicTacToe() {
4+
return (
5+
<>
6+
7+
</>
8+
9+
10+
)
11+
}

src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom/client';
33
import './index.css';
4+
import './components/TicTacToe/Square/styles.css';
45
import App from './App';
56
import reportWebVitals from './reportWebVitals';
67

0 commit comments

Comments
 (0)