Skip to content

Commit a746a00

Browse files
committed
game difficulty
1 parent a8a5818 commit a746a00

File tree

5 files changed

+123
-23
lines changed

5 files changed

+123
-23
lines changed

src/assets/constants.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export const operations = ["+", "-", "*"];
33
export const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
44

55
export const themes = ["default", "dawn", "relay", "moonlit asteroid", "mini"];
6+
7+
export const gameDifficulties = ["easy", "middle", "hard"];

src/assets/utils.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import _ from "lodash";
33
import { themes } from "./constants";
44

55
export const applyOperation = (value, operation, num) => {
6+
console.log(value, operation, num);
67
switch (operation) {
78
case "+":
89
return value + num;
@@ -34,7 +35,11 @@ export const addPointsToTheScore = () => {
3435
export const setNextTheme = () => {
3536
let nextTheme;
3637
if (window.localStorage.getItem("theme")) {
37-
nextTheme = themes[(themes.indexOf(window.localStorage.getItem("theme")) + 1)%themes.length];
38+
nextTheme =
39+
themes[
40+
(themes.indexOf(window.localStorage.getItem("theme")) + 1) %
41+
themes.length
42+
];
3843
} else nextTheme = _.sample(themes);
3944
window.localStorage.setItem("theme", nextTheme);
4045
return nextTheme;

src/components/Dropdown.jsx

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from "react";
2+
import styled from "styled-components";
3+
4+
const Dropdown = ({ items, isMobile, onChange }) => {
5+
return (
6+
<StyledSelect onChange={onChange} isMobile={isMobile} name="select">
7+
{items.map((item) => {
8+
return <StyledOption value={item}>{item}</StyledOption>;
9+
})}
10+
</StyledSelect>
11+
);
12+
};
13+
14+
const StyledSelect = styled.select`
15+
background: rgba(208, 208, 208, 0.1);
16+
backdrop-filter: blur(10px);
17+
-webkit-backdrop-filter: blur(10px);
18+
border-radius: 10px;
19+
padding: 16px;
20+
font-size: ${(props) => (props.isMobile ? "40px" : "16px")};
21+
border: none;
22+
cursor: pointer;
23+
color: #ffffff;
24+
${(props) => (props.isMobile ? "height: 140px; padding-left: 40px;" : null)}
25+
text-shadow: 1px 1px 2px rgb(0 0 0);
26+
-moz-appearance: none; /* Firefox */
27+
-webkit-appearance: none; /* Safari and Chrome */
28+
appearance: none;
29+
`;
30+
31+
const StyledOption = styled.option`
32+
color: black;
33+
font-size: ${(props) => (props.isMobile ? "40px" : "16px")};
34+
`;
35+
36+
export default Dropdown;

src/pages/StartPage.jsx

+52-18
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ import MainTitle from "../components/MainTitle";
66
import MobileMainTitle from "../components/MobileMainTitle";
77
import MainContainer from "../components/MainContainer";
88
import Card from "../components/Card";
9+
import Dropdown from "../components/Dropdown";
910
import styled from "styled-components";
1011

12+
import { gameDifficulties } from "../assets/constants";
13+
import { setGameDifficulty } from "../store/gameSlice";
14+
1115
import { Link } from "react-router-dom";
1216

1317
import { useSelector, useDispatch } from "react-redux";
@@ -18,6 +22,11 @@ import { useEffect } from "react";
1822
const StartPage = () => {
1923
const dispatch = useDispatch();
2024

25+
const handleChange = (e) => {
26+
const { value } = e.target;
27+
dispatch(setGameDifficulty(value));
28+
};
29+
2130
return (
2231
<Background>
2332
<BrowserView>
@@ -38,14 +47,18 @@ const StartPage = () => {
3847
</StyledText>
3948
</Container>
4049
<Container>
41-
<Link
42-
to="/game"
43-
onClick={() => {
44-
dispatch(gameInit());
45-
}}
46-
>
47-
<Button type="primary">Start game</Button>
48-
</Link>
50+
<StyledWrapper>
51+
<StyledDifficultyText>Difficulty:</StyledDifficultyText>
52+
<Dropdown onChange={handleChange} items={gameDifficulties} />
53+
<StyledLink
54+
to="/game"
55+
onClick={() => {
56+
dispatch(gameInit());
57+
}}
58+
>
59+
<Button type="primary">Start game</Button>
60+
</StyledLink>
61+
</StyledWrapper>
4962
</Container>
5063
</MainContainer>
5164
</BrowserView>
@@ -66,16 +79,20 @@ const StartPage = () => {
6679
</StyledMobileText>
6780
</MobileContainer>
6881
<MobileContainer>
69-
<Link
70-
to="/game"
71-
onClick={() => {
72-
dispatch(gameInit());
73-
}}
74-
>
75-
<Button isMobile type="primary">
76-
Start game
77-
</Button>
78-
</Link>
82+
<StyledWrapper>
83+
<StyledDifficultyText>Difficulty:</StyledDifficultyText>
84+
<Dropdown isMobile items={gameDifficulties} />
85+
<StyledMobileLink
86+
to="/game"
87+
onClick={() => {
88+
dispatch(gameInit());
89+
}}
90+
>
91+
<Button isMobile type="primary">
92+
Start game
93+
</Button>
94+
</StyledMobileLink>
95+
</StyledWrapper>
7996
</MobileContainer>
8097
</MobileView>
8198
</Background>
@@ -106,4 +123,21 @@ const StyledMobileTitle = styled.div`
106123
font-family: "Lato", sans-serif;
107124
`;
108125

126+
const StyledWrapper = styled.div`
127+
display: flex;
128+
flex-direction: column;
129+
`;
130+
131+
const StyledLink = styled(Link)`
132+
margin-top: 8px;
133+
`;
134+
135+
const StyledDifficultyText = styled.span`
136+
margin-bottom: 4px;
137+
`;
138+
139+
const StyledMobileLink = styled(Link)`
140+
margin-top: 20px;
141+
`;
142+
109143
export default StartPage;

src/store/gameSlice.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,22 @@ export const gameSlice = createSlice({
1616
},
1717
reducers: {
1818
gameInit: (state) => {
19-
state.current = _.random(1, 10);
20-
state.steps = _.random(2, 3);
19+
switch (state.gameDifficulty) {
20+
case "easy":
21+
state.steps = _.random(2, 3);
22+
state.current = _.random(1, 5);
23+
break;
24+
case "hard":
25+
state.steps = _.random(3, 4);
26+
state.current = _.random(1, 25);
27+
break;
28+
default:
29+
case "middle":
30+
state.steps = _.random(2, 3);
31+
state.current = _.random(1, 10);
32+
break;
33+
}
34+
console.log("before", state.steps);
2135
state.operations = _.sampleSize(operations, 3);
2236
state.operands = _.sampleSize(numbers, 3);
2337
state.target = getTarget(
@@ -27,6 +41,7 @@ export const gameSlice = createSlice({
2741
state.operands
2842
);
2943
state.steps += 1;
44+
console.log("after", state.steps);
3045
state.gameState = "in progress";
3146
return state;
3247
},
@@ -37,10 +52,18 @@ export const gameSlice = createSlice({
3752
updateGameState: (state, action) => {
3853
state.gameState = action.payload;
3954
},
55+
setGameDifficulty: (state, action) => {
56+
state.gameDifficulty = action.payload;
57+
},
4058
},
4159
});
4260

43-
export const { gameInit, generateTarget, updateCurrent, updateGameState } =
44-
gameSlice.actions;
61+
export const {
62+
gameInit,
63+
generateTarget,
64+
updateCurrent,
65+
updateGameState,
66+
setGameDifficulty,
67+
} = gameSlice.actions;
4568

4669
export default gameSlice.reducer;

0 commit comments

Comments
 (0)