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 public string[,] tab_wyn = new string[3, 50];
- static void Main(string[] args)
- {
- string plik = "wyniki.txt";
- int i = 0, j = 0;
- foreach (string linia in File.ReadLines(plik))
- {
- if (i < 3)
- {
- tab_wyn[j, i] = linia;
- }
- else
- {
- i = 0;
- j++;
- tab_wyn[j, i] = linia;
- }
- i++;
- }
- 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());
- Console.Clear();
- int[,] plansza = new int[wymiar, wymiar];
- int[] tablica = new int[wymiar];
- for (int i = 0; i < wymiar; i++)
- {
- for (int j = 0; j < wymiar; j++)
- {
- plansza[i, j] = 0;
- tablica[i] = 0;
- }
- }
- bool koniec = false;
- 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");
- }
- bool licz = false;
- int x1 = 0;
- while (!licz)
- {
- Console.WriteLine("Ruch gracza " + Gracz1 + ". Podaj współrzędną:");
- x1 = Convert.ToInt32(Console.ReadLine());
- if (x1 < 0)
- {
- licz = true;
- }
- else
- {
- Console.WriteLine("Ruch gracza " + Gracz1 + ". Podaj współrzędną:");
- x1 = Convert.ToInt32(Console.ReadLine());
- }
- }
- //szukanie pierwszego wolnego miejsca w kol
- bool zajete = false;
- bool obszar = false;
- int y = 0;
- while (!obszar)
- {
- if (x1 >= wymiar || tablica[x1] >= wymiar)
- {
- Console.WriteLine("Podałeś współrzędną oo zbyt dużej wartości");
- Console.WriteLine("Ruch gracza " + Gracz1 + ". Podaj współrzędną:");
- try
- {
- x1 = Convert.ToInt32(Console.ReadLine());
- }
- catch (Exception)
- {
- Console.WriteLine("\nNie podales liczby!");
- }
- }
- else obszar = true;
- }
- while (!zajete)
- {
- if (plansza[x1, y] != 0)
- {
- plansza[x1, y - 1] = 1;
- zajete = true;
- tablica[x1] += 1;
- continue;
- }
- else if (plansza[x1, y] == 0 && y == wymiar - 1)
- {
- plansza[x1, y] = 1;
- zajete = true;
- tablica[x1] += 1;
- 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) + 1;
- }
- 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) + 1;
- }
- 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) + 1;
- }
- if (liczba < 4)
- {
- // skos \
- liczba =
- ((x1 - 1 >= 0 && y + 1 < 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 + 3 < wymiar) ? plansza[x1 - 3, y + 3] == 1 ? 1 : 0 : 0) +
- ((x1 + 1 < wymiar && y - 1 >= 0) ? plansza[x1 + 1, 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) + 1;
- }
- if (liczba >= 4)
- {
- 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;
- obszar = false;
- y = 0;
- while (!obszar)
- {
- if (x1 >= wymiar || tablica[x1] >= wymiar)
- {
- Console.WriteLine("Podałeś współrzędną oo zbyt dużej wartości");
- Console.WriteLine("Ruch gracza " + Gracz2 + ". Podaj współrzędną:");
- try
- {
- x1 = Convert.ToInt32(Console.ReadLine());
- }
- catch (Exception)
- {
- Console.WriteLine("\nNie podales liczby!");
- }
- }
- else obszar = true;
- }
- while (!zajete)
- {
- if (plansza[x1, y] != 0)
- {
- plansza[x1, y - 1] = 2;
- zajete = true;
- tablica[x1] += 1;
- continue;
- }
- else if (plansza[x1, y] == 0 && y == wymiar - 1)
- {
- plansza[x1, y] = 2;
- zajete = true;
- tablica[x1] += 1;
- 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) + 1;
- }
- 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) + 1;
- }
- 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) + 1;
- }
- if (liczba < 4)
- {
- // skos \
- liczba =
- ((x1 - 1 >= 0 && y + 1 < 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 + 3 < wymiar) ? plansza[x1 - 3, y + 3] == 2 ? 1 : 0 : 0) +
- ((x1 + 1 < wymiar && y - 1 >= 0) ? plansza[x1 + 1, 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) + 1;
- }
- if (liczba >= 4)
- {
- Console.WriteLine("wygral gracz " + Gracz2);
- Console.ReadLine();
- //obsluga wynikow itp
- koniec = true;
- continue;
- }
- }
- }
- static public void Tabela_Wyniki()
- {
- int i = 0, j = 0;
- Console.Clear();
- Console.WriteLine("Tabela wyników:\n");
- while (tab_wyn[0, j++] != "" && j <= 51)
- {
- Console.Write(tab_wyn[0, j] + "\t");
- Console.Write(tab_wyn[1, j] + "\t");
- Console.Write(tab_wyn[2, j] + "\n");
- }
- 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 Wynik_Ogolny(string Gracz)
- {
- }
- 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