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;
- namespace ConsoleApp1
- {
- class Program
- {
- static int readInt(string komunikat, uint min = uint.MinValue, uint max = uint.MaxValue)
- {
- string str;
- int liczba;
- while (true)
- {
- Console.Write(komunikat);
- str = Console.ReadLine();
- try
- {
- liczba = int.Parse(str);
- if ((liczba <= max) && (liczba >= min)) break;
- else Console.WriteLine("Wprowadzona liczba jest spoza zakresu <{0},{1}> ", min, max);
- }
- catch (FormatException)
- {
- Console.WriteLine("Wprowadzono liczbę w złym formacie");
- }
- catch (OverflowException)
- {
- Console.WriteLine("Liczba przekroczyła zakres dla typu uint ");
- }
- catch (ArgumentNullException) // ^Z
- {
- Console.WriteLine("Napotkano koniec strumienia");
- }
- Console.WriteLine("Spróbuj jeszcze raz");
- }//koniec wprowadzania liczby elementów
- return liczba;
- }
- static void szablon(string[,] p)
- {
- Console.Clear();
- Console.WriteLine(" {0} | {1} | {2} ", p[0, 0], p[0, 1], p[0, 2]);
- Console.WriteLine("---+---+---");
- Console.WriteLine(" {0} | {1} | {2} ", p[1, 0], p[1, 1], p[1, 2]);
- Console.WriteLine("---+---+---");
- Console.WriteLine(" {0} | {1} | {2} ", p[2, 0], p[2, 1], p[2, 2]);
- }
- static void wybierzPole(string[,] p, string znak)
- {
- while (true)
- {
- int numerPola = readInt("Podaj numer pola dla gracza " + znak + " : ", 1, 9);
- if (!sprawdzPole(p, numerPola, znak))
- Console.WriteLine("Wybrane pole jest już wypełnione");
- else break;
- }
- }
- static bool sprawdzPole(string[,] p, int nr, string znak)
- {
- int x = 0;
- bool puste = true;
- for (int w = 0; w < 3; w++)
- for (int k = 0; k < 3; k++)
- {
- x++;
- if (x == nr)
- if ((p[w, k] == "O") || (p[w, k] == "X"))
- {
- puste = false;
- }
- else
- {
- p[w, k] = znak;
- szablon(p);
- }
- }
- return puste;
- }
- static bool testKonca(string[,] p, string znak)
- {
- bool koniec = false;
- string tmp;
- //wiersze
- for (int w = 0; w < 3; w++)
- {
- tmp = znak;
- for (int k = 0; k < 3; k++)
- {
- if (tmp != p[w, k])
- {
- tmp = "";
- break;
- }
- }
- if (tmp != "") return true;
- }
- //kolumny
- for (int k = 0; k < 3; k++)
- {
- tmp = znak;
- for (int w = 0; w < 3; w++)
- {
- if (tmp != p[w, k])
- {
- tmp = "";
- break;
- }
- }
- return true;
- }
- // przekątna1
- tmp = znak;
- for (int w=0; w<3;w++)
- {
- for (int k=0;k<3;k++)
- if(w==k)
- if(p[w,k] !=znak)
- {
- tmp = "";
- break;
- }
- }
- if(tmp !="") return true;
- //przekatna2
- tmp = znak;
- for (int w=0;w<3; w++)
- {
- for (int k = 0; k < 3; k++)
- if (w + k == 2)
- if (p[w, k] != znak) tmp = "";
- }
- if (tmp != "") return true;
- //remis
- for (int w = 0; w < 3; w++)
- for (int k = 0; k < 3; k++)
- if (p[w, k] != "X" && p[w, k] != "O") return false;
- Console.WriteLine("Remis");
- return koniec;
- }
- static void Main(string[] args)
- {
- string[,] pola = new string[3, 3];
- int nr = 0;
- for (int w = 0; w < 3; w++)
- for (int k = 0; k < 3; k++)
- {
- nr++;
- pola[w, k] = nr.ToString();
- }
- szablon(pola);
- while (true)
- {
- wybierzPole(pola, "X");
- if (testKonca(pola, "X"))
- {
- Console.WriteLine("Wygrał gracz X");
- break;
- }
- wybierzPole(pola, "O");
- if (testKonca(pola, "O"))
- {
- Console.WriteLine("Wygrał gracz O");
- break;
- }
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement