Manuel "Keanu"
Aqui se encontra o código Pascal do programa de "Batalha Naval" com anotações. O programa usa if's, cases, booleanos, arrays, repeats...
Program batalha_naval_II_O ;
var mplayer,mcom,mresults:array[1..4,1..4] of char;
mplayer2,mcom2,mresults2:array[1..8,1..8] of char;
cheat,jog,i,j,h,l,r,t,rpx,rpy,plc1,plc2,choice,minic:integer;
x,y:char;
user:string;
test1,test2,test3,test4,test5:boolean;
Procedure minigame;
begin
minic:=minic+1;
writeln('clicked ',minic,' times');
end;
Procedure instructions;
begin
writeln('This is a game of luck, your objective is ');
writeln('to guess where the enemy has placed his boat');
writeln('befores he finds where your boat is ');
writeln('Good luck ',user,' :) ');
writeln('*Remeber this game is case sensitive. ');
writeln;
writeln('Cheatcodes: ');
writeln('-->112 ');
writeln('-->533 ');
writeln('-->777 ');
writeln('-->233 ');
writeln('-->9000 ');
writeln('-->0 to exit ');
writeln(' press enter ');
readln;
clrscr;
end;
Procedure createboard;
var ch:char;
Begin
//procedimento de atribuição das grelhas de jogo e de resultados
for i:=2 to 4 do
begin
ch:='A';
ch:=chr(ord(ch)-2+i);
mplayer[i,1]:=ch;
mcom[i,1]:=ch;
mresults[i,1]:=ch;
//para aumentar tamanho doo tabuleiro mudar a letra para a correspondente ao numero maximo da coluna
ch:='D';
ch:=chr(ord(ch)-2+i);
mplayer[1,i]:=ch;
mcom[1,i]:=ch;
mresults[1,i]:=ch;
End;
for i:=2 to 4 do
for j:=2 to 4 do
begin
mplayer[i,j]:='?';
mcom[i,j]:='?';
mresults[i,j]:='?';
end;
mplayer[1,1]:='X';
mcom[1,1]:='X';
mresults[1,1]:='X';
End;
Procedure createboard2;
var ch2:char;
Begin
//procedimento de atribuição das grelhas de jogo e de resultados
for i:=2 to 8 do
begin
ch2:='A';
ch2:=chr(ord(ch2)-2+i);
mplayer2[i,1]:=ch2;
mcom2[i,1]:=ch2;
mresults2[i,1]:=ch2;
//para aumentar tamanho doo tabuleiro mudar a letra para a correspondente ao numero maximo da coluna
ch2:='H';
ch2:=chr(ord(ch2)-2+i);
mplayer2[1,i]:=ch2;
mcom2[1,i]:=ch2;
mresults2[1,i]:=ch2;
End;
for i:=2 to 8 do
for j:=2 to 8 do
begin
mplayer2[i,j]:='?';
mcom2[i,j]:='?';
mresults2[i,j]:='?';
end;
mplayer2[1,1]:='X';
mcom2[1,1]:='X';
mresults2[1,1]:='X';
End;
Procedure ciclecount;
Begin
jog:=jog+1; //atribuir jog mais tarde para jog:=0
End;
Procedure showptable;
Begin
writeln('round ',jog);
writeln(user,' table');
//grelha do jogador
for i:=1 to 4 do
begin
writeln;
writeln;
for j:=1 to 4 do
write(mplayer[i,j]:-5);
end;
End;
Procedure showptable2;
Begin
writeln('round ',jog);
writeln(user,' table');
//grelha do jogador
for i:=1 to 8 do
begin
writeln;
writeln;
for j:=1 to 8 do
write(mplayer2[i,j]:-5);
end;
End;
Procedure showctable;
Begin
//grelha do computador
writeln('You can see it....');
for i:=1 to 4 do
begin
writeln;
writeln;
for j:=1 to 4 do
write(mcom[i,j]:-5);
end;
End;
Procedure showctable2;
Begin
//grelha do computador
writeln('You can see it....');
for i:=1 to 8 do
begin
writeln;
writeln;
for j:=1 to 8 do
write(mcom2[i,j]:-5);
end;
End;
Procedure showrtable;
Begin
writeln('Computer table');
writeln;
writeln('round ',jog);
for i:=1 to 4 do
begin
writeln;
writeln;
for j:=1 to 4 do
write(mresults[i,j]:-5);
End;
End;
Procedure showrtable2;
Begin
writeln('Computer table');
writeln;
writeln('round ',jog);
for i:=1 to 8 do
begin
writeln;
writeln;
for j:=1 to 8 do
write(mresults2[i,j]:-5);
End;
End;
Begin
clrscr;
writeln('Tell me your name to begin: ');
read(user);
clrscr;
writeln(' ___ ___ ___ ___ ___ ');
writeln(' /\__\ /\ \ /\__\ /\ \ /\__\ ');
writeln(' /::| | /::\ \ /:/ / /::\ \ /:/ / ');
writeln(' /:|:| | /:/\:\ \ /:/ / /:/\:\ \ /:/ / ');
writeln(' /:/|:| |__ /::\~\:\ \ /:/__/ ___ /::\~\:\ \ /:/ / ');
writeln('/:/ |:| /\__\ /:/\:\ \:\__\ |:| | /\__\ /:/\:\ \:\__\ /:/__/ ');
writeln('\/__|:|/:/ / \/__\:\/:/ / |:| |/:/ / \/__\:\/:/ / \:\ \ ');
writeln(' |:/:/ / \::/ / |:|__/:/ / \::/ / \:\ \ ');
writeln(' |::/ / /:/ / \::::/__/ /:/ / \:\ \ ');
writeln(' /:/ / /:/ / ~~~~ /:/ / \:\__\ ');
writeln(' \/__/ \/__/ \/__/ \/__/ ');
writeln(' by Manuel Silva');
writeln;
writeln;
writeln;
writeln(' ____ _ ');
writeln(' | _ \| | __ _ _ _ ');
writeln(' | |_) | |/ _` | | | | ');
writeln(' | __/| | (_| | |_| | ');
writeln(' |_| |_|\__,_|\__, | ');
writeln(' |___/ ');
writeln;
writeln(' (PRESS ENTER ',user,')');
readln;
test5:=false;
repeat;
createboard;
clrscr;
test3:=true;
writeln(' ---------------- ');
writeln(' | (1)START GAME | ');
writeln(' | (2)HOW TO PLAY | ');
writeln(' | (3)Cheats | ');
writeln(' | (4)Quit | ');
writeln(' ---------------- ');
writeln;
writeln;
repeat
readln(choice);
case choice of
1:begin
clrscr;
showptable;
writeln;
//colocaçao de peça do jogador
writeln('Choose a line between A, B or C for your Boat');
repeat
readln(x);
test1:=true;
case x of
'A':plc1:=2;
'B':plc1:=3;
'C':plc1:=4;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
writeln('Choose a collum D, E or F for your Boat');
repeat
readln(y);
test1:=true;
case y of
'D':plc2:=2;
'E':plc2:=3;
'F':plc2:=4;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
mplayer[plc1,plc2]:='#';
readln;
clrscr;
writeln(user,'´s boat (#) has been placed');
writeln;
showptable;
readln;
clrscr;
//next part
//procedimento de colocaçao da peça do computador
randomize;
rpx:=random(3)+2;
rpy:=random(3)+2;
mcom[rpx,rpy]:='x';
writeln('The computer has placed his boat');
readln;
clrscr;
//next part
test4:=false;
jog:=0;
//IMPORTANT
repeat
ciclecount;
writeln;
showrtable;
//ataque do jogador
writeln;
writeln('Choose a line between A, B or C to launch a missile on the enemy boat');
repeat
readln(x);
test1:=true;
case x of
'A':h:=2;
'B':h:=3;
'C':h:=4;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
writeln;
writeln;
writeln('Choose the collum D, E or F of the missile launch');
repeat
readln(y);
test1:=true;
case y of
'D':l:=2;
'E':l:=3;
'F':l:=4;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
readln;
clrscr;
//confirmaçao de embate
Mresults[h,l]:='x';
If Mcom[h,l]=mcom[rpx,rpy] then
begin
test4:=true;
clrscr;
writeln('__ _____ _ _ __ _____ _ _ _ _ _ ');
writeln('\ \ / / _ \| | | | \ \ / / _ \| \ | | | | | | | | ');
writeln(' \ V / | | | | | | \ \ /\ / / | | | \| | | | | | | | ');
writeln(' | || |_| | |_| | \ V V /| |_| | |\ | |_| |_| |_| ');
writeln(' |_| \___/ \___/ \_/\_/ \___/|_| \_| (_) (_) (_) ');
readln;
writeln('You won ',user,', in only ',jog,' rounds');
readln;
end
else
begin
writeln('You failed the missile attack an (x) was placed where you failed!!!');
readln;
clrscr;
writeln('Computer attacking...');
readln;
//next part
//ataque do computador
randomize;
r:=random(3)+2;
t:=random(3)+2;
//confirmaçao de embate
If mplayer[r,t]=mplayer[plc1,plc2] then
begin
test4:=true;
clrscr;
writeln('__ _____ _ _ _ ___ ____ _____ __ ');
writeln('\ \ / / _ \| | | | | | / _ \/ ___|_ _| _ / / ');
writeln(' \ V / | | | | | | | | | | | \___ \ | | (_) | | ');
writeln(' | || |_| | |_| | | |___ |_| |___) || | _ | | ');
writeln(' |_| \___/ \___/ |_____\___/|____/ |_| (_) | | ');
writeln(' \_\ ');
readln;
writeln('You lost ',user,', in only ',jog,' rounds');
end
else
begin
writeln('He failed the missile attack!!!');
mplayer[r,t]:='!';
readln;
clrscr;
showptable;
readln;
clrscr;
showrtable;
readln;
clrscr;
end;
End;
until test4=true;
End;
2:instructions;
3: begin
writeln('waiting for cheatcode ',user,':');
readln(cheat);
case cheat of
112: begin
clrscr;
writeln('__ _____ _ _ __ _____ _ _ _ _ _ ');
writeln('\ \ / / _ \| | | | \ \ / / _ \| \ | | | | | | | | ');
writeln(' \ V / | | | | | | \ \ /\ / / | | | \| | | | | | | | ');
writeln(' | || |_| | |_| | \ V V /| |_| | |\ | |_| |_| |_| ');
writeln(' |_| \___/ \___/ \_/\_/ \___/|_| \_| (_) (_) (_) ');
readln;
writeln('You won because everyone deserves to win sometimes');
readln;
end;
533: begin
writeln('You will be able to see your enemy boat!!!');
readln;
clrscr;
showptable;
writeln;
//colocaçao de peça do jogador
writeln('Choose a line between A, B or C for your Boat');
repeat
readln(x);
test1:=true;
case x of
'A':plc1:=2;
'B':plc1:=3;
'C':plc1:=4;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
writeln('Choose a collum D, E or F for your Boat');
repeat
readln(y);
test1:=true;
case y of
'D':plc2:=2;
'E':plc2:=3;
'F':plc2:=4;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
mplayer[plc1,plc2]:='#';
readln;
writeln(user,'´s boat (#) has been placed');
writeln;
showptable;
readln;
clrscr;
//next part
//procedimento de colocaçao da peça do computador
randomize;
rpx:=random(3)+2;
rpy:=random(3)+2;
mcom[rpx,rpy]:='x';
writeln('The computer has placed his boat');
readln;
clrscr;
//next part
test4:=false;
jog:=0;
//IMPORTANT
repeat
ciclecount;
writeln;
showctable;
//ataque do jogador
writeln;
writeln('Choose a line between A, B or C to launch a missile on the enemy boat');
repeat
readln(x);
test1:=true;
case x of
'A':h:=2;
'B':h:=3;
'C':h:=4;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
writeln;
writeln;
writeln('Choose the collum D, E or F of the missile launch');
repeat
readln(y);
test1:=true;
case y of
'D':l:=2;
'E':l:=3;
'F':l:=4;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
readln;
clrscr;
//confirmaçao de embate
Mresults[h,l]:='x';
If Mcom[h,l]=mcom[rpx,rpy] then
begin
test4:=true;
clrscr;
writeln('__ _____ _ _ __ _____ _ _ _ _ _ ');
writeln('\ \ / / _ \| | | | \ \ / / _ \| \ | | | | | | | | ');
writeln(' \ V / | | | | | | \ \ /\ / / | | | \| | | | | | | | ');
writeln(' | || |_| | |_| | \ V V /| |_| | |\ | |_| |_| |_| ');
writeln(' |_| \___/ \___/ \_/\_/ \___/|_| \_| (_) (_) (_) ');
readln;
writeln('You won ',user,', in only ',jog,' rounds');
readln;
end
else
begin
writeln('You failed the missile attack an (x) was placed where you failed!!!');
readln;
clrscr;
writeln('Computer attacking...');
readln;
//next part
//ataque do computador
randomize;
r:=random(3)+2;
t:=random(3)+2;
//confirmaçao de embate
If mplayer[r,t]=mplayer[plc1,plc2] then
begin
test4:=true;
clrscr;
writeln('__ _____ _ _ _ ___ ____ _____ __ ');
writeln('\ \ / / _ \| | | | | | / _ \/ ___|_ _| _ / / ');
writeln(' \ V / | | | | | | | | | | | \___ \ | | (_) | | ');
writeln(' | || |_| | |_| | | |___ |_| |___) || | _ | | ');
writeln(' |_| \___/ \___/ |_____\___/|____/ |_| (_) | | ');
writeln(' \_\ ');
readln;
writeln('You lost ',user,', in only ',jog,' rounds');
end
else
begin
writeln('He failed the missile attack!!!');
mplayer[r,t]:='!';
readln;
clrscr;
showptable;
readln;
clrscr;
showrtable;
readln;
clrscr;
end;
End;
until test4=true;
end;
777: begin
createboard2;
writeln('You will play in a 7*7 board!!!');
readln;
clrscr;
showptable2;
writeln;
//colocaçao de peça do jogador
writeln('Choose a line between A, B, C, D, E, F or G for your Boat');
repeat
readln(x);
test1:=true;
case x of
'A':plc1:=2;
'B':plc1:=3;
'C':plc1:=4;
'D':plc1:=5;
'E':plc1:=6;
'F':plc1:=7;
'G':plc1:=8;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
writeln('Choose a collum H, I, J, K, L, M or N for your Boat');
repeat
readln(y);
test1:=true;
case y of
'H':plc2:=2;
'I':plc2:=3;
'J':plc2:=4;
'K':plc2:=5;
'L':plc2:=6;
'M':plc2:=7;
'N':plc2:=8;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
mplayer2[plc1,plc2]:='#';
readln;
clrscr;
writeln(user,'´s boat (#) has been placed');
writeln;
showptable2;
readln;
clrscr;
//next part
//procedimento de colocaçao da peça do computador
randomize;
rpx:=random(7)+2;
rpy:=random(7)+2;
mcom2[rpx,rpy]:='x';
writeln('The computer has placed his boat');
readln;
clrscr;
//next part
test4:=false;
jog:=0;
//IMPORTANT
repeat
ciclecount;
writeln;
showrtable2;
//ataque do jogador
writeln;
writeln('Choose a line between A, B, C, D, E, F or G');
writeln('to launch a missile on the enemy boat:');
repeat
readln(x);
test1:=true;
case x of
'A':h:=2;
'B':h:=3;
'C':h:=4;
'D':h:=5;
'E':h:=6;
'F':h:=7;
'G':h:=8;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
writeln;
writeln;
writeln('Choose the collum (H, I, J, K, L, M or N)');
writeln('of the missile launch:');
repeat
readln(y);
test1:=true;
case y of
'H':l:=2;
'I':l:=3;
'J':l:=4;
'K':l:=5;
'L':l:=6;
'M':l:=7;
'N':l:=8;
else
writeln('Wrong keyword'); test1:=false;
end;
until test1=true;
readln;
clrscr;
//confirmaçao de embate
Mresults2[h,l]:='x';
If Mcom2[h,l]=mcom2[rpx,rpy] then
begin
test4:=true;
clrscr;
writeln('__ _____ _ _ __ _____ _ _ _ _ _ ');
writeln('\ \ / / _ \| | | | \ \ / / _ \| \ | | | | | | | | ');
writeln(' \ V / | | | | | | \ \ /\ / / | | | \| | | | | | | | ');
writeln(' | || |_| | |_| | \ V V /| |_| | |\ | |_| |_| |_| ');
writeln(' |_| \___/ \___/ \_/\_/ \___/|_| \_| (_) (_) (_) ');
readln;
writeln('You won ',user,', in only ',jog,' rounds');
readln;
end
else
begin
writeln('You failed the missile attack an (x) was placed where you failed!!!');
readln;
clrscr;
writeln('Computer attacking...');
readln;
//next part
//ataque do computador
randomize;
r:=random(7)+2;
t:=random(7)+2;
//confirmaçao de embate
If mplayer2[r,t]=mplayer2[plc1,plc2] then
begin
test4:=true;
clrscr;
writeln('__ _____ _ _ _ ___ ____ _____ __ ');
writeln('\ \ / / _ \| | | | | | / _ \/ ___|_ _| _ / / ');
writeln(' \ V / | | | | | | | | | | | \___ \ | | (_) | | ');
writeln(' | || |_| | |_| | | |___ |_| |___) || | _ | | ');
writeln(' |_| \___/ \___/ |_____\___/|____/ |_| (_) | | ');
writeln(' \_\ ');
readln;
writeln('You lost ',user,', in only ',jog,' rounds');
end
else
begin
writeln('He failed the missile attack!!!');
mplayer2[r,t]:='!';
readln;
clrscr;
showptable2;
readln;
clrscr;
showrtable2;
readln;
clrscr;
end;
End;
until test4=true;
end;
233: begin
clrscr;
writeln('THIS CHEAT CODE DOES......NOTHING!');
readln;
end;
9000: begin
clrscr;
writeln('The best minigame ever press enter over 9000!!!!!!');
minic:=0;
repeat
minigame;
readln;
until minic=9000;
writeln('You did it, you actually did it congratulations!');
end;
end;
end;
4:test5:=true;
else
test3:=false
end;
until test3=true
until test5=true
End.