-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamePage.xaml.cpp
149 lines (105 loc) · 3.66 KB
/
GamePage.xaml.cpp
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
//
// GamePage.xaml.cpp
// Implementation of the GamePage class
//
#include "pch.h"
#include "MainPage.xaml.h"
#include "GamePage.xaml.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace ThatNumbersGame;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
/* constructor */
GamePage::GamePage() {
InitializeComponent();
// set up timer, delay = 100ms
TimeSpan timeout;
timeout.Duration = 100 * 1000 * 10;
oDisplayTimer->Interval = timeout;
oDisplayTimer->Tick += ref new EventHandler<Object^>(this, &GamePage::timerTick);
}
/* onClick event for next button */
void ThatNumbersGame::GamePage::btnNext_onClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) {
advanceState();
}
/* timer tick */
void ThatNumbersGame::GamePage::timerTick(Object^ sender, Object^ e) {
oDisplayTimer->Stop();
lblStatus->Text = "Enter the number you saw";
txtAnswer->Text = "";
btnNext->Visibility = Windows::UI::Xaml::Visibility::Visible;
txtAnswer->Visibility = Windows::UI::Xaml::Visibility::Visible;
lblNumber->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
txtAnswer->Focus(Windows::UI::Xaml::FocusState::Programmatic);
// change state
eCurrentGameState = EGameState::ANSWERING;
}
void ThatNumbersGame::GamePage::txtAnswer_keyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e) {
if (e->Key == Windows::System::VirtualKey::Enter) {
advanceState();
}
}
void ThatNumbersGame::GamePage::advanceState() {
switch (eCurrentGameState) {
case EGameState::WAITING:
// generate number
srand(time(NULL));
{
long long number = std::rand() % ((long long)std::pow(10, iNumDigits)) + (std::pow(10, iNumDigits - 1) - 1);
sLastNumber = number.ToString();
lblNumber->Text = number.ToString();
}
// update ui
btnNext->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
lblNumber->Visibility = Windows::UI::Xaml::Visibility::Visible;
// start timer
oDisplayTimer->Start();
// change state
eCurrentGameState = EGameState::DISPLAYING;
break;
case EGameState::DISPLAYING:
// shouldn't happen; button is invisible
break;
case EGameState::ANSWERING: {
// check user's answer
bool userCorrect = (txtAnswer->Text->Equals(sLastNumber));
if (userCorrect) {
// update ui
lblStatus->Text = "Correct! Next: " + ++iNumDigits + " digits...";
txtAnswer->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
// change state
eCurrentGameState = EGameState::WAITING;
}
else {
// update ui
Windows::UI::Xaml::Documents::Run^ line1 = ref new Windows::UI::Xaml::Documents::Run;
line1->Text = "Aw no :(";
Windows::UI::Xaml::Documents::Run^ line2 = ref new Windows::UI::Xaml::Documents::Run;
line2->Text = "Final score: " + (iNumDigits - 1) + " digits.";
lblStatus->Inlines->Clear();
lblStatus->Inlines->Append(line1);
lblStatus->Inlines->Append(ref new Windows::UI::Xaml::Documents::LineBreak);
lblStatus->Inlines->Append(line2);
txtAnswer->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
iNumDigits = 1;
// change state
eCurrentGameState = EGameState::LOST;
}
}
break;
case LOST:
Frame->Navigate(ThatNumbersGame::MainPage::typeid);
break;
}
}