Advertisement
Guest User

TicTac

a guest
Feb 6th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class Tic
  6. {
  7.     static string[,] TicTac = new string[3, 3];
  8.     static bool[,] canBeChanged = new bool[3, 3];
  9.     static int firstWins = 0;
  10.     static int secondWins = 0;
  11.     static int draws = 0;
  12.     static int currentPlayer = 1;
  13.     static bool isFull = true;
  14.  
  15.     static void TicTacSolver(int row, int col)
  16.     {
  17.         //Console.SetBufferSize(80, 500);
  18.  
  19.         if (checkHorizontal(0) || checkHorizontal(1) || checkHorizontal(2) || checkVertical(0) || checkVertical(1) || checkVertical(2) || checkFirstDiagonal() || checkSecondDiagonal())
  20.         {
  21.             if (currentPlayer == 1)
  22.             {
  23.                 secondWins++;
  24.                 //Console.WriteLine("Second wins!");
  25.                 return;
  26.             }
  27.             else if (currentPlayer == 2)
  28.             {
  29.                 firstWins++;
  30.                 //Console.WriteLine("First wins");
  31.                 return;
  32.             }
  33.         }
  34.  
  35.         isFull = true;
  36.  
  37.         for (int i = 0; i < 3; i++)
  38.         {
  39.             for (int j = 0; j < 3; j++)
  40.             {
  41.                 if (TicTac[i, j] == "-")
  42.                 {
  43.                     isFull = false;
  44.                 }
  45.             }
  46.         }
  47.  
  48.         if (isFull)
  49.         {
  50.             if (checkDraw())
  51.             {
  52.                 draws++;
  53.                 //Console.WriteLine("Draw!");
  54.                 return;
  55.             }
  56.         }
  57.  
  58.         for (int i = 0; i < 3; i++)
  59.         {
  60.             for (int j = 0; j < 3; j++)
  61.             {
  62.                 if (TicTac[i, j] == "-")
  63.                 {
  64.                     if (currentPlayer == 1)
  65.                     {
  66.                         TicTac[i, j] = "X";
  67.                         currentPlayer = 2;
  68.                     }
  69.                     else
  70.                     {
  71.                         TicTac[i, j] = "O";
  72.                         currentPlayer = 1;
  73.                     }
  74.  
  75.                     //PrintTicTac();
  76.  
  77.                     TicTacSolver(i, j);
  78.  
  79.                     TicTac[i, j] = "-";
  80.  
  81.                     if (currentPlayer == 1)
  82.                     {
  83.                         currentPlayer = 2;
  84.                     }
  85.                     else
  86.                     {
  87.                         currentPlayer = 1;
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.     }
  93.  
  94.     static bool checkDraw()
  95.     {
  96.         bool isDraw = true;
  97.         for (int i = 0; i < 3; i++)
  98.         {
  99.             for (int j = 0; j < 3; j++)
  100.             {
  101.                 if (TicTac[i, j] == "-")
  102.                 {
  103.                     isDraw = false;
  104.                 }
  105.             }
  106.         }
  107.  
  108.         return isDraw;
  109.     }
  110.  
  111.     static void PrintTicTac()
  112.     {
  113.         for (int i = 0; i < 3; i++)
  114.         {
  115.             for (int j = 0; j < 3; j++)
  116.             {
  117.                 Console.Write(TicTac[i,j]);
  118.             }
  119.             Console.WriteLine();
  120.         }
  121.         Console.WriteLine();
  122.     }
  123.  
  124.     static bool checkHorizontal(int hor)
  125.     {
  126.         if ((TicTac[hor, 0] == TicTac[hor, 1]) && (TicTac[hor, 2] == TicTac[hor, 1]))
  127.         {
  128.             if (TicTac[hor, 0] != "-" && TicTac[hor, 1] != "-" && TicTac[hor, 0] != "-")
  129.             return true;
  130.         }
  131.  
  132.         return false;
  133.     }
  134.  
  135.     static bool checkVertical(int ver)
  136.     {
  137.         if ((TicTac[0, ver] == TicTac[1, ver]) && (TicTac[1, ver]==TicTac[2, ver]))
  138.         {
  139.             if (TicTac[0, ver] != "-" && TicTac[1, ver] != "-" && TicTac[2, ver] != "-")
  140.             return true;
  141.         }
  142.  
  143.         return false;
  144.     }
  145.  
  146.     static bool checkFirstDiagonal()
  147.     {
  148.         if ((TicTac[0, 0] == TicTac[1, 1]) && (TicTac[2, 2] == TicTac[1, 1]))
  149.         {  
  150.             if (TicTac[0, 0] != "-" && TicTac[1, 1] != "-" && TicTac[2, 2] != "-")
  151.             return true;
  152.         }
  153.  
  154.         return false;
  155.     }
  156.  
  157.     static bool checkSecondDiagonal()
  158.     {
  159.         if ((TicTac[0, 2] == TicTac[1, 1]) && (TicTac[2, 0] == TicTac[1, 1]))
  160.         {
  161.             if (TicTac[0, 2] != "-" && TicTac[1, 1] != "-" && TicTac[2, 0] != "-")
  162.             return true;
  163.         }
  164.  
  165.         return false;
  166.     }
  167.  
  168.     static void Main()
  169.     {
  170.         for (int i = 0; i < 3; i++)
  171.         {
  172.             string input = Console.ReadLine();
  173.  
  174.             for (int j = 0; j < input.Length; j++)
  175.             {
  176.                 if (input[j] == '-')
  177.                 {
  178.                     TicTac[i, j] = "-";
  179.                     canBeChanged[i, j] = true;
  180.                 }
  181.                 else
  182.                 {
  183.                     TicTac[i, j] = input[j].ToString();
  184.                     canBeChanged[i, j] = false;
  185.                 }
  186.             }
  187.         }
  188.  
  189.         int numberOfEmptyLines = 1;
  190.  
  191.         //for (int i = 0; i < 3; i++)
  192.         //{
  193.         //    for (int j = 0; j < 3; j++)
  194.         //    {
  195.         //        if (TicTac[i, j] == "-")
  196.         //        {
  197.         //            TicTacSolver(i, j);
  198.         //            numberOfEmptyLines++;
  199.         //        }
  200.         //    }
  201.         //}
  202.  
  203.         int Xtimes = 0;
  204.         int Otimes = 0;
  205.  
  206.         for (int i = 0; i < 3; i++)
  207.         {
  208.             for (int j = 0; j < 3; j++)
  209.             {
  210.                 if (TicTac[i, j] == "X")
  211.                 {
  212.                     Xtimes++;
  213.                 }
  214.                 if (TicTac[i, j] == "O")
  215.                 {
  216.                     Otimes++;
  217.                 }
  218.             }
  219.         }
  220.  
  221.         if (Xtimes > Otimes)
  222.         {
  223.             currentPlayer = 2;
  224.         }
  225.  
  226.         TicTacSolver(0, 0);
  227.  
  228.         Console.WriteLine(firstWins/numberOfEmptyLines);
  229.         Console.WriteLine(draws/numberOfEmptyLines);
  230.         Console.WriteLine(secondWins/numberOfEmptyLines);
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement