Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.47 KB | None | 0 0
  1. program bNaval;
  2. uses crt;
  3. type
  4. casa = record
  5. atirou:boolean; // ja atirou nessa casa ou nao
  6. id: char; // "-" para agua, "O" para barco e "X" para barco atingido
  7. tipo:string; // 'agua' ou 'couraçado' ou 'cruzador' etc.
  8. end;
  9.  
  10. barco = record
  11. nome:string;
  12. tamanho:integer; // quantas casas
  13. numId:integer; // numero de identificacao do barco (cour=1, cruz=2, dest=3, sub=4 hidro=5)
  14. qtosPode:integer; // quantos desse barco pode botar
  15. qtosRestam:integer; // quantos ainda faltam botar
  16. end;
  17.  
  18. tab=array[1..15,1..15] of casa;
  19. emb=array[1..5] of barco;
  20. acertouTiro=array[1..5] of integer;
  21.  
  22. var tabuleiro:tab;
  23. embarcacoes:emb;
  24. i,j,k,contaTiros:integer;
  25. resposta:char;
  26. acertou:acertouTiro;
  27.  
  28.  
  29. // ----------------------------FUNCOES E PROCEDIMENTOS-----------------------------------
  30.  
  31. //-----Tratamento de erro----------
  32.  
  33.  
  34. procedure digInt(minInt, maxInt: integer; var valInt:integer);
  35. // procedimento para orientar o usuário, permitir que ele digite um valor inteiro, testar se houve erro e testar se o valor está dentro do ////escopo de valores aceitos para a variável
  36. var
  37. erro: integer; // variável para o código de erro
  38. Begin
  39. repeat
  40. {$I-} // para desativar rotina padrão de tratamento de erro
  41. readln (valInt);
  42. {$I+} // para reativar a rotina padrão de tratamento de erro
  43. erro := 1; // para fazer todos os testes
  44. if ioresult <> 0 then writeln('O valor digitado não é um inteiro, digite novamente')
  45. else if (valInt > maxInt) then writeln('O valor digitado é maior que o valor máximo permitido. Digite novamente.')
  46. else if(valInt<minInt) then writeln('O valor digitado é menor que o valor mínimo permitido. Digite novamente.')
  47. else erro := 0; // para aceitar o valor digitado após passar os testes
  48. until erro = 0;
  49. end;
  50. //-------------------------------------
  51.  
  52. function continuar(var resposta:char):boolean;
  53.  
  54. begin
  55.  
  56. writeln ('Você deseja continuar no jogo? s/n');
  57. readln(resposta);
  58. if upcase(resposta) = 'S' then
  59. begin
  60. writeln ('Continue jogando então!');
  61. continuar:=true;
  62. end;
  63. if upcase(resposta) <> 'N' then
  64. begin
  65. if upcase(resposta) <> 'S' then writeln ('Escreva S ou N.')
  66. end;
  67. if upcase(resposta) = 'N' then
  68. begin
  69. writeln ('Você desistiu do jogo.');
  70. continuar:=false;
  71. end;
  72.  
  73. end;
  74.  
  75. //----------------------------------------------------------
  76.  
  77. function numBarco(posx,y:integer;tabule:tab):integer;
  78. begin
  79. if tabule[posx,y].tipo='couraçado' then numBarco:=1;
  80. if tabule[posx,y].tipo='cruzador' then numBarco:=2;
  81. if tabule[posx,y].tipo='destroyer' then numBarco:=3;
  82. if tabule[posx,y].tipo='submarino' then numBarco:=4;
  83. if tabule[posx,y].tipo='hidro aviao' then numBarco:=5;
  84. end;
  85. //-------------------------------------------------------------
  86.  
  87. procedure imprimeTab(var tabu:tab);
  88. var i,j:integer;
  89. begin
  90. writeln(' A B C D E F G H I J K L M N O');
  91. for j:=1 to 15 do
  92. begin
  93. if j<10 then write(' ',j,' ')
  94. else write(j,' ');
  95. for i:=1 to 15 do
  96. begin
  97. if i<15 then write(tabu[i,j].id,' ')
  98. else writeln(tabu[i,j].id);
  99. end;
  100. end;
  101. end;
  102.  
  103. procedure atira(var tabule:tab;embar:emb;acert:acertouTiro);
  104. var
  105. x:char;
  106. y,posx:integer;
  107.  
  108. begin
  109.  
  110. writeln('Jogador 2, atire! Escolha primeiro a letra (que corresponde à coluna):');
  111. repeat
  112. readln(x);
  113. if ((ord(x)-96)<1) or ((ord(x)-96)>15) then writeln('Digite uma letra válida:');
  114. until ((ord(x)-96)>=1) and ((ord(x)-96)<=15);
  115. posx:=ord(x)-96;
  116. writeln('Agora digite o número (que corresponde à linha):');
  117. digInt(1,15,y);
  118. if tabule[posx,y].tipo='agua' then
  119. begin
  120. writeln('ÁGUA!!!');
  121. tabule[posx,y].id:='A';
  122. end
  123. else
  124. begin
  125. writeln('Voce acertou um ',tabule[posx,y].tipo,'!');
  126. acert[numBarco(posx,y,tabule)]:=acert[numBarco(posx,y,tabule)]+1;
  127. tabule[posx,y].id:='O';
  128. if acert[numBarco(posx,y,tabule)]=embar[numBarco(posx,y,tabule)].tamanho then writeln('Parabéns, você afundou um ',tabule[posx,y].tipo,'!');
  129. end;
  130. end;
  131.  
  132.  
  133. function podeBotarAi(k:integer;var coordX:char;coordY:integer;tabule:tab;embar:emb):boolean;
  134. begin
  135. podeBotarAi:=true;
  136.  
  137. // verifica as 8 casas rodeando a casa escolhida, além da própria casa e testa se é agua ou barco
  138.  
  139. if (tabule[ord(coordX)-96,coordY].id='O') or (tabule[ord(coordX)-95,coordY].id='O') or (tabule[ord(coordX)-95,coordY-1].id='O') or (tabule[ord(coordX)-95,coordY+1].id='O') or (tabule[ord(coordX)-97,coordY].id='O') or (tabule[ord(coordX)-97,coordY-1].id='O') or (tabule[ord(coordX)-97,coordY+1].id='O') or (tabule[ord(coordX)-96,coordY-1].id='O') or (tabule[ord(coordX)-96,coordY+1].id='O') then
  140. begin
  141. podeBotarAi:=false;
  142. writeln('Você não pode botar o seu barco aí! Lembre-se: nenhuma embarcacao pode encostar em outra nem sair dos limites do tabuleiro. Digite de novo:');
  143. end
  144. else podeBotarAi:=true;
  145.  
  146.  
  147.  
  148.  
  149.  
  150. if embar[k].numId=4 then // no caso do submarino, testa se ele esta encostando na borda
  151. begin
  152. if (ord(coordX)-96=1) or (ord(coordX)-96=15) or (coordY=1) or (coordY=15) then
  153. begin
  154. writeln('Os submarinos não podem encostar na borda! Digite de novo:');
  155. podeBotarAi:=false;
  156. end;
  157. end;
  158. end;
  159.  
  160.  
  161. procedure poeBarco(k:integer;var tabule:tab;embar:emb);
  162. var x:char;
  163. posx,y,opcao,i:integer;
  164. pdBotar:boolean;
  165.  
  166. begin
  167. repeat
  168. pdBotar:=true;
  169. if embar[k].numId<>4 then writeln('Digite a "casa-chefe" do seu ',embar[k].nome,' ',(embar[k].qtosPode)-(embar[k].qtosRestam)+1,' (',embar[k].tamanho,' casas). Digite primeiro a letra: ')
  170. else writeln('Digite em que casa quer posicionar seu submarino ',(embar[4].qtosPode)-(embar[4].qtosRestam)+1,' (só 1 casa) Digite primeiro a letra (que corresponde à coluna): ');
  171. repeat
  172. repeat
  173. readln(x);
  174. if ((ord(x)-96)<1) or ((ord(x)-96)>15) then writeln('Digite uma letra válida:');
  175. until ((ord(x)-96)>=1) and ((ord(x)-96)<=15);
  176. writeln('Agora digite o número (que corresponde à linha):');
  177. digInt(1,15,y);
  178. podeBotarAi(k,x,y,tabule,embar);
  179. until podeBotarAi(k,x,y,tabule,embar)=true;
  180. posx:=ord(x)-96;
  181. if embar[k].numId<4 then
  182. begin
  183. writeln('Digite a orientação do seu ',embar[k].nome,' ',(embar[k].qtosPode)-(embar[k].qtosRestam)+1,': ');
  184. writeln('Digite o numero correspondente a opcao desejada');
  185. writeln(' 1. Horizontal');
  186. writeln(' 2. Vertical');
  187. digInt(1,2,opcao);
  188. case opcao of
  189. 1: begin
  190. for i:=0 to (embar[k].tamanho) do
  191. begin
  192. if (tabule[posx+i,y].id='O') or (tabule[posx+i,y-1].id='O') or (tabule[posx+i,y+1].id='O') or ((k=1) and (posx>10)) or ((k=2) and (posx>11)) or ((k=3) and (posx>13)) then pdBotar:=false;
  193.  
  194.  
  195. end;
  196. if pdBotar=false then
  197. begin
  198. repeat
  199. pdBotar:=true;
  200. writeln('Nenhum barco pode encostar em outro nem passar dos limites do tabuleiro. Digite de novo as coordenadas:');
  201. readln(x,y);
  202. podeBotarAi(k,x,y,tabule,embar);
  203. posx:=ord(x)-96;
  204. if ((k=1) and (posx>10)) or ((k=2) and (posx>11)) or ((k=3) and (posx>13)) then pdBotar:=false;
  205.  
  206. for i:=1 to (embar[k].tamanho) do
  207. begin
  208. if (tabule[posx+i,y].id='O') or (tabule[posx+i,y-1].id='O') or (tabule[posx+i,y+1].id='O')then pdBotar:=false;
  209. end;
  210. until pdBotar;
  211. end;
  212.  
  213.  
  214. for i:=0 to (embar[k].tamanho)-1 do
  215. begin
  216. if pdBotar then
  217. begin
  218. tabule[posx+i,y].id:='O';
  219. tabule[posx+i,y].tipo:=embar[k].nome;
  220. end;
  221.  
  222. end;
  223. end;
  224.  
  225. 2: begin
  226. for i:=1 to (embar[k].tamanho)-1 do
  227. begin
  228. if (tabule[posx,y+i].id='O') or (tabule[posx-1,y+i].id='O') or (tabule[posx+1,y+i].id='O') or ((k=1) and (y>11)) or ((k=2) and (y>12)) or ((k=3) and (y=15)) then pdBotar:=false;
  229. end;
  230.  
  231. if pdBotar=false then
  232. begin
  233. repeat
  234. writeln('Nenhum barco pode encostar em outro nem passar dos limites do tabuleiro. Digite de novo as coordenadas:');
  235. readln(x,y);
  236. podeBotarAi(k,x,y,tabule,embar);
  237. if ((k=1) and (y>11)) or ((k=2) and (y>12)) or ((k=3) and (y=15)) then pdBotar:=false;
  238. posx:=ord(x)-96;
  239. if tabule[posx,y+i].id='-' then pdBotar:=true;
  240. until pdBotar;
  241. end;
  242.  
  243. for i:=0 to (embar[k].tamanho)-1 do
  244. begin
  245. tabule[posx,y+i].id:='O';
  246. tabule[posx+i,y].tipo:=embar[k].nome;
  247. end;
  248. end;
  249. end;
  250. end
  251. else
  252. begin
  253. if embar[k].numId=4 then
  254. begin
  255. repeat
  256. podeBotarAi(k,x,y,tabule,embar);
  257. until podeBotarAi(k,x,y,tabule,embar)=true;
  258. tabuleiro[posx,y].id:='O';
  259. tabuleiro[posx,y].tipo:='submarino';
  260. end;
  261.  
  262. if embar[k].numId=5 then
  263. begin
  264. writeln('Digite o numero correspondente a opcao desejada');
  265. writeln(' 1. Apontando pra cima');
  266. writeln(' 2. Apontando pra direita');
  267. writeln(' 3. Apontando pra baixo');
  268. writeln(' 4. Apontando pra esquerda');
  269. digInt(1,4,opcao);
  270. case opcao of
  271. 1: begin
  272. if (posx=1) or (posx=15) or (y=14) or (y=15) then
  273. begin
  274. repeat
  275. writeln('Você não posicionou direito o hidro avião. Digite de novo a "casa-chefe":');
  276. readln(x,y);
  277. podeBotarAi(k,x,y,tabule,embar);
  278. until (posx<>1) and (posx<>15) and (y<>14) and (y<>15);
  279. end;
  280. if (posx<>1) and (posx<>15) and (y<>14) and (y<>15) then
  281. begin
  282. tabuleiro[posx,y].id:='O';
  283. tabuleiro[posx,y].tipo:='hidro aviao';
  284. tabuleiro[posx-1,y+1].id:='O';
  285. tabuleiro[posx-1,y+1].tipo:='hidro aviao';
  286. tabuleiro[posx+1,y+1].id:='O';
  287. tabuleiro[posx+1,y+1].tipo:='hidro aviao';
  288. end;
  289. end;
  290.  
  291. 2: begin
  292. if (posx=1) or (posx=2) or (y=1) or (y=15) then
  293. begin
  294. repeat
  295. writeln('Você não posicionou direito o hidro avião. Digite de novo a "casa-chefe":');
  296. readln(x,y);
  297. podeBotarAi(k,x,y,tabule,embar);
  298. until (posx<>1) and (posx<>2) and (y<>1) and (y<>15);
  299. end;
  300. tabuleiro[posx,y].id:='O';
  301. tabuleiro[posx,y].tipo:='hidro aviao';
  302. tabuleiro[posx-1,y-1].id:='O';
  303. tabuleiro[posx-1,y-1].tipo:='hidro aviao';
  304. tabuleiro[posx-1,y+1].id:='O';
  305. tabuleiro[posx-1,y+1].tipo:='hidro aviao';
  306. end;
  307.  
  308. 3: begin
  309. if (posx=1) or (posx=15) or (y=1) or (y=2) then
  310. begin
  311. repeat
  312. writeln('Você não posicionou direito o hidro avião. Digite de novo a "casa-chefe":');
  313. readln(x,y);
  314. podeBotarAi(k,x,y,tabule,embar);
  315. until (posx<>1) and (posx<>15) and (y<>1) and (y<>2);
  316. end;
  317. tabuleiro[posx,y].id:='O';
  318. tabuleiro[posx,y].tipo:='hidro aviao';
  319. tabuleiro[posx-1,y-1].id:='O';
  320. tabuleiro[posx-1,y-1].tipo:='hidro aviao';
  321. tabuleiro[posx+1,y-1].id:='O';
  322. tabuleiro[posx+1,y-1].tipo:='hidro aviao';
  323. end;
  324.  
  325. 4: begin
  326. if (posx=14) or (posx=15) or (y=1) or (y=15) then
  327. begin
  328. repeat
  329. writeln('Você não posicionou direito o hidro avião. Digite de novo a "casa-chefe":');
  330. readln(x,y);
  331. podeBotarAi(k,x,y,tabule,embar);
  332. until (posx<>14) and (posx<>15) and (y<>1) and (y<>15);
  333. end;
  334. tabuleiro[posx,y].id:='O';
  335. tabuleiro[posx,y].tipo:='hidro aviao';
  336. tabuleiro[posx+1,y-1].id:='O';
  337. tabuleiro[posx+1,y-1].tipo:='hidro aviao';
  338. tabuleiro[posx+1,y+1].id:='O';
  339. tabuleiro[posx+1,y+1].tipo:='hidro aviao';
  340. end;
  341. end;
  342. end;
  343.  
  344. end;
  345. embar[k].qtosRestam:=embar[k].qtosRestam-1;
  346. imprimeTab(tabule);
  347. until embar[k].qtosRestam=0;
  348.  
  349. end;
  350.  
  351.  
  352.  
  353.  
  354. // ----------------------------------------PROGRAMA PRINCIPAL-----------------------------------------------------------//
  355.  
  356. begin
  357. clrscr;
  358. // INICIALIZACAO DAS VARIAVEIS
  359. for i:=1 to 15 do
  360. begin
  361. for j:=1 to 15 do
  362. begin
  363. tabuleiro[i,j].atirou:=false;
  364. tabuleiro[i,j].id:='-';
  365. tabuleiro[i,j].tipo:='agua';
  366. end;
  367. end;
  368. embarcacoes[1].nome:='couracado';
  369. embarcacoes[1].tamanho:=5;
  370. embarcacoes[1].numId:=1;
  371. embarcacoes[1].qtosPode:=1;
  372. embarcacoes[1].qtosRestam:=1;
  373.  
  374. embarcacoes[2].nome:='cruzador';
  375. embarcacoes[2].tamanho:=4;
  376. embarcacoes[2].numId:=2;
  377. embarcacoes[2].qtosPode:=2;
  378. embarcacoes[2].qtosRestam:=2;
  379.  
  380. embarcacoes[3].nome:='destroyer';
  381. embarcacoes[3].tamanho:=2;
  382. embarcacoes[3].numId:=3;
  383. embarcacoes[3].qtosPode:=3;
  384. embarcacoes[3].qtosRestam:=3;
  385.  
  386. embarcacoes[4].nome:='submarino';
  387. embarcacoes[4].tamanho:=1;
  388. embarcacoes[4].numId:=4;
  389. embarcacoes[4].qtosPode:=4;
  390. embarcacoes[4].qtosRestam:=4;
  391.  
  392. embarcacoes[5].nome:='hidro aviao';
  393. embarcacoes[5].tamanho:=3; //tirar sepa
  394. embarcacoes[5].numId:=5;
  395. embarcacoes[5].qtosPode:=4;
  396. embarcacoes[5].qtosRestam:=4;
  397.  
  398. contaTiros:=0;
  399. for i:=1 to 5 do acertou[i]:=0;
  400.  
  401. //
  402. writeln('Bem- vindo a batalha naval:');
  403. writeln('Jogador 1, agora você irá botar os seus barcos no tabuleiro. Você tem direito a 1 couraçado (5 casas), 2 cruzadores (4 casas), 3 destroyers (2 casas), 4 submarinos (1 casa) e 4 hidroaviões, que são diferentes e serão melhor explicados depois. Para cada embarcação você deverá digitar a posição da casa que encabeça a embarcação e depois você escolherá a orientação do barco.');
  404. writeln('Confuso? Então lá vai um exemplo:');
  405. // EXPLICAR MELHOR AS REGRAS PRA POSICIONAR OS BARCOS E TAL
  406. writeln('Quando for posicionar o couraçado, você poderia por exemplo escolher a posição A1. Se escolher a orientação horizontal, o seu couraçado ocupará as casas que vão de A1 a E1, se escolher vertical ele irá de A1 a A5');
  407. writeln('');
  408. writeln(' A B C D E F G H..... A B C D E F G H......');
  409. writeln('1 O O O O O - - -..... 1 O - - - - - - -......');
  410. writeln('2 - - - - - - - -..... 2 O - - - - - - -......');
  411. writeln('3 - - - - - - - -..... 3 O - - - - - - -......');
  412. writeln('4 - - - - - - - -..... 4 O - - - - - - -......');
  413. writeln('5 - - - - - - - -..... 5 O - - - - - - -......');
  414. writeln('6 - - - - - - - -..... 6 - - - - - - - -......');
  415. writeln('.........etc...........................etc............');
  416. writeln('Aperte qualquer tecla para continuar...');
  417. // BLA BLA BLA
  418. //..............
  419. textbackground(blue);
  420. textcolor(white);
  421. readkey;
  422. clrscr;
  423. imprimeTab(tabuleiro);
  424. k:=1;
  425.  
  426. repeat
  427. poeBarco(k,tabuleiro,embarcacoes);
  428. continuar(resposta);
  429. k:=k+1;
  430. until (k=6) or (continuar(resposta)=false);
  431.  
  432. clrscr;
  433. if continuar(resposta) then
  434. begin
  435. for i:=1 to 15 do
  436. begin
  437. for j:=1 to 15 do
  438. begin
  439. tabuleiro[i,j].atirou:=false;
  440. tabuleiro[i,j].id:='-';
  441. end;
  442. end;
  443.  
  444. repeat
  445. writeln('Deseja continuar atirando? s/n');
  446. readln(resposta);
  447. if resposta='s' then
  448. begin
  449. atira(tabuleiro,embarcacoes,acertou);
  450. delay(1000);
  451. contaTiros:=contaTiros+1;
  452. imprimeTab(tabuleiro);
  453. end
  454. else
  455. begin
  456. if resposta<>'n' then writeln('Digite uma letra válida:')
  457. else writeln('Obrigado por usar o jogo OTARIO!');
  458. end;
  459.  
  460. until resposta='n';
  461.  
  462. writeln('Você deu ',contaTiros,' tiros');
  463. end
  464. else writeln('Obrigado por usar o jogo OTARIO!');
  465.  
  466.  
  467. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement