TangJicheng commited on
Commit
7cfaaee
1 Parent(s): 2bd6b75

chore: add katakana

Browse files
Files changed (3) hide show
  1. index.html +12 -17
  2. script.js +157 -0
  3. style.css +20 -22
index.html CHANGED
@@ -1,19 +1,14 @@
1
  <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
  </html>
 
1
  <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Katakana</title>
7
+ <link rel="stylesheet" href="styles.css">
8
+ </head>
9
+ <body>
10
+ <div id="chineseWords" class="wordContainer"></div>
11
+ <div id="englishWords" class="wordContainer"></div>
12
+ <script src="script.js"></script>
13
+ </body>
 
 
 
 
 
14
  </html>
script.js ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const fruitMap = {
2
+ 'あ':'a',
3
+ 'い':'i',
4
+ 'う':'u',
5
+ 'え':'e',
6
+ 'お':'o',
7
+ 'か':'ka',
8
+ 'き':'ki',
9
+ 'く':'ku',
10
+ 'け':'ke',
11
+ 'こ':'ko',
12
+ 'さ':'sa',
13
+ 'し':'shi',
14
+ 'す':'su',
15
+ 'せ':'se',
16
+ 'そ':'so',
17
+ 'た':'ta',
18
+ 'ち':'chi',
19
+ 'つ':'tsu',
20
+ 'て':'te',
21
+ 'と':'to',
22
+ 'な':'na',
23
+ 'に':'ni',
24
+ 'ぬ':'nu',
25
+ 'ね':'ne',
26
+ 'の':'no',
27
+ 'は':'ha',
28
+ 'ひ':'hi',
29
+ 'ふ':'fu',
30
+ 'へ':'he',
31
+ 'ほ':'ho',
32
+ 'ま':'ma',
33
+ 'み':'mi',
34
+ 'む':'mu',
35
+ 'め':'me',
36
+ 'も':'mo',
37
+ 'や':'ya',
38
+ 'ゆ':'yu',
39
+ 'よ':'yo',
40
+ 'ら':'ra',
41
+ 'り':'ri',
42
+ 'る':'ru',
43
+ 'れ':'re',
44
+ 'ろ':'ro',
45
+ 'わ':'wa',
46
+ 'を':'wo',
47
+ 'ん':'n'
48
+ };
49
+
50
+
51
+ function shuffleArray(array) {
52
+ for (let i = array.length - 1; i > 0; i--) {
53
+ const j = Math.floor(Math.random() * (i + 1));
54
+ [array[i], array[j]] = [array[j], array[i]]; // 交换
55
+ }
56
+ }
57
+
58
+ const totalWords = 4;
59
+
60
+ function getRandomSubmap(map, n) {
61
+ const keys = Object.keys(map);
62
+ const shuffledKeys = keys.sort(() => Math.random() - 0.5);
63
+ const newMap = {};
64
+
65
+ for (let i = 0; i < n && i < shuffledKeys.length; i++) {
66
+ const key = shuffledKeys[i];
67
+ newMap[key] = map[key];
68
+ }
69
+
70
+ return newMap;
71
+ }
72
+
73
+
74
+ function generateWords() {
75
+ newMap = getRandomSubmap(fruitMap, totalWords)
76
+ const chineseWords = Object.keys(newMap);
77
+ const englishWords = Object.values(newMap);
78
+
79
+ shuffleArray(chineseWords);
80
+ shuffleArray(englishWords);
81
+
82
+ const chineseContainer = document.getElementById('chineseWords');
83
+ const englishContainer = document.getElementById('englishWords');
84
+
85
+ chineseContainer.innerHTML = '';
86
+ englishContainer.innerHTML = '';
87
+
88
+ chineseWords.forEach(word => {
89
+ const button = document.createElement('button');
90
+ button.textContent = word;
91
+ button.className = 'button';
92
+ button.onclick = () => wordClicked(word, 'chinese', button); // 将按钮元素也传递给wordClicked函数
93
+ chineseContainer.appendChild(button);
94
+ });
95
+
96
+ englishWords.forEach(word => {
97
+ const button = document.createElement('button');
98
+ button.textContent = word;
99
+ button.className = 'button';
100
+ button.onclick = () => wordClicked(word, 'english', button); // 将按钮元素也传递给wordClicked函数
101
+ englishContainer.appendChild(button);
102
+ });
103
+ }
104
+
105
+ let selectedWords = [];
106
+ let matchCount = 0;
107
+ function checkMatch() {
108
+ const selected = [...selectedWords]; // 保存对selectedWords的引用
109
+ selectedWords = []; // 立即重置selectedWords数组
110
+
111
+ const [firstButton, secondButton] = selected;
112
+ if (firstButton.language === secondButton.language) {
113
+ alert('请选择不同语言的单词!');
114
+ resetButtonColor(firstButton);
115
+ resetButtonColor(secondButton);
116
+ } else {
117
+ const chineseWord = firstButton.language === 'chinese' ? firstButton.word : secondButton.word;
118
+ if (fruitMap[chineseWord] === (firstButton.language === 'english' ? firstButton.word : secondButton.word)) {
119
+ matchCount++;
120
+ firstButton.element.style.backgroundColor = 'green';
121
+ secondButton.element.style.backgroundColor = 'green';
122
+
123
+ if (matchCount === totalWords) {
124
+ alert('全部匹配成功!');
125
+ matchCount = 0;
126
+ generateWords();
127
+ }
128
+ } else {
129
+ alert('匹配失败,请重试!');
130
+ resetButtonColor(firstButton);
131
+ resetButtonColor(secondButton);
132
+ }
133
+ }
134
+ }
135
+
136
+ function resetButtonColor(button) {
137
+ // 只有当按钮颜色不是绿色时才重置颜色
138
+ if (button.element.style.backgroundColor !== 'green') {
139
+ button.element.style.backgroundColor = '';
140
+ }
141
+ }
142
+
143
+ function wordClicked(word, language, element) {
144
+ // 如果按钮颜色已经是绿色,则不允许用户选择
145
+ if (element.style.backgroundColor === 'green') {
146
+ return;
147
+ }
148
+
149
+ selectedWords.push({ word, language, element }); // 将元素也传递到对象中
150
+ element.style.backgroundColor = 'yellow'; // 当单词被选中时,改变按钮颜色为黄色
151
+ if (selectedWords.length === 2) {
152
+ checkMatch();
153
+ selectedWords = [];
154
+ }
155
+ }
156
+
157
+ window.onload = generateWords;
style.css CHANGED
@@ -1,28 +1,26 @@
1
  body {
2
- padding: 2rem;
3
- font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
 
 
 
 
 
4
  }
5
 
6
- h1 {
7
- font-size: 16px;
8
- margin-top: 0;
 
 
 
 
9
  }
10
 
11
- p {
12
- color: rgb(107, 114, 128);
13
- font-size: 15px;
14
- margin-bottom: 10px;
15
- margin-top: 5px;
16
- }
17
-
18
- .card {
19
- max-width: 620px;
20
- margin: 0 auto;
21
- padding: 16px;
22
- border: 1px solid lightgray;
23
- border-radius: 16px;
24
- }
25
-
26
- .card p:last-child {
27
- margin-bottom: 0;
28
  }
 
1
  body {
2
+ display: flex;
3
+ flex-direction: column;
4
+ align-items: center;
5
+ justify-content: center;
6
+ min-height: 100vh;
7
+ margin: 0;
8
+ font-family: Arial, sans-serif;
9
  }
10
 
11
+ .wordContainer {
12
+ display: grid;
13
+ grid-template-columns: repeat(4, 1fr); /* 每行显示4个按钮 */
14
+ gap: 10px;
15
+ justify-content: center;
16
+ width: 80%;
17
+ margin-bottom: 20px;
18
  }
19
 
20
+ .button {
21
+ padding: 10px;
22
+ border: 1px solid black;
23
+ font-size: 18px;
24
+ text-align: center;
25
+ cursor: pointer;
 
 
 
 
 
 
 
 
 
 
 
26
  }