コード例 #1
0
        private TicTacToeOutcome <Player> CheckLine(Game game, List <Tuple <int, int> > line)
        {
            List <string> pieces = new List <string>();

            foreach (Tuple <int, int> i in line)
            {
                pieces.Add(game.getPiece(i.Item1, i.Item2));
            }

            if (pieces.Contains("X") && pieces.Contains("O"))
            {
                return(TicTacToeOutcome <Player> .Draw);
            }

            if (pieces.Contains(""))
            {
                return(TicTacToeOutcome <Player> .Undecided);
            }

            if (pieces.Contains("X"))
            {
                return(TicTacToeOutcome <Player> .NewWin(Player.CROSS, line));
            }
            return(TicTacToeOutcome <Player> .NewWin(Player.NOUGHT, line));
        }
コード例 #2
0
        //Determine the outcome of a single line
        public TicTacToeOutcome <Player> LineOutcome(List <Tuple <int, int> > line, Game game)
        {
            List <Player> listObj = LineAsPlayers(line, game);

            if (listObj.Contains(Nought) && listObj.Contains(Cross))
            {
                return(TicTacToeOutcome <Player> .Draw);
            }
            if (listObj.TrueForAll((obj) => obj == Nought))
            {
                return(TicTacToeOutcome <Player> .NewWin(Nought, line.AsEnumerable()));
            }
            if (listObj.TrueForAll((obj) => obj == Cross))
            {
                return(TicTacToeOutcome <Player> .NewWin(Cross, line.AsEnumerable()));
            }
            return(TicTacToeOutcome <Player> .Undecided);
        }
コード例 #3
0
 /*
  * GameOutcome: return the outcome of the game, in Winning case, the NewWin function is called
  *              to create a win case
  * params:
  * @ game : Game
  * return: TicTacToeOutcome<Player>
  */
 public TicTacToeOutcome <Player> GameOutcome(Game game)
 {
     if (Won(game, "X").Any())
     {
         return(TicTacToeOutcome <Player> .NewWin(Cross, Won(game, "X")));
     }
     else if (Won(game, "O").Any())
     {
         return(TicTacToeOutcome <Player> .NewWin(Nought, Won(game, "O")));
     }
     else if (Draw(game))
     {
         return(TicTacToeOutcome <Player> .Draw);
     }
     else
     {
         return(TicTacToeOutcome <Player> .Undecided);
     }
 }
コード例 #4
0
        public TicTacToeOutcome <Player> GameOutcome(Game game)
        {
            TicTacToeOutcome <Player> result = TicTacToeOutcome <Player> .Draw;
            List <Tuple <int, int>[]> lines  = GenerateLines(game);

            for (int i = 0; i < lines.Count; i++)
            {
                string [] line = new string [game.Size];
                for (int p = 0; p < lines[i].Length; p++)
                {
                    Tuple <int, int> coord = lines[i][p];
                    line[p] = game.Board[coord.Item1, coord.Item2];
                }
                Tuple <int, int>[] z = lines[i];
                int x_count          = Array.FindAll(line, e => e == "X").Length;
                int o_count          = Array.FindAll(line, e => e == "O").Length;
                int space_count      = Array.FindAll(line, e => e == "").Length;
                if (result == TicTacToeOutcome <Player> .Draw || result == TicTacToeOutcome <Player> .Undecided)
                {
                    if (x_count == game.Size)
                    {
                        result = TicTacToeOutcome <Player> .NewWin(Player.Cross, lines[i]);
                    }
                    else if (o_count == game.Size)
                    {
                        result = TicTacToeOutcome <Player> .NewWin(Player.Nought, lines[i]);
                    }
                    else if (o_count >= 1 && x_count >= 1 && result == TicTacToeOutcome <Player> .Draw)
                    {
                        result = TicTacToeOutcome <Player> .Draw;
                    }
                    else
                    {
                        result = TicTacToeOutcome <Player> .Undecided;
                    }
                }
            }
            return(result);
        }
コード例 #5
0
        public TicTacToeOutcome <Player> GameOutcome(Game game)
        {
            // Winning Lines
            List <List <Tuple <int, int> > > boardList = new List <List <Tuple <int, int> > >();

            for (int i = 0; i <= game.Size - 1; i++)
            {
                // HORIZONTAL LINE
                List <Tuple <int, int> > horizontalSeq = new List <Tuple <int, int> >();
                for (int j = 0; j <= game.Size - 1; j++)
                {
                    horizontalSeq.Add(Tuple.Create(i, j));
                }
                boardList.Add(horizontalSeq);
            }
            // VERTICAL LINE
            for (int i = 0; i <= game.Size - 1; i++)
            {
                List <Tuple <int, int> > verticalSeq = new List <Tuple <int, int> >();
                for (int j = 0; j <= game.Size - 1; j++)
                {
                    verticalSeq.Add(Tuple.Create(j, i));
                }
                boardList.Add(verticalSeq);
            }

            // DIAGONAL LINE \
            List <Tuple <int, int> > diagonalLeftSeq = new List <Tuple <int, int> >();

            for (int i = 0; i <= game.Size - 1; i++)
            {
                diagonalLeftSeq.Add(Tuple.Create(i, i));
            }
            boardList.Add(diagonalLeftSeq);

            // DIAGONAL LINE /
            List <Tuple <int, int> > diagonalRightSeq = new List <Tuple <int, int> >();

            for (int i = 0; i <= game.Size - 1; i++)
            {
                diagonalRightSeq.Add(Tuple.Create(i, game.Size - 1 - i));
            }
            boardList.Add(diagonalRightSeq);

            List <TicTacToeOutcome <Player> > outcomeList = new List <TicTacToeOutcome <Player> >();

            foreach (List <Tuple <int, int> > lineList in boardList)
            {
                List <string> currentStateList = new List <string>();
                foreach (Tuple <int, int> i in lineList)
                {
                    currentStateList.Add(game.Board[i.Item1, i.Item2]);
                }

                if (currentStateList.TrueForAll(x => x == "O"))
                {
                    outcomeList.Add(TicTacToeOutcome <Player> .NewWin(Nought, lineList));
                }
                else if (currentStateList.TrueForAll(x => x == "X"))
                {
                    outcomeList.Add(TicTacToeOutcome <Player> .NewWin(Cross, lineList));
                }
                else if (currentStateList.TrueForAll(x => x == ""))
                {
                    outcomeList.Add(TicTacToeOutcome <Player> .Undecided);
                }
                else
                {
                    if (currentStateList.Contains("O") && currentStateList.Contains("X"))
                    {
                        outcomeList.Add(TicTacToeOutcome <Player> .Draw);
                    }
                    else
                    {
                        outcomeList.Add(TicTacToeOutcome <Player> .Undecided);
                    }
                }
            }

            // If it contains a win, return win
            foreach (TicTacToeOutcome <Player> i in outcomeList)
            {
                if (i.IsWin)
                {
                    return(i);
                }
            }
            if (outcomeList.TrueForAll(x => x.IsDraw))
            {
                return(TicTacToeOutcome <Player> .Draw);
            }
            else
            {
                return(TicTacToeOutcome <Player> .Undecided);
            }
        }
        // Note to tutor: Placed in one function
        // to make finding Win and draw convenient
        public TicTacToeOutcome <Player> GameOutcome(Game game)
        {
            // Check how many lines have draws
            int numDraw = 0;
            // Define variables for reuse
            int countN = 0;                                                        // Count Noughts
            int countC = 0;                                                        // Count Crosses
            List <Tuple <int, int> > winningLine = new List <Tuple <int, int> >(); // Assume a winning line

            //Horizontal Lines
            // For each row
            for (int x = 0; x < game.Size; x++)
            {
                countN      = 0;
                countC      = 0;
                winningLine = new List <Tuple <int, int> >();
                // For each column
                for (int y = 0; y < game.Size; y++)
                {
                    Tuple <int, int> coOrd = Tuple.Create <int, int>(x, y);
                    winningLine.Add(coOrd);
                    if (game.getPiece(x, y) == "X")
                    {
                        countC++;
                    }
                    else if (game.getPiece(x, y) == "O")
                    {
                        countN++;
                    }
                }

                // Check for wins and draw
                if (countN == game.Size)
                {
                    return(TicTacToeOutcome <Player> .NewWin(Player.NOUGHT, winningLine));
                }
                else if (countC == game.Size)
                {
                    return(TicTacToeOutcome <Player> .NewWin(Player.CROSS, winningLine));
                }
                else if ((countC > 0) && (countN > 0))
                {
                    numDraw++;
                }
            }

            //Vertical Lines
            // For each column
            for (int y = 0; y < game.Size; y++)
            {
                countN      = 0;
                countC      = 0;
                winningLine = new List <Tuple <int, int> >();
                // For each row
                for (int x = 0; x < game.Size; x++)
                {
                    Tuple <int, int> coOrd = Tuple.Create <int, int>(x, y);
                    winningLine.Add(coOrd);
                    if (game.getPiece(x, y) == "X")
                    {
                        countC++;
                    }
                    else if (game.getPiece(x, y) == "O")
                    {
                        countN++;
                    }
                }

                // Check for wins and draw
                if (countN == game.Size)
                {
                    return(TicTacToeOutcome <Player> .NewWin(Player.NOUGHT, winningLine));
                }
                else if (countC == game.Size)
                {
                    return(TicTacToeOutcome <Player> .NewWin(Player.CROSS, winningLine));
                }
                else if ((countC > 0) && (countN > 0))
                {
                    numDraw++;
                }
            }

            // Check Top Left Diagonal
            countN      = 0;
            countC      = 0;
            winningLine = new List <Tuple <int, int> >();
            // Top left diagonal is where both co-ordinates are thee same
            for (int x = 0; x < game.Size; x++)
            {
                Tuple <int, int> coOrd = Tuple.Create <int, int>(x, x);
                winningLine.Add(coOrd);
                if (game.getPiece(x, x) == "X")
                {
                    countC++;
                }
                else if (game.getPiece(x, x) == "O")
                {
                    countN++;
                }
            }

            // Check for wins and draw
            if (countN == game.Size)
            {
                return(TicTacToeOutcome <Player> .NewWin(Player.NOUGHT, winningLine));
            }
            else if (countC == game.Size)
            {
                return(TicTacToeOutcome <Player> .NewWin(Player.CROSS, winningLine));
            }
            else if ((countC > 0) && (countN > 0))
            {
                numDraw++;
            }

            // Top right diagonal
            // Reset Variables
            countN      = 0;
            countC      = 0;
            winningLine = new List <Tuple <int, int> >();
            for (int x = 0; x < game.Size; x++)
            {
                Tuple <int, int> coOrd = Tuple.Create <int, int>(x, game.Size - 1 - x);
                winningLine.Add(coOrd);
                if (game.getPiece(x, game.Size - 1 - x) == "X")
                {
                    countC++;
                }
                else if (game.getPiece(x, game.Size - 1 - x) == "O")
                {
                    countN++;
                }
            }

            // Check for wins and draw
            if (countN == game.Size)
            {
                return(TicTacToeOutcome <Player> .NewWin(Player.NOUGHT, winningLine));
            }
            else if (countC == game.Size)
            {
                return(TicTacToeOutcome <Player> .NewWin(Player.CROSS, winningLine));
            }
            else if ((countC > 0) && (countN > 0))
            {
                numDraw++;
            }

            // Only draws if the number of draws fond
            // equal to number of lines
            if (numDraw == (game.Size * 2 + 2))
            {
                return(TicTacToeOutcome <Player> .Draw);
            }

            // Else undecided
            return(TicTacToeOutcome <Player> .Undecided);
        }
コード例 #7
0
ファイル: Model.cs プロジェクト: Pi-lot/TicTacToe-MiniMax
        public TicTacToeOutcome <Player> GameOutcome(Game game)
        {
            Player winner;

            bool win  = false;
            bool draw = false;

            int winningLine = 0;

            string nought = "O";
            string cross  = "X";

            int draws = 0;

            int rowCountX = 0;
            int rowCountO = 0;

            int colCountX = 0;
            int colCountO = 0;

            int dia0CountX = 0;
            int dia0CountO = 0;
            int dia1CountX = 0;
            int dia1CountO = 0;

            for (int i = 0; i < game.Size; i++)
            {
                for (int j = 0; j < game.Size; j++)
                {
                    if (game.getPiece(i, j) == cross)
                    {
                        rowCountX++;
                    }
                    else if (game.getPiece(i, j) == nought)
                    {
                        rowCountO++;
                    }

                    if (game.getPiece(j, i) == cross)
                    {
                        colCountX++;
                    }
                    else if (game.getPiece(j, i) == nought)
                    {
                        colCountO++;
                    }
                }

                if (game.getPiece(i, i) == cross)
                {
                    dia0CountX++;
                }
                else if (game.getPiece(i, i) == nought)
                {
                    dia0CountO++;
                }

                if (game.getPiece(game.Size - 1 - i, i) == cross)
                {
                    dia1CountX++;
                }
                else if (game.getPiece(game.Size - 1 - i, i) == nought)
                {
                    dia1CountO++;
                }

                if (rowCountO == game.Size || rowCountX == game.Size || colCountO == game.Size || colCountX == game.Size ||
                    dia0CountO == game.Size || dia0CountX == game.Size || dia1CountO == game.Size || dia1CountX == game.Size)
                {
                    win = true;
                }

                else if (rowCountO + rowCountX == game.Size)
                {
                    draws++;
                }

                winningLine = i;

                if (win)
                {
                    break;
                }

                rowCountO = 0;
                rowCountX = 0;
                colCountO = 0;
                colCountX = 0;
            }

            List <System.Tuple <int, int> > line = new List <System.Tuple <int, int> >();

            System.Tuple <int, int> tuple;
            if (rowCountO == game.Size || rowCountX == game.Size)
            {
                for (int i = 0; i < game.Size; i++)
                {
                    tuple = new System.Tuple <int, int>(winningLine, i);
                    line.Add(tuple);
                }
            }
            else if (colCountO == game.Size || colCountX == game.Size)
            {
                for (int i = 0; i < game.Size; i++)
                {
                    tuple = new System.Tuple <int, int>(i, winningLine);
                    line.Add(tuple);
                }
            }
            else if (dia0CountO == game.Size || dia0CountX == game.Size)
            {
                for (int i = 0; i < game.Size; i++)
                {
                    tuple = new System.Tuple <int, int>(i, i);
                    line.Add(tuple);
                }
            }
            else
            {
                for (int i = 0; i < game.Size; i++)
                {
                    tuple = new System.Tuple <int, int>(game.Size - 1 - i, i);
                    line.Add(tuple);
                }
            }


            if (draws == game.Size)
            {
                draw = true;
            }

            if (win)
            {
                if (rowCountO == game.Size || colCountO == game.Size || dia0CountO == game.Size || dia1CountO == game.Size)
                {
                    winner = Player.Nought;
                }
                else
                {
                    winner = Player.Cross;
                }
                return(TicTacToeOutcome <Player> .NewWin(winner, line));
            }
            else if (draw)
            {
                return(TicTacToeOutcome <Player> .Draw);
            }
            else
            {
                return(TicTacToeOutcome <Player> .Undecided);
            }
        }