Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program bNaval;
- uses crt;
- type
- casa = record
- atirou:boolean; // ja atirou nessa casa ou nao
- id: char; // "-" para agua, "O" para barco e "X" para barco atingido
- tipo:string; // 'agua' ou 'couraçado' ou 'cruzador' etc.
- end;
- barco = record
- nome:string;
- tamanho:integer; // quantas casas
- numId:integer; // numero de identificacao do barco (cour=1, cruz=2, dest=3, sub=4 hidro=5)
- qtosPode:integer; // quantos desse barco pode botar
- qtosRestam:integer; // quantos ainda faltam botar
- end;
- tab=array[1..15,1..15] of casa;
- emb=array[1..5] of barco;
- acertouTiro=array[1..5] of integer;
- var tabuleiro:tab;
- embarcacoes:emb;
- i,j,k,contaTiros:integer;
- resposta:char;
- acertou:acertouTiro;
- // ----------------------------FUNCOES E PROCEDIMENTOS-----------------------------------
- //-----Tratamento de erro----------
- procedure digInt(minInt, maxInt: integer; var valInt:integer);
- // 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
- var
- erro: integer; // variável para o código de erro
- Begin
- repeat
- {$I-} // para desativar rotina padrão de tratamento de erro
- readln (valInt);
- {$I+} // para reativar a rotina padrão de tratamento de erro
- erro := 1; // para fazer todos os testes
- if ioresult <> 0 then writeln('O valor digitado não é um inteiro, digite novamente')
- else if (valInt > maxInt) then writeln('O valor digitado é maior que o valor máximo permitido. Digite novamente.')
- else if(valInt<minInt) then writeln('O valor digitado é menor que o valor mínimo permitido. Digite novamente.')
- else erro := 0; // para aceitar o valor digitado após passar os testes
- until erro = 0;
- end;
- //-------------------------------------
- function continuar(var resposta:char):boolean;
- begin
- writeln ('Você deseja continuar no jogo? s/n');
- readln(resposta);
- if upcase(resposta) = 'S' then
- begin
- writeln ('Continue jogando então!');
- continuar:=true;
- end;
- if upcase(resposta) <> 'N' then
- begin
- if upcase(resposta) <> 'S' then writeln ('Escreva S ou N.')
- end;
- if upcase(resposta) = 'N' then
- begin
- writeln ('Você desistiu do jogo.');
- continuar:=false;
- end;
- end;
- //----------------------------------------------------------
- function numBarco(posx,y:integer;tabule:tab):integer;
- begin
- if tabule[posx,y].tipo='couraçado' then numBarco:=1;
- if tabule[posx,y].tipo='cruzador' then numBarco:=2;
- if tabule[posx,y].tipo='destroyer' then numBarco:=3;
- if tabule[posx,y].tipo='submarino' then numBarco:=4;
- if tabule[posx,y].tipo='hidro aviao' then numBarco:=5;
- end;
- //-------------------------------------------------------------
- procedure imprimeTab(var tabu:tab);
- var i,j:integer;
- begin
- writeln(' A B C D E F G H I J K L M N O');
- for j:=1 to 15 do
- begin
- if j<10 then write(' ',j,' ')
- else write(j,' ');
- for i:=1 to 15 do
- begin
- if i<15 then write(tabu[i,j].id,' ')
- else writeln(tabu[i,j].id);
- end;
- end;
- end;
- procedure atira(var tabule:tab;embar:emb;acert:acertouTiro);
- var
- x:char;
- y,posx:integer;
- begin
- writeln('Jogador 2, atire! Escolha primeiro a letra (que corresponde à coluna):');
- repeat
- readln(x);
- if ((ord(x)-96)<1) or ((ord(x)-96)>15) then writeln('Digite uma letra válida:');
- until ((ord(x)-96)>=1) and ((ord(x)-96)<=15);
- posx:=ord(x)-96;
- writeln('Agora digite o número (que corresponde à linha):');
- digInt(1,15,y);
- if tabule[posx,y].tipo='agua' then
- begin
- writeln('ÁGUA!!!');
- tabule[posx,y].id:='A';
- end
- else
- begin
- writeln('Voce acertou um ',tabule[posx,y].tipo,'!');
- acert[numBarco(posx,y,tabule)]:=acert[numBarco(posx,y,tabule)]+1;
- tabule[posx,y].id:='O';
- if acert[numBarco(posx,y,tabule)]=embar[numBarco(posx,y,tabule)].tamanho then writeln('Parabéns, você afundou um ',tabule[posx,y].tipo,'!');
- end;
- end;
- function podeBotarAi(k:integer;var coordX:char;coordY:integer;tabule:tab;embar:emb):boolean;
- begin
- podeBotarAi:=true;
- // verifica as 8 casas rodeando a casa escolhida, além da própria casa e testa se é agua ou barco
- 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
- begin
- podeBotarAi:=false;
- 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:');
- end
- else podeBotarAi:=true;
- if embar[k].numId=4 then // no caso do submarino, testa se ele esta encostando na borda
- begin
- if (ord(coordX)-96=1) or (ord(coordX)-96=15) or (coordY=1) or (coordY=15) then
- begin
- writeln('Os submarinos não podem encostar na borda! Digite de novo:');
- podeBotarAi:=false;
- end;
- end;
- end;
- procedure poeBarco(k:integer;var tabule:tab;embar:emb);
- var x:char;
- posx,y,opcao,i:integer;
- pdBotar:boolean;
- begin
- repeat
- pdBotar:=true;
- 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: ')
- 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): ');
- repeat
- repeat
- readln(x);
- if ((ord(x)-96)<1) or ((ord(x)-96)>15) then writeln('Digite uma letra válida:');
- until ((ord(x)-96)>=1) and ((ord(x)-96)<=15);
- writeln('Agora digite o número (que corresponde à linha):');
- digInt(1,15,y);
- podeBotarAi(k,x,y,tabule,embar);
- until podeBotarAi(k,x,y,tabule,embar)=true;
- posx:=ord(x)-96;
- if embar[k].numId<4 then
- begin
- writeln('Digite a orientação do seu ',embar[k].nome,' ',(embar[k].qtosPode)-(embar[k].qtosRestam)+1,': ');
- writeln('Digite o numero correspondente a opcao desejada');
- writeln(' 1. Horizontal');
- writeln(' 2. Vertical');
- digInt(1,2,opcao);
- case opcao of
- 1: begin
- for i:=0 to (embar[k].tamanho) do
- begin
- 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;
- end;
- if pdBotar=false then
- begin
- repeat
- pdBotar:=true;
- writeln('Nenhum barco pode encostar em outro nem passar dos limites do tabuleiro. Digite de novo as coordenadas:');
- readln(x,y);
- podeBotarAi(k,x,y,tabule,embar);
- posx:=ord(x)-96;
- if ((k=1) and (posx>10)) or ((k=2) and (posx>11)) or ((k=3) and (posx>13)) then pdBotar:=false;
- for i:=1 to (embar[k].tamanho) do
- begin
- 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;
- end;
- until pdBotar;
- end;
- for i:=0 to (embar[k].tamanho)-1 do
- begin
- if pdBotar then
- begin
- tabule[posx+i,y].id:='O';
- tabule[posx+i,y].tipo:=embar[k].nome;
- end;
- end;
- end;
- 2: begin
- for i:=1 to (embar[k].tamanho)-1 do
- begin
- 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;
- end;
- if pdBotar=false then
- begin
- repeat
- writeln('Nenhum barco pode encostar em outro nem passar dos limites do tabuleiro. Digite de novo as coordenadas:');
- readln(x,y);
- podeBotarAi(k,x,y,tabule,embar);
- if ((k=1) and (y>11)) or ((k=2) and (y>12)) or ((k=3) and (y=15)) then pdBotar:=false;
- posx:=ord(x)-96;
- if tabule[posx,y+i].id='-' then pdBotar:=true;
- until pdBotar;
- end;
- for i:=0 to (embar[k].tamanho)-1 do
- begin
- tabule[posx,y+i].id:='O';
- tabule[posx+i,y].tipo:=embar[k].nome;
- end;
- end;
- end;
- end
- else
- begin
- if embar[k].numId=4 then
- begin
- repeat
- podeBotarAi(k,x,y,tabule,embar);
- until podeBotarAi(k,x,y,tabule,embar)=true;
- tabuleiro[posx,y].id:='O';
- tabuleiro[posx,y].tipo:='submarino';
- end;
- if embar[k].numId=5 then
- begin
- writeln('Digite o numero correspondente a opcao desejada');
- writeln(' 1. Apontando pra cima');
- writeln(' 2. Apontando pra direita');
- writeln(' 3. Apontando pra baixo');
- writeln(' 4. Apontando pra esquerda');
- digInt(1,4,opcao);
- case opcao of
- 1: begin
- if (posx=1) or (posx=15) or (y=14) or (y=15) then
- begin
- repeat
- writeln('Você não posicionou direito o hidro avião. Digite de novo a "casa-chefe":');
- readln(x,y);
- podeBotarAi(k,x,y,tabule,embar);
- until (posx<>1) and (posx<>15) and (y<>14) and (y<>15);
- end;
- if (posx<>1) and (posx<>15) and (y<>14) and (y<>15) then
- begin
- tabuleiro[posx,y].id:='O';
- tabuleiro[posx,y].tipo:='hidro aviao';
- tabuleiro[posx-1,y+1].id:='O';
- tabuleiro[posx-1,y+1].tipo:='hidro aviao';
- tabuleiro[posx+1,y+1].id:='O';
- tabuleiro[posx+1,y+1].tipo:='hidro aviao';
- end;
- end;
- 2: begin
- if (posx=1) or (posx=2) or (y=1) or (y=15) then
- begin
- repeat
- writeln('Você não posicionou direito o hidro avião. Digite de novo a "casa-chefe":');
- readln(x,y);
- podeBotarAi(k,x,y,tabule,embar);
- until (posx<>1) and (posx<>2) and (y<>1) and (y<>15);
- end;
- tabuleiro[posx,y].id:='O';
- tabuleiro[posx,y].tipo:='hidro aviao';
- tabuleiro[posx-1,y-1].id:='O';
- tabuleiro[posx-1,y-1].tipo:='hidro aviao';
- tabuleiro[posx-1,y+1].id:='O';
- tabuleiro[posx-1,y+1].tipo:='hidro aviao';
- end;
- 3: begin
- if (posx=1) or (posx=15) or (y=1) or (y=2) then
- begin
- repeat
- writeln('Você não posicionou direito o hidro avião. Digite de novo a "casa-chefe":');
- readln(x,y);
- podeBotarAi(k,x,y,tabule,embar);
- until (posx<>1) and (posx<>15) and (y<>1) and (y<>2);
- end;
- tabuleiro[posx,y].id:='O';
- tabuleiro[posx,y].tipo:='hidro aviao';
- tabuleiro[posx-1,y-1].id:='O';
- tabuleiro[posx-1,y-1].tipo:='hidro aviao';
- tabuleiro[posx+1,y-1].id:='O';
- tabuleiro[posx+1,y-1].tipo:='hidro aviao';
- end;
- 4: begin
- if (posx=14) or (posx=15) or (y=1) or (y=15) then
- begin
- repeat
- writeln('Você não posicionou direito o hidro avião. Digite de novo a "casa-chefe":');
- readln(x,y);
- podeBotarAi(k,x,y,tabule,embar);
- until (posx<>14) and (posx<>15) and (y<>1) and (y<>15);
- end;
- tabuleiro[posx,y].id:='O';
- tabuleiro[posx,y].tipo:='hidro aviao';
- tabuleiro[posx+1,y-1].id:='O';
- tabuleiro[posx+1,y-1].tipo:='hidro aviao';
- tabuleiro[posx+1,y+1].id:='O';
- tabuleiro[posx+1,y+1].tipo:='hidro aviao';
- end;
- end;
- end;
- end;
- embar[k].qtosRestam:=embar[k].qtosRestam-1;
- imprimeTab(tabule);
- until embar[k].qtosRestam=0;
- end;
- // ----------------------------------------PROGRAMA PRINCIPAL-----------------------------------------------------------//
- begin
- clrscr;
- // INICIALIZACAO DAS VARIAVEIS
- for i:=1 to 15 do
- begin
- for j:=1 to 15 do
- begin
- tabuleiro[i,j].atirou:=false;
- tabuleiro[i,j].id:='-';
- tabuleiro[i,j].tipo:='agua';
- end;
- end;
- embarcacoes[1].nome:='couracado';
- embarcacoes[1].tamanho:=5;
- embarcacoes[1].numId:=1;
- embarcacoes[1].qtosPode:=1;
- embarcacoes[1].qtosRestam:=1;
- embarcacoes[2].nome:='cruzador';
- embarcacoes[2].tamanho:=4;
- embarcacoes[2].numId:=2;
- embarcacoes[2].qtosPode:=2;
- embarcacoes[2].qtosRestam:=2;
- embarcacoes[3].nome:='destroyer';
- embarcacoes[3].tamanho:=2;
- embarcacoes[3].numId:=3;
- embarcacoes[3].qtosPode:=3;
- embarcacoes[3].qtosRestam:=3;
- embarcacoes[4].nome:='submarino';
- embarcacoes[4].tamanho:=1;
- embarcacoes[4].numId:=4;
- embarcacoes[4].qtosPode:=4;
- embarcacoes[4].qtosRestam:=4;
- embarcacoes[5].nome:='hidro aviao';
- embarcacoes[5].tamanho:=3; //tirar sepa
- embarcacoes[5].numId:=5;
- embarcacoes[5].qtosPode:=4;
- embarcacoes[5].qtosRestam:=4;
- contaTiros:=0;
- for i:=1 to 5 do acertou[i]:=0;
- //
- writeln('Bem- vindo a batalha naval:');
- 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.');
- writeln('Confuso? Então lá vai um exemplo:');
- // EXPLICAR MELHOR AS REGRAS PRA POSICIONAR OS BARCOS E TAL
- 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');
- writeln('');
- writeln(' A B C D E F G H..... A B C D E F G H......');
- writeln('1 O O O O O - - -..... 1 O - - - - - - -......');
- writeln('2 - - - - - - - -..... 2 O - - - - - - -......');
- writeln('3 - - - - - - - -..... 3 O - - - - - - -......');
- writeln('4 - - - - - - - -..... 4 O - - - - - - -......');
- writeln('5 - - - - - - - -..... 5 O - - - - - - -......');
- writeln('6 - - - - - - - -..... 6 - - - - - - - -......');
- writeln('.........etc...........................etc............');
- writeln('Aperte qualquer tecla para continuar...');
- // BLA BLA BLA
- //..............
- textbackground(blue);
- textcolor(white);
- readkey;
- clrscr;
- imprimeTab(tabuleiro);
- k:=1;
- repeat
- poeBarco(k,tabuleiro,embarcacoes);
- continuar(resposta);
- k:=k+1;
- until (k=6) or (continuar(resposta)=false);
- clrscr;
- if continuar(resposta) then
- begin
- for i:=1 to 15 do
- begin
- for j:=1 to 15 do
- begin
- tabuleiro[i,j].atirou:=false;
- tabuleiro[i,j].id:='-';
- end;
- end;
- repeat
- writeln('Deseja continuar atirando? s/n');
- readln(resposta);
- if resposta='s' then
- begin
- atira(tabuleiro,embarcacoes,acertou);
- delay(1000);
- contaTiros:=contaTiros+1;
- imprimeTab(tabuleiro);
- end
- else
- begin
- if resposta<>'n' then writeln('Digite uma letra válida:')
- else writeln('Obrigado por usar o jogo OTARIO!');
- end;
- until resposta='n';
- writeln('Você deu ',contaTiros,' tiros');
- end
- else writeln('Obrigado por usar o jogo OTARIO!');
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement