- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <conio.h>
const int x_size(20), y_size(10); int x_pos(x_size/2+1); int y_pos(y_size/2+1);
enum border_types{lineNL, single, singleNL};
enum directions{UpLeft=1, UpRight, DownLeft, DownRight}dir;
void draw_border(enum border_types borders) {
do{
if(borders == single || borders == singleNL) break;
for(int i=0; i<x_size+1; i++)
putchar('#');
}while(false);
putchar('#');
if(borders == singleNL || borders == lineNL) std::cout << '\n';}
void display_update() {
system("cls");
draw_border(lineNL);
for(int i=1; i<=y_size; i++)
{
draw_border(single);
for(int j=1; j<=x_size; j++)
{
if(j == x_pos && i == y_pos)
{
putchar('x');
continue;
}
putchar(32);
}
draw_border(singleNL);;
}
draw_border(lineNL);
std::cout << "X: " << x_pos << "\tY: " << y_pos;}
void logic() {
switch(x_pos)
{
case 1:
if(dir == UpLeft) dir = UpRight;
if(dir == DownLeft) dir = DownRight;
break;
case x_size:
if(dir == UpRight) dir = UpLeft;
if(dir == DownRight) dir = DownLeft;
}
switch(y_pos)
{
case 1:
if(dir == UpLeft) dir = DownLeft;
if(dir == UpRight) dir = DownRight;
break;
case y_size:
if(dir == DownLeft) dir = UpLeft;
if(dir == DownRight) dir = UpRight;
}}
void move() {
switch(dir)
{
case UpLeft:
x_pos--;
y_pos--;
break;
case UpRight:
x_pos++;
y_pos--;
break;
case DownLeft:
x_pos--;
y_pos++;
break;
case DownRight:
x_pos++;
y_pos++;
}}
int main() {
srand(time(0));
rand();
switch(rand()%4+1)
{
case UpLeft:
dir = UpLeft;
break;
case UpRight:
dir = UpRight;
break;
case DownLeft:
dir = DownLeft;
break;
case DownRight:
dir = DownRight;
}
while(!kbhit())
{
display_update();
logic();
move();
}
return 0;}
Сорян, пришлось уплотнить фигурные скобки, чтобы код уместился в 100 строк.