Advertisement
Guest User

Untitled

a guest
Sep 6th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Board
  5. {
  6.     public char[,] Cells = new char[3, 3];
  7.     public int FreePostions { get; set; }
  8.  
  9.     public Board()
  10.     {
  11.         for (int r = 0; r < 3; r++)
  12.         {
  13.             for (int c = 0; c < 3; c++)
  14.             {
  15.                 this.Cells[r, c] = '-';
  16.             }
  17.         }
  18.  
  19.         this.FreePostions = 9;
  20.     }
  21. }
  22.  
  23. public class TicTacToe
  24. {
  25.     public static void Main()
  26.     {
  27.         Board board = new Board();
  28.         ReadBoard(board);
  29.  
  30.         Dictionary<string, int> resultCounts = new Dictionary<string, int>();
  31.         resultCounts.Add("X", 0);
  32.         resultCounts.Add("O", 0);
  33.         resultCounts.Add("Draw", 0);
  34.  
  35.         char playerOnTurn;
  36.         if (board.FreePostions % 2 == 0)
  37.         {
  38.             playerOnTurn = 'O';
  39.         }
  40.         else
  41.         {
  42.             playerOnTurn = 'X';
  43.         }
  44.        
  45.         FindResult(board, resultCounts, playerOnTurn);
  46.  
  47.         PrintResult(resultCounts);
  48.     }
  49.    
  50.     private static void ReadBoard(Board board)
  51.     {
  52.         for (int i = 0; i < 3; i++)
  53.         {
  54.             for (int j = 0; j < 3; j++)
  55.             {
  56.                 char symbol = (char)Console.Read();
  57.                 if (symbol == '-')
  58.                 {
  59.                     board.Cells[i, j] = symbol;
  60.                 }
  61.                 else
  62.                 {
  63.                     board.Cells[i, j] = symbol;
  64.                     board.FreePostions--;
  65.                 }
  66.             }
  67.             Console.ReadLine();
  68.         }
  69.     }
  70.  
  71.     private static void FindResult(Board board, Dictionary<string, int> resultCounts, char playerOnTurn)
  72.     {
  73.         // Recursion bottom
  74.         char winner = ' ';
  75.         if (CheckWin(board, out winner))
  76.         {
  77.             if (winner == 'X')
  78.             {
  79.                 resultCounts["X"]++;
  80.                 return;
  81.             }
  82.             else
  83.             {
  84.                 resultCounts["O"]++;
  85.                 return;
  86.             }
  87.         }
  88.  
  89.         if (board.FreePostions == 0)
  90.         {
  91.             resultCounts["Draw"]++;
  92.             return;
  93.         }
  94.  
  95.         // Recursive call
  96.         for (int row = 0; row < 3; row++)
  97.         {
  98.             for (int col = 0; col < 3; col++)
  99.             {
  100.                 if (board.Cells[row,col] == '-')
  101.                 {
  102.                     board.Cells[row, col] = playerOnTurn;
  103.                     board.FreePostions--;
  104.                    
  105.                     FindResult(board, resultCounts, SwitchPlayerOnTurn(playerOnTurn));
  106.  
  107.                     board.Cells[row, col] = '-';
  108.                     board.FreePostions++;
  109.                 }
  110.             }
  111.         }
  112.     }
  113.  
  114.     private static bool CheckWin(Board board, out char winner)
  115.     {
  116.         bool result = false;
  117.         winner = ' ';
  118.  
  119.         for (int i = 0; i < 3; i++)
  120.         {
  121.             // By rows
  122.             if (board.Cells[i, 0] != '-' && board.Cells[i, 0] == board.Cells[i, 1] && board.Cells[i, 0] == board.Cells[i, 2])
  123.             {
  124.                 winner = board.Cells[i, 0];
  125.                 return true;
  126.             }
  127.  
  128.             // By columns
  129.             if (board.Cells[0, i] != '-' && board.Cells[0, i] == board.Cells[1, i] && board.Cells[0, i] == board.Cells[2, i])
  130.             {
  131.                 winner = board.Cells[0, i];
  132.                 return true;
  133.             }
  134.         }
  135.  
  136.         // By diags
  137.         if (board.Cells[0, 0] != '-' && board.Cells[0, 0] == board.Cells[1, 1] && board.Cells[0, 0] == board.Cells[2, 2])
  138.         {
  139.             winner = board.Cells[0, 0];
  140.             return true;
  141.         }
  142.         if (board.Cells[0, 2] != '-' && board.Cells[0, 2] == board.Cells[1, 1] && board.Cells[0, 2] == board.Cells[2, 0])
  143.         {
  144.             winner = board.Cells[0, 2];
  145.             return true;
  146.         }
  147.  
  148.         return result;
  149.     }
  150.  
  151.     private static char SwitchPlayerOnTurn(char playerOnTurn)
  152.     {
  153.         if (playerOnTurn == 'X')
  154.         {
  155.             playerOnTurn = 'O';
  156.         }
  157.         else
  158.         {
  159.             playerOnTurn = 'X';
  160.         }
  161.  
  162.         return playerOnTurn;
  163.     }
  164.  
  165.     private static void PrintResult(Dictionary<string, int> resultCounts)
  166.     {
  167.         Console.WriteLine(resultCounts["X"]);
  168.         Console.WriteLine(resultCounts["Draw"]);
  169.         Console.WriteLine(resultCounts["O"]);
  170.     }
  171.  
  172.     private static void PrintBoard(Board board, Dictionary<string, int> resultCounts)
  173.     {
  174.         for (int r = 0; r < 3; r++)
  175.         {
  176.             for (int c = 0; c < 3; c++)
  177.             {
  178.                 Console.Write(board.Cells[r, c]);
  179.             }
  180.             Console.WriteLine();
  181.         }
  182.  
  183.         foreach (var entry in resultCounts)
  184.         {
  185.             Console.WriteLine("{0} {1}", entry.Key, entry.Value);
  186.         }
  187.         Console.WriteLine();
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement