-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
196 lines (170 loc) · 4.7 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { useEffect, useState } from 'react';
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, SafeAreaView, View, ScrollView, Alert } from 'react-native';
import { colors, CLEAR, ENTER, colorsToEmoji } from './src/constants';
import Keyboard from './src/components/Keyboard';
import * as Clipboard from 'expo-clipboard';
const NUMBER_OF_TRIES = 6;
const copyArray = (arr) => {
return [...arr.map((rows) => [...rows])]
}
export default function App() {
const word = "hello";
const letters = word.split("");
const [rows, setRows] = useState(
new Array(NUMBER_OF_TRIES).fill(
new Array(letters.length).fill("")
))
const [curRow, setCurRow] = useState(0);
const [curCol, setCurCol] = useState(0);
const [gameState, setGameState] = useState("playing"); // won, lost, playing
useEffect(() => {
if (curRow > 0){
checkGameState();
}
}, [curRow]);
const checkGameState = () => {
if (checkIfWon() && gameState !== "won"){
Alert.alert("Hurray", "You won!", [
{text: "Share", onPress: shareScore },
]);
setGameState("won");
} else if(checkIfLost() && gameState !== "lost"){
Alert.alert("Oops", "Try again tomorrow");
setGameState("lost");
}
};
const shareScore = () => {
const textMap = rows.map((row, i) =>
row.map((cell, j) => colorsToEmoji[getCellBGColor(i, j)]).join("")).filter((row) => row).join("\n");
const textToShare = `Wordle Clone \n${textMap}`;
Clipboard.setStringAsync(textToShare);
Alert.alert("Copied Successfully", "Share your score on zcamp");
};
const onKeyPressed = (key) => {
if (gameState !== "playing"){
return;
}
const updatedRows = copyArray(rows);
if(key === CLEAR){
const prevCol = curCol - 1;
if (prevCol >= 0){
updatedRows[curRow][prevCol] = "";
setRows(updatedRows);
setCurCol(prevCol);
}
return;
}
if(key === ENTER){
if(curCol === rows[0].length){
setCurRow(curRow + 1);
setCurCol(0);
}
return;
}
if (curCol < rows[0].length){
updatedRows[curRow][curCol] = key;
setRows(updatedRows);
setCurCol(curCol + 1);
}
}
const isCellActive = (row, col) => {
return row === curRow && col === curCol;
}
const getCellBGColor = (row, col) => {
const letter = rows[row][col];
if (row >= curRow){
return colors.black;
}
if(letter === letters[col]){
return colors.primary;
}
if (letters.includes(letter)){
return colors.secondary;
}
return colors.darkgrey;
};
const getAllLettersWithColor = (color) => {
return rows.flatMap((row, i) =>
row.filter((cell, j) => getCellBGColor(i, j) === color)
);
};
const greenCaps = getAllLettersWithColor(colors.primary);
const yellowCaps = getAllLettersWithColor(colors.secondary);
const greyCaps = getAllLettersWithColor(colors.darkgrey);
const checkIfWon = () => {
const row = rows[curRow - 1];
return row.every((letter, i) => letter === letters[i]);
};
const checkIfLost = () => {
return !checkIfWon() && curRow === rows.length;
}
return (
<SafeAreaView style={styles.container}>
<StatusBar style="light" />
<Text style={styles.title}>WORDLE</Text>
<ScrollView style={styles.map}>
{rows.map(( row, i ) => (
<View key={i} style={styles.row}>
{row.map((letter, j) => (
<View key={j} style={[styles.cell, {
borderColor: isCellActive(i, j)
? colors.lightgrey
: colors.darkgrey,
backgroundColor: getCellBGColor(i, j)
},
]}>
<Text style={styles.cellText}>
{letter.toUpperCase()}
</Text>
</View>
))}
</View>
))}
</ScrollView>
<Keyboard
onKeyPressed={onKeyPressed}
greenCaps={greenCaps}
yellowCaps={yellowCaps}
greyCaps={greyCaps} />
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: colors.black,
alignItems: 'center',
},
title: {
color: colors.lightgrey,
fontSize: 32,
fontWeight: "bold",
letterSpacing: 7,
top: 40
},
map: {
marginVertical: 60,
alignSelf: "stretch",
},
row: {
alignSelf: "stretch",
flexDirection: "row",
justifyContent: "center",
},
cell: {
borderWidth: 3,
borderColor: colors.darkgrey,
flex: 1,
aspectRatio: 1,
margin: 3,
justifyContent: "center",
alignItems: "center",
maxWidth: 70,
},
cellText: {
color: colors.lightgrey,
fontWeight: "bold",
fontSize: 28,
}
});