Share Pastebin
Guest
Public paste!

Tic Tac Toe

By: a guest | Mar 16th, 2010 | Syntax: C# | Size: 12.01 KB | Hits: 116 | Expires: Never
Copy text to clipboard
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         static bool GvolOk(int r, int c)
  10.         {
  11.             return r < 3 && c < 3;
  12.         }
  13.         static void Print(char[,] m)
  14.         {
  15.             Console.WriteLine("     0         1       2   ");
  16.             for (int i = 0; i < m.GetLength(0); i++)
  17.             {
  18.  
  19.                 Console.WriteLine();
  20.                 Console.Write(i);
  21.                 for (int k = 0; k < m.GetLength(1); k++)
  22.                 {
  23.  
  24.                     Console.ForegroundColor = ConsoleColor.White;
  25.                     if (k != m.GetLength(1) - 1)
  26.                         Console.Write("  " + m[i, k] + "     |");
  27.                     else
  28.                         Console.Write("   " + m[i, k] + "    ");
  29.                 }
  30.                 Console.WriteLine();
  31.                 Console.WriteLine("  -------|--------|--------");
  32.                 Console.WriteLine();
  33.             }
  34.             Console.ResetColor();
  35.         }
  36.         static bool Full(char[,] m)
  37.         {
  38.             for (int i = 0; i < m.GetLength(0); i++)
  39.             {
  40.                 for (int k = 0; k < m.GetLength(1); k++)
  41.                 {
  42.                     if (!(m[i, k] == 'X' || m[i, k] == 'O'))
  43.                         return false;
  44.                 }
  45.             }
  46.  
  47.             return true;
  48.         }
  49.         static bool Amodot(char[,] m)
  50.         {
  51.             if (m[0, 0] == m[1, 0] && m[0, 0] == m[2, 0] && m[0, 0] != ' ')
  52.                 return true;
  53.             if (m[0, 1] == m[1, 1] && m[0, 1] == m[2, 1] && m[0, 1] != ' ')
  54.                 return true;
  55.             if (m[0, 2] == m[1, 2] && m[0, 2] == m[2, 2] && m[0, 2] != ' ')
  56.                 return true;
  57.             return false;
  58.         }
  59.         static bool Shorot(char[,] m)
  60.         {
  61.             if (m[0, 0] == m[0, 1] && m[0, 0] == m[0, 2] && m[0, 0] != ' ')
  62.                 return true;
  63.             if (m[1, 0] == m[1, 1] && m[1, 0] == m[1, 2] && m[1, 0] != ' ')
  64.                 return true;
  65.             if (m[2, 0] == m[2, 1] && m[2, 0] == m[2, 2] && m[2, 0] != ' ')
  66.                 return true;
  67.             return false;
  68.         }
  69.         static bool Alahson1(char[,] m)
  70.         {
  71.             if (m[0, 0] == m[1, 1] && m[0, 0] == m[2, 2] && m[0, 0] != ' ')
  72.                 return true;
  73.             return false;
  74.         }
  75.         static bool Alahson2(char[,] m)
  76.         {
  77.             if (m[0, 2] == m[1, 1] && m[0, 2] == m[2, 0] && m[0, 2] != ' ')
  78.                 return true;
  79.             return false;
  80.         }
  81.         static bool Win(char[,] m)
  82.         {
  83.             return Amodot(m) || Shorot(m) || Alahson1(m) || Alahson2(m);
  84.         }
  85.         static bool Tafos(char[,] m, int a, int b)
  86.         {
  87.             return m[a, b] == ' ';
  88.         }
  89.         static void Player1(char[,] m, string str)
  90.         {
  91.             Console.ForegroundColor = ConsoleColor.White;
  92.             Console.WriteLine("- " + str + "'s turn (X) -");
  93.             Console.Write("Enter Row: ");
  94.             int r = int.Parse(Console.ReadLine());
  95.             Console.Write("Enter Colom: ");
  96.             int c = int.Parse(Console.ReadLine());
  97.             while (!GvolOk(r, c))
  98.             {
  99.                 Console.Write("Enter Row: ");
  100.                 r = int.Parse(Console.ReadLine());
  101.                 Console.Write("Enter Colom: ");
  102.                 c = int.Parse(Console.ReadLine());
  103.             }
  104.             while (!Tafos(m, r, c))
  105.             {
  106.                 Console.Write("Enter Row: ");
  107.                 r = int.Parse(Console.ReadLine());
  108.                 Console.Write("Enter Colom: ");
  109.                 c = int.Parse(Console.ReadLine());
  110.             }
  111.             m[r, c] = 'X';
  112.         }
  113.         static void Player2(char[,] m, string str)
  114.         {
  115.             Console.ForegroundColor = ConsoleColor.White;
  116.             Console.WriteLine("- " + str + "'s turn (O) -");
  117.             Console.Write("Enter Row: ");
  118.             int r = int.Parse(Console.ReadLine());
  119.             Console.Write("Enter Colom: ");
  120.             int c = int.Parse(Console.ReadLine());
  121.             while (!GvolOk(r, c))
  122.             {
  123.                 Console.Write("Enter Row: ");
  124.                 r = int.Parse(Console.ReadLine());
  125.                 Console.Write("Enter Colom: ");
  126.                 c = int.Parse(Console.ReadLine());
  127.             }
  128.             while (!Tafos(m, r, c))
  129.             {
  130.                 Console.Write("Enter Row: ");
  131.                 r = int.Parse(Console.ReadLine());
  132.                 Console.Write("Enter Colom: ");
  133.                 c = int.Parse(Console.ReadLine());
  134.             }
  135.             m[r, c] = 'O';
  136.         }
  137.         static void LoadingGame(char[,] m)
  138.         {
  139.             System.Threading.Thread.Sleep(250);
  140.             for (int i = 0; i <= 10; i++)
  141.             {
  142.                 Console.Write("Loading Game...../ ");
  143.                 System.Threading.Thread.Sleep(50);
  144.                 Console.Clear();
  145.                 Console.Write("Loading Game.....- ");
  146.                 System.Threading.Thread.Sleep(50);
  147.                 Console.Clear();
  148.                 Console.Write("Loading Game.....\\ ");
  149.                 System.Threading.Thread.Sleep(50);
  150.                 Console.Clear();
  151.                 Console.Write("Loading Game.....| ");
  152.                 System.Threading.Thread.Sleep(50);
  153.                 Console.Clear();
  154.             }
  155.         }
  156.         static void Initalizing(char[,] m)
  157.         {
  158.             System.Threading.Thread.Sleep(250);
  159.             for (int i = 0; i <= 10; i++)
  160.             {
  161.                 Console.Write("Initalizing Data...../ ");
  162.                 System.Threading.Thread.Sleep(50);
  163.                 Console.Clear();
  164.                 Console.Write("Initalizing Data.....-");
  165.                 System.Threading.Thread.Sleep(50);
  166.                 Console.Clear();
  167.                 Console.Write("Initalizing Data.....\\ ");
  168.                 System.Threading.Thread.Sleep(50);
  169.                 Console.Clear();
  170.                 Console.Write("Initalizing Data.....| ");
  171.                 System.Threading.Thread.Sleep(50);
  172.                 Console.Clear();
  173.             }
  174.         }
  175.         static void IposXO(char[,] m)
  176.         {
  177.             for (int i = 0; i < m.GetLength(0); i++)
  178.                 for (int k = 0; k < m.GetLength(1); k++)
  179.                     m[i, k] = ' ';
  180.         }
  181.         static void Main(string[] args)
  182.         {
  183.             Console.ForegroundColor = ConsoleColor.White;
  184.             char[,] xo = new char[3, 3];
  185.             IposXO(xo);
  186.             bool player = true;
  187.             LoadingGame(xo);
  188.             Console.Write("Player #1 Enter your name: ");
  189.             string name1 = Console.ReadLine();
  190.             Console.Write("Player #2 Enter your name: ");
  191.             string name2 = Console.ReadLine();
  192.             string answer;
  193.             bool again = true;
  194.             bool x1 = Full(xo);
  195.             bool x2 = Win(xo);
  196.             Initalizing(xo);
  197.             while (!x1 && !x2 && again)
  198.             {
  199.                 Print(xo);
  200.                 Console.ForegroundColor = ConsoleColor.White;
  201.                 Console.WriteLine();
  202.                 Console.WriteLine("*********************************");
  203.                 Console.WriteLine();
  204.                 player = !player;
  205.                 if (!player)
  206.                     Player1(xo, name1);
  207.                 else
  208.                     Player2(xo, name2);
  209.                 if (Win(xo) && !player)
  210.                 {
  211.                     Console.Clear();
  212.                     Print(xo);
  213.                     Console.ForegroundColor = ConsoleColor.White;
  214.                     Console.WriteLine("GAME OVER! " + name1 + " won the game.");
  215.                     Console.WriteLine("want to play again? yes/no");
  216.                     answer = Console.ReadLine();
  217.                     if (answer == "no" || answer == "NO")
  218.                         again = false;
  219.                     else
  220.                         if (answer == "yes" || answer == "YES")
  221.                         {
  222.                             IposXO(xo);
  223.                             x1 = false;
  224.                             x2 = false;
  225.                             player = true;
  226.                             Console.Clear();
  227.                             LoadingGame(xo);
  228.                             Console.Write("Player #1 Enter your name: ");
  229.                             name1 = Console.ReadLine();
  230.                             Console.Write("Player #2 Enter your name: ");
  231.                             name2 = Console.ReadLine();
  232.                             Initalizing(xo);
  233.                         }
  234.                         else
  235.                         {
  236.                             Console.Clear();
  237.                             Console.ReadKey();
  238.                         }
  239.                 }
  240.                 else
  241.                     if (Win(xo) && player)
  242.                     {
  243.                         Console.Clear();
  244.                         Print(xo);
  245.                         Console.WriteLine();
  246.                         Console.ForegroundColor = ConsoleColor.White;
  247.                         Console.WriteLine("GAME OVER! " + name2 + " won the game.");
  248.                         Console.WriteLine("want to play again? yes/no");
  249.                         answer = Console.ReadLine();
  250.                         if (answer == "no" || answer == "NO")
  251.                             again = false;
  252.                         else
  253.                             if (answer == "yes" || answer == "YES")
  254.                             {
  255.                                 IposXO(xo);
  256.                                 x1 = false;
  257.                                 x2 = false;
  258.                                 player = true;
  259.                                 LoadingGame(xo);
  260.                                 Console.Write("Player #1 Enter your name: ");
  261.                                 name1 = Console.ReadLine();
  262.                                 Console.Write("Player #2 Enter your name: ");
  263.                                 name2 = Console.ReadLine();
  264.                                 Initalizing(xo);
  265.                             }
  266.                             else
  267.                             {
  268.                                 Console.Clear();
  269.                                 Console.ReadKey();
  270.                             }
  271.                     }
  272.                     else
  273.                         if (Full(xo))
  274.                         {
  275.                             Console.Clear();
  276.                             Print(xo);
  277.                             Console.WriteLine();
  278.                             Console.ForegroundColor = ConsoleColor.White;
  279.                             Console.WriteLine("GAME OVER! nobody won the game");
  280.                             Console.WriteLine("want to play again? yes/no");
  281.                             answer = Console.ReadLine();
  282.                             if (answer == "no" || answer == "NO")
  283.                                 again = false;
  284.                             else
  285.                                 if (answer == "yes" || answer == "YES")
  286.                                 {
  287.                                     IposXO(xo);
  288.                                     x1 = false;
  289.                                     x2 = false;
  290.                                     player = true;
  291.                                     LoadingGame(xo);
  292.                                     Console.Write("Player #1 Enter your name: ");
  293.                                     name1 = Console.ReadLine();
  294.                                     Console.Write("Player #2 Enter your name: ");
  295.                                     name2 = Console.ReadLine();
  296.                                     Initalizing(xo);
  297.                                 }
  298.                                 else
  299.                                 {
  300.                                     Console.Clear();
  301.                                     Console.Read();
  302.                                 }
  303.                         }
  304.  
  305.                 Console.Clear();
  306.             }
  307.         }
  308.  
  309.     }
  310. }