Advertisement
roneygomes

TicTacToe

Jun 15th, 2011
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 7.51 KB | None | 0 0
  1. PROGRAM JogoDaVelha;
  2.  
  3. USES crt;
  4.  
  5. VAR
  6.     tabuleiro : array[1..3, 1..3] of char;
  7.     teste : array[1..8] of boolean;
  8.     colunas : set of char;
  9.  
  10.     i,
  11.     j,
  12.     linha,
  13.     repetir,
  14.     score_1,
  15.     score_2,
  16.     coluna_1 : integer;
  17.  
  18.     coluna : char;
  19.  
  20.     vitoria,
  21.     valido : boolean;
  22.  
  23. procedure exibeGrade();
  24.     begin
  25.         writeln('  A   B   C  ');
  26.         write('1 ', tabuleiro[1,1]);
  27.         write(' | ', tabuleiro[1,2]);
  28.         write(' | ', tabuleiro[1,3]);
  29.         writeln(' 1');
  30.         writeln(' ---+---+--- ');
  31.         write('2 ', tabuleiro[2,1]);
  32.         write(' | ', tabuleiro[2,2]);
  33.         write(' | ', tabuleiro[2,3]);
  34.         writeln(' 2');
  35.         writeln(' ---+---+--- ');
  36.         write('3 ', tabuleiro[3,1]);
  37.         write(' | ', tabuleiro[3,2]);
  38.         write(' | ', tabuleiro[3,3]);
  39.         writeln(' 3');
  40.         writeln('  A   B   C  ');
  41.    end;
  42.  
  43. {procedure zeraTabuleiro();
  44.    begin
  45.         for i := 1 to 3 do
  46.             for j := 1 to 3 do
  47.                 tabuleiro[i, j] := 9;
  48.    end;
  49. }
  50. procedure vezJogador_1();
  51.     begin
  52.         writeln(' ');
  53.         writeln('Jogador 1, entre com a linha: ');
  54.         readln(linha);
  55.  
  56.         repeat
  57.               if ((linha <> 1) and (linha <> 2) and (linha <> 3)) then
  58.               begin
  59.                    valido := false;
  60.                    writeln('Valor invalido, insira novamente: ');
  61.                    read(linha);
  62.               end
  63.  
  64.               else valido := true;
  65.         until valido;
  66.  
  67.         writeln(' ');
  68.         writeln('Jogador 1, entre com a coluna: ');
  69.         readln(coluna);
  70.  
  71.         repeat
  72.               if (coluna in colunas) then
  73.                  valido := true
  74.               else begin
  75.                    valido := false;
  76.                    writeln('Valor inválido, insira novamente: ');
  77.                    read(coluna);
  78.                    end
  79.         until valido;
  80.  
  81.         if ((coluna = 'A') or (coluna = 'a')) then
  82.            coluna_1 := 1;
  83.  
  84.         if ((coluna = 'B') or (coluna = 'b')) then
  85.            coluna_1 := 2;
  86.  
  87.         if ((coluna = 'C') or (coluna = 'c')) then
  88.            coluna_1 := 3;
  89.  
  90.         tabuleiro[linha, coluna_1] := '1';
  91.    end;
  92.  
  93. procedure vezJogador_2();
  94.    begin
  95.         writeln(' ');
  96.         writeln('Jogador 2, entre com a linha: ');
  97.         readln(linha);
  98.  
  99.         repeat
  100.               if ((linha <> 1) and (linha <> 2) and (linha <> 3)) then
  101.               begin
  102.                    valido := false;
  103.                    writeln('Valor inválido, insira novamente: ');
  104.                    read(linha);
  105.               end
  106.  
  107.               else valido := true;
  108.         until valido;
  109.  
  110.         writeln(' ');
  111.         writeln('Jogador 2, entre com a coluna: ');
  112.         readln(coluna);
  113.  
  114.         repeat
  115.               if (coluna in colunas) then
  116.                  valido := true
  117.               else begin
  118.                    valido := false;
  119.                    writeln('Valor inválido, insira novamente: ');
  120.                    read(coluna);
  121.                    end
  122.              
  123.         until valido;
  124.  
  125.         if ((coluna = 'A') or (coluna = 'a')) then
  126.            coluna_1 := 1;
  127.  
  128.         if ((coluna = 'B') or (coluna = 'b')) then
  129.            coluna_1 := 2;
  130.  
  131.         if ((coluna = 'C') or (coluna = 'c')) then
  132.            coluna_1 := 3;
  133.  
  134.         tabuleiro[linha, coluna_1] := '2';
  135.    end;
  136.  
  137.  
  138.  
  139. function vitoriaJogador_1() : boolean;
  140.    begin
  141.         for i := 1 to 3 do
  142.         begin
  143.             if tabuleiro[1, i] = '1' then
  144.                 teste[1] := true
  145.             else
  146.                 teste[1] := false;
  147.                
  148.             if tabuleiro[2, i] = '1' then
  149.                 teste[2] := true
  150.             else
  151.                 teste[2] := false;
  152.                
  153.             if tabuleiro[3, i] = '1' then
  154.                 teste[3] := true
  155.             else
  156.                 teste[3] := false;
  157.            
  158.             if tabuleiro[i, 1] = '1' then
  159.                 teste[4] := true
  160.             else
  161.                 teste[4] := false;
  162.                
  163.             if tabuleiro[i, 2] = '1' then
  164.                 teste[5] := true
  165.             else
  166.                 teste[5] := false;
  167.                
  168.             if tabuleiro[i, 3] = '1' then
  169.                 teste[6] := true
  170.             else
  171.                 teste[6] := false;
  172.            
  173.             if tabuleiro[i, i] = '1' then
  174.                 teste[7] := true
  175.             else
  176.                 teste[7] := false;
  177.              
  178.         end;
  179.  
  180.         for i:= 1 to 3 do
  181.             for j := 3 downto 1 do
  182.                 if tabuleiro[i, j] = '1' then
  183.                     teste[8] := true
  184.                 else
  185.                     teste[8] := false;
  186.  
  187.         for i := 1 to 8 do
  188.             if teste[i] = true then
  189.             begin
  190.                 vitoriaJogador_1 := true;
  191.                 break; 
  192.             end
  193.    end;
  194.  
  195. function vitoriaJogador_2() : boolean;
  196.    begin
  197.         for i := 1 to 3 do
  198.         begin
  199.             if tabuleiro[1, i] = '2' then
  200.                 teste[1] := true
  201.             else
  202.                 teste[1] := false;
  203.                
  204.             if tabuleiro[2, i] = '2' then
  205.                 teste[2] := true
  206.             else
  207.                 teste[2] := false;
  208.                
  209.             if tabuleiro[3, i] = '2' then
  210.                 teste[3] := true
  211.             else
  212.                 teste[3] := false;
  213.            
  214.             if tabuleiro[i, 1] = '2' then
  215.                 teste[4] := true
  216.             else
  217.                 teste[4] := false;
  218.                
  219.             if tabuleiro[i, 2] = '2' then
  220.                 teste[5] := true
  221.             else
  222.                 teste[5] := false;
  223.                
  224.             if tabuleiro[i, 3] = '2' then
  225.                 teste[6] := true
  226.             else
  227.                 teste[6] := false;
  228.            
  229.             if tabuleiro[i, i] = '2' then
  230.                 teste[7] := true
  231.             else
  232.                 teste[7] := false;
  233.              
  234.         end;
  235.  
  236.         for i:= 1 to 3 do
  237.             for j := 3 downto 1 do
  238.                 if tabuleiro[i, j] = '2' then
  239.                     teste[8] := true
  240.                 else
  241.                     teste[8] := false;
  242.  
  243.         for i := 1 to 8 do
  244.             if teste[i] = true then
  245.             begin
  246.                 vitoriaJogador_2 := true;
  247.                 break; 
  248.             end
  249.    end;
  250. procedure jogo();
  251.    begin
  252.         //zeraTabuleiro();
  253.         vitoria := false;
  254.  
  255.         writeln(' ');
  256.         writeln('Placar: ');
  257.         writeln('  Jogador 1: ', score_1);
  258.         writeln('  Jogador 2: ', score_2);
  259.  
  260.         while not(vitoria) do
  261.         begin
  262.              clrscr;
  263.              exibeGrade();
  264.  
  265.              vezJogador_1();
  266.              vitoria := vitoriaJogador_1();
  267.  
  268.              if vitoria then
  269.              begin
  270.                   writeln('Jogador 1 venceu.');
  271.  
  272.                   repeat
  273.                         writeln(' ');
  274.                         writeln('Deseja jogar novamente? [1 - sim / 2 - nao]');
  275.                         readln(repetir);
  276.  
  277.                         if repetir = 1 then
  278.                         begin
  279.                             score_1 := score_1 + 1;
  280.                             jogo();
  281.                         end
  282.                         else
  283.                             if repetir = 2 then
  284.                                 break
  285.                             else
  286.                                 begin
  287.                                     writeln('Valor inválido! [1 - sim / 2 - nao]');
  288.                                     readln(repetir);
  289.                                 end
  290.  
  291.                   until ((repetir = 1) or (repetir = 2));
  292.              end;
  293.  
  294.              clrscr;
  295.              exibeGrade();
  296.              vezJogador_2();
  297.              vitoria := vitoriaJogador_2();
  298.  
  299.              if vitoria then
  300.              begin
  301.                   writeln('Jogador 2 venceu.');
  302.  
  303.                   repeat
  304.                         writeln('Deseja jogar novamente? [1 - sim / 2 - nao]');
  305.                         readln(repetir);
  306.  
  307.                         if repetir = 1 then
  308.                         begin
  309.                             score_2 := score_2 + 1;
  310.                             jogo();
  311.                         end
  312.                         else
  313.                             if repetir = 2 then
  314.                                 break
  315.                             else
  316.                                 begin
  317.                                     writeln('Valor inválido! [1 - sim / 2 - nao]');
  318.                                     readln(repetir);
  319.                                 end
  320.  
  321.                   until ((repetir = 1) or (repetir = 2))
  322.              end
  323.         end
  324.     end;
  325.  
  326. BEGIN
  327.      score_1 := 0;
  328.      score_2 := 0;
  329.      colunas := ['A', 'a', 'B', 'b', 'C', 'c'];
  330.      jogo();
  331. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement