Example #1
0
 public void CheckForDraw()
 {
     if (Moves.Count() >= 9 && GameStatus == Status.InProgress)
     {
         GameStatus = Status.Draw;
         ColorDraw();
     }
 }
Example #2
0
        /// <summary>
        /// Return 0 if cat game. 1 if player. -1 if computer. Null if no winner.
        /// </summary>
        /// <returns></returns>
        public int?Winner()
        {
            // Rows
            foreach (var row in this)
            {
                if (row.Sum(f => f) == 3)
                {
                    return(1);
                }
                if (row.Sum(f => f) == -3)
                {
                    return(-1);
                }
            }

            // Columns
            for (int col = 0; col < 3; col++)
            {
                if (this[0][col] + this[1][col] + this[2][col] == 3)
                {
                    return(1);
                }
                if (this[0][col] + this[1][col] + this[2][col] == -3)
                {
                    return(-1);
                }
            }

            // Diagonal 1
            if (this[0][0] + this[1][1] + this[2][2] == 3)
            {
                return(1);
            }
            if (this[0][0] + this[1][1] + this[2][2] == -3)
            {
                return(-1);
            }

            // Diagonal 2
            if (this[0][2] + this[1][1] + this[2][0] == 3)
            {
                return(1);
            }
            if (this[0][2] + this[1][1] + this[2][0] == -3)
            {
                return(-1);
            }

            // Cat game
            if (Moves.Count() == 9)
            {
                return(0);
            }

            // No winner.
            return(null);
        }