Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //10x10 polje; ladje: 5x1, 4x1, 3x1, 2x2, 1x2
- // g++ raspbian linux
- //* --> ladja; # --> neodkrito; O --> prazno
- //skupaj 18 ladjic!
- #include <iostream>
- #include <iomanip>
- #include <ctime>
- #include <cstdlib>
- #define size 10
- using namespace std;
- void vnos_ai(char tab[][size]);
- void vnos_usr(char tab[][size]);
- void vpis_ai(char tab[][size], char fake[][size], int &cnt, bool &prev, int &x, int &y, bool &left, bool &right, bool &top, bool &bot);
- void vpis_usr(char tab[][size], char fake[][size], int &cnt);
- //void izpis(char ai[][size]);
- //void potopljeno(char ai[][size], char polje[][size], int ai_cnt, int usr_cnt);
- //jbool zmaga (int ai_cnt, int usr_cnt);
- int main()
- {
- srand(time(NULL));
- bool prev = false, bot, top, left, right;
- int x, y;
- int ai_cnt = 0, usr_cnt = 0;
- char fake[size][size];
- char polje[size][size];
- char fake_a[size][size];
- for (int i = 0; i < size; i++) // zapolne polja z #
- for (int j = 0; j < size; j++)
- {
- polje[i][j] = '#';
- fake_a[i][j] = '#';
- }
- char ai[size][size];
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- {
- ai[i][j] = '#';
- fake[i][j] = '#';
- }
- vnos_ai(ai);
- vnos_usr(polje);
- bool tst = true;
- do {
- if (tst)
- {
- vpis_ai(polje, fake_a, ai_cnt, prev, x, y, left, right, top, bot);
- tst = false;
- }
- else
- {
- vpis_usr(ai, fake, usr_cnt);
- tst = true;
- }
- } while (ai_cnt < 18 && usr_cnt < 18);
- if (ai_cnt == 18)
- cout << endl << "zmaga ai";
- else
- cout << endl << "zmaga uporabnik";
- cout << endl << endl;
- return 0;
- }
- void vpis_usr(char tab[][size], char fake[][size], int &cnt)
- {
- for (int i = -1; i < size; i++)
- {
- if (i > -1) cout << setw(2) << i;
- for (int j = -1; j < size; j++)
- {
- if (i > -1 && j > -1) cout << setw(2) << fake[i][j];
- else if (i == -1 && j == -1) cout << setw(2) << " ";
- else if (i == -1) cout << setw(2) << j;
- }
- cout << endl;
- }
- unsigned int x, y;
- cout << "Vpisi x koordinato(0-9): ";
- do {
- cin >> x;
- } while (x > 9);
- cout << "Vpisi y koordinato(0-9): ";
- do {
- cin >> y;
- } while (y > 9);
- if (tab[y][x] == '*')
- {
- cout << "zadel si ladjo!\n";
- fake[y][x] = '*';
- cnt++;
- }
- else
- {
- cout << "zgresil si!\n";
- fake[y][x] = 'O';
- }
- }
- void vpis_ai(char tab[][size], char fake[][size], int &cnt, bool &prev, int &x, int &y, bool &left, bool &right, bool &top, bool &bot)
- {
- int i = 0, j = 0;
- if (prev)
- { //fake[y][x] prejsni zadetek
- if (!top)
- {
- for (i = 0; y + i >= 0; i--)
- {
- if (fake[y + i][x] == '#')
- {
- fake[y + i][x] = '*';
- if (tab[y + i][x] == '#')
- top = true;
- else
- top = false;
- break;
- }
- }
- }
- else if (!bot)
- {
- for (i = 0; y + i < 10; i++)
- {
- if (fake[y + i][x] == '#')
- {
- fake[y + i][x] = '*';
- if (tab[y + i][x] == '#')
- bot = true;
- else bot = false;
- break;
- }
- }
- }
- else if (!right)
- {
- for (j = 0; x + j < 10; j++)
- {
- if (fake[y][x + j] == '#')
- {
- fake[y][x + j] = '*';
- if (tab[y][x + j] == '#')
- right = true;
- else right = false;
- break;
- }
- }
- }
- else if (!left)
- {
- for (j = 0; x + j >= 0; j--)
- {
- if (fake[y][x + j] == '#')
- {
- fake[y][x + j] = '*';
- if (tab[y][x + j] == '#')
- left = true;
- else left = false;
- break;
- }
- }
- }
- else prev = false;
- }
- if (prev == false)
- {
- do{
- x = rand() % 10;
- y = rand() % 10;
- left = false; top = false; right = false; bot = false;
- } while (fake[y][x] == '*');
- }
- cout << "ai je ciljal na (" << x + j << ", " << y + i << ")" << endl;
- fake[y][x] = '*';
- if (tab[y + i][x + j] == '*')
- {
- cnt++;
- prev = true;
- cout << "zadel je" << endl;
- }
- else {
- cout << "zgresil je" << endl;
- }
- cout << "pogled AI:" << endl;
- for (int h = 0; h < size; h++)
- {
- for (int f = 0; f < size; f++)
- cout << setw(2) << fake[h][f];
- cout << endl;
- }
- cout << endl << endl;
- }
- void vnos_ai(char tab[][size])
- {
- int x, y;
- bool rot = rand() % 2;
- if (rot) // horizontalno 5
- {
- x = rand() % 6;
- y = rand() % 10;
- for (int i = 0; i < 5; i++)
- tab[y][i + x] = '*';
- }
- else // vertikalno
- {
- x = rand() % 10;
- y = rand() % 6;
- for (int i = 0; i < 5; i++)
- tab[i + y][x] = '*';
- }
- bool check = true;
- rot = rand() % 2;
- if (rot) // horizontalno 4
- {
- do {
- check = true;
- x = rand() % 7;
- y = rand() % 10;
- for (int i = 0; i < 4; i++)
- if (tab[y][x + i] == '*')
- check = false;
- } while (!check);
- for (int i = 0; i < 4; i++)
- tab[y][x + i] = '*';
- }
- else // vertikalno
- {
- do{
- check = true;
- x = rand() % 10;
- y = rand() % 7;
- for (int i = 0; i < 4; i++)
- if (tab[y + i][x] == '*')
- check = false;
- } while (!check);
- for (int i = 0; i < 4; i++)
- tab[y + i][x] = '*';
- }
- rot = rand() % 2;
- if (rot) // horizontalno 3
- {
- do {
- check = true;
- x = rand() % 8;
- y = rand() % 10;
- for (int i = 0; i < 3; i++)
- if (tab[y][x + i] == '*')
- check = false;
- } while (!check);
- for (int i = 0; i < 3; i++)
- tab[y][x + i] = '*';
- }
- else //vertikalno
- {
- do {
- check = true;
- x = rand() % 10;
- y = rand() % 8;
- for (int i = 0; i < 3; i++)
- if (tab[y + i][x] == '*')
- check = false;
- } while (!check);
- for (int i = 0; i < 3; i++)
- tab[y + i][x] = '*';
- }
- for (int g = 0; g < 2; g++)
- {
- rot = rand() % 2;
- if (rot) // horizontalno 2
- {
- do {
- check = true;
- x = rand() % 9;
- y = rand() % 10;
- if (tab[y][x] == '*' || tab[y][x + 1] == '*')
- check = false;
- } while (!check);
- tab[y][x] = '*'; tab[y][x + 1] = '*';
- }
- else //vertikalno
- {
- do {
- check = true;
- x = rand() % 10;
- y = rand() % 9;
- if (tab[y][x] == '*' || tab[y + 1][x] == '*')
- check = false;
- } while (!check);
- tab[y][x] = '*'; tab[y + 1][x] = '*';
- }
- }
- for (int i = 0; i < 2; i++)
- {
- do {
- check = true;
- x = rand() % 10;
- y = rand() % 10;
- if (tab[x][y] == '*')
- check = false;
- } while (!check);
- tab[x][y] = '*';
- }
- for (int i = 0; i < size; i++){
- for (int j = 0; j < size; j++)
- cout << setw(2) << tab[i][j];
- cout << endl;
- }
- }
- void vnos_usr(char tab[][size])
- {
- bool rot, check;
- unsigned int x, y;
- cout << "Vnesi orientacijo ladje 5 (0 vertikalno, 1 horizontalno): ";
- cin >> rot;
- if (rot)// horizontalno
- {
- cout << "Vnesi vrstico (0-9): ";
- do {
- cin >> y;
- } while (y > 9);
- cout << "Vnesi stolpec(0-5): ";
- do {
- cin >> x;
- } while (x > 5);
- for (int i = 0; i < 5; i++)
- tab[y][x + i] = '*';
- }
- else //vertikalno
- {
- cout << "Vnesi vrstico (0-5): ";
- do {
- cin >> y;
- } while (y > 5);
- cout << "Vnesi stolpec (0-9): ";
- do {
- cin >> x;
- } while (x > 9);
- for (int i = 0; i < 5; i++)
- tab[y + i][x] = '*';
- }
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- cout << setw(2) << tab[i][j];
- cout << endl;
- }
- do {
- check = true;
- cout << "Vnesi orientacijo ladje 4 (0 vertikalno, 1 horizontalno): ";
- cin >> rot;
- if (rot) // hor
- {
- cout << "Vnesi vrstico (0-9): ";
- do{
- cin >> y;
- } while (y > 9);
- cout << "Vnesi stolpec (0-6): ";
- do {
- cin >> x;
- } while (x > 6);
- }
- else //ver
- {
- cout << "Vnesi vrstico (0-6): ";
- do {
- cin >> y;
- } while (y > 6);
- cout << "Vnesi stolpec (0-9): ";
- do{
- cin >> x;
- } while (x > 9);
- }
- for (int i = 0; i < 4; i++)
- {
- if (rot)
- {
- if (tab[y][x + i] == '*')
- check = false;
- }
- else
- {
- if (tab[y + i][x] == '*')
- check = false;
- }
- }
- if (!check)
- cout << "Ladja se prekriva, vpisi ponovno" << endl;
- } while (!check);
- for (int i = 0; i < 4; i++)
- {
- if (rot)
- tab[y][x + i] = '*';
- else
- tab[y + i][x] = '*';
- }
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- cout << setw(2) << tab[i][j];
- cout << endl;
- }
- do{
- check = true;
- cout << "Vnesi orientacijo ladje 3 (0 vertikalno, 1 horizontalno): ";
- cin >> rot;
- if (rot) // horizontalno
- {
- cout << "Vnesi vrstico (0-9): ";
- do{
- cin >> y;
- } while (y > 9);
- cout << "Vnesi stoplec (0-7): ";
- do {
- cin >> x;
- } while (x > 7);
- }
- else {
- cout << "Vnesi vrstico (0-7): ";
- do {
- cin >> y;
- } while (y > 7);
- cout << "Vnesi stolpec (0-9): ";
- do {
- cin >> x;
- } while (x > 9);
- }
- for (int i = 0; i < 3; i++)
- {
- if (rot)
- {
- if (tab[y][x + i] == '*') check = false;
- }
- else
- if (tab[y + i][x] == '*') check = false;
- }
- if (!check)
- cout << "Ladja se prekriva, vpisi ponovno." << endl;
- } while (!check);
- for (int i = 0; i < 3; i++)
- {
- if (rot)
- tab[y][x + i] = '*';
- else
- tab[y + i][x] = '*';
- }
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- cout << setw(2) << tab[i][j];
- cout << endl;
- }
- for (int huehue = 0; huehue < 2; huehue++)
- {
- do {
- check = true;
- cout << "Vnesi orientacijo ladje 2 (0 vertikalno, 1 hotizontalno): ";
- cin >> rot;
- if (rot) // hor
- {
- cout << "Vnesi vrstico (0-9): ";
- do {
- cin >> y;
- } while (y > 9);
- cout << "Vnesi stolpec (0-8): ";
- do {
- cin >> x;
- } while (x > 8);
- }
- else
- {
- cout << "Vnesi vrstico (0-8): ";
- do {
- cin >> y;
- } while (y > 8);
- cout << "Vnesi stolpec (0-9): ";
- do {
- cin >> x;
- } while (x > 9);
- }
- if (rot)
- {
- if (tab[y][x] == '*' || tab[y][x + 1] == '*')
- check = false;
- }
- else
- if (tab[y][x] == '*' || tab[y + 1][x] == '*')
- check = false;
- if (!check)
- cout << "Ladja se prekriva, vpisi ponovno." << endl;
- } while (!check);
- if (rot)
- {
- tab[y][x] = '*'; tab[y][x + 1] = '*';
- }
- else
- {
- tab[y][x] = '*'; tab[y + 1][x] = '*';
- }
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- cout << setw(2) << tab[i][j];
- cout << endl;
- }
- }
- for (int i = 0; i < 2; i++)
- {
- do{
- check = true;
- cout << "Vnesi vrstico (0-9): ";
- do {
- cin >> y;
- } while (y > 9);
- cout << "Vnesi stolpec (0-9): ";
- do {
- cin >> x;
- } while (x > 9);
- if (tab[y][x] == '*')
- {
- cout << "Ladja se prekriva, vpisi ponovno." << endl;
- check = false;
- }
- } while (!check);
- tab[y][x] = '*';
- for (int i = 0; i < size; i++)
- {
- for (int j = 0; j < size; j++)
- cout << setw(2) << tab[i][j];
- cout << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement