-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarel.hpp
1780 lines (1576 loc) · 40.7 KB
/
karel.hpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
karel - Karel robot definitions.
@author PUC-Minas - ICEI
@version 0.1 - 2020
Inspired by the original work (reference):
Miroslav Binas <mirek.binas at tuke dot sk>
(c) 2010, 2016
https://git.kpi.fei.tuke.sk/kpi/karel-the-robot
*/
// import dependencies
// ------------------------------------ OS compatibility section
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
// ------------------------------------ global definitions
#ifndef _KAREL_HPP_
#define _KAREL_HPP_
// global definitions
#define NORTH 0
#define EAST 1
#define SOUTH 2
#define WEST 3
#define HWALL '_'
#define VWALL '|'
#define BEEPER '1'
#define BOX '#'
#define LIFO 0
#define FIFO 1
#define OPEN 0
#define CLOSED 1
// global constants
#define WIDTH 10
#define HEIGHT 10
#define DEPTH 20
#define MAX_AVENUES WIDTH
#define MAX_STREETS HEIGHT
#define MAX_COMMANDS 300
// global variables (application context)
#if defined (unix) || defined (_unix) || defined (_unix)
#define OS_DELAY 1000
#else
#define OS_DELAY 1
#endif
long baseDelay = 100 ;
long stepDelay = 1 ;
bool has_debug = false;
bool has_Text = false;
// ------------------------------------ global variables
#define NO_ERROR 0
char error_txt [ 40 ];
char debug_txt [ 40 ];
char message [ 20 ];
char msg_txt [ 20 ];
char msg_board [ 22 ][20];
// ------------------------------------ prototypes
void show_Text ( const char* message );
// ------------------------------------ global I/O control definitions
/**
clrscr - Clear screen.
OBS: OS dependent !!!
*/
void clrscr ( )
{
#if defined (unix) || defined (_unix) || defined (_unix)
// system ( "clear" ); // for Linux
// system ( "reset" ); // for Linux
printf ( "\033c" );
#else
system ( "cls" ); // for Windows
#endif
} // end clrscr ( )
/**
clreol - Clear till the end of line.
OBS: DISABLED
*/
void clreol ( )
{
} // end clreol ( )
/**
gotoxy - Move focus to screen coordinates.
@param x - x-axis coordinate
@param y - y-axis coordinate
OBS: DISABLED
*/
void gotoxy ( int x, int y )
{
} // end gotoxy ( )
/**
delay - Sleep for a given number of ticks.
@param ticks - number of ticks to rest
OBS.: (ANSI-C compatible)
*/
void delay( clock_t ticks )
{
clock_t now,
stop = clock( ) + ticks;
do
{ now = clock( ); }
while ( now <= stop );
} // end delay
/**
setSpeed - Set new step delay base.
@param newStepDelay - new base for time control
*/
void set_Speed ( int newStep )
{
if ( 1 <= newStep && newStep < 1000 )
{
stepDelay = OS_DELAY * baseDelay * newStep;
}
} // end setSpeed ( )
// ------------------------------------ global error treatment
/**
error_code - Keep error code (global).
*/
int error_code = NO_ERROR;
/**
set_Error - Set a new error code.
@param code to be set
*/
void set_Error ( int code )
{
error_code = code;
} // end set_Error ( )
/**
get_Error - Copy current error code.
@return current error code
*/
int get_Error ( )
{
return ( error_code );
} // end get_Error ( )
/**
has_Error - Test if there's any error code set.
@return true, if there is an error code set;
false, otherwise
*/
int has_Error ( )
{
return ( error_code != NO_ERROR );
} // end has_Error ( )
/**
show_Error - Show error message.
@param message corresponding to an error code set
*/
void show_Error ( const char* message )
{
error_code = 1;
show_Text ( message );
} // end show_Error ( )
/**
fatal_Error - Show error message and finishes program.
@param message corresponding to an fatal error code set
*/
void fatal_Error ( char* message )
{
printf ( "\n%s\n", message );
exit ( error_code );
} // end fatal_Error ( )
// ------------------------------------ global type definitions
/**
world - World object definition.
*/
class World
{
public: // world public container
int height;
int width;
int depth;
char name [20]; // identification
// object container
char data [ HEIGHT ][ WIDTH ][ DEPTH ];
World ( ); // constructor
~ World ( ); // destructor
int avenues ( );
int streets ( );
bool areValid ( int avenue, int street );
char get ( int avenue, int street );
int search ( int avenue, int street, char object );
int has_slot ( int avenue, int street );
int has_HWALL ( int avenue, int street );
int has_VWALL ( int avenue, int street );
int has_BEEPER ( int avenue, int street );
int has_BEEPERS ( int avenue, int street );
int has_BOX ( int avenue, int street );
int has_ROBOT ( int avenue, int street, char symbol );
void show ( );
void reset ( );
void create ( const char* name );
void save ( const char* fileName );
void read ( const char* fileName );
void set ( int avenue, int street, char object );
void remove ( int avenue, int street, int object );
void close ( );
};
/**
ref_world - Reference to a World object.
*/
typedef World* ref_World; // reference
// create a global reference to an object
ref_World world = new World( );
/**
box - Box object definition.
*/
class Box
{
public: // box public container
int status; // open (']') or closed ('[')
int avenue; // x-axis position
int street; // y-axis position
int direction; // LIFO or FIFO
int beepers; // in box
char symbol; // badge
char name [20]; // identification
void create ( int avenue , int street ,
int direction, int beepers,
char* name , int status );
int nbeepers ( );
int isEmpty ( );
int checkStatus ( );
void open ( );
void close ( );
int pullBeeper ( );
void pushBeeper ( );
};
/**
ref_box - Reference to a Box object.
*/
typedef Box* ref_Box;
/**
robot - Robot object definition.
*/
class Robot
{
public: // robot public container
int status; // is it running (1), or not (0)
int avenue; // x-axis position
int street; // y-axis position
int direction; //
int beepers; // in bag
char symbol; // badge
char name [20]; // identification
Robot( ); // constructor
~Robot( ); // destructor
bool checkStatus ( );
void turnOn ( );
void turnOff ( );
bool frontIsClear ( );
void create ( int avenue, int street,
int direction, int beepers,
const char* name );
void set ( int x, int y );
void move ( );
void turnLeft ( );
void pickBeeper ( );
void putBeeper ( );
void pickBeeperFromBox ( ref_Box box );
void putBeeperIntoBox ( ref_Box box );
bool beepersInBag ( );
int nbeepers ( );
void faceWest ( );
void faceEast ( );
void faceSouth ( );
void faceNorth ( );
void face ( int direction );
bool facing ( int direction );
bool facingWest ( );
bool facingEast ( );
bool facingSouth ( );
bool facingNorth ( );
bool rightIsClear ( );
bool leftIsClear ( );
bool nextToARobot ( );
bool nextToABeeper( );
bool nextToABox ( );
int xAvenue ( );
int yStreet ( );
bool areYouHere ( int avenue, int street );
void debugOff ( );
void debugOn ( );
void debug ( );
};
/**
ref_robot - Reference to a Robot structure.
*/
typedef Robot* ref_Robot;
// ------------------------------------ global (public) context
// CAUTION: Use with care.
// MUST be changed only if objects changes.
/**
world_now - Current reference to a World structure.
*/
ref_World world_now = nullptr;
/**
robot_now - Current reference to a Robot structure.
*/
ref_Robot robot_now = nullptr;
/**
box_now - Current reference to a Box structure.
*/
ref_Box box_now = nullptr;
// ------------------------------------ input/output treatment
/**
show_Text - Show text message.
@param message to be shown.
*/
void show_Text ( const char* message )
{
char copy [80]; // copy of original message
if ( message != nullptr )
{
strcpy ( copy, message );
copy [19] = '\0'; // set message length limit=20
strcpy ( msg_txt, copy ); // prepare message for exhibition
has_Text = true;
world->show ( );
}
} // end show_Text ( )
/**
show_Board - Show basic robot board.
@param line for the message.
@param message to be shown.
*/
void show_Board ( int line, char* message )
{
char copy [80]; // copy of original message
if ( message != nullptr &&
(1 <= line && line <= 20) )
{
strcpy ( copy, message );
copy [19] = '\0'; // set message length limit=20
strcpy ( msg_board[line+1], copy ); // prepare message for exhibition
has_Text = true;
world->show ( );
}
} // end show_Board ( )
/**
show_Screen - Show 22x80 screen mirror.
@param screen - symbol matrix to be shown
*/
void show_Screen ( char screen [ ][80] )
{
int x = 0;
for ( x = 21; x >= 0; x-- )
{
printf ( "%s\n", screen [ x ] );
msg_board [ x ][0] = '\0';
}
// getchar( );
delay ( stepDelay );
if ( has_debug || has_Error( ) || has_Text )
{
fflush (stdin);
getchar ( );
error_code = NO_ERROR;
error_txt[0] = '\0';
msg_txt [0] = '\0';
message [0] = '\0';
has_Text = false;
}
} // end show_Screen ( )
// ------------------------------------ world definitions
World::World ()
{
}
World::~World ()
{
}
/**
avenues - Retrieve current world number of avenues.
@return number of avenues, if there is any;
zero, otherwise
*/
int World::avenues ( )
{
return ( width );
} // end avenues ( )
/**
streets - Retrieve current world number of streets.
@return number of streets, if there is any;
zero, otherwise
*/
int World::streets ( )
{
return ( height );
} // end streets ( )
/**
areValid - Check if avenue and street are valid.
@return true, if avenue and street are valid;
false, otherwise
@param avenue - slot (x)
@param street - slot (y)
*/
bool World::areValid ( int avenue, int street )
{
return ( (1 <= avenue && avenue <= avenues( ) ) &&
(1 <= street && street <= streets( ) ) );
} // end areValid ( )
/**
get_world - Retrieve top information of a world slot.
@param avenue - slot (x)
@param street - slot (y)
*/
char World::get ( int avenue, int street )
{
char c = ' ';
if ( areValid ( avenue, street ) )
{
c = data [ street-1 ][ avenue-1 ][ 0 ];
}
return ( c );
} // end get_World ( )
/**
search_world - Search a world slot for an object.
@param avenue - slot (x)
@param street - slot (y)
@param object - to be searched
*/
int World::search ( int avenue, int street, char object )
{
int result = -1;
int z = -1;
char c;
if ( areValid ( avenue, street ) )
{
do
{
z = z + 1;
c = data [ street-1 ][ avenue-1 ][ z ];
}
while ( z < depth-1 && c != object );
if ( c == object )
result = z;
}
return ( result );
} // end search_World ( )
/**
has_slot - Retrieve a free space at a world slot.
@return free space index, if there is one;
(-1), otherwise
@param avenue - slot (x)
@param street - slot (y)
*/
int World::has_slot ( int avenue, int street )
{
return ( search ( avenue, street, '.' ) );
} // end has_slot ( )
/**
has_HWALL - Retrieve a horizontal wall index at a world slot.
@return wall index, if there is one;
zero, otherwise
@param avenue - slot (x)
@param street - slot (y)
*/
int World::has_HWALL ( int avenue, int street )
{
return ( search ( avenue, street, HWALL ) );
} // end has_HWALL ( )
/**
has_VWALL - Retrieve a vertical wall index at a world slot.
@return wall index, if there is one;
zero, otherwise
@param avenue - slot (x)
@param street - slot (y)
*/
int World::has_VWALL ( int avenue, int street )
{
return ( search ( avenue, street, VWALL ) );
} // end has_VWALL ( )
/**
has_BEEPER - Retrive a beeper index at a world slot.
@return beeper index, if there is one;
zero, otherwise
@param avenue - slot (x)
@param street - slot (y)
*/
int World::has_BEEPER ( int avenue, int street )
{
return ( search ( avenue, street, BEEPER ) );
} // end has_BEEPER ( )
/**
has_BEEPERS - Retrive number of beepers at a world slot.
@return number of beepers
@param avenue - slot (x)
@param street - slot (y)
*/
int World::has_BEEPERS ( int avenue, int street )
{
int result = 0;
int z = -1;
char c;
if ( areValid ( avenue, street ) )
{
do
{
z = z + 1;
c = data [ street-1 ][ avenue-1 ][ z ];
if ( c == BEEPER )
{
result = result + 1;
}
}
while ( z < depth-1 );
}
return ( result );
} // end has_BEEPER ( )
/**
has_Box - Retrive a box index at a world slot.
@return box index, if there is one;
zero, otherwise
@param avenue - slot (x)
@param street - slot (y)
*/
int World::has_BOX ( int avenue, int street )
{
int result = 0;
int z = -1;
char c;
if ( areValid ( avenue, street ) )
{
do
{
z = z + 1;
c = data [ street-1 ][ avenue-1 ][ z ];
if ( 'a' <= c && c <= 'z' )
{
result = c;
}
}
while ( z < depth-1 );
}
return ( result );
} // end has_BOX ( )
/**
has_ROBOT - Retrive if there is a specified robot at a world slot.
@return robot index, if there is one;
zero, otherwise
@param avenue - slot (x)
@param street - slot (y)
@param symbol - robot badge symbol
*/
int World::has_ROBOT ( int avenue, int street, char symbol )
{
return ( search ( avenue, street, symbol ) );
} // end has_ROBOT ( )
/**
show - Show current visible items in the world slots.
*/
void World::show ( )
{
int avenue, street;
int x, y, k;
char c;
char screen [23][80];
char board [23][80];
strcpy ( board [21], "ST.+-------------------------------------------+ " );
strcpy ( board [20], "10 | . . . . . . . . . . | " );
strcpy ( board [19], " | | " );
strcpy ( board [18], " 9 | . . . . . . . . . . | " );
strcpy ( board [17], " | | " );
strcpy ( board [16], " 8 | . . . . . . . . . . | " );
strcpy ( board [15], " | | " );
strcpy ( board [14], " 7 | . . . . . . . . . . | " );
strcpy ( board [13], " | | " );
strcpy ( board [12], " 6 | . . . . . . . . . . | " );
strcpy ( board [11], " | | " );
strcpy ( board [10], " 5 | . . . . . . . . . . | " );
strcpy ( board [ 9], " | | " );
strcpy ( board [ 8], " 4 | . . . . . . . . . . | " );
strcpy ( board [ 7], " | | " );
strcpy ( board [ 6], " 3 | . . . . . . . . . . | " );
strcpy ( board [ 5], " | | " );
strcpy ( board [ 4], " 2 | . . . . . . . . . . | " );
strcpy ( board [ 3], " | | " );
strcpy ( board [ 2], " 1 | . . . . . . . . . . | " );
strcpy ( board [ 1], " +-------------------------------------------+ " );
strcpy ( board [ 0], "AVE. 1 2 3 4 5 6 7 8 9 10 " );
clrscr ( );
strcpy ( screen [21], board [21] );
strcpy ( screen [ 1], board [ 1] );
strcpy ( screen [ 0], board [ 0] );
for ( street = 1; street <= streets( ); street++ )
{
x = 2*street;
strcpy ( screen [ x ], board [ x ] );
strcpy ( screen [ x+1 ], board [ x+1 ] );
for ( avenue = 1; avenue <= avenues( ); avenue++ )
{
y = 4*avenue;
k = has_VWALL ( avenue, street );
if ( k >= 0 && avenue < width )
{
screen [ x ][ y+5 ] = '|';
if ( 1 < street && street < height )
{
screen [ x-1 ][ y+5 ] = '|';
}
}
k = has_HWALL ( avenue, street );
if ( k >= 0 && street < height )
{
screen [ x+1 ][ y+2 ] = '_';
screen [ x+1 ][ y+3 ] = '_';
screen [ x+1 ][ y+4 ] = '_';
}
k = has_BEEPERS ( avenue, street );
if ( k > 0 )
{
screen [ x ][ y+2 ] = '(';
if ( 0 <= k && k <= 9 )
{ screen [ x ][ y+3 ] = (char) ((int) '0' + k); }
else
{ screen [ x ][ y+3 ] = '+'; }
screen [ x ][ y+4 ] = ')';
}
k = has_BOX ( avenue, street );
if ( k > 0 )
{
screen [ x ][ y+2 ] = '[';
screen [ x ][ y+3 ] = k ;
screen [ x ][ y+4 ] = ']';
}
c = get ( avenue, street );
if ( 'A' <= c && c <= 'Z' )
{
screen [ x ][ y+3 ] = c ;
}
}
strcat ( screen [ street ], msg_board [ street ] );
}
// compose bottom lines
strcat ( screen [21], name );
debug_txt [19] = '\0';
strcat ( screen [ 1], debug_txt );
msg_txt [19] = '\0';
strcat ( screen [ 0], msg_txt );
show_Screen ( screen );
} // end World::show ( )
/**
reset - Clean up world slots.
*/
void World::reset ( )
{
int x, y, z;
for ( x=0; x < height; x++ )
for ( y=0; y < width; y++ )
for ( z=0; z < depth; z++ )
{
data [x][y][z] = '.';
}
for ( x = 0; x < 21; x = x+1 )
{
msg_board [ x ][0] = '\0';
}
debug_txt [0] = '\0';
msg_txt [0] = '\0';
} // end World::reset ( )
/**
create - Set default world dimensions.
*/
void World::create ( const char* name )
{
height = HEIGHT;
width = WIDTH ;
depth = DEPTH ;
if ( name != nullptr && strlen(name) > 0 )
{
strcpy ( this->name, name );
}
else
{
strcpy ( this->name, "Karel_Robot_-_v0.1" );
}
reset ( );
} // end World::create ( )
/**
set - Install an object at a world slot.
@param avenue - slot (x)
@param street - slot (y)
@param object - object badge symbol
*/
void World::set ( int avenue, int street, char object )
{
int x, y;
if ( areValid ( avenue, street ) )
{
switch ( object )
{
case BEEPER:
y = has_BOX ( avenue, street );
if ( y > 0 )
{
show_Error ( "ERROR: Use box." );
}
else
{
x = has_slot ( avenue, street );
if ( x < 0 )
{
show_Error ( "ERROR: No slot free." );
}
else
{
data [ street-1 ][ avenue-1 ][ x ] = BEEPER;
}
}
break;
case BOX:
x = has_slot ( avenue, street );
if ( x < 0 )
{
show_Error ( "ERROR: No slot free." );
}
else
{
data [ street-1 ][ avenue-1 ][ x ] = BOX;
}
break;
case HWALL:
x = has_HWALL ( avenue, street );
if ( x >= 0 )
{
show_Error ( "ERROR: HWall here" );
}
else
{
x = has_slot ( avenue, street );
if ( x < 0 )
{
show_Error ( "ERROR: No slot free." );
}
else
{
data [ street-1 ][ avenue-1 ][ x ] = HWALL;
}
}
break;
case VWALL:
x = has_VWALL ( avenue, street );
if ( x >= 0 )
{
show_Error ( "ERROR: VWALL here." );
}
else
{
x = has_slot ( avenue, street );
if ( x < 0 )
{
show_Error ( "ERROR: No slot free." );
}
else
{
data [ street-1 ][ avenue-1 ][ x ] = VWALL;
}
}
break;
default:
if ( ('A' <= object && object <= 'Z') ||
('a' <= object && object <= 'z') )
{
x = has_ROBOT ( avenue, street, object );
if ( x >= 0 )
{
show_Error ( "ERROR: Robot is here." );
}
else
{
x = has_slot ( avenue, street );
if ( x < 0 )
{
show_Error ( "ERROR: No slot free." );
}
else
{
for ( y=depth-1; y>0; y-- )
{
data [ street-1 ][ avenue-1 ][ y ]
= data [ street-1 ][ avenue-1 ][ y-1 ];
}
data [ street-1 ][ avenue-1 ][ 0 ] = object;
}
}
}
}
show ( );
}
} // end World::set ( )
/**
remove - Remove an object at a world slot.
@param avenue - slot (x)
@param street - slot (y)
@param object - to be removed
*/
void World::remove ( int avenue, int street, int object )
{
int x, y;
x = search ( avenue, street, object );
if ( x < 0 )
{
show_Error ( "ERROR: Object does not exist." );
}
else
{
for ( y = x; y < depth-1; y=y+1 )
{
data [ street-1 ][ avenue-1 ][ y ]
= data [ street-1 ][ avenue-1 ][ y+1 ];
}
data [ street-1 ][ avenue-1 ][ depth-1 ] = '.';
}
} // end World::remove ( )
/**
save - Write current world items to a file.
@param fileName - file identification name
*/
void World::save ( const char* fileName )
{
FILE* f_world;
int x, y, z;
char c;
f_world = fopen ( fileName, "wt" );
// save current name
fprintf ( f_world, "%s\n",
name );
// save current boundaries
fprintf ( f_world, "%d %d %d\n",
height, width, depth );
// search for data and save them
for ( x=0; x < height; x=x+1 )
for ( y=0; y < width ; y=y+1 )
{
z=0;
do
{
c = data [x][y][z];
if ( (c == '|' || c == '_') ||
('1' <= c && c <= '9') )
{
fprintf ( f_world, "%d %d %d %c\n", x, y, z, data[x][y][z] );
}
z = z + 1;
}
while ( z < depth && c != '.' );
}
fclose ( f_world );
} // end World::save ( )
/**
read - Read a file to restore world items.
@param fileName - file identification name
*/
void World::read ( const char* fileName )
{
FILE* f_world;
int x, y, z;
char c;
char name[80];
create ( "Karel_Robot_-_v0.1" );
f_world = fopen ( fileName, "rt" );
if ( f_world )
{
fscanf ( f_world, "%s",
name );
fscanf ( f_world, "%d %d %d",
&x, &y, &z );
if ( 0 < x && x <= HEIGHT &&
0 < y && y <= WIDTH &&
0 < z && z <= DEPTH )
{
// set world boundaries
height = x;
width = y;
depth = z;
// set world name
if ( name != nullptr )
{
strcpy ( this->name, name );
}
// read data and fill world slots
fscanf ( f_world, "%d %d %d %c", &x, &y, &z, &c );
while ( ! feof ( f_world ) )
{
if ( 0 <= x && x < height &&
0 <= y && y < width &&
0 <= z && z < depth )