Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.60 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. using System.IO;
  7.  
  8. namespace ConsoleApplication2
  9. {
  10.     class Program
  11.     {
  12.        static public string[,] tab_wyn = new string[3, 50];
  13.         static void Main(string[] args)
  14.         {
  15.             string plik = "wyniki.txt";
  16.             int i = 0, j = 0;
  17.             foreach (string linia in File.ReadLines(plik))
  18.             {
  19.                 if (i < 3)
  20.                 {
  21.                     tab_wyn[j, i] = linia;
  22.                 }
  23.                 else
  24.                 {
  25.                     i = 0;
  26.                     j++;
  27.                     tab_wyn[j, i] = linia;
  28.                 }
  29.                 i++;
  30.             }
  31.             Menu();
  32.         }
  33.         static public void Menu()
  34.         {
  35.             int wybor = 0;
  36.             Console.Clear();
  37.             Console.WriteLine("Gra Connect 4\n");
  38.             Console.WriteLine("MENU:");
  39.             Console.WriteLine("1. Nowa Gra");
  40.             Console.WriteLine("2. Wyniki");
  41.             Console.WriteLine("3. Informacje o autorze");
  42.             Console.WriteLine("4. Wyjście\n");
  43.             Console.WriteLine("Wybierz opcję z menu:");
  44.             try
  45.             {
  46.                 wybor = Convert.ToInt32(Console.ReadLine());
  47.             }
  48.             catch (Exception)
  49.             {
  50.                 Console.WriteLine("\nNie podales liczby!");
  51.             }
  52.  
  53.             switch (wybor)
  54.             {
  55.                 case 1:
  56.                     Gra();
  57.                     break;
  58.  
  59.                 case 2:
  60.                     Tabela_Wyniki();
  61.                     break;
  62.                 case 3:
  63.                     Autor();
  64.                     break;
  65.                 case 4:
  66.                     Koniec();
  67.                     break;
  68.                 default:
  69.                     Console.WriteLine("\nProszę wybrac  opcję z menu od 1 do 4");
  70.                     Console.ReadLine();
  71.                     Menu();
  72.                     break;
  73.             }
  74.         }
  75.  
  76.         static public void Gra()
  77.         {
  78.             string Gracz1, Gracz2;
  79.             Console.Clear();
  80.             Console.WriteLine("Nowa Gra\n");
  81.             Console.WriteLine("Podaj nazwę  gracza  1:");
  82.             Gracz1 = Console.ReadLine();
  83.             Console.WriteLine("Podaj nazwę gracza 2:");
  84.             Gracz2 = Console.ReadLine();
  85.             Console.WriteLine("Podaj wymiar planszy:");
  86.             int wymiar = Convert.ToInt32(Console.ReadLine());
  87.             Console.Clear();
  88.             int[,] plansza = new int[wymiar, wymiar];
  89.             int[] tablica = new int[wymiar];
  90.             for (int i = 0; i < wymiar; i++)
  91.             {
  92.                 for (int j = 0; j < wymiar; j++)
  93.                 {
  94.                     plansza[i, j] = 0;
  95.                     tablica[i] = 0;
  96.                 }
  97.             }
  98.  
  99.             bool koniec = false;
  100.             while (!koniec)
  101.             {
  102.                 Console.Clear();
  103.  
  104.                 for (int k = 0; k < wymiar; k++)
  105.                 {
  106.                     Console.Write(k + " ");
  107.                 }
  108.                 Console.Write("\n");
  109.                 for (int k = 0; k < wymiar; k++)
  110.                 {
  111.                     Console.Write("--");
  112.                 }
  113.                 Console.Write("\n");
  114.  
  115.                 for (int i = 0; i < wymiar; i++)
  116.                 {
  117.                     for (int j = 0; j < wymiar; j++)
  118.                     {
  119.                         Console.Write(plansza[j, i] + " ");
  120.                     }
  121.                     Console.Write("\n");
  122.                 }
  123.  
  124.                 bool licz = false;
  125.                 int x1 = 0;
  126.  
  127.                 while (!licz)
  128.                 {
  129.                     Console.WriteLine("Ruch gracza " + Gracz1 + ". Podaj współrzędną:");
  130.                     x1 = Convert.ToInt32(Console.ReadLine());
  131.                     if (x1 < 0)
  132.                     {
  133.                         licz = true;
  134.                     }
  135.                     else
  136.                     {
  137.                         Console.WriteLine("Ruch gracza " + Gracz1 + ". Podaj współrzędną:");
  138.                         x1 = Convert.ToInt32(Console.ReadLine());
  139.                     }
  140.                 }
  141.  
  142.  
  143.  
  144.  
  145.                 //szukanie pierwszego wolnego miejsca w kol
  146.                 bool zajete = false;
  147.                 bool obszar = false;
  148.                 int y = 0;
  149.  
  150.                     while (!obszar)
  151.                     {
  152.                         if (x1 >= wymiar || tablica[x1] >= wymiar)
  153.                         {
  154.                             Console.WriteLine("Podałeś współrzędną oo zbyt dużej wartości");
  155.                             Console.WriteLine("Ruch gracza " + Gracz1 + ". Podaj współrzędną:");
  156.                             try
  157.                             {
  158.                                 x1 = Convert.ToInt32(Console.ReadLine());
  159.                             }
  160.                             catch (Exception)
  161.                             {
  162.                                 Console.WriteLine("\nNie podales liczby!");
  163.                             }
  164.                         }
  165.                         else obszar = true;
  166.                     }
  167.                 while (!zajete)
  168.                 {
  169.                     if (plansza[x1, y] != 0)
  170.                     {
  171.                         plansza[x1, y - 1] = 1;
  172.                         zajete = true;
  173.                         tablica[x1] += 1;
  174.                         continue;
  175.                     }
  176.                     else if (plansza[x1, y] == 0 && y == wymiar - 1)
  177.                     {
  178.                         plansza[x1, y] = 1;
  179.                         zajete = true;
  180.                         tablica[x1] += 1;
  181.                         continue;
  182.                     }
  183.                     y++;
  184.                 }
  185.  
  186.  
  187.                 int liczba = 0;
  188.                 if (liczba < 4)
  189.                 {
  190.                     //poziomo
  191.                     liczba =
  192.                         ((x1 - 1 >= 0) ? plansza[x1 - 1, y] == 1 ? 1 : 0 : 0) +
  193.                         ((x1 - 2 >= 0) ? plansza[x1 - 2, y] == 1 ? 1 : 0 : 0) +
  194.                         ((x1 - 3 >= 0) ? plansza[x1 - 3, y] == 1 ? 1 : 0 : 0) +
  195.                         ((x1 + 1 < wymiar) ? plansza[x1 + 1, y] == 1 ? 1 : 0 : 0) +
  196.                         ((x1 + 2 < wymiar) ? plansza[x1 + 2, y] == 1 ? 1 : 0 : 0) +
  197.                         ((x1 + 3 < wymiar) ? plansza[x1 + 3, y] == 1 ? 1 : 0 : 0) + 1;
  198.                 }
  199.                 if (liczba < 4)
  200.                 {
  201.                     //pionowo
  202.                     liczba =
  203.                         ((y - 1 >= 0) ? plansza[x1, y - 1] == 1 ? 1 : 0 : 0) +
  204.                         ((y - 2 >= 0) ? plansza[x1, y - 2] == 1 ? 1 : 0 : 0) +
  205.                         ((y - 3 >= 0) ? plansza[x1, y - 3] == 1 ? 1 : 0 : 0) +
  206.                         ((y + 1 < wymiar) ? plansza[x1, y + 1] == 1 ? 1 : 0 : 0) +
  207.                         ((y + 2 < wymiar) ? plansza[x1, y + 2] == 1 ? 1 : 0 : 0) +
  208.                         ((y + 3 < wymiar) ? plansza[x1, y + 3] == 1 ? 1 : 0 : 0) + 1;
  209.                 }
  210.                 if (liczba < 4)
  211.                 {
  212.                     // skos /
  213.                     liczba =
  214.                         ((x1 - 1 >= 0 && y - 1 >= 0) ? plansza[x1 - 1, y - 1] == 1 ? 1 : 0 : 0) +
  215.                         ((x1 - 2 >= 0 && y - 2 >= 0) ? plansza[x1 - 2, y - 2] == 1 ? 1 : 0 : 0) +
  216.                         ((x1 - 3 >= 0 && y - 3 >= 0) ? plansza[x1 - 3, y - 3] == 1 ? 1 : 0 : 0) +
  217.                         ((x1 + 1 < wymiar && y + 1 < wymiar) ? plansza[x1 + 1, y + 1] == 1 ? 1 : 0 : 0) +
  218.                         ((x1 + 2 < wymiar && y + 2 < wymiar) ? plansza[x1 + 2, y + 2] == 1 ? 1 : 0 : 0) +
  219.                         ((x1 + 3 < wymiar && y + 3 < wymiar) ? plansza[x1 + 3, y + 3] == 1 ? 1 : 0 : 0) + 1;
  220.                 }
  221.                 if (liczba < 4)
  222.                 {
  223.                     // skos \
  224.                     liczba =
  225.                         ((x1 - 1 >= 0 && y + 1 < wymiar) ? plansza[x1 - 1, y + 1] == 1 ? 1 : 0 : 0) +
  226.                         ((x1 - 2 >= 0 && y + 2 < wymiar) ? plansza[x1 - 2, y + 2] == 1 ? 1 : 0 : 0) +
  227.                         ((x1 - 3 >= 0 && y + 3 < wymiar) ? plansza[x1 - 3, y + 3] == 1 ? 1 : 0 : 0) +
  228.                         ((x1 + 1 < wymiar && y - 1 >= 0) ? plansza[x1 + 1, y - 1] == 1 ? 1 : 0 : 0) +
  229.                         ((x1 + 2 < wymiar && y - 2 >= 0) ? plansza[x1 + 2, y - 2] == 1 ? 1 : 0 : 0) +
  230.                         ((x1 + 3 < wymiar && y - 3 >= 0) ? plansza[x1 + 3, y - 3] == 1 ? 1 : 0 : 0) + 1;
  231.                 }
  232.                 if (liczba >= 4)
  233.                 {
  234.                     Console.WriteLine("wygral gracz " + Gracz1);
  235.                     Console.ReadLine();
  236.  
  237.                     //obsluga wynikow itp
  238.  
  239.  
  240.  
  241.                     koniec = true;
  242.                     continue;
  243.                 }
  244.  
  245.  
  246.  
  247.                 //////// gracz 2 //////////////////
  248.  
  249.                 Console.WriteLine("Ruch gracza " + Gracz2 + ". Podaj współrzędną:");
  250.                 x1 = Convert.ToInt32(Console.ReadLine());
  251.  
  252.                 //szukanie pierwszego wolnego miejsca w kol
  253.                 zajete = false;
  254.                 obszar = false;
  255.                 y = 0;
  256.                 while (!obszar)
  257.                 {
  258.                     if (x1 >= wymiar || tablica[x1] >= wymiar)
  259.                     {
  260.                         Console.WriteLine("Podałeś współrzędną oo zbyt dużej wartości");
  261.                         Console.WriteLine("Ruch gracza " + Gracz2 + ". Podaj współrzędną:");
  262.                         try
  263.                         {
  264.                             x1 = Convert.ToInt32(Console.ReadLine());
  265.                         }
  266.                         catch (Exception)
  267.                         {
  268.                             Console.WriteLine("\nNie podales liczby!");
  269.                         }
  270.                     }
  271.                     else obszar = true;
  272.                 }
  273.                     while (!zajete)
  274.                     {
  275.                         if (plansza[x1, y] != 0)
  276.                         {
  277.                             plansza[x1, y - 1] = 2;
  278.                             zajete = true;
  279.                             tablica[x1] += 1;
  280.                             continue;
  281.                         }
  282.                         else if (plansza[x1, y] == 0 && y == wymiar - 1)
  283.                         {
  284.                             plansza[x1, y] = 2;
  285.                             zajete = true;
  286.                             tablica[x1] += 1;
  287.                             continue;
  288.                         }
  289.                         y++;
  290.                     }
  291.  
  292.  
  293.                     liczba = 0;
  294.                     if (liczba < 4)
  295.                     {
  296.                         //poziomo
  297.                         liczba =
  298.                             ((x1 - 1 >= 0) ? plansza[x1 - 1, y] == 2 ? 1 : 0 : 0) +
  299.                             ((x1 - 2 >= 0) ? plansza[x1 - 2, y] == 2 ? 1 : 0 : 0) +
  300.                             ((x1 - 3 >= 0) ? plansza[x1 - 3, y] == 2 ? 1 : 0 : 0) +
  301.                             ((x1 + 1 < wymiar) ? plansza[x1 + 1, y] == 2 ? 1 : 0 : 0) +
  302.                             ((x1 + 2 < wymiar) ? plansza[x1 + 2, y] == 2 ? 1 : 0 : 0) +
  303.                             ((x1 + 3 < wymiar) ? plansza[x1 + 3, y] == 2 ? 1 : 0 : 0) + 1;
  304.                     }
  305.                     if (liczba < 4)
  306.                     {
  307.                         //pionowo
  308.                         liczba =
  309.                             ((y - 1 >= 0) ? plansza[x1, y - 1] == 2 ? 1 : 0 : 0) +
  310.                             ((y - 2 >= 0) ? plansza[x1, y - 2] == 2 ? 1 : 0 : 0) +
  311.                             ((y - 3 >= 0) ? plansza[x1, y - 3] == 2 ? 1 : 0 : 0) +
  312.                             ((y + 1 < wymiar) ? plansza[x1, y + 1] == 2 ? 1 : 0 : 0) +
  313.                             ((y + 2 < wymiar) ? plansza[x1, y + 2] == 2 ? 1 : 0 : 0) +
  314.                             ((y + 3 < wymiar) ? plansza[x1, y + 3] == 2 ? 1 : 0 : 0) + 1;
  315.                     }
  316.                     if (liczba < 4)
  317.                     {
  318.                         // skos /
  319.                         liczba =
  320.                             ((x1 - 1 >= 0 && y - 1 >= 0) ? plansza[x1 - 1, y - 1] == 2 ? 1 : 0 : 0) +
  321.                             ((x1 - 2 >= 0 && y - 2 >= 0) ? plansza[x1 - 2, y - 2] == 2 ? 1 : 0 : 0) +
  322.                             ((x1 - 3 >= 0 && y - 3 >= 0) ? plansza[x1 - 3, y - 3] == 2 ? 1 : 0 : 0) +
  323.                             ((x1 + 1 < wymiar && y + 1 < wymiar) ? plansza[x1 + 1, y + 1] == 2 ? 1 : 0 : 0) +
  324.                             ((x1 + 2 < wymiar && y + 2 < wymiar) ? plansza[x1 + 2, y + 2] == 2 ? 1 : 0 : 0) +
  325.                             ((x1 + 3 < wymiar && y + 3 < wymiar) ? plansza[x1 + 3, y + 3] == 2 ? 1 : 0 : 0) + 1;
  326.                     }
  327.                     if (liczba < 4)
  328.                     {
  329.                         // skos \
  330.                         liczba =
  331.                             ((x1 - 1 >= 0 && y + 1 < wymiar) ? plansza[x1 - 1, y + 1] == 2 ? 1 : 0 : 0) +
  332.                             ((x1 - 2 >= 0 && y + 2 < wymiar) ? plansza[x1 - 2, y + 2] == 2 ? 1 : 0 : 0) +
  333.                             ((x1 - 3 >= 0 && y + 3 < wymiar) ? plansza[x1 - 3, y + 3] == 2 ? 1 : 0 : 0) +
  334.                             ((x1 + 1 < wymiar && y - 1 >= 0) ? plansza[x1 + 1, y - 1] == 2 ? 1 : 0 : 0) +
  335.                             ((x1 + 2 < wymiar && y - 2 >= 0) ? plansza[x1 + 2, y - 2] == 2 ? 1 : 0 : 0) +
  336.                             ((x1 + 3 < wymiar && y - 3 >= 0) ? plansza[x1 + 3, y - 3] == 2 ? 1 : 0 : 0) + 1;
  337.                     }
  338.                     if (liczba >= 4)
  339.                     {
  340.                         Console.WriteLine("wygral gracz " + Gracz2);
  341.                         Console.ReadLine();
  342.  
  343.                         //obsluga wynikow itp
  344.  
  345.                         koniec = true;
  346.                         continue;
  347.                     }
  348.                 }
  349.  
  350.  
  351.  
  352.  
  353.             }
  354.  
  355.         static public void Tabela_Wyniki()
  356.         {
  357.             int i = 0, j = 0;
  358.             Console.Clear();
  359.             Console.WriteLine("Tabela wyników:\n");
  360.             while (tab_wyn[0, j++] != "" && j <= 51)
  361.             {
  362.                 Console.Write(tab_wyn[0, j] + "\t");
  363.                 Console.Write(tab_wyn[1, j] + "\t");
  364.                 Console.Write(tab_wyn[2, j] + "\n");
  365.             }
  366.             Console.WriteLine("Aby  przejść dalej wciśnij przycisk  Enter\n");
  367.             Console.ReadLine();
  368.             Wyniki();
  369.         }
  370.  
  371.         static public void Wyniki()
  372.         {
  373.             int wybor = 0;
  374.             Console.Clear();
  375.             Console.WriteLine("Co chesz zrobić?\n");
  376.             Console.WriteLine("1. Pokaż wyniki pary graczy");
  377.             Console.WriteLine("2. Pokaż wynik jednego gracza");
  378.             Console.WriteLine("3. Wróc do Menu");
  379.             try
  380.             {
  381.                 wybor = Convert.ToInt32(Console.ReadLine());
  382.             }
  383.             catch (Exception)
  384.             {
  385.                 Console.WriteLine("\nNie podales liczby!");
  386.             }
  387.  
  388.             switch (wybor)
  389.             {
  390.                 case 1:
  391.                     //Para();
  392.                     break;
  393.                 case 2:
  394.                     //Jeden();
  395.                     break;
  396.                 case 3:
  397.                     Menu();
  398.                     break;
  399.                 default:
  400.                     Console.WriteLine("\nProszę wybrac  opcję z menu od 1 do 3");
  401.                     Console.WriteLine("\nWciśnij Enter  aby spróbować ponownie");
  402.                     Console.ReadLine();
  403.                     Wyniki();
  404.                     break;
  405.             }
  406.         }
  407.  
  408.         static public void Autor()
  409.         {
  410.  
  411.             Console.Clear();
  412.             Console.WriteLine("Informacje o autorze:\n");
  413.             Console.WriteLine("Aby wrócić wciśnij klawisz Enter.\n");
  414.             Console.ReadLine();
  415.             Menu();
  416.         }
  417.  
  418.         static public void Wynik_Ogolny(string Gracz)
  419.         {
  420.  
  421.  
  422.         }
  423.  
  424.         static public void Koniec()
  425.         {
  426.             int wybor = 0;
  427.             Console.Clear();
  428.             Console.WriteLine("Czy napewno chcesz opuścić program? (t/n)\n");
  429.             wybor = Console.Read();
  430.             if (wybor == 116)
  431.             {
  432.  
  433.             }
  434.             else
  435.                 if (wybor == 110)
  436.                 {
  437.                     Menu();
  438.                 }
  439.                 else
  440.                 {
  441.                     Console.WriteLine("Podaj  odpowiedź Tak (t) lub Nie (n)");
  442.                     Console.ReadLine();
  443.                     Koniec();
  444.                 }
  445.         }
  446.     }
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement