Čaute
Mam nasledovný problém mam za domácu úlohu naprogramovať hru loďky keďže som v c začiatočník zriešil som nasledovný kód potreboval by som poradiť prečo tento kód vypisuje iba jednu loďku ktorá je ukázaná hviezdičkou. Aké zmeny by sme mi odporučili.
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
int X[5], Y[5], x, y, x2 = 5, y2 = 5, s;
void gotoxy(int x1, int y1)
{
COORD cord;
cord.X = x1;
cord.Y = y1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), cord);
}
int random_number(int min_num, int max_num)
{
int result = 0, low_num = 0, hi_num = 0;
if (min_num<max_num)
{
low_num = min_num;
hi_num = max_num + 1;
}
else{
low_num = max_num + 1;
hi_num = min_num;
}
srand((unsigned int)time(NULL));
result = (rand() % (hi_num - low_num)) + low_num;
return result;
}
int PoziciaVHracomPoli(int x, int y)
{
switch (x)
{
case 2: x2 = 1; break;
case 4: x2 = 2; break;
case 6: x2 = 3; break;
case 8: x2 = 4; break;
case 10: x2 = 5; break;
case 12: x2 = 6; break;
case 14: x2 = 7; break;
case 16: x2 = 8; break;
case 18: x2 = 9; break;
case 20: x2 = 10; break;
}
switch (y)
{
case 1: y2 = 1; break;
case 2: y2 = 2; break;
case 3: y2 = 3; break;
case 4: y2 = 4; break;
case 5: y2 = 5; break;
case 6: y2 = 6; break;
case 7: y2 = 7; break;
case 8: y2 = 8; break;
case 9: y2 = 9; break;
case 10: y2 = 10; break;
}
return x2, y2;
}
void pozicia()
{
gotoxy(0, 11);
printf("Tvoja pozicia je : %d , %d", x2, y2);
gotoxy(x, y);
}
void vyhodnocovanie()
{
}
int _tmain(int argc, _TCHAR* argv[])
{
int p = 0;
printf("***Vitajte v hre lodky***");
printf("\n***Hru zacnete stlacenim tlacidla ENTER****");
getchar();
system("CLS");
gotoxy(1, 0);
for (int i = 1; i <= 10; i++)
{
printf(" ");
printf("%d", i);
}
for (int i = 1; i <= 10; i++)
{
printf("\n");
printf("%d", i);
}
pozicia();
int i = 0;
while (i<5){
X[i] = random_number(2, 20);
Y[i] = random_number(2, 10);
if ((X[i] % 2) == 0)
{
gotoxy(X[i], Y[i]);
printf("*");
}
else if (i == 0)
{
i = 0;
}
else
{
i--;
}
i++;
}
x = 6, y = 5;
unsigned char a;
gotoxy(x, y);
do{
a = _getch();
if (a == 224)a = _getch();
switch (a)
{
case 72: y -= 1; if (y <= 0)y = 1; gotoxy(x, y); pozicia(); break; //hore
case 75: x -= 2; if (x <= 1)x = 2; gotoxy(x, y); pozicia(); break; //dolava
case 77: x += 2; if (x >= 21)x = 20; gotoxy(x, y); pozicia(); break; //doprava
case 80: y += 1; if (y >= 11)y = 10; gotoxy(x, y); pozicia(); break; //dole
case 'g':
{
for (s = 0; s <= 4; s++)
{
if (x == X[s] && y == Y[s])
{
p++;
printf("X");
}
}
if (p == 5)
{
gotoxy(25, 12);
printf("Vyhral si na pokus : %d", pocetPokusov());
}
break;
default: gotoxy(x, y);
}
}
PoziciaVHracomPoli(x, y);
} while (1);
getchar();
getchar();
return 0;
}