1+ #ifndef ENDLESS_H
2+ #define ENDLESS_H
3+
4+ namespace Endless
5+ {
6+ int ONJUMP = 0 ;
7+ int GAMEDIED = 0 ;
8+
9+ void windowsize_protect ()
10+ {
11+ while (ENDLESS_OUTPUT_STOP == 0 )
12+ {
13+ windowsize ();
14+ msleep (OUTPUT_TIME);
15+ }
16+ }
17+
18+ inline void stop (int type = 0 )
19+ {
20+ GAMEDIED = 1 , OUTPUT_STOP = 1 ;
21+ if (type == 0 )
22+ {
23+ syscls ();
24+ // MOVETO (0, 0);
25+ printf (" :( You died.\n " );
26+ printf (" \n Press any key to exit.\n " );
27+ }
28+ }
29+
30+ inline bool pos_legal (int x, int y)
31+ {
32+ return x > 0 && x <= 5 ;
33+ }
34+
35+ void pos_legal_protect ()
36+ {
37+ while (GAMEDIED == 0 )
38+ {
39+ if (! pos_legal (x, y)) stop ();
40+ msleep (OUTPUT_TIME);
41+ }
42+ }
43+
44+ inline void move_g ()
45+ {
46+ while (endless_field[x + 1 ][y] == 0 )
47+ {
48+ endless_field[x][y] = 0 ;
49+ endless_field[x + 1 ][y] = 1 ;
50+ x ++;
51+ msleep (MOVEUP_TIME);
52+ }
53+ }
54+
55+ void move_g_protect ()
56+ {
57+ while (OUTPUT_STOP == 0 )
58+ {
59+ if (ONJUMP == 0 ) move_g ();
60+ msleep (OUTPUT_TIME);
61+ }
62+ }
63+
64+ void update_pos ()
65+ {
66+ strcpy (BOTTOM_RIGHT_INFO, " (" );
67+ strcat (BOTTOM_RIGHT_INFO, inttochar (x).c_str ());
68+ strcat (BOTTOM_RIGHT_INFO, " , " );
69+ strcat (BOTTOM_RIGHT_INFO, inttochar (y).c_str ());
70+ strcat (BOTTOM_RIGHT_INFO, " ) H" );
71+ strcat (BOTTOM_RIGHT_INFO, inttochar (WINDOW_X).c_str ());
72+ strcat (BOTTOM_RIGHT_INFO, " W" );
73+ strcat (BOTTOM_RIGHT_INFO, inttochar (WINDOW_Y).c_str ());
74+ }
75+
76+ void allinone_protect ()
77+ {
78+ while (GAMEDIED == 0 )
79+ {
80+ windowsize ();
81+
82+ if (! pos_legal (x, y)) stop ();
83+
84+ update_pos ();
85+
86+ msleep (OUTPUT_TIME * 10 );
87+ }
88+ }
89+
90+ void jump ()
91+ {
92+ if (ONJUMP) return ;
93+ ONJUMP = 1 ;
94+ for (int i = 1 ; i < 3 ; i ++)
95+ {
96+ if (endless_field[x - 1 ][y] == 0 )
97+ {
98+ endless_field[x][y] = 0 ;
99+ endless_field[x - 1 ][y] = 1 ;
100+ x --;
101+ msleep (MOVEUP_TIME);
102+ }
103+ else
104+ {
105+ stop ();
106+ }
107+ }
108+ ONJUMP = 0 ;
109+ }
110+
111+ void tokenize (std::string const &str, const char * delim,
112+ std::vector<std::string> &out)
113+ {
114+ char *token = strtok (const_cast <char *>(str.c_str ()), delim);
115+ while (token != nullptr )
116+ {
117+ out.push_back (std::string (token));
118+ token = strtok (nullptr , delim);
119+ }
120+ }
121+
122+ void command ()
123+ {
124+ while (true )
125+ {
126+ int read = keyboard ();
127+ if (read == 27 ) return ;
128+ if (read == 127 && strlen (BOTTOM_LEFT_INFO) > 1 )
129+ {
130+ BOTTOM_LEFT_INFO[strlen (BOTTOM_LEFT_INFO) - 1 ] = ' \0 ' ;
131+ }
132+ else if ((read >= ' a' && read <= ' z' ) || (read >= ' 0' && read <= ' 9' ) || read == ' ' )
133+ {
134+ char tmp[2 ] = {char (read)};
135+ strcat (BOTTOM_LEFT_INFO, tmp);
136+ }
137+ else if (read == 10 )
138+ {
139+ string str (BOTTOM_LEFT_INFO);
140+ vector <string> arg;
141+ tokenize (str, " " , arg);
142+ if (arg[0 ] == " /pos" )
143+ {
144+ if (arg.size () == 3 )
145+ {
146+ int ix, iy;
147+ sscanf (arg[1 ].c_str (), " %d" , &ix);
148+ sscanf (arg[2 ].c_str (), " %d" , &iy);
149+ if (field[ix][iy].issafe () && pos_legal (ix, iy))
150+ {
151+ field[x][y].user = 0 , field[ix][iy].user = 1 ;
152+ x = ix, y = iy;
153+ return ;
154+ }
155+ }
156+ strcpy (BOTTOM_LEFT_INFO, " Error" );
157+ msleep (OUTPUT_TIME * 1000 );
158+ return ;
159+ }
160+ }
161+ }
162+ }
163+
164+ int ctrl ()
165+ {
166+ x = sx, y = sy;
167+ ONJUMP = 0 , GAMEDIED = 0 ;
168+ strcpy (OUTPUT_RIGHT_INFO, " Endless" );
169+
170+ thread allinone_thread (allinone_protect);
171+ thread g_thread (move_g_protect);
172+ allinone_thread.detach ();
173+ g_thread.detach ();
174+
175+ int read;
176+ while (true )
177+ {
178+ strcpy (BOTTOM_LEFT_INFO, " Ready" );
179+ read = 0 ;
180+ while ((read < 1 || read > 4 ) && read != 9 && read != 32 && read != ' /' )
181+ {
182+ read = check (keyboard ());
183+ if (GAMEDIED == 1 )
184+ {
185+ read = 114514 ;
186+ break ;
187+ }
188+ }
189+ if (read == ' /' )
190+ {
191+ strcpy (BOTTOM_LEFT_INFO, " /" );
192+ command ();
193+ }
194+ strcpy (BOTTOM_LEFT_INFO, " Working..." );
195+ // if (read == 3 || read == 4)
196+ // {
197+ // //left
198+ // move_left_right (dy[read]);
199+ // }
200+ if (read == 1 || read == 32 )
201+ {
202+ // jump
203+ thread jump_thread (jump);
204+ jump_thread.detach ();
205+ }
206+ if (read == 9 )
207+ {
208+ strcpy (BOTTOM_LEFT_INFO, " Exit" );
209+ stop (1 );
210+ return -1 ;
211+ }
212+
213+ if (GAMEDIED == 1 || read == 114514 )
214+ {
215+ return 0 ;
216+ }
217+ }
218+ }
219+ }
220+
221+ #endif
0 commit comments