Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROGRAM JogoDaVelha;
- USES crt;
- VAR
- tabuleiro : array[1..3, 1..3] of char;
- teste : array[1..8] of boolean;
- colunas : set of char;
- i,
- j,
- linha,
- repetir,
- score_1,
- score_2,
- coluna_1 : integer;
- coluna : char;
- vitoria,
- valido : boolean;
- procedure exibeGrade();
- begin
- writeln(' A B C ');
- write('1 ', tabuleiro[1,1]);
- write(' | ', tabuleiro[1,2]);
- write(' | ', tabuleiro[1,3]);
- writeln(' 1');
- writeln(' ---+---+--- ');
- write('2 ', tabuleiro[2,1]);
- write(' | ', tabuleiro[2,2]);
- write(' | ', tabuleiro[2,3]);
- writeln(' 2');
- writeln(' ---+---+--- ');
- write('3 ', tabuleiro[3,1]);
- write(' | ', tabuleiro[3,2]);
- write(' | ', tabuleiro[3,3]);
- writeln(' 3');
- writeln(' A B C ');
- end;
- {procedure zeraTabuleiro();
- begin
- for i := 1 to 3 do
- for j := 1 to 3 do
- tabuleiro[i, j] := 9;
- end;
- }
- procedure vezJogador_1();
- begin
- writeln(' ');
- writeln('Jogador 1, entre com a linha: ');
- readln(linha);
- repeat
- if ((linha <> 1) and (linha <> 2) and (linha <> 3)) then
- begin
- valido := false;
- writeln('Valor invalido, insira novamente: ');
- read(linha);
- end
- else valido := true;
- until valido;
- writeln(' ');
- writeln('Jogador 1, entre com a coluna: ');
- readln(coluna);
- repeat
- if (coluna in colunas) then
- valido := true
- else begin
- valido := false;
- writeln('Valor inválido, insira novamente: ');
- read(coluna);
- end
- until valido;
- if ((coluna = 'A') or (coluna = 'a')) then
- coluna_1 := 1;
- if ((coluna = 'B') or (coluna = 'b')) then
- coluna_1 := 2;
- if ((coluna = 'C') or (coluna = 'c')) then
- coluna_1 := 3;
- tabuleiro[linha, coluna_1] := '1';
- end;
- procedure vezJogador_2();
- begin
- writeln(' ');
- writeln('Jogador 2, entre com a linha: ');
- readln(linha);
- repeat
- if ((linha <> 1) and (linha <> 2) and (linha <> 3)) then
- begin
- valido := false;
- writeln('Valor inválido, insira novamente: ');
- read(linha);
- end
- else valido := true;
- until valido;
- writeln(' ');
- writeln('Jogador 2, entre com a coluna: ');
- readln(coluna);
- repeat
- if (coluna in colunas) then
- valido := true
- else begin
- valido := false;
- writeln('Valor inválido, insira novamente: ');
- read(coluna);
- end
- until valido;
- if ((coluna = 'A') or (coluna = 'a')) then
- coluna_1 := 1;
- if ((coluna = 'B') or (coluna = 'b')) then
- coluna_1 := 2;
- if ((coluna = 'C') or (coluna = 'c')) then
- coluna_1 := 3;
- tabuleiro[linha, coluna_1] := '2';
- end;
- function vitoriaJogador_1() : boolean;
- begin
- for i := 1 to 3 do
- begin
- if tabuleiro[1, i] = '1' then
- teste[1] := true
- else
- teste[1] := false;
- if tabuleiro[2, i] = '1' then
- teste[2] := true
- else
- teste[2] := false;
- if tabuleiro[3, i] = '1' then
- teste[3] := true
- else
- teste[3] := false;
- if tabuleiro[i, 1] = '1' then
- teste[4] := true
- else
- teste[4] := false;
- if tabuleiro[i, 2] = '1' then
- teste[5] := true
- else
- teste[5] := false;
- if tabuleiro[i, 3] = '1' then
- teste[6] := true
- else
- teste[6] := false;
- if tabuleiro[i, i] = '1' then
- teste[7] := true
- else
- teste[7] := false;
- end;
- for i:= 1 to 3 do
- for j := 3 downto 1 do
- if tabuleiro[i, j] = '1' then
- teste[8] := true
- else
- teste[8] := false;
- for i := 1 to 8 do
- if teste[i] = true then
- begin
- vitoriaJogador_1 := true;
- break;
- end
- end;
- function vitoriaJogador_2() : boolean;
- begin
- for i := 1 to 3 do
- begin
- if tabuleiro[1, i] = '2' then
- teste[1] := true
- else
- teste[1] := false;
- if tabuleiro[2, i] = '2' then
- teste[2] := true
- else
- teste[2] := false;
- if tabuleiro[3, i] = '2' then
- teste[3] := true
- else
- teste[3] := false;
- if tabuleiro[i, 1] = '2' then
- teste[4] := true
- else
- teste[4] := false;
- if tabuleiro[i, 2] = '2' then
- teste[5] := true
- else
- teste[5] := false;
- if tabuleiro[i, 3] = '2' then
- teste[6] := true
- else
- teste[6] := false;
- if tabuleiro[i, i] = '2' then
- teste[7] := true
- else
- teste[7] := false;
- end;
- for i:= 1 to 3 do
- for j := 3 downto 1 do
- if tabuleiro[i, j] = '2' then
- teste[8] := true
- else
- teste[8] := false;
- for i := 1 to 8 do
- if teste[i] = true then
- begin
- vitoriaJogador_2 := true;
- break;
- end
- end;
- procedure jogo();
- begin
- //zeraTabuleiro();
- vitoria := false;
- writeln(' ');
- writeln('Placar: ');
- writeln(' Jogador 1: ', score_1);
- writeln(' Jogador 2: ', score_2);
- while not(vitoria) do
- begin
- clrscr;
- exibeGrade();
- vezJogador_1();
- vitoria := vitoriaJogador_1();
- if vitoria then
- begin
- writeln('Jogador 1 venceu.');
- repeat
- writeln(' ');
- writeln('Deseja jogar novamente? [1 - sim / 2 - nao]');
- readln(repetir);
- if repetir = 1 then
- begin
- score_1 := score_1 + 1;
- jogo();
- end
- else
- if repetir = 2 then
- break
- else
- begin
- writeln('Valor inválido! [1 - sim / 2 - nao]');
- readln(repetir);
- end
- until ((repetir = 1) or (repetir = 2));
- end;
- clrscr;
- exibeGrade();
- vezJogador_2();
- vitoria := vitoriaJogador_2();
- if vitoria then
- begin
- writeln('Jogador 2 venceu.');
- repeat
- writeln('Deseja jogar novamente? [1 - sim / 2 - nao]');
- readln(repetir);
- if repetir = 1 then
- begin
- score_2 := score_2 + 1;
- jogo();
- end
- else
- if repetir = 2 then
- break
- else
- begin
- writeln('Valor inválido! [1 - sim / 2 - nao]');
- readln(repetir);
- end
- until ((repetir = 1) or (repetir = 2))
- end
- end
- end;
- BEGIN
- score_1 := 0;
- score_2 := 0;
- colunas := ['A', 'a', 'B', 'b', 'C', 'c'];
- jogo();
- END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement