Advertisement
Guest User

Untitled

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