title: Calculate Tic-Tac-Toe Game Winner C# || Online Practice assessment test question ||
body: <p><a href="https://i.stack.imgur.com/gl6wd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gl6wd.png" alt="enter image description here" /></a></p>
  <p>I'm trying to figure out if I coded this correctly according to directions within the image.
  Please inform me on what I did wrong.</p>
  <p>// Here's my Code think it right but not 100% sure</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Reflection;
  
  namespace TicTacToe.Template
  {
  public enum CellState
  {
  [Description(&quot;EMPTY&quot;)]
  Empty,
  [Description(&quot;X&quot;)]
  X,
  [Description(&quot;O&quot;)]
  O
  }
  
  public enum Winner
  {
  [Description(&quot;IN_PROGRESS&quot;)]
  InProgress,
  [Description(&quot;TIED&quot;)]
  Tied,
  [Description(&quot;X&quot;)]
  X,
  [Description(&quot;O&quot;)]
  O
  }
  
  public static class EnumExtensions
  {
  public static string GetDescription(this Enum value)
  {
  Type type = value.GetType();
  string name = Enum.GetName(type, value);
  if (name != null)
  {
  FieldInfo field = type.GetField(name);
  if (field != null)
  {
  DescriptionAttribute attr =
  Attribute.GetCustomAttribute(field,
  typeof(DescriptionAttribute)) as DescriptionAttribute;
  if (attr != null)
  {
  return attr.Description;
  }
  }
  }
  return null;
  }
  }
  
  public class TicTacToe
  {
  
  /**
  * Evaluates the game board to determine the winner.
  * &lt;p&gt;
  * If the game is still in progress, then this method returns InProgress.
  * If the game is tied, then this method returns Tied.
  * Otherwise, returns the winner - either X or O.
  *
  * @param cells - an array of 9 game cells that forms the 3x3 grid of the board.
  * @return the Winner - InProgress, Tied, X, or O
  */
  </code></pre>
  <p>//Main part of the code where I added in functionality this function below was blank before.</p>
  <pre><code>public static Winner CalculateWinner(CellState[] cells)
          { 
              if (cells[0] == cells[1] &amp;&amp; cells[1] == cells[2])
              {
                   return Winner.X;
              }
              else if (cells[3] == cells[4] &amp;&amp; cells[4] == cells[5])
              {
                  return Winner.X;
              }
              else if (cells[6] == cells[7] &amp;&amp; cells[7] == cells[8])
              {
                  return Winner.X;
              }    
              else if (cells[0] == cells[3] &amp;&amp; cells[3] == cells[6])
              {
                  return Winner.X;
              }
              else if (cells[1] == cells[4] &amp;&amp; cells[4] == cells[7])
              {
                  return Winner.X;
              }
              else if (cells[2] == cells[5] &amp;&amp; cells[5] == cells[8])
              {
                  return Winner.X;
              }
              else if (cells[0] == cells[4] &amp;&amp; cells[4] == cells[8])
              {
                  return Winner.X;
              }
              else if (cells[2] == cells[4] &amp;&amp; cells[4] == cells[6])
              {
                  return Winner.X;
              }
              else
              {
                  return Winner.Tied;
              }
          }
  
  public static CellState[] ConvertInputLineToCellStateArray(string inputLine)
  {
  if (inputLine.Length != 9)
  {
  throw new ArgumentException(&quot;Invalid state string. Should have 9 characters.&quot;);
  }
  
  List&lt;CellState&gt; cellStates = new List&lt;CellState&gt;();
  
  foreach (char c in inputLine)
  {
  if (c == '_') cellStates.Add(CellState.Empty);
  if (c == 'X') cellStates.Add(CellState.X);
  if (c == 'O') cellStates.Add(CellState.O);
  }
  
  return cellStates.ToArray();
  }
  
  public static void Main(string[] args)
  {
  string inputLine;
  
  while ((inputLine = Console.ReadLine()) != null)
  {
  Console.WriteLine(CalculateWinner(ConvertInputLineToCellStateArray(inputLine)).GetDescription());
  }
  }
  }
  }
  </code></pre>
  <p>PLease ignore these paragraphs below it just basic Rules, info and history about Tic-Tac Toe. It wasn't letting me post cause it was saying I had too much code. Explanation  in the image Above. Thank You.
  Tic-Tac-Toe is a simple and fun game for 2 players, X and O. It is played on a 3x3 grid. Each player's goal is to make 3 in a row.</p>
  <p>Typically, X starts first, but in Gametable's Tabletop Tic Tac Toe, Player 1 starts first on the first game and Player 2 (or the computer) starts first on the next game. The starting player continues to alternate from game to game. This helps keep gameplay fair over time.</p>
  <p>Players take turns placing their Mark, X or O, on an open square in the grid. The first player to make 3 of their own mark in a row vertically, horizontally, or diagonally wins the game.</p>
  <p>If all 9 squares are filled and neither player has 3 in a row, the game is considered a Tie.</p>
  <p>The expert artificial intelligence (AI) will play a perfect game. Try your best to Tie it for as long as you can!</p>
  <p>ZERO-SUM GAME
  Tic-Tac-Toe is a zero-sum game, which means that if both players are playing their best, the game will end in a Tie. However, if you learn how to play tic-tac-toe and master some simple strategies, then you'll be able to not only play, but to win the majority of the time.</p>
  <p>HISTORY
  Tic-Tac-Toe is a long beloved classic pen and paper or board game for two players. Variations of Tic Tac Toe have been played since the Roman Empire and 3 in a row games can be traced all the way back to ancient Egypt!</p>
  <p>NOUGHTS AND CROSSES
  Tic Tac Toe is known by a few other names around the world. The most common of these is &quot;Noughts and Crosses&quot; and it is sometimes simply referred to as the &quot;XO game.&quot;</p>
  <p>gametable.org
  Gametable is an upcoming interactive entertainment company for the desktop and mobile world. We currently have three free online games- dots and boxes, checkers, and tic tac toe. We're actively developing more fun titles by the day. We're offering ad-free online games for a limited time so pull up a chair to the game table today! We value your privacy as much as you do! Privacy Policy, Cookie Policy, and Terms &amp; Conditions.</p>