Новичок
Джуниор
Регистрация: 12.01.2014
Сообщений: 1
|
Нерабочий код змейки (C)
Всем доброго времени суток!
Пожалуйста, помогите понять, почему код не работает, и что с ним нужно сделать, чтобы он заработал.
Заранее спасибо
Код:
# include <iostream>
# include <windows.h>
# include <time.h>
# include <conio.h>
using namespace std;
enum keys
{
Top = 72,
Down = 80,
Left = 75,
Right = 77
};
void direction(int move_x, int move_y)
{
char symbol = _getch();
if (symbol == Top)
{
if(move_x != 1 || move_y != 0)
{
move_x = -1; move_y = 0;
}
}
if (symbol == Right)
{
if(move_x != 0 || move_y != -1)
{
move_x = 0; move_y = 1;
}
}
if (symbol == Down)
{
if(move_x != -1 || move_y != 0)
{
move_x = 1; move_y = 0;
}
}
if (symbol == Left)
{
if(move_x != 0 || move_y != 1)
{
move_x = 0; move_y = -1;
}
}
}
void border(int a[][1000], int N, int M)
{
system("cls");
for (int i = 0; i <= N + 1; ++i)
{
for (int j = 0; j <= M + 1; ++j)
{
cout << (i == 0 || j == 0 || i == N + 1 || j == M + 1 ? '#' : a[i][j]) << (j <= M ? "" : "\n");
}
}
}
void clear (int size, int a[][1000], int x[], int y[])
{
for (int i = 1; i <= size; ++i)
{
a[x[i]][y[i]] = ' ';
}
}
void look (int size, int a[][1000], int x[], int y[])
{
for (int i = 1; i <= size; ++i)
{
if (i == 1)
{
a[x[i]][y[i]] = ':';
}
else
{
a[x[i]][y[i]] = '@';
}
}
}
int game_over (int size, int a[][1000], int x[], int y[], int N, int M)
{
for (int i = 2; i <= size; ++i)
{
if (x[1] == x[i] && y[1] == y[i])
{
return true;
}
else if (x[1] > N)
{
return true;
}
else if (x[1] < 1)
{
return true;
}
else if (y[1] > M)
{
return true;
}
else if (y[1] < 1)
{
return true;
}
else
{
return false;
}
}
if (game_over (size, a, x, y, N, M) == true)
{
cout << "You Lose!" << endl;
system("pause");
}
}
void next (int size, int a[][1000], int x[], int y[], int move_x, int move_y, int food_x, int food_y)
{
look (size, a, x, y);
clear (size, a, x, y);
int N = 22;
int M = 40;
for (int i = size; i >= 2; --i)
{
x[i] = x[i - 1];
y[i] = y[i - 1];
}
x[1] += move_x;
y[1] += move_y;
if(x[1] == food_x && y[1] == food_y)
{
size++;
food_x = -1;
food_y = -1;
}
}
int food_check(int food_x, int food_y)
{
if(food_x == -1 && food_y == -1)
{
return false;
}
else
{
return true;
}
}
void place_food (int food_x, int food_y, int a[][1000], int x[], int y[], int N, int M)
{
srand(time(0));
for (int i = 1; i <= 9; ++i)
{
int x = rand();
int y = rand();
if (x < 0)
{
x *= -1;
}
if (y < 0)
{
y *= -1;
}
x %= (N + 1);
y %= (M + 1);
if (x == 0)
{
++x;
}
if (y == 0)
{
++y;
}
food_x = x;
food_y = y;
a[x][y] = '*';
}
}
void main ()
{
int size = 3;
const int ar_size = 500;
int x[ar_size];
int y[ar_size];
int a[1000][1000];
int move_x = 0;
int move_y = 1;
int food_x = -1;
int food_y = -1;
x[1] = 1;
y[1] = 2;
x[2] = 1;
y[2] = 1;
int N = 22;
int M = 40;
while (true)
{
look (size, a, x, y);
if (_kbhit() == true)
{
direction (move_x, move_y);
}
next (size, a, x, y, move_x, move_y, food_x, food_y);
if(food_check(food_x, food_y) == false)
{
place_food(food_x, food_y, a, x, y, N, M);
}
border(a, N, M);
Sleep (500);
}
}
|