SAPER [C++] - problem potrzebna pomoc

0

Witam, uczę się programowania w codebloxie na informatyce w liceum i muszę napisać gre saper na zaliczenie. Niestety mam problem ponieważ program nie wypisuje w tabeli/planszy wybranych pól 'o' oraz po trafieniu w bombę nie wyświetla się komunikat o przegranej. Bardzo proszę o pomoc```cpp

#include <iostream>
#include <time.h>
#include <cstdlib>

const int SZEROKOSC_TABLICY = 10;
const int WYSOKOSC_TABLICY = 10;

using namespace std;

int main()
{
    char tab[30] = { 'X' }, znaki[SZEROKOSC_TABLICY][WYSOKOSC_TABLICY]= { ' ' };
    int h = 0, g = 0, x=1, wspolrzedna_y=0, wspolrzedna_x=0,  proby, bomby=30;
    srand(time(NULL));

    cout << "                    " << "SAPER" << endl;

    cout << endl;
    cout << "+------------------------------------------+" << endl;
    cout << "| x| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|";
    cout << endl;

    for (int k = 0; k <10; k++)
    {
        do
        {
            h = rand()%10;
            g = rand()%10;
        }
        while (znaki[h][g]=='X');
        znaki[h][g] = 'X';
    }

    for (int i = 0; i < 10; i++)
    {
        if(x < 10)
        cout << "| " << x;
        else
            cout << "|" << x;
            x = x + 1;
        for (int j = 0; j < 10; j++)
        {
          cout << "|" << "   ";
        }
        cout << "|";
        cout << endl;
    }
    cout << "+------------------------------------------+" << endl;
    cout << endl;
    x = 1;
        cout << "  Podaj wspolrzedne: ";
    do
    {
        cin >> wspolrzedna_x;
        cin >> wspolrzedna_y;
        wspolrzedna_y = wspolrzedna_y - 1;
        wspolrzedna_x = wspolrzedna_x - 1;


    if(znaki[wspolrzedna_x][wspolrzedna_y]=='X')
    {

        cout << " Wdepnales na mine! Przegrales!" << endl;
    }

    else
    {
        znaki[wspolrzedna_x][wspolrzedna_y]=tab['o'];
        cout << " Przezyles, probuj dalej" << endl;
    }
    cout << "+------------------------------------------+" << endl;
    cout << "| x| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|";
    cout << endl;

    for (int i = 0; i < 10; i++)
    {
        if(x < 10)
        cout << "| " << x;
        else
            cout << "|" << x;
            x = x + 1;
        for (int j = 0; j < 10; j++)
        {
            if(znaki[i][j]==' ')
            cout << "| " << znaki[i][j] << " ";
            else
            cout << "|" << "   ";
        }
        cout << "|";
        cout << endl;
    }
    cout << "+------------------------------------------+" << endl;
    cout << endl;
    x = 1;

        cout << endl;
        cout << " Podaj nastepne wspolrzedne" << endl;
    }

        while(znaki[wspolrzedna_x][wspolrzedna_y]!='X');

        system("cls" );

        if(znaki[wspolrzedna_x][wspolrzedna_y]=='o')
            cout << endl << " Przejales wszystkie bezpieczne pola. Wygrales!" << endl;

    return 0;
}
1

Problem w wypisywaniu leży najpewniej w:

znaki[wspolrzedna_x][wspolrzedna_y]=tab['o'];

nie wiem czym jest tab[], ponieważ nigdzie w kodzie nie mogłem tego znaleźć, ale jeśli chcesz do tablicy wpisać znak, zrób to po prostu:

znaki[wspolrzedna_x][wspolrzedna_y] = 'o';

edit: znalazłem tab[] jednak z tego co widzę nie jest ona nigdzie używana i zawiera zawsze tylko znaki 'X', więc nie ma w niej znaku o, który chciałbyś wrzucić to znaki[].
Nie możesz też wołać znaku z tab[] korzystając z 'o'. Tak działa słownik, nie tablica.


Bardzo mylące i utrudniające formatowanie kodu.
Na przykład:
while(znaki[wspolrzedna_x][wspolrzedna_y]!='X'); w Twoim kodzie tyczy się pętli do{}, jednak sformatowane w ten sposób każe myśleć, że chcesz czekać w nieskończoność aż znak w konkretnych współrzędnych nie będzie 'X'.
Normalnie pomogłyby wcięcia, jednak tutaj ma się wrażenie, że są one całkowicie losowe i nie powinno się na nich polegać.

0

Wywalić całość, zrobić porządnie.
Zacznij od sensownej definicji modelu, na szybko:

#include <iostream>
#include <cstdint>
using namespace std;

const uint8_t maxTableWidth=39,maxTableHeight=25;
const uint8_t maxTableWidthBounded=1+maxTableWidth+1;
const uint8_t maxTableHeightBounded=1+maxTableHeight+1;

struct Field { bool mine,visible; };
struct Map
{
    Field tb[maxTableHeightBounded][maxTableWidthBounded];
    uint8_t usedTableHeight,usedTableWidth;
};

void generateRandomMap(Map &map,uint8_t usedTableHeight,uint8_t usedTableWidth) 
{ 
	map.usedTableHeight=usedTableHeight;
	map.usedTableWidth=usedTableWidth;
	for(uint8_t y=0;y<maxTableHeightBounded;++y)
	{
		for(uint8_t x=0;x<maxTableWidthBounded;++x)
		{
			map.tb[y][x].mine=false;
			map.tb[y][x].visible=false;
		}
	}
	// na razie testowa mapa
	map.tb[3][5].mine=map.tb[5][3].mine=true;
}

uint8_t showCount(Map &map,uint8_t posy,uint8_t posx)
{
	if(map.tb[posy][posx].mine) return 9; // Mine at posy,posx - return imposible value
	uint8_t count=0;
	for(uint8_t y=0;y<3;++y)
	{
		for(uint8_t x=0;x<3;++x)
		{
			count+=map.tb[posy+y-1][posx+x-1].mine;
		}
	}
	return count;
}

char showSign(Map &map,uint8_t posy,uint8_t posx)
{
	static const char signs[]="012345678*";
	return map.tb[posy][posx].visible?signs[showCount(map,posy,posx)]:'?';
}

char coordValue(uint8_t pos)
{
	return (char)('A'+pos-1);
}

void showMap(Map &map)
{
	for(uint8_t y=0;y<=map.usedTableHeight;++y,cout<<endl)
	{
		for(uint8_t x=0;x<=map.usedTableWidth;++x)
		{
			cout<<' '<<(y?(x?showSign(map,y,x):coordValue(y)):(x?coordValue(x):' '));
		}
	}
}

void floodVisible(Map &map,uint8_t posy,uint8_t posx)
{
	if((0>=posy)||(posy>map.usedTableHeight)) return;
	if((0>=posx)||(posx>map.usedTableWidth)) return;
	if(map.tb[posy][posx].visible) return;
	map.tb[posy][posx].visible=true;
	if(showCount(map,posy,posx)) return;
	for(uint8_t y=0;y<3;++y)
	{
		for(uint8_t x=0;x<3;++x)
		{
			if(x!=y) floodVisible(map,posy+y-1,posx+x-1);
		}
	}
}

bool markVisible(Map &map,uint8_t posy,uint8_t posx)
{
	floodVisible(map,posy,posx);	
	showMap(map);
	return !map.tb[posy][posx].mine;
}

int main()
{
	Map map;
	generateRandomMap(map,10,10);
	showMap(map);	
	if(!markVisible(map,4,4)) cout<<"boom";	cout<<endl;
	if(!markVisible(map,4,3)) cout<<"boom";	cout<<endl;
	if(!markVisible(map,3,4)) cout<<"boom";	cout<<endl;
	if(!markVisible(map,4,5)) cout<<"boom";	cout<<endl;
	if(!markVisible(map,5,4)) cout<<"boom";	cout<<endl;	
	if(!markVisible(map,3,3)) cout<<"boom";	cout<<endl;
	if(!markVisible(map,5,5)) cout<<"boom";	cout<<endl;
	if(!markVisible(map,3,5)) cout<<"boom";	cout<<endl;
	return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1