Advertisement
sandrovieira

Jogo de Damas (Draughts Game) WIP on C#, for DOS

Feb 19th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Jogo_de_Damas
  8. {
  9.     class Program
  10.     {
  11.         static string Cvr(int x)                        // Função para converssão dos números do vetor em peças e espaços.
  12.         {
  13.             string y = "";
  14.             if (x == 1)
  15.                 y = "(  )";
  16.             if (x == 3)
  17.                 y = "(##)";
  18.             if (x == 0)
  19.                 y = "    ";
  20.  
  21.             return y;
  22.         }
  23.         static bool Permitido(string x)                 // Verifica se o caractere inserido pelo jogador é permitido
  24.         {
  25.             switch (x)
  26.             {
  27.                 case "a1":
  28.                 case "A1":
  29.                 case "a2":
  30.                 case "A2":
  31.                 case "a3":
  32.                 case "A3":
  33.                 case "a4":
  34.                 case "A4":
  35.                 case "a5":
  36.                 case "A5":
  37.                 case "a6":
  38.                 case "A6":
  39.                 case "a7":
  40.                 case "A7":
  41.                 case "A8":
  42.                 case "a8":
  43.                 case "b1":
  44.                 case "B1":
  45.                 case "b2":
  46.                 case "B2":
  47.                 case "b3":
  48.                 case "B3":
  49.                 case "b4":
  50.                 case "B4":
  51.                 case "b5":
  52.                 case "B5":
  53.                 case "b6":
  54.                 case "B6":
  55.                 case "b7":
  56.                 case "B7":
  57.                 case "b8":
  58.                 case "B8":
  59.                 case "c1":
  60.                 case "C1":
  61.                 case "c2":
  62.                 case "C2":
  63.                 case "c3":
  64.                 case "C3":
  65.                 case "c4":
  66.                 case "C4":
  67.                 case "c5":
  68.                 case "C5":
  69.                 case "c6":
  70.                 case "C6":
  71.                 case "c7":
  72.                 case "C7":
  73.                 case "c8":
  74.                 case "C8":
  75.                 case "d1":
  76.                 case "D1":
  77.                 case "d2":
  78.                 case "D2":
  79.                 case "d3":
  80.                 case "D3":
  81.                 case "d4":
  82.                 case "D4":
  83.                 case "d5":
  84.                 case "D5":
  85.                 case "d6":
  86.                 case "D6":
  87.                 case "d7":
  88.                 case "D7":
  89.                 case "D8":
  90.                 case "d8":
  91.                 case "e1":
  92.                 case "E1":
  93.                 case "e2":
  94.                 case "E2":
  95.                 case "e3":
  96.                 case "E3":
  97.                 case "e4":
  98.                 case "E4":
  99.                 case "e5":
  100.                 case "E5":
  101.                 case "e6":
  102.                 case "E6":
  103.                 case "e7":
  104.                 case "E7":
  105.                 case "E8":
  106.                 case "e8":
  107.                 case "f1":
  108.                 case "F1":
  109.                 case "f2":
  110.                 case "F2":
  111.                 case "f3":
  112.                 case "F3":
  113.                 case "f4":
  114.                 case "F4":
  115.                 case "f5":
  116.                 case "F5":
  117.                 case "f6":
  118.                 case "F6":
  119.                 case "f7":
  120.                 case "F7":
  121.                 case "f8":
  122.                 case "F8":
  123.                 case "g1":
  124.                 case "G1":
  125.                 case "g2":
  126.                 case "G2":
  127.                 case "g3":
  128.                 case "G3":
  129.                 case "g4":
  130.                 case "G4":
  131.                 case "g5":
  132.                 case "G5":
  133.                 case "g6":
  134.                 case "G6":
  135.                 case "g7":
  136.                 case "G7":
  137.                 case "g8":
  138.                 case "G8":
  139.                 case "h1":
  140.                 case "H1":
  141.                 case "h2":
  142.                 case "H2":
  143.                 case "h3":
  144.                 case "H3":
  145.                 case "h4":
  146.                 case "H4":
  147.                 case "h5":
  148.                 case "H5":
  149.                 case "h6":
  150.                 case "H6":
  151.                 case "h7":
  152.                 case "H7":
  153.                 case "H8":
  154.                 case "h8":
  155.                     return true;
  156.                 default:
  157.                     return false;
  158.  
  159.             }
  160.  
  161.         }
  162.         static string ConversorCoordenada(string x)     // Converte a número, para ser usado no sistema da matriz
  163.         {
  164.             string y = "";
  165.             if (x[0] == 'a' || x[0] == 'A')
  166.             {
  167.                 y = "1" + x[1];
  168.             }
  169.             if (x[0] == 'b' || x[0] == 'B')
  170.             {
  171.                 y = "2" + x[1];
  172.             }
  173.             if (x[0] == 'c' || x[0] == 'C')
  174.             {
  175.                 y = "3" + x[1];
  176.             }
  177.             if (x[0] == 'd' || x[0] == 'D')
  178.             {
  179.                 y = "4" + x[1];
  180.             }
  181.             if (x[0] == 'e' || x[0] == 'E')
  182.             {
  183.                 y = "5" + x[1];
  184.             }
  185.             if (x[0] == 'f' || x[0] == 'F')
  186.             {
  187.                 y = "6" + x[1];
  188.             }
  189.             if (x[0] == 'g' || x[0] == 'G')
  190.             {
  191.                 y = "7" + x[1];
  192.             }
  193.             if (x[0] == 'h' || x[0] == 'H')
  194.             {
  195.                 y = "8" + x[1];
  196.             }
  197.             return y;
  198.         }
  199.         static void Main(string[] args)
  200.         {
  201.             int fim_de_jogo = 0;                        // Variável que determina o fim do jogo
  202.             int[,] tab = new int[10, 10];               // Matriz para o tabuleiro e peças
  203.             string escolha_peca = "";                   // Variável que armazena a escolha de peça
  204.             string movimentacao_peca = "";              // Variável que armazena o local que o jogador decide movimentá-la
  205.             bool varredura = true;                      // Variável que dita se há a possibilidade de jogada em cadeia
  206.             bool jogador1 = true;
  207.             bool jogada = false;
  208.             int x1 = 0, x2 = 0;
  209.             int y1 = 0, y2 = 0;
  210.  
  211.  
  212.             #region Posicionamento inicial das peças no Tabuleiro
  213.  
  214.             for (int i = 0; i < 10; i++)
  215.             {
  216.                 tab[i, 0] = 99;
  217.                 tab[i, 9] = 99;
  218.                 tab[0, i] = 99;
  219.                 tab[9, i] = 99;
  220.             }
  221.             for (int l = 1; l <= 8; l++)
  222.             {
  223.                 if (l <= 3 || l >= 6)
  224.                 {
  225.                     for (int c = 1; c <= 8; c++)
  226.                     {
  227.                         if (l % 2 == 0)
  228.                         {
  229.                             if (c % 2 == 1)
  230.                             {
  231.                                 if (l <= 2)
  232.                                     tab[l, c] = 1;
  233.                                 if (l >= 5)
  234.                                     tab[l, c] = 3;
  235.                             }
  236.                         }
  237.                         else
  238.                         {
  239.                             if (c % 2 == 0)
  240.                             {
  241.                                 if (l <= 3)
  242.                                     tab[l, c] = 1;
  243.                                 if (l >= 6)
  244.                                     tab[l, c] = 3;
  245.                             }
  246.                         }
  247.                     }
  248.                 }
  249.             }
  250.             #endregion
  251.  
  252.             for (int i = 0; i < 10; i++)
  253.             {
  254.                 for (int j = 0; j < 10; j++)
  255.                 {
  256.                     Console.Write(tab[i, j] + "\t");
  257.                 }
  258.                 Console.WriteLine();
  259.             }
  260.  
  261.             while (fim_de_jogo == 0)        // Estrutura de repetição para o jogo
  262.             {
  263.                 # region Reperesentação Gráfica do Tabuleiro
  264.                 Console.WriteLine("\n         A      B      C      D     E      F      G      H\n");
  265.                 Console.WriteLine("     .------.------.------.------.------.------.------.------.");
  266.                 Console.WriteLine(" 1   | {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} |", Cvr(tab[1, 1]), Cvr(tab[1, 2]), Cvr(tab[1, 3]), Cvr(tab[1, 4]), Cvr(tab[1, 5]), Cvr(tab[1, 6]), Cvr(tab[1, 7]), Cvr(tab[1, 8]));
  267.                 Console.WriteLine("     |------|------|------|------|------|------|------|------|");
  268.                 Console.WriteLine(" 2   | {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} |", Cvr(tab[2, 1]), Cvr(tab[2, 2]), Cvr(tab[2, 3]), Cvr(tab[2, 4]), Cvr(tab[2, 5]), Cvr(tab[2, 6]), Cvr(tab[2, 7]), Cvr(tab[2, 8]));
  269.                 Console.WriteLine("     |------|------|------|------|------|------|------|------|");
  270.                 Console.WriteLine(" 3   | {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} |", Cvr(tab[3, 1]), Cvr(tab[3, 2]), Cvr(tab[3, 3]), Cvr(tab[3, 4]), Cvr(tab[3, 5]), Cvr(tab[3, 6]), Cvr(tab[3, 7]), Cvr(tab[3, 8]));
  271.                 Console.WriteLine("     |------|------|------|------|------|------|------|------|");
  272.                 Console.WriteLine(" 4   | {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} |", Cvr(tab[4, 1]), Cvr(tab[4, 2]), Cvr(tab[4, 3]), Cvr(tab[4, 4]), Cvr(tab[4, 5]), Cvr(tab[4, 6]), Cvr(tab[4, 7]), Cvr(tab[4, 8]));
  273.                 Console.WriteLine("     |------|------|------|------|------|------|------|------|");
  274.                 Console.WriteLine(" 5   | {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} |", Cvr(tab[5, 1]), Cvr(tab[5, 2]), Cvr(tab[5, 3]), Cvr(tab[5, 4]), Cvr(tab[5, 5]), Cvr(tab[5, 6]), Cvr(tab[5, 7]), Cvr(tab[5, 8]));
  275.                 Console.WriteLine("     |------|------|------|------|------|------|------|------|");
  276.                 Console.WriteLine(" 6   | {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} |", Cvr(tab[6, 1]), Cvr(tab[6, 2]), Cvr(tab[6, 3]), Cvr(tab[6, 4]), Cvr(tab[6, 5]), Cvr(tab[6, 6]), Cvr(tab[6, 7]), Cvr(tab[6, 8]));
  277.                 Console.WriteLine("     |------|------|------|------|------|------|------|------|");
  278.                 Console.WriteLine(" 7   | {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} |", Cvr(tab[7, 1]), Cvr(tab[7, 2]), Cvr(tab[7, 3]), Cvr(tab[7, 4]), Cvr(tab[7, 5]), Cvr(tab[7, 6]), Cvr(tab[7, 7]), Cvr(tab[7, 8]));
  279.                 Console.WriteLine("     |------|------|------|------|------|------|------|------|");
  280.                 Console.WriteLine(" 8   | {0} | {1} | {2} | {3} | {4} | {5} | {6} | {7} |", Cvr(tab[8, 1]), Cvr(tab[8, 2]), Cvr(tab[8, 3]), Cvr(tab[8, 4]), Cvr(tab[8, 5]), Cvr(tab[8, 6]), Cvr(tab[8, 7]), Cvr(tab[8, 8]));
  281.                 Console.WriteLine("     `------'------'------'------'------'------'------'------'\n\n");
  282.  
  283.                 #endregion
  284.                 if (jogador1)
  285.                 {
  286.                     Console.ForegroundColor = ConsoleColor.Yellow;
  287.                     Console.WriteLine("Turno do jogador 1.");
  288.                     Console.ForegroundColor = ConsoleColor.Gray;
  289.                 }
  290.                 else
  291.                 {
  292.                     Console.ForegroundColor = ConsoleColor.Cyan;
  293.                     Console.WriteLine("Turno do jogador 2.");
  294.                     Console.ForegroundColor = ConsoleColor.Gray;
  295.                 }
  296.                 Console.Write("Escolha uma peça para mover: ");
  297.                 escolha_peca = Console.ReadLine();
  298.                 if (Permitido(escolha_peca))
  299.                 {
  300.                     Console.Write("Decida a nova posição para a peça: ");
  301.                     movimentacao_peca = Console.ReadLine();
  302.                     if (Permitido(movimentacao_peca))
  303.                     {
  304.                         // Converssão das letras para numerais para serem usados nos índices da matriz
  305.  
  306.                         escolha_peca = ConversorCoordenada(escolha_peca);
  307.                         movimentacao_peca = ConversorCoordenada(movimentacao_peca);
  308.  
  309.  
  310.                         // Condicional: Movimentação Jogador 1
  311.  
  312.                         if (jogador1)
  313.                         {
  314.                             // Inverssão de posicionamento para o sistema de coordenada
  315.  
  316.                             y1 = Convert.ToInt16(escolha_peca) / 10;
  317.                             x1 = Convert.ToInt16(escolha_peca) % 10;
  318.  
  319.                             // Condicional: A primeira coordenada dita a localização duma peça do jogador 1?
  320.  
  321.                             if (tab[x1, y1] == 1 || tab[x1, y1] == 11)
  322.                             {
  323.  
  324.                                 // Inverssão de posicionamento para o sistema de coordenada
  325.  
  326.                                 y2 = Convert.ToInt16(movimentacao_peca) / 10;
  327.                                 x2 = Convert.ToInt16(movimentacao_peca) % 10;
  328.  
  329.  
  330.                                 // Condicional: A segunda coordenada dita a localização de um espaço vazio?
  331.  
  332.                                 if (tab[x2, y2] == 0)
  333.                                 {
  334.                                     // Sequência de Condicionais para Determinar o Tipo de Movimentação
  335.  
  336.                                     // Movimentação Normal
  337.  
  338.                                     if ((x1 + y1 + 2) == (x2 + y2) && (tab[x1, y1] + tab[x2, y2]) == 1 || ((x1 + y1) == (x2 + y2) && (tab[x1, y1] + tab[x2, y2] == 1)))
  339.                                     {
  340.                                         tab[x1, y1] = 0;
  341.                                         tab[x2, y2] = 1;
  342.                                         jogador1 = false;
  343.  
  344.                                     }
  345.  
  346.                                     // Movimentação de ataque simples: Primeiro Quadrante
  347.  
  348.                                     if ((x1 + y1) == (x2 + y2) && (tab[x1 + 2, y1 - 2] + tab[x1 + 1, y1 - 1] + tab[x2, y2]) == 4)
  349.                                     {
  350.                                         tab[x1 + 1, y1 - 1] = 0;
  351.                                         tab[x1, y1] = 0;
  352.                                         tab[x2, y2] = 1;
  353.                                         jogador1 = false;
  354.  
  355.                                     }
  356.  
  357.                                     // Movimentação de ataque simples: Segundo Quadrante
  358.  
  359.                                     if ((x1 + y1 - 4) == (x2 + y2) && (tab[x1, y1] + tab[x1 - 1, y1 - 1] + tab[x2, y2]) == 4)
  360.                                     {
  361.                                         tab[x1 - 1, y1 - 1] = 0;
  362.                                         tab[x1, y1] = 0;
  363.                                         tab[x2, y2] = 1;
  364.                                         jogador1 = false;
  365.  
  366.                                     }
  367.  
  368.                                     // Movimentação de ataque simples: Terceiro Quadrante
  369.  
  370.                                     if ((x1 + y1) == (x2 + y2) && (tab[x1, y1] + tab[x1 + 1, y1 - 1] + tab[x2, y2]) == 4)
  371.                                     {
  372.                                         tab[x1 + 1, y1 - 1] = 0;
  373.                                         tab[x1, y1] = 0;
  374.                                         tab[x2, y2] = 1;
  375.                                         jogador1 = false;
  376.  
  377.                                     }
  378.  
  379.                                     // Movimentação de ataque simples: Quarto Quadrante
  380.  
  381.                                     if ((x1 + y1 + 4) == (x2 + y2) && (tab[x1, y1] + tab[x1 + 1, y1 + 1] + tab[x2, y2]) == 4)
  382.                                     {
  383.                                         tab[x1 + 1, y1 + 1] = 0;
  384.                                         tab[x1, y1] = 0;
  385.                                         tab[x2, y2] = 1;
  386.                                         jogador1 = false;
  387.  
  388.                                     }
  389.  
  390.                                     // Caso nenhuma condicional de movimentação seja atendida
  391.                                     if (jogador1)
  392.                                         Console.WriteLine("Movimentação inválida.");
  393.  
  394.                                     // Caso haja a possibilidade de uma jogada em cadeia
  395.  
  396.                                     while (varredura)
  397.                                     {
  398.                                         if (((x1 + y1) == (x2 + y2) && (tab[x1 + 2, y1 - 2] + tab[x1 + 1, y1 - 1] + tab[x2, y2]) == 4) || ((x1 + y1 - 4) == (x2 + y2) && (tab[x1, y1] + tab[x1 - 1, y1 - 1] + tab[x2, y2]) == 4)((x1 + y1) == (x2 + y2) && (tab[x1, y1] + tab[x1 + 1, y1 - 1] + tab[x2, y2]) == 4) || ((x1 + y1 + 4) == (x2 + y2) && (tab[x1, y1] + tab[x1 + 1, y1 + 1] + tab[x2, y2]) == 4))
  399.                                         {
  400.                                             Console.ForegroundColor = ConsoleColor.Green;
  401.                                             Console.WriteLine("Movimentação em cadeia detectada. Continue a jogada.");
  402.                                             Console.ForegroundColor = ConsoleColor.Gray;
  403.                                             jogador1 = true;
  404.                                         }
  405.                                         else
  406.                                         {
  407.                                             varredura = false;
  408.                                         }
  409.                                     }
  410.  
  411.                                 }
  412.                                 else
  413.                                 {
  414.                                     Console.WriteLine("Movimentação inválida.");
  415.                                 }
  416.                             }
  417.                             else
  418.                             {
  419.                                 Console.WriteLine("Movimentação inválida.");
  420.                             }
  421.                         }
  422.                         else
  423.                         {
  424.                             // Movimentaçaõ jogador 2
  425.                             y1 = Convert.ToInt16(escolha_peca) / 10;
  426.                             x1 = Convert.ToInt16(escolha_peca) % 10;
  427.                             if (tab[x1, y1] == 3 || tab[x1, y1] == 33)
  428.                             {
  429.                                 y2 = Convert.ToInt16(movimentacao_peca) / 10;
  430.                                 x2 = Convert.ToInt16(movimentacao_peca) % 10;
  431.                                 if (tab[x2, y2] == 0)
  432.                                 {
  433.  
  434.                                     // ---------------- Movimentação Jogador 2
  435.                                     if ((x1 + y1 - 2) == (x2 + y2) && (tab[x1, y1] + tab[x2, y2]) == 3 || ((x1 + y1) == (x2 + y2) && (tab[x1, y1] + tab[x2, y2] == 3)))
  436.                                     {
  437.                                         tab[x1, y1] = 0;
  438.  
  439.  
  440.                                         tab[x2, y2] = 3;
  441.                                         jogador1 = true;
  442.                                     }
  443.  
  444.  
  445.                                 }
  446.                                 else
  447.                                 {
  448.                                     Console.WriteLine("Movimentação inválida.");
  449.                                 }
  450.                             }
  451.                             else
  452.                             {
  453.                                 Console.WriteLine("Movimentação inválida.");
  454.                             }
  455.  
  456.                         }
  457.  
  458.                     }
  459.                     else
  460.                     {
  461.                         Console.WriteLine("Entrada inválida. Tente novamente.");
  462.                         jogada = false;
  463.                     }
  464.                 }
  465.                 else
  466.                 {
  467.                     Console.WriteLine("Entrada inválida. Tente novamente.");
  468.                     jogada = false;
  469.  
  470.                 }
  471.                 if (jogada)
  472.                 {
  473.  
  474.  
  475.  
  476.                 }
  477.                 Console.ReadKey();
  478.                 Console.Clear();
  479.             }
  480.             Console.ReadKey();
  481.         }
  482.     }
  483. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement