回答編集履歴

2

見直しキャンペーン中

2023/07/29 09:21

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,611 +1,307 @@
1
1
  たくさん回答も付いてるうえBAもされているようですが^^;
2
2
 
3
-
4
-
5
3
  結局やりたいことって「問題の順番・選択肢の順番をランダムにしたい」ということですよね?
6
-
7
4
  難しく考えすぎというか、外野であれこれしようとしすぎというか。
8
5
 
9
-
10
-
11
6
  今回の場合は単に取得時に、都度シャッフルした配列・リストを返せばいいだけに見えます。
12
-
13
7
  カテゴリもなぜ文字列で渡さなければならないかが、コードから読み取れません(リスト自体を渡せばいいだけでは?)
14
8
 
15
-
16
-
17
9
  xamlがわからないので何とも言えませんが、`ItemsControl`等をうまく使うとコレクション内の個数を気にせず表示できます(回答は選択肢だけで使っていますが、カテゴリでも同じように作れます)
18
10
 
19
-
20
-
21
11
  コードがないとイメージしにくそうなので、非常に雑ですがサンプルです(ファイル云々は省略)
22
12
 
23
-
24
-
25
13
  MainWindow
26
-
27
- ```xaml
14
+ ```xml
28
-
29
15
  <NavigationWindow
30
-
31
16
  x:Class="Questions370911.MainWindow"
32
-
33
17
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
34
-
35
18
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
36
-
37
19
  Width="800"
38
-
39
20
  Height="450"
40
-
41
21
  ShowsNavigationUI="False"
42
-
43
22
  Source="/CategoryPage.xaml" />
44
-
45
- ```
23
+ ```
46
-
47
-
48
-
24
+
49
- ```C#
25
+ ```cs
50
-
51
26
  using System;
52
-
53
27
  using System.Collections.Generic;
54
-
55
28
  using System.Linq;
56
29
 
57
-
58
-
59
30
  namespace Questions370911
60
-
61
31
  {
62
-
63
32
  public class QuizData
64
-
65
33
  {
66
-
67
34
  public string Question { get; }
68
35
 
69
-
70
-
71
36
  // プロパティにして取得のたびにシャッフルした配列を返す
72
-
73
37
  public string[] Choices
74
-
75
- {
38
+ {
76
-
77
39
  get
78
-
79
40
  {
80
-
81
41
  return _Choices.OrderBy(_ => Guid.NewGuid()).ToArray();
82
-
83
42
  }
84
-
85
- }
43
+ }
86
-
87
44
  // ↑↓同じ意味
88
-
89
45
  //public string[] Choices => _Choices.OrderBy(_ => Guid.NewGuid()).ToArray();
90
46
 
91
-
92
-
93
47
  public string Answer { get; }
94
48
 
95
-
96
-
97
49
  private readonly string[] _Choices;
98
50
 
99
-
100
-
101
51
  public QuizData(string question, params string[] choices)
102
-
103
- {
52
+ {
104
-
105
53
  Question = question;
106
-
107
54
  _Choices = choices;
108
-
109
55
  Answer = choices[0];
110
-
111
- }
56
+ }
112
-
113
57
  }
114
58
 
115
59
 
116
-
117
-
118
-
119
60
  public partial class MainWindow // : Window
120
-
121
61
  {
122
-
123
62
  // ここも
124
-
125
63
  public List<QuizData> A_QuizData => _A_QuizData.OrderBy(_ => Guid.NewGuid()).ToList();
126
-
127
64
  public List<QuizData> B_QuizData => _B_QuizData.OrderBy(_ => Guid.NewGuid()).ToList();
128
-
129
65
  public List<QuizData> C_QuizData => _C_QuizData.OrderBy(_ => Guid.NewGuid()).ToList();
130
66
 
131
-
132
-
133
67
  private readonly List<QuizData> _A_QuizData = new List<QuizData>
134
-
135
- {
68
+ {
136
-
137
69
  new QuizData("史上初の家庭用ゲーム機とされるものは?", "オデッセイ(マグナボックス)", "ホーム・ポン(アタリ)", "カラーテレビゲーム15(任天堂)"),
138
-
139
70
  new QuizData("ファミコンのコントローラーにはマイクが付いている?", "○", "×"),
140
-
141
71
  new QuizData("スーパーファミコンソフト「ドラゴンクエスト6 幻の大地」の定価(税抜)は?", "11,400円", "14,800円", "9,700円", "5,980円"),
142
-
143
72
  new QuizData("PlayStationの同時発売ソフトは?", "リッジレーサー(ナムコ)", "モータートゥーン・グランプリ(SCE)", "チョロQ(タカラ)"),
144
-
145
73
  };
146
-
147
74
  private readonly List<QuizData> _B_QuizData = new List<QuizData>
148
-
149
- {
75
+ {
150
-
151
76
  new QuizData("hogehoge", "A", "B", "C"),
152
-
153
77
  };
154
-
155
78
  private readonly List<QuizData> _C_QuizData = new List<QuizData>
156
-
157
- {
79
+ {
158
-
159
80
  new QuizData("fugafuga", "1", "2", "3", "4"),
160
-
161
81
  };
162
82
 
163
83
 
164
-
165
-
166
-
167
84
  public MainWindow() => InitializeComponent();
168
-
169
85
  }
170
-
171
86
  }
172
-
173
- ```
87
+ ```
174
-
175
-
176
88
 
177
89
  CategoryPage
178
-
179
- ```xaml
90
+ ```xml
180
-
181
91
  <Page
182
-
183
92
  x:Class="Questions370911.CategoryPage"
184
-
185
93
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
186
-
187
94
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
188
-
189
95
  <UniformGrid Columns="3">
190
-
191
96
  <Button Click="ButtonA_Click" Content="おじさんゲーマークイズ" />
192
-
193
97
  <Button Click="ButtonB_Click" Content="hogehoge" />
194
-
195
98
  <Button Click="ButtonC_Click" Content="fugafuga" />
196
-
197
99
  </UniformGrid>
198
-
199
100
  </Page>
200
-
201
- ```
101
+ ```
202
-
203
-
204
-
102
+
205
- ```C#
103
+ ```cs
206
-
207
104
  using System.Windows;
208
-
209
105
  using System.Windows.Controls;
210
-
211
106
  using System.Windows.Navigation;
212
107
 
213
-
214
-
215
108
  namespace Questions370911
216
-
217
109
  {
218
-
219
110
  public partial class CategoryPage : Page
220
-
221
111
  {
222
-
223
112
  // こうしてしまうと「今」表示しているMainWindowとは別のMainWindowを作ってしまっている!
224
-
225
113
  //readonly MainWindow _main = new MainWindow();
226
-
227
114
  private readonly MainWindow _main;
228
115
 
229
-
230
-
231
116
  public CategoryPage()
232
-
233
- {
117
+ {
234
-
235
118
  InitializeComponent();
236
119
 
237
-
238
-
239
120
  // 手軽にやるならこう
240
-
241
121
  _main = (MainWindow)Application.Current.MainWindow;
242
-
243
- }
122
+ }
244
-
245
-
246
123
 
247
124
  private void ButtonA_Click(object sender, RoutedEventArgs e)
248
-
249
125
  => NavigationService.Navigate(new QuizPage(_main.A_QuizData));
250
-
251
126
  private void ButtonB_Click(object sender, RoutedEventArgs e)
252
-
253
127
  => NavigationService.Navigate(new QuizPage(_main.B_QuizData));
254
-
255
128
  private void ButtonC_Click(object sender, RoutedEventArgs e)
256
-
257
129
  => NavigationService.Navigate(new QuizPage(_main.C_QuizData));
258
-
259
130
  }
260
-
261
131
  }
262
-
263
- ```
132
+ ```
264
-
265
-
266
133
 
267
134
  QuizPage
268
-
269
- ```xaml
135
+ ```xml
270
-
271
136
  <Page
272
-
273
137
  x:Class="Questions370911.QuizPage"
274
-
275
138
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
276
-
277
139
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
278
-
279
140
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
280
-
281
141
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
282
-
283
142
  d:DesignHeight="450"
284
-
285
143
  d:DesignWidth="800"
286
-
287
144
  mc:Ignorable="d">
288
-
289
145
  <Page.Resources>
290
-
291
146
  <Style TargetType="TextBlock">
292
-
293
147
  <Setter Property="HorizontalAlignment" Value="Center" />
294
-
295
148
  <Setter Property="VerticalAlignment" Value="Center" />
296
-
297
149
  </Style>
298
-
299
150
  <Style TargetType="Button">
300
-
301
151
  <Setter Property="HorizontalAlignment" Value="Center" />
302
-
303
152
  <Setter Property="VerticalAlignment" Value="Center" />
304
-
305
153
  </Style>
306
-
307
154
  </Page.Resources>
308
-
309
155
  <Grid>
310
-
311
156
  <!--
312
-
313
157
  TabControlを使った深い意味はありません。
314
-
315
158
  比較的シンプルでデザインも確認しやすいって程度です。
316
-
317
159
  -->
318
-
319
160
  <TabControl x:Name="tabControl" BorderThickness="0">
320
-
321
161
  <TabItem Header="問題" Visibility="Collapsed">
322
-
323
162
  <Grid>
324
-
325
163
  <Grid.RowDefinitions>
326
-
327
- <RowDefinition />
164
+ <RowDefinition />
328
-
329
- <RowDefinition />
165
+ <RowDefinition />
330
-
331
166
  </Grid.RowDefinitions>
332
-
333
167
  <TextBlock x:Name="questionTextBlock" Text="問題文" />
334
-
335
168
  <ItemsControl
336
-
337
169
  x:Name="itemsControl"
338
-
339
170
  Grid.Row="1"
340
-
341
171
  HorizontalAlignment="Center"
342
-
343
172
  VerticalAlignment="Center"
344
-
345
173
  d:ItemsSource="{d:SampleData}"
346
-
347
174
  Button.Click="AnswerButton_Click">
348
-
349
175
  <ItemsControl.ItemsPanel>
350
-
351
176
  <ItemsPanelTemplate>
352
-
353
177
  <WrapPanel />
354
-
355
178
  </ItemsPanelTemplate>
356
-
357
179
  </ItemsControl.ItemsPanel>
358
-
359
180
  <ItemsControl.ItemTemplate>
360
-
361
181
  <DataTemplate>
362
-
363
182
  <Button Margin="5" Content="{Binding}" />
364
-
365
183
  </DataTemplate>
366
-
367
184
  </ItemsControl.ItemTemplate>
368
-
369
185
  </ItemsControl>
370
-
371
186
  </Grid>
372
-
373
187
  </TabItem>
374
188
 
375
-
376
-
377
189
  <TabItem Header="正解" Visibility="Collapsed">
378
-
379
190
  <Grid>
380
-
381
191
  <Grid.RowDefinitions>
382
-
383
- <RowDefinition />
192
+ <RowDefinition />
384
-
385
- <RowDefinition />
193
+ <RowDefinition />
386
-
387
194
  </Grid.RowDefinitions>
388
-
389
195
  <TextBlock Text="正解" />
390
-
391
196
  <Button
392
-
393
197
  Grid.Row="1"
394
-
395
198
  Click="NextButton_Click"
396
-
397
199
  Content="次へ" />
398
-
399
200
  </Grid>
400
-
401
201
  </TabItem>
402
202
 
403
-
404
-
405
203
  <TabItem Header="不正解" Visibility="Collapsed">
406
-
407
204
  <Grid>
408
-
409
205
  <Grid.RowDefinitions>
410
-
411
- <RowDefinition />
206
+ <RowDefinition />
412
-
413
- <RowDefinition />
207
+ <RowDefinition />
414
-
415
208
  </Grid.RowDefinitions>
416
-
417
209
  <TextBlock Text="不正解" />
418
-
419
210
  <Button
420
-
421
211
  Grid.Row="1"
422
-
423
212
  Click="NextButton_Click"
424
-
425
213
  Content="次へ" />
426
-
427
214
  </Grid>
428
-
429
215
  </TabItem>
430
216
 
431
-
432
-
433
217
  <TabItem Header="成績" Visibility="Collapsed">
434
-
435
218
  <Grid>
436
-
437
219
  <Grid.RowDefinitions>
438
-
439
- <RowDefinition />
220
+ <RowDefinition />
440
-
441
- <RowDefinition />
221
+ <RowDefinition />
442
-
443
222
  </Grid.RowDefinitions>
444
-
445
223
  <TextBlock x:Name="resultTextBlock" Text="〇問中〇問正解" />
446
-
447
224
  <Button
448
-
449
225
  Grid.Row="1"
450
-
451
226
  Click="BackButton_Click"
452
-
453
227
  Content="戻る" />
454
-
455
228
  </Grid>
456
-
457
229
  </TabItem>
458
-
459
230
  </TabControl>
460
-
461
231
  </Grid>
462
-
463
232
  </Page>
464
-
465
- ```
233
+ ```
466
-
467
-
468
-
234
+
469
- ```C#
235
+ ```cs
470
-
471
236
  using System;
472
-
473
237
  using System.Collections.Generic;
474
-
475
238
  using System.Windows;
476
-
477
239
  using System.Windows.Controls;
478
-
479
240
  using System.Windows.Navigation;
480
241
 
481
-
482
-
483
242
  namespace Questions370911
484
-
485
243
  {
486
-
487
244
  public partial class QuizPage : Page
488
-
489
245
  {
490
-
491
246
  private readonly List<QuizData> quizData;
492
-
493
247
  private int index;
494
-
495
248
  private int correct;
496
249
 
497
-
498
-
499
250
  // わざわざ文字列でカテゴリを渡すより、直接リストをもらったほうが早いでしょう
500
-
501
251
  // カテゴリ名も欲しければ、KOZ6.0さんの回答のようにクラスにまとめましょう
502
-
503
252
  public QuizPage(List<QuizData> quizData)
504
-
505
- {
253
+ {
506
-
507
254
  InitializeComponent();
508
255
 
509
-
510
-
511
256
  this.quizData = quizData;
512
257
 
513
-
514
-
515
258
  questionTextBlock.Text = quizData[index].Question;
516
-
517
259
  itemsControl.ItemsSource = quizData[index].Choices;
518
-
519
260
  tabControl.SelectedIndex = 0;
520
-
521
- }
261
+ }
522
-
523
-
524
262
 
525
263
  private void AnswerButton_Click(object sender, RoutedEventArgs e)
526
-
527
- {
264
+ {
528
-
529
265
  if (e.OriginalSource is Button button)
530
-
531
266
  {
532
-
533
267
  if ((string)button.Content == quizData[index].Answer)
534
-
535
268
  {
536
-
537
269
  tabControl.SelectedIndex = 1;
538
-
539
270
  correct++;
540
-
541
271
  }
542
-
543
272
  else
544
-
545
273
  {
546
-
547
274
  tabControl.SelectedIndex = 2;
548
-
549
275
  }
550
276
 
551
-
552
-
553
277
  index++;
554
-
555
278
  }
556
-
557
- }
279
+ }
558
-
559
-
560
280
 
561
281
  private void NextButton_Click(object sender, RoutedEventArgs e)
562
-
563
- {
282
+ {
564
-
565
283
  if (index < quizData.Count)
566
-
567
284
  {
568
-
569
285
  questionTextBlock.Text = quizData[index].Question;
570
-
571
286
  itemsControl.ItemsSource = quizData[index].Choices;
572
-
573
287
  tabControl.SelectedIndex = 0;
574
-
575
288
  }
576
-
577
289
  else
578
-
579
290
  {
580
-
581
291
  resultTextBlock.Text = $"{quizData.Count}問中{correct}問正解";
582
-
583
292
  tabControl.SelectedIndex = 3;
584
-
585
293
  }
586
-
587
- }
294
+ }
588
-
589
-
590
295
 
591
296
  private void BackButton_Click(object sender, RoutedEventArgs e)
592
-
593
297
  => NavigationService.Navigate(new Uri("/CategoryPage.xaml", UriKind.Relative));
594
-
595
298
  }
596
-
597
299
  }
598
-
599
- ```
300
+ ```
600
-
301
+ ![アプリ動画](https://ddjkaamml8q8x.cloudfront.net/questions/2023-07-29/a4144649-e69a-4091-b718-8227e124ecb2.gif)
601
-
602
302
 
603
303
  ---
604
304
 
605
-
606
-
607
305
  それより`_main = new MainWindow();`のほうが気になります。
608
-
609
306
  **今**表示している`MainWindow`とは別の`MainWindow`を作ってしまっています。
610
-
611
307
  これでは無駄に何度もファイルを読むことになります。

1

クリーンしたら同じになってたw

2021/11/26 00:13

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -70,7 +70,19 @@
70
70
 
71
71
  // プロパティにして取得のたびにシャッフルした配列を返す
72
72
 
73
+ public string[] Choices
74
+
75
+ {
76
+
77
+ get
78
+
79
+ {
80
+
73
- public string[] Choices => _Choices.OrderBy(_ => Guid.NewGuid()).ToArray();
81
+ return _Choices.OrderBy(_ => Guid.NewGuid()).ToArray();
82
+
83
+ }
84
+
85
+ }
74
86
 
75
87
  // ↑↓同じ意味
76
88