Advertisement
Guest User

Erro!!!!!!!

a guest
Apr 19th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <vector>
  6. #include <iomanip>
  7. #include <Windows.h>
  8. #include <ctime>
  9. #include <cstdlib>
  10.  
  11. #define BLACK 0
  12. #define BLUE 1
  13. #define GREEN 2
  14. #define CYAN 3
  15. #define RED 4
  16. #define MAGENTA 5
  17. #define BROWN 6
  18. #define LIGHTGRAY 7
  19. #define DARKGRAY 8
  20. #define LIGHT BLUE 9
  21. #define LIGHT GREEN 10
  22. #define LIGHT CYAN 11
  23. #define LIGHTRED 12
  24. #define LIGHT MAGENTA 13
  25. #define YELLOW 14
  26. #define WHITE 15
  27.  
  28. using namespace std;
  29. const int numerodebarcos = 5;
  30.  
  31. 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" };
  32.  
  33. vector<vector <int> > usedColors;
  34.  
  35. void gotoxy(int x, int y)
  36. {
  37. COORD coord;
  38. coord.X = x;
  39. coord.Y = y;
  40. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  41. }
  42.  
  43.  
  44. void setcolor(unsigned int color, unsigned int background_color)
  45. {
  46. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  47. if (background_color == BLACK)
  48. SetConsoleTextAttribute(hCon, color);
  49. else
  50. SetConsoleTextAttribute(hCon, color | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
  51. }
  52.  
  53.  
  54. void setcolor(unsigned int color)
  55. {
  56. HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  57. SetConsoleTextAttribute(hcon, color);
  58. }
  59.  
  60. struct navio
  61. {
  62. int tamanho, numero;
  63. string nome, cor;
  64. char codigo;
  65. };
  66.  
  67. void LimparEcrã()
  68. {
  69. COORD coordScreen = { 0, 0 }; // upper left corner
  70. DWORD cCharsWritten;
  71. DWORD dwConSize;
  72. HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
  73. CONSOLE_SCREEN_BUFFER_INFO csbi;
  74. GetConsoleScreenBufferInfo(hCon, &csbi);
  75. dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  76. // fill with spaces
  77. FillConsoleOutputCharacter(hCon, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
  78. GetConsoleScreenBufferInfo(hCon, &csbi);
  79. FillConsoleOutputAttribute(hCon, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
  80. // cursor to upper left corner
  81. SetConsoleCursorPosition(hCon, coordScreen);
  82.  
  83. }
  84. void EscreverTabuleiro(const vector <vector <char>> &tabuleiro)
  85. {
  86. cout << " ";
  87. for (int i = 0; i < tabuleiro.at(0).size(); i++)
  88. {
  89. cout << (char)('a' + i) << " ";
  90. }
  91. cout << endl;
  92.  
  93. for (int i = 0; i < tabuleiro.size(); i++)
  94. {
  95. setcolor(LIGHTGRAY);
  96. cout << (char)('A' + i);
  97. for (int j = 0; j < tabuleiro.at(0).size(); j++)
  98. {
  99. if (tabuleiro.at(i).at(j) == '.')
  100. setcolor(BLUE, LIGHTGRAY);
  101. else
  102. {
  103. for (int in = 0; in < usedColors.size(); in++)
  104. {
  105. if (int(tabuleiro.at(i).at(j)) == usedColors.at(in).at(0));
  106. {
  107. setcolor(usedColors.at(in).at(1));
  108. break;
  109. }
  110. }
  111. }
  112. cout << " " << tabuleiro.at(i).at(j);
  113. }
  114. cout << endl;
  115. }
  116. cout << endl << endl;
  117. setcolor(LIGHTGRAY);
  118. }
  119.  
  120. void Buscarnavio(string nomedoficheiro, vector<navio>&frota)
  121. {
  122. ifstream ficheiro;
  123. string primeiralinha;
  124. int i = 0;
  125. ficheiro.open(nomedoficheiro);
  126. getline(ficheiro, primeiralinha);
  127. while (!ficheiro.eof())
  128. {
  129. frota.resize(i + 1);
  130. ficheiro >> frota[i].numero;
  131. ficheiro.ignore(1000, '-');
  132. ficheiro.ignore(1000, ' ');
  133. getline(ficheiro, frota[i].nome, ' ');
  134. ficheiro.ignore(1000, '-');
  135. ficheiro.ignore(1000, ' ');
  136. ficheiro >> frota[i].tamanho;
  137. ficheiro.ignore(1000, '-');
  138. ficheiro.ignore(1000, ' ');
  139. ficheiro >> frota[i].codigo;
  140. ficheiro.ignore(1000, '-');
  141. ficheiro.ignore(1000, ' ');
  142. getline(ficheiro, frota[i].cor);
  143. i++;
  144. }
  145. }
  146. bool PossivelEscreverTab(string coordenadas, int comprimentonave, int linhas, int colunas, vector <vector <char>> &tabuleiro)
  147. {
  148. if (toupper(coordenadas.at(2)) != 'H' && toupper(coordenadas.at(2)) != 'V') // se nao estiver escrito vertical ou horizontal
  149. return false;
  150.  
  151. if ((int)coordenadas.at(0) < ('A') || (int)coordenadas.at(0) > (linhas + 'A')) //passa do J
  152. return false;
  153. else if ((int)coordenadas.at(1) < ('a') || (int)coordenadas.at(1) > (colunas + 'a')) //passa do j
  154. return false;
  155.  
  156. if (coordenadas.at(2) == 'h' || coordenadas.at(2) == 'H')
  157. {
  158. if (coordenadas.at(1) + (comprimentonave - 1) < 'a' || coordenadas.at(1) + (comprimentonave - 1) > colunas + 'a' - 1) //sai fora do tabuleiro? h
  159. return false;
  160. }
  161.  
  162. if (coordenadas.at(2) == 'v' || coordenadas.at(2) == 'V')
  163. {
  164. if (coordenadas.at(0) + (comprimentonave - 1) < 'A' || coordenadas.at(0) + (comprimentonave - 1) > linhas + 'A' - 1) //Sai fora do tabuleiro? v
  165. return false;
  166. }
  167.  
  168. if (coordenadas.at(2) == 'h' || coordenadas.at(2) == 'H') // verificar se ha pontos em alguma coordenada do futuro navio horizontal
  169. {
  170. for (int i = 0; i < comprimentonave; i++)
  171. if (tabuleiro.at(coordenadas.at(0) - 'A').at(coordenadas.at(1) - 'a' + i) != '.')
  172. return false;
  173. }
  174.  
  175. if (coordenadas.at(2) == 'v' || coordenadas.at(2) == 'V')
  176. {
  177. for (int i = 0; i < comprimentonave; i++) //verificar se ha pontos em alguma coordenada do futuro navio vertical
  178. if (tabuleiro.at(coordenadas.at(0) - 'A' + i).at(coordenadas.at(1) - 'a') != '.')
  179. return false;
  180. }
  181.  
  182. return true;
  183. }
  184.  
  185.  
  186. void AtualizaTabuleiro(string coordenadas, vector <vector <char>> &tabuleiro, int comprimentonave, char codigo)
  187. {
  188. //Se for Horizontal
  189.  
  190. if (coordenadas.at(2) == 'h' || coordenadas.at(2) == 'H')
  191. {
  192. for (int i = 0; i < comprimentonave; i++)
  193. {
  194. tabuleiro.at(coordenadas.at(0) - 'A').at(coordenadas.at(1) - 'a' + i) = codigo;
  195. }
  196.  
  197. }
  198.  
  199. //Se for Vertical
  200.  
  201. if (coordenadas.at(2) == 'v' || coordenadas.at(2) == 'V')
  202. {
  203. for (int i = 0; i < comprimentonave; i++)
  204. {
  205. (tabuleiro.at(coordenadas.at(0) + i - 'A').at(coordenadas.at(1) - 'a') = codigo);
  206. }
  207. }
  208.  
  209. }
  210.  
  211. void JogarManual(vector<navio>&frota, int linhas, int colunas, vector <vector <char>> &tabuleiro)
  212. {
  213. string coordenadas;
  214. unsigned int i;
  215. for (i = 0; i < frota.size(); i++)
  216. {
  217. navio n = frota[i];
  218. while (n.numero>0)
  219. {
  220. cout << n.codigo << " - " << n.nome << ". " << "Tamanho = " << n.tamanho << ". Falta(m) " << n.numero << endl;;
  221. cout << "LINHA(A.." << (char)('A' + linhas - 1) << ") COLUNAS(a.." << (char)('a' + colunas - 1) << ")" << " ORIENTACAO (H V)?";
  222. cin >> coordenadas;
  223. if (PossivelEscreverTab(coordenadas, n.tamanho, linhas, colunas, tabuleiro))
  224. {
  225. AtualizaTabuleiro(coordenadas, tabuleiro, n.tamanho, n.codigo);
  226. n.numero--;
  227. EscreverTabuleiro(tabuleiro);
  228.  
  229. }
  230. else
  231. {
  232. setcolor(4);
  233. cout << "Coordenadas incompativeis com o tabuleiro!" << endl;;
  234. setcolor(7);
  235. }
  236. }
  237.  
  238. }
  239. LimparEcrã();
  240. EscreverTabuleiro(tabuleiro);
  241.  
  242. }
  243.  
  244.  
  245. int randomBetween(int n1, int n2)
  246. {
  247. return n1 + rand() % (n2 - n1 + 1);
  248. }
  249.  
  250.  
  251. void JogarAutomatico(int numerodebarcos, vector <vector <char>> &tabuleiro, int linhas, int colunas, vector<navio>&frota)
  252. {
  253. srand((unsigned)time(NULL));
  254.  
  255. int x, y, c;
  256. string coordenadas = " ";
  257.  
  258. for (int i = 0; i < numerodebarcos; i++)
  259. {
  260. navio n = frota[i];
  261. for (int j = 0; j < n.numero; j++)
  262. {
  263. do
  264. {
  265. x = (char)randomBetween('A', 'A' + linhas - 1);
  266. y = (char)randomBetween('a', 'a' + colunas - 1);
  267. if (randomBetween(0, 1) == 0)
  268. c = 'H';
  269. else c = 'V';
  270. coordenadas.at(0) = x;
  271. coordenadas.at(1) = y;
  272. coordenadas.at(2) = c;
  273.  
  274. } while (!PossivelEscreverTab(coordenadas, n.tamanho, linhas, colunas, tabuleiro));
  275. AtualizaTabuleiro(coordenadas, tabuleiro, n.tamanho, n.codigo);
  276. }
  277. }
  278.  
  279.  
  280. LimparEcrã();
  281. EscreverTabuleiro(tabuleiro);
  282.  
  283. }
  284.  
  285.  
  286. int main()
  287. {
  288. int i;
  289. string linha;
  290. string modo;
  291. ifstream f;
  292. vector<navio>frota;
  293. f.open("TABULEIRO.txt");
  294. getline(f, linha);
  295. stringstream ss(linha);
  296. string s;
  297.  
  298.  
  299.  
  300. int linhas, colunas;
  301. ss >> s >> linhas >> s >> colunas;
  302.  
  303. vector<vector <char>> tabuleiro(linhas, vector<char>(colunas, '.'));
  304. EscreverTabuleiro(tabuleiro);
  305.  
  306. while (!f.eof()){
  307. getline(f, linha);
  308. }
  309. f.close();
  310. Buscarnavio("TABULEIRO.txt", frota);
  311.  
  312. setcolor(6);
  313. cout << "Bem-vindo!" << endl;
  314. setcolor(7);
  315.  
  316. bool valido = false;
  317. do {
  318. cout << endl << "Modo Manual ou Auto? ";
  319. cin >> modo;
  320.  
  321. int i = 0;
  322. while (i < modo.length())
  323. {
  324.  
  325. modo.at(i) = toupper(modo.at(i));
  326. i++;
  327. }
  328.  
  329.  
  330. if (modo == "AUTO")
  331. {
  332. JogarAutomatico(numerodebarcos, tabuleiro, linhas, colunas, frota);
  333. valido = true;
  334. }
  335. else if (modo == "MANUAL")
  336. {
  337. JogarManual(frota, linhas, colunas, tabuleiro);
  338. valido = true;
  339. }
  340. else{ cout << "Modo de jogo invalido"; }
  341. } while (!valido);
  342. system("PAUSE");
  343.  
  344.  
  345. navio n = frota[i];
  346. 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
  347. {
  348. n.cor.at(i) = tolower(n.cor.at(i));
  349. }
  350.  
  351. for (int i = 0; i < colors.size(); i++) //preencher o vetor cores com o codigo ascci do simbolo
  352. { //e o codigo da cor que utilizador escreveu
  353. if (colors.at(i) == n.cor)
  354. {
  355. usedColors.push_back(vector <int>(2));
  356. usedColors.at(usedColors.size() - 1).at(0) = (int)n.codigo;
  357. usedColors.at(usedColors.size() - 1).at(1) = i;
  358. break;
  359. }
  360. }
  361.  
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement