-
Notifications
You must be signed in to change notification settings - Fork 2
/
WordSearch.js
156 lines (143 loc) · 4.15 KB
/
WordSearch.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
import React, { useState } from 'react';
import { View, Text, Button, StyleSheet, FlatList, TouchableOpacity } from 'react-native';
// List of Words in Word Search
const wordList = ['TREE', 'FLOWER', 'FAST', 'FANCY', 'THREAD'];
// Function to randomly generate a grid of letters based on size passed in
const generateGrid = (size) => {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // Define the alphabet
const getRandomLetter = () => {
const randomIndex = Math.floor(Math.random() * alphabet.length);
return alphabet[randomIndex];
};
const grid = [];
// Fill grid with random letters
for (let i = 0; i < size; i++) {
const row = [];
for (let j = 0; j < size; j++) {
row.push(getRandomLetter());
}
grid.push(row);
}
// Replace some letters with words from the wordList
for (const word of wordList) {
const wordLength = word.length;
let row, col;
do {
row = Math.floor(Math.random() * size);
col = Math.floor(Math.random() * (size - wordLength + 1));
} while (row + wordLength > size || col + wordLength > size);
for (let i = 0; i < wordLength; i++) {
grid[row][col + i] = word[i];
}
}
return grid;
};
const WordSearchGame = () => {
const [gridSize] = useState(7); // Change grid size as needed
const [grid, setGrid] = useState(generateGrid(gridSize));
const [selectedCoordinates, setSelectedCoordinates] = useState([]);
const [foundWords, setFoundWords] = useState([]);
const handleWordSelection = (row, col) => {
const cellIndex = selectedCoordinates.findIndex(
({ selectedRow, selectedCol }) => selectedRow === row && selectedCol === col
);
if (cellIndex !== -1) {
// If already selected, deselect it
const updatedSelection = [...selectedCoordinates];
updatedSelection.splice(cellIndex, 1);
setSelectedCoordinates(updatedSelection);
} else {
// If not selected, select it
setSelectedCoordinates([...selectedCoordinates, { selectedRow: row, selectedCol: col }]);
}
};
return (
<View style={styles.container}>
<Text style={styles.title}>Word Search Game</Text>
<View style={styles.gridContainer}>
<FlatList
data={grid}
renderItem={({ item: row, index: rowIndex }) => (
<View style={styles.row}>
{row.map((letter, colIndex) => (
<TouchableOpacity
key={`${rowIndex}-${colIndex}`}
style={[
styles.gridCell,
selectedCoordinates.some(
({ selectedRow, selectedCol }) =>
selectedRow === rowIndex && selectedCol === colIndex
) && styles.selectedCell,
]}
onPress={() => handleWordSelection(rowIndex, colIndex)}
>
<Text>{letter}</Text>
</TouchableOpacity>
))}
</View>
)}
/>
</View>
<View style={styles.foundWords}>
{foundWords.map((word) => (
<Text key={word} style={styles.foundWord}>
{word}
</Text>
))}
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
alignItems: 'center',
justifyContent: 'center',
},
title: {
marginTop: 20,
fontSize: 36,
fontWeight: 'bold',
marginTop: 10,
color: 'white',
},
gridContainer: {
marginTop: 40,
},
wordList: {
marginTop: 10,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
},
row: {
flexDirection: 'row',
},
gridCell: {
width: 40,
height: 40,
margin: 3,
backgroundColor: 'white',
justifyContent: 'center', // Center vertically
alignItems: 'center', // Center horizontally
borderRadius: '5'
},
selectedCell: {
backgroundColor: 'lightblue',
},
selectedWord: {
marginTop: 20,
fontSize: 18,
},
foundWords: {
marginTop: 10,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
},
foundWord: {
fontSize: 18,
textDecorationLine: 'line-through',
marginRight: 10,
},
});
export default WordSearchGame;