Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- Menu();
- }
- static public void Menu()
- {
- int wybor = 0;
- Console.Clear();
- Console.WriteLine("Gra Connect 4\n");
- Console.WriteLine("MENU:");
- Console.WriteLine("1. Nowa Gra");
- Console.WriteLine("2. Wyniki");
- Console.WriteLine("3. Informacje o autorze");
- Console.WriteLine("4. Wyjście\n");
- Console.WriteLine("Wybierz opcję z menu:");
- try
- {
- wybor = Convert.ToInt32(Console.ReadLine());
- }
- catch (Exception)
- {
- Console.WriteLine("\nNie podales liczby!");
- }
- switch (wybor)
- {
- case 1:
- Gra();
- break;
- case 2:
- Tabela_Wyniki();
- break;
- case 3:
- Autor();
- break;
- case 4:
- Koniec();
- break;
- default:
- Console.WriteLine("\nProszę wybrac opcję z menu od 1 do 4");
- Console.ReadLine();
- Menu();
- break;
- }
- }
- static public void Gra()
- {
- string Gracz1, Gracz2;
- Console.Clear();
- Console.WriteLine("Nowa Gra\n");
- Console.WriteLine("Podaj nazwę gracza 1:");
- Gracz1 = Console.ReadLine();
- Console.WriteLine("Podaj nazwę gracza 2:");
- Gracz2 = Console.ReadLine();
- Console.WriteLine("Podaj wymiar planszy:");
- int wymiar = Convert.ToInt32(Console.ReadLine());
- int x1 = 0;
- Console.Clear();
- int[,] plansza = new int[wymiar, wymiar];
- for (int i = 0; i < wymiar; i++) for (int j = 0; j < wymiar; j++) plansza[i, j] = 0;
- int zakres = wymiar - 1;
- bool koniec = false;
- string zakonczenie;
- while (!koniec)
- {
- Console.Clear();
- for (int k = 0; k < wymiar; k++)
- {
- Console.Write(k+" ");
- }
- Console.Write("\n");
- for (int k = 0; k < wymiar; k++)
- {
- Console.Write("--");
- }
- Console.Write("\n");
- for (int i = 0; i < wymiar; i++)
- {
- for (int j = 0; j < wymiar; j++)
- {
- Console.Write(plansza[j, i] + " ");
- }
- Console.Write("\n");
- }
- Console.WriteLine("Ruch gracza " + Gracz1 + ". Podaj współrzędną:");
- zakonczenie = Console.ReadLine();
- if (zakonczenie == "koniec")
- {
- Koniec();
- break;
- }
- try
- {
- zakonczenie = Convert.ToInt32(Console.ReadLine());
- }
- catch (Exception)
- {
- Console.WriteLine("\nNie podales liczby!");
- }
- x1 = zakonczenie;
- if (x1 > wymiar)
- {
- Console.WriteLine("Wybierz liczbę z zakresu od 0 do "+zakres);
- try
- {
- x1 = Convert.ToInt32(Console.ReadLine());
- }
- catch (Exception)
- {
- Console.WriteLine("\nNie podales liczby!");
- }
- }
- //szukanie pierwszego wolnego miejsca w kol
- bool zajete = false;
- int y = 0;
- while (!zajete)
- {
- if (plansza[x1, y] != 0)
- {
- plansza[x1, y - 1] = 1;
- zajete = true;
- continue;
- }
- else if (plansza[x1, y] == 0 && y == wymiar - 1)
- {
- plansza[x1, y] = 1;
- zajete = true;
- continue;
- }
- y++;
- }
- int liczba = 0;
- if (liczba < 4)
- {
- //poziomo
- liczba =
- ((x1 - 1 >= 0) ? plansza[x1 - 1, y] == 1 ? 1 : 0 : 0) +
- ((x1 - 2 >= 0) ? plansza[x1 - 2, y] == 1 ? 1 : 0 : 0) +
- ((x1 - 3 >= 0) ? plansza[x1 - 3, y] == 1 ? 1 : 0 : 0) +
- ((x1 + 1 < wymiar) ? plansza[x1 + 1, y] == 1 ? 1 : 0 : 0) +
- ((x1 + 2 < wymiar) ? plansza[x1 + 2, y] == 1 ? 1 : 0 : 0) +
- ((x1 + 3 < wymiar) ? plansza[x1 + 3, y] == 1 ? 1 : 0 : 0);
- }
- else if (liczba < 4)
- {
- //pionowo
- liczba =
- ((y - 1 >= 0) ? plansza[x1, y - 1] == 1 ? 1 : 0 : 0) +
- ((y - 2 >= 0) ? plansza[x1, y - 2] == 1 ? 1 : 0 : 0) +
- ((y - 3 >= 0) ? plansza[x1, y - 3] == 1 ? 1 : 0 : 0) +
- ((y + 1 < wymiar) ? plansza[x1, y + 1] == 1 ? 1 : 0 : 0) +
- ((y + 2 < wymiar) ? plansza[x1, y + 2] == 1 ? 1 : 0 : 0) +
- ((y + 3 < wymiar) ? plansza[x1, y + 3] == 1 ? 1 : 0 : 0);
- }
- else if (liczba < 4)
- {
- // skos \
- liczba =
- ((x1 - 1 >= 0 && y - 1 >= 0) ? plansza[x1 - 1, y - 1] == 1 ? 1 : 0 : 0) +
- ((x1 - 2 >= 0 && y - 2 >= 0) ? plansza[x1 - 2, y - 2] == 1 ? 1 : 0 : 0) +
- ((x1 - 3 >= 0 && y - 3 >= 0) ? plansza[x1 - 3, y - 3] == 1 ? 1 : 0 : 0) +
- ((x1 + 1 < wymiar && y + 1 < wymiar) ? plansza[x1 + 1, y + 1] == 1 ? 1 : 0 : 0) +
- ((x1 + 2 < wymiar && y + 2 < wymiar) ? plansza[x1 + 2, y + 2] == 1 ? 1 : 0 : 0) +
- ((x1 + 3 < wymiar && y + 3 < wymiar) ? plansza[x1 + 3, y + 3] == 1 ? 1 : 0 : 0);
- }
- else if (liczba < 4)
- {
- // skos /
- liczba =
- ((x1 - 1 >= 0 && y + 3 < wymiar) ? plansza[x1 - 1, y + 1] == 1 ? 1 : 0 : 0) +
- ((x1 - 2 >= 0 && y + 2 < wymiar) ? plansza[x1 - 2, y + 2] == 1 ? 1 : 0 : 0) +
- ((x1 - 3 >= 0 && y + 1 < wymiar) ? plansza[x1 - 3, y + 3] == 1 ? 1 : 0 : 0) +
- ((x1 + 1 < wymiar && y - 1 >= 0) ? plansza[x1 + 2, y - 1] == 1 ? 1 : 0 : 0) +
- ((x1 + 2 < wymiar && y - 2 >= 0) ? plansza[x1 + 2, y - 2] == 1 ? 1 : 0 : 0) +
- ((x1 + 3 < wymiar && y - 3 >= 0) ? plansza[x1 + 3, y - 3] == 1 ? 1 : 0 : 0);
- }
- else
- {
- Console.WriteLine("wygral gracz " + Gracz1);
- Console.ReadLine();
- //obsluga wynikow itp
- koniec = true;
- continue;
- }
- //////// gracz 2 //////////////////
- Console.WriteLine("Ruch gracza " + Gracz2 + ". Podaj współrzędną:");
- x1 = Convert.ToInt32(Console.ReadLine());
- //szukanie pierwszego wolnego miejsca w kol
- zajete = false;
- y = 0;
- while (!zajete)
- {
- if (plansza[x1, y] != 0)
- {
- plansza[x1, y - 1] = 2;
- zajete = true;
- continue;
- }
- else if (plansza[x1, y] == 0 && y == wymiar - 1)
- {
- plansza[x1, y] = 2;
- zajete = true;
- continue;
- }
- y++;
- }
- liczba = 0;
- if (liczba < 4)
- {
- //poziomo
- liczba =
- ((x1 - 1 >= 0) ? plansza[x1 - 1, y] == 2 ? 1 : 0 : 0) +
- ((x1 - 2 >= 0) ? plansza[x1 - 2, y] == 2 ? 1 : 0 : 0) +
- ((x1 - 3 >= 0) ? plansza[x1 - 3, y] == 2 ? 1 : 0 : 0) +
- ((x1 + 1 < wymiar) ? plansza[x1 + 1, y] == 2 ? 1 : 0 : 0) +
- ((x1 + 2 < wymiar) ? plansza[x1 + 2, y] == 2 ? 1 : 0 : 0) +
- ((x1 + 3 < wymiar) ? plansza[x1 + 3, y] == 2 ? 1 : 0 : 0);
- }
- else if (liczba < 4)
- {
- //pionowo
- liczba =
- ((y - 1 >= 0) ? plansza[x1, y - 1] == 2 ? 1 : 0 : 0) +
- ((y - 2 >= 0) ? plansza[x1, y - 2] == 2 ? 1 : 0 : 0) +
- ((y - 3 >= 0) ? plansza[x1, y - 3] == 2 ? 1 : 0 : 0) +
- ((y + 1 < wymiar) ? plansza[x1, y + 1] == 2 ? 1 : 0 : 0) +
- ((y + 2 < wymiar) ? plansza[x1, y + 2] == 2 ? 1 : 0 : 0) +
- ((y + 3 < wymiar) ? plansza[x1, y + 3] == 2 ? 1 : 0 : 0);
- }
- else if (liczba < 4)
- {
- // skos \
- liczba =
- ((x1 - 1 >= 0 && y - 1 >= 0) ? plansza[x1 - 1, y - 1] == 2 ? 1 : 0 : 0) +
- ((x1 - 2 >= 0 && y - 2 >= 0) ? plansza[x1 - 2, y - 2] == 2 ? 1 : 0 : 0) +
- ((x1 - 3 >= 0 && y - 3 >= 0) ? plansza[x1 - 3, y - 3] == 2 ? 1 : 0 : 0) +
- ((x1 + 1 < wymiar && y + 1 < wymiar) ? plansza[x1 + 1, y + 1] == 2 ? 1 : 0 : 0) +
- ((x1 + 2 < wymiar && y + 2 < wymiar) ? plansza[x1 + 2, y + 2] == 2 ? 1 : 0 : 0) +
- ((x1 + 3 < wymiar && y + 3 < wymiar) ? plansza[x1 + 3, y + 3] == 2 ? 1 : 0 : 0);
- }
- else if (liczba < 4)
- {
- // skos /
- liczba =
- ((x1 - 1 >= 0 && y + 3 < wymiar) ? plansza[x1 - 1, y + 1] == 2 ? 1 : 0 : 0) +
- ((x1 - 2 >= 0 && y + 2 < wymiar) ? plansza[x1 - 2, y + 2] == 2 ? 1 : 0 : 0) +
- ((x1 - 3 >= 0 && y + 1 < wymiar) ? plansza[x1 - 3, y + 3] == 2 ? 1 : 0 : 0) +
- ((x1 + 1 < wymiar && y - 1 >= 0) ? plansza[x1 + 2, y - 1] == 2 ? 1 : 0 : 0) +
- ((x1 + 2 < wymiar && y - 2 >= 0) ? plansza[x1 + 2, y - 2] == 2 ? 1 : 0 : 0) +
- ((x1 + 3 < wymiar && y - 3 >= 0) ? plansza[x1 + 3, y - 3] == 2 ? 1 : 0 : 0);
- }
- else
- {
- Console.WriteLine("wygral gracz " + Gracz2);
- Console.ReadLine();
- //obsluga wynikow itp
- koniec = true;
- continue;
- }
- }
- }
- static public void Tabela_Wyniki()
- {
- Console.Clear();
- Console.WriteLine("Tabela wyników:\n");
- string line;
- int count = 0;
- // Read the file and display it line by line.
- System.IO.StreamReader file =
- new System.IO.StreamReader(@"D:\test.txt");
- while ((line = file.ReadLine()) != null)
- {
- Console.Write(line + " - ");
- count++;
- if (count == 3)
- {
- Console.WriteLine("");
- count = 0;
- }
- }
- file.Close();
- Console.WriteLine("Aby przejść dalej wciśnij przycisk Enter\n");
- Console.ReadLine();
- Wyniki();
- }
- static public void Wyniki()
- {
- int wybor = 0;
- Console.Clear();
- Console.WriteLine("Co chesz zrobić?\n");
- Console.WriteLine("1. Pokaż wyniki pary graczy");
- Console.WriteLine("2. Pokaż wynik jednego gracza");
- Console.WriteLine("3. Wróc do Menu");
- try
- {
- wybor = Convert.ToInt32(Console.ReadLine());
- }
- catch (Exception)
- {
- Console.WriteLine("\nNie podales liczby!");
- }
- switch (wybor)
- {
- case 1:
- Para();
- break;
- case 2:
- Jeden();
- break;
- case 3:
- Menu();
- break;
- default:
- Console.WriteLine("\nProszę wybrac opcję z menu od 1 do 3");
- Console.WriteLine("\nWciśnij Enter aby spróbować ponownie");
- Console.ReadLine();
- Wyniki();
- break;
- }
- }
- static public void Autor()
- {
- Console.Clear();
- Console.WriteLine("Informacje o autorze:\n");
- Console.WriteLine("Aby wrócić wciśnij klawisz Enter.\n");
- Console.ReadLine();
- Menu();
- }
- static public void Jeden()
- {
- string Gracz;
- Console.Clear();
- Console.WriteLine("Podaj nazwę gracza:\n");
- Gracz = Console.ReadLine();
- Console.WriteLine("Wynik dla gracza " + Gracz + " to: ");
- Console.WriteLine("Aby wrócić wciśnij klawisz Enter.\n");
- Console.ReadLine();
- }
- static public void Para()
- {
- string Gracz1, Gracz2;
- Console.Clear();
- Console.WriteLine("Podaj nazwę gracza nr 1:\n");
- Gracz1 = Console.ReadLine();
- Console.WriteLine("Podaj nazwę gracza nr 2:\n");
- Gracz2 = Console.ReadLine();
- Console.WriteLine("Wyniki: ");
- Console.WriteLine("Aby wrócić wciśnij klawisz Enter.\n");
- Console.ReadLine();
- }
- static public void Wygrana(string wygrany, string przegrany)
- {
- }
- static public void Koniec()
- {
- int wybor = 0;
- Console.Clear();
- Console.WriteLine("Czy napewno chcesz opuścić program? (t/n)\n");
- wybor = Console.Read();
- if (wybor == 116)
- {
- }
- else
- if (wybor == 110)
- {
- Menu();
- }
- else
- {
- Console.WriteLine("Podaj odpowiedź Tak (t) lub Nie (n)");
- Console.ReadLine();
- Koniec();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement