SHARE
TWEET

tic-tac-toe

a guest Sep 12th, 2013 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class Program
  6. {
  7.     static int[] board = new int[9];
  8.     static int[] numofWins = new int[3]; // 0 - "X" player, 1 - "O" player, 2 - tie
  9.     static int filledPositions = 0;
  10.  
  11.     static bool HaveWinner()
  12.     {
  13.         return (board[0] != 2 && board[0] == board[1] && board[0] == board[2]) ||
  14.                (board[3] != 2 && board[3] == board[4] && board[3] == board[5]) ||
  15.                (board[6] != 2 && board[6] == board[7] && board[6] == board[8]) ||
  16.                (board[0] != 2 && board[0] == board[3] && board[0] == board[6]) ||
  17.                (board[1] != 2 && board[1] == board[4] && board[1] == board[7]) ||
  18.                (board[2] != 2 && board[2] == board[5] && board[2] == board[8]) ||
  19.                (board[0] != 2 && board[0] == board[4] && board[0] == board[8]) ||
  20.                (board[2] != 2 && board[2] == board[4] && board[2] == board[6]);
  21.     }
  22.  
  23.     static void SolveBoard(int player)
  24.     {
  25.         for (int i = 0; i < 9; i++)
  26.             if (board[i] == 2)
  27.             {
  28.                 board[i] = player;
  29.                 filledPositions++;
  30.                 if (HaveWinner())
  31.                     numofWins[player]++;
  32.                 else if (filledPositions < 9)
  33.                     SolveBoard(1 - player);
  34.                 else
  35.                     numofWins[2]++;
  36.                 filledPositions--;
  37.                 board[i] = 2;
  38.             }
  39.     }
  40.  
  41.  
  42.     static void Main(string[] args)
  43.     {
  44.         int numOfOs = 0;
  45.         int numOfXs = 0;
  46.         int player = 0;
  47.         string s = "";
  48.         for (int i = 0; i < 9; i++)
  49.         {
  50.             if (i % 3 == 0)
  51.                 s = Console.ReadLine();
  52.             if (s[i % 3] == 'X')
  53.             {
  54.                 board[i] = 0;
  55.                 numOfXs++;
  56.             }
  57.             else if (s[i % 3] == 'O')
  58.             {
  59.                 board[i] = 1;
  60.                 numOfOs++;
  61.             }
  62.             else board[i] = 2;
  63.             filledPositions = numOfOs + numOfXs;
  64.             if (numOfXs > numOfOs)
  65.                 player = 1;
  66.             else
  67.                 player = 0;
  68.         }
  69.         SolveBoard(player);
  70.         Console.WriteLine(numofWins[0]);
  71.         Console.WriteLine(numofWins[2]);
  72.         Console.WriteLine(numofWins[1]);
  73.     }
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top