繼承練習0328


有夠難搞,BUG一堆

內容大概是在螢幕上顯示一些變數,並且用LinkedList串起來,每過一秒變數減1,歸零時刪除。

可以用鍵盤做一些操作。

目前問題是若只剩下一個變數,把他刪除會出事,可能直接強制結束之類的。

C++
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
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;

#define Map_Width 50
#define Map_Height 30

class Node{
private:
int val;
int x;
int y;
clock_t StartTime;
clock_t CurrentTime;
public:
Node *next;
void AddVal(){
if(val < 9) ++val;
}
void ReduceVal(){
--val;
}
int Die(){
if(val <= 0) return true;
return false;
}
void Reset(){
gotoxy(x, y);
cout << " ";
}
Node(){
val = 9;
x = rand() % Map_Width;
y = rand() % Map_Height;
StartTime = clock();
CurrentTime = clock();
show();
}
~Node(){
gotoxy(x, y);
cout << " ";
}
void gotoxy(int xpos, int ypos){
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
}
void show(){
UpDateTime();
gotoxy(x, y);
cout << val;
}
void UpDateTime(){
CurrentTime = clock();
if((double)(CurrentTime-StartTime)/CLOCKS_PER_SEC > 1){
StartTime = clock();
--val;
}
}
};

class LinkedList{
private:
Node *p;
public:
void reset(){
p->Reset();
p = NULL;
}
Node* GetP(){
return p;
}
LinkedList(){
p = NULL;
}
void Insert(Node *newNode){
if(p == NULL){
p = newNode;
p->next = newNode;
}
else {
newNode->next = p->next;
p->next = newNode;
p = newNode;
}
}
void Grow(){
if(p != NULL){
Node *buffer = p->next;
do {
buffer->AddVal();
buffer = buffer->next;
}while(p->next != buffer);
}
}
void Down(){
if(p != NULL){
Node *buffer = p->next;
do {
buffer->ReduceVal();
buffer = buffer->next;
}while(p->next != buffer);
}
}
};

class UI{
private:
char key;
public:
void Action(LinkedList *list){
switch (GetKey())
{
case 'A':
case 'a':
{
Node *newNode = new Node;
list->Insert(newNode);
break;
}
case '+':
list->Grow();
break;
case '-':
list->Down();
break;
default:
break;
}

UpDate(list);
}
int GetKey(){
if(_kbhit()){
key = _getch();
return key;
}
return 0;
}
void UpDate(LinkedList *list){
if(list->GetP() != NULL){
Node *p = list->GetP();
do {
p->show();
if(p->next->Die()){
Node *buffer = p->next;
if(buffer == p){
list->reset();
break;
}
p->next = p->next->next;
delete buffer;
}
p = p->next;
}while(p != list->GetP());
}
}
};

void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

int main(){
HideCursor();
srand(time(NULL));
LinkedList *list = new LinkedList;
UI *ui = new UI;

while(true){
ui->Action(list);
}

return 0;
}