Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <iomanip>
- #include <Windows.h>
- #include <ctime>
- #include <cstdlib>
- #define BLACK 0
- #define BLUE 1
- #define GREEN 2
- #define CYAN 3
- #define RED 4
- #define MAGENTA 5
- #define BROWN 6
- #define LIGHTGRAY 7
- #define DARKGRAY 8
- #define LIGHT BLUE 9
- #define LIGHT GREEN 10
- #define LIGHT CYAN 11
- #define LIGHTRED 12
- #define LIGHT MAGENTA 13
- #define YELLOW 14
- #define WHITE 15
- using namespace std;
- const int numerodebarcos = 5;
- vector<string> colors = { "preto", "azul", "verde", "ciano", "vermelho", "magenta", "castanho", "cinzento_claro", "cinzento_escuro", "azul_claro", "verde_claro", "ciano_claro", "vermelho_claro", "magenta_claro", "amarelo", "branco" };
- vector<vector <int> > usedColors;
- void gotoxy(int x, int y)
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- void setcolor(unsigned int color, unsigned int background_color)
- {
- HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
- if (background_color == BLACK)
- SetConsoleTextAttribute(hCon, color);
- else
- SetConsoleTextAttribute(hCon, color | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
- }
- void setcolor(unsigned int color)
- {
- HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(hcon, color);
- }
- struct navio
- {
- int tamanho, numero;
- string nome, cor;
- char codigo;
- };
- void LimparEcrã()
- {
- COORD coordScreen = { 0, 0 }; // upper left corner
- DWORD cCharsWritten;
- DWORD dwConSize;
- HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- GetConsoleScreenBufferInfo(hCon, &csbi);
- dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
- // fill with spaces
- FillConsoleOutputCharacter(hCon, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
- GetConsoleScreenBufferInfo(hCon, &csbi);
- FillConsoleOutputAttribute(hCon, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
- // cursor to upper left corner
- SetConsoleCursorPosition(hCon, coordScreen);
- }
- void EscreverTabuleiro(const vector <vector <char>> &tabuleiro)
- {
- cout << " ";
- for (int i = 0; i < tabuleiro.at(0).size(); i++)
- {
- cout << (char)('a' + i) << " ";
- }
- cout << endl;
- for (int i = 0; i < tabuleiro.size(); i++)
- {
- setcolor(LIGHTGRAY);
- cout << (char)('A' + i);
- for (int j = 0; j < tabuleiro.at(0).size(); j++)
- {
- if (tabuleiro.at(i).at(j) == '.')
- setcolor(BLUE, LIGHTGRAY);
- else
- {
- for (int in = 0; in < usedColors.size(); in++)
- {
- if (int(tabuleiro.at(i).at(j)) == usedColors.at(in).at(0));
- {
- setcolor(usedColors.at(in).at(1));
- break;
- }
- }
- }
- cout << " " << tabuleiro.at(i).at(j);
- }
- cout << endl;
- }
- cout << endl << endl;
- setcolor(LIGHTGRAY);
- }
- void Buscarnavio(string nomedoficheiro, vector<navio>&frota)
- {
- ifstream ficheiro;
- string primeiralinha;
- int i = 0;
- ficheiro.open(nomedoficheiro);
- getline(ficheiro, primeiralinha);
- while (!ficheiro.eof())
- {
- frota.resize(i + 1);
- ficheiro >> frota[i].numero;
- ficheiro.ignore(1000, '-');
- ficheiro.ignore(1000, ' ');
- getline(ficheiro, frota[i].nome, ' ');
- ficheiro.ignore(1000, '-');
- ficheiro.ignore(1000, ' ');
- ficheiro >> frota[i].tamanho;
- ficheiro.ignore(1000, '-');
- ficheiro.ignore(1000, ' ');
- ficheiro >> frota[i].codigo;
- ficheiro.ignore(1000, '-');
- ficheiro.ignore(1000, ' ');
- getline(ficheiro, frota[i].cor);
- i++;
- }
- }
- bool PossivelEscreverTab(string coordenadas, int comprimentonave, int linhas, int colunas, vector <vector <char>> &tabuleiro)
- {
- if (toupper(coordenadas.at(2)) != 'H' && toupper(coordenadas.at(2)) != 'V') // se nao estiver escrito vertical ou horizontal
- return false;
- if ((int)coordenadas.at(0) < ('A') || (int)coordenadas.at(0) > (linhas + 'A')) //passa do J
- return false;
- else if ((int)coordenadas.at(1) < ('a') || (int)coordenadas.at(1) > (colunas + 'a')) //passa do j
- return false;
- if (coordenadas.at(2) == 'h' || coordenadas.at(2) == 'H')
- {
- if (coordenadas.at(1) + (comprimentonave - 1) < 'a' || coordenadas.at(1) + (comprimentonave - 1) > colunas + 'a' - 1) //sai fora do tabuleiro? h
- return false;
- }
- if (coordenadas.at(2) == 'v' || coordenadas.at(2) == 'V')
- {
- if (coordenadas.at(0) + (comprimentonave - 1) < 'A' || coordenadas.at(0) + (comprimentonave - 1) > linhas + 'A' - 1) //Sai fora do tabuleiro? v
- return false;
- }
- if (coordenadas.at(2) == 'h' || coordenadas.at(2) == 'H') // verificar se ha pontos em alguma coordenada do futuro navio horizontal
- {
- for (int i = 0; i < comprimentonave; i++)
- if (tabuleiro.at(coordenadas.at(0) - 'A').at(coordenadas.at(1) - 'a' + i) != '.')
- return false;
- }
- if (coordenadas.at(2) == 'v' || coordenadas.at(2) == 'V')
- {
- for (int i = 0; i < comprimentonave; i++) //verificar se ha pontos em alguma coordenada do futuro navio vertical
- if (tabuleiro.at(coordenadas.at(0) - 'A' + i).at(coordenadas.at(1) - 'a') != '.')
- return false;
- }
- return true;
- }
- void AtualizaTabuleiro(string coordenadas, vector <vector <char>> &tabuleiro, int comprimentonave, char codigo)
- {
- //Se for Horizontal
- if (coordenadas.at(2) == 'h' || coordenadas.at(2) == 'H')
- {
- for (int i = 0; i < comprimentonave; i++)
- {
- tabuleiro.at(coordenadas.at(0) - 'A').at(coordenadas.at(1) - 'a' + i) = codigo;
- }
- }
- //Se for Vertical
- if (coordenadas.at(2) == 'v' || coordenadas.at(2) == 'V')
- {
- for (int i = 0; i < comprimentonave; i++)
- {
- (tabuleiro.at(coordenadas.at(0) + i - 'A').at(coordenadas.at(1) - 'a') = codigo);
- }
- }
- }
- void JogarManual(vector<navio>&frota, int linhas, int colunas, vector <vector <char>> &tabuleiro)
- {
- string coordenadas;
- unsigned int i;
- for (i = 0; i < frota.size(); i++)
- {
- navio n = frota[i];
- while (n.numero>0)
- {
- cout << n.codigo << " - " << n.nome << ". " << "Tamanho = " << n.tamanho << ". Falta(m) " << n.numero << endl;;
- cout << "LINHA(A.." << (char)('A' + linhas - 1) << ") COLUNAS(a.." << (char)('a' + colunas - 1) << ")" << " ORIENTACAO (H V)?";
- cin >> coordenadas;
- if (PossivelEscreverTab(coordenadas, n.tamanho, linhas, colunas, tabuleiro))
- {
- AtualizaTabuleiro(coordenadas, tabuleiro, n.tamanho, n.codigo);
- n.numero--;
- EscreverTabuleiro(tabuleiro);
- }
- else
- {
- setcolor(4);
- cout << "Coordenadas incompativeis com o tabuleiro!" << endl;;
- setcolor(7);
- }
- }
- }
- LimparEcrã();
- EscreverTabuleiro(tabuleiro);
- }
- int randomBetween(int n1, int n2)
- {
- return n1 + rand() % (n2 - n1 + 1);
- }
- void JogarAutomatico(int numerodebarcos, vector <vector <char>> &tabuleiro, int linhas, int colunas, vector<navio>&frota)
- {
- srand((unsigned)time(NULL));
- int x, y, c;
- string coordenadas = " ";
- for (int i = 0; i < numerodebarcos; i++)
- {
- navio n = frota[i];
- for (int j = 0; j < n.numero; j++)
- {
- do
- {
- x = (char)randomBetween('A', 'A' + linhas - 1);
- y = (char)randomBetween('a', 'a' + colunas - 1);
- if (randomBetween(0, 1) == 0)
- c = 'H';
- else c = 'V';
- coordenadas.at(0) = x;
- coordenadas.at(1) = y;
- coordenadas.at(2) = c;
- } while (!PossivelEscreverTab(coordenadas, n.tamanho, linhas, colunas, tabuleiro));
- AtualizaTabuleiro(coordenadas, tabuleiro, n.tamanho, n.codigo);
- }
- }
- LimparEcrã();
- EscreverTabuleiro(tabuleiro);
- }
- int main()
- {
- int i;
- string linha;
- string modo;
- ifstream f;
- vector<navio>frota;
- f.open("TABULEIRO.txt");
- getline(f, linha);
- stringstream ss(linha);
- string s;
- int linhas, colunas;
- ss >> s >> linhas >> s >> colunas;
- vector<vector <char>> tabuleiro(linhas, vector<char>(colunas, '.'));
- EscreverTabuleiro(tabuleiro);
- while (!f.eof()){
- getline(f, linha);
- }
- f.close();
- Buscarnavio("TABULEIRO.txt", frota);
- setcolor(6);
- cout << "Bem-vindo!" << endl;
- setcolor(7);
- bool valido = false;
- do {
- cout << endl << "Modo Manual ou Auto? ";
- cin >> modo;
- int i = 0;
- while (i < modo.length())
- {
- modo.at(i) = toupper(modo.at(i));
- i++;
- }
- if (modo == "AUTO")
- {
- JogarAutomatico(numerodebarcos, tabuleiro, linhas, colunas, frota);
- valido = true;
- }
- else if (modo == "MANUAL")
- {
- JogarManual(frota, linhas, colunas, tabuleiro);
- valido = true;
- }
- else{ cout << "Modo de jogo invalido"; }
- } while (!valido);
- system("PAUSE");
- navio n = frota[i];
- for (int i = 0; i < n.cor.length(); i++) //(por cor a minusculas),colocar a cor escrita pelo utilizador em minusculas e comparar com o vetor cores para associar ao simbolo o numero da cor correspondente
- {
- n.cor.at(i) = tolower(n.cor.at(i));
- }
- for (int i = 0; i < colors.size(); i++) //preencher o vetor cores com o codigo ascci do simbolo
- { //e o codigo da cor que utilizador escreveu
- if (colors.at(i) == n.cor)
- {
- usedColors.push_back(vector <int>(2));
- usedColors.at(usedColors.size() - 1).at(0) = (int)n.codigo;
- usedColors.at(usedColors.size() - 1).at(1) = i;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement