Esempio n. 1
0
        private bool IsGameOver(BoardSlotValues[,] slotsArray, BoardSlotValues playerType)
        {
            BoardSlotValues[,] winStates = new BoardSlotValues[8, 3] {
                { slotsArray[0, 0], slotsArray[0, 1], slotsArray[0, 2] },
                { slotsArray[1, 0], slotsArray[1, 1], slotsArray[1, 2] },
                { slotsArray[2, 0], slotsArray[2, 1], slotsArray[2, 2] },
                { slotsArray[0, 0], slotsArray[1, 0], slotsArray[2, 0] },
                { slotsArray[0, 1], slotsArray[1, 1], slotsArray[2, 1] },
                { slotsArray[0, 2], slotsArray[1, 2], slotsArray[2, 2] },
                { slotsArray[0, 0], slotsArray[1, 1], slotsArray[2, 2] },
                { slotsArray[2, 0], slotsArray[1, 1], slotsArray[0, 2] }
            };

            for (int i = 0; i < winStates.GetLength(0); i++)
            {
                int filled = 0;
                for (int j = 0; j < winStates.GetLength(1); j++)
                {
                    if (winStates[i, j] == playerType)
                    {
                        filled++;
                    }
                }

                if (filled == 3)
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
        private BoardSlotValues[,] ConvertSlotsToArray(List <BoardSlotValues> slots)
        {
            int size = (int)Math.Sqrt(slots.Count);

            BoardSlotValues[,] slotsArray = new BoardSlotValues[size, size];

            for (int x = 0; x < size; x++)
            {
                for (int y = 0; y < size; y++)
                {
                    slotsArray[x, y] = slots[y * size + x];
                }
            }

            return(slotsArray);
        }
Esempio n. 3
0
        private int[] MiniMax(Board board, BoardSlotValues[,] slots, int emptySlots, BoardSlotValues playerType)
        {
            int[] bestMove;

            if (playerType == board.AIValue)
            {
                bestMove = new int[3] {
                    -1, -1, -1000
                }
            }
            ;
            else
            {
                bestMove = new int[3] {
                    -1, -1, 1000
                }
            };

            if (emptySlots == 0 || IsGameOver(slots, board.AIValue) || IsGameOver(slots, board.PlayerValue))
            {
                return(EvaluateScore(board, slots));
            }

            foreach (KeyValuePair <Point, BoardSlotValues> kv in GetFreeSlots(slots))
            {
                int x = kv.Key.X;
                int y = kv.Key.Y;

                slots[x, y] = playerType;
                int[] score = MiniMax(board, slots, emptySlots - 1, playerType == board.AIValue ? board.PlayerValue : board.AIValue);
                slots[x, y] = 0;
                score[0]    = x;
                score[1]    = y;

                if (playerType == board.AIValue && score[2] > bestMove[2])
                {
                    bestMove = score;
                }
                else if (playerType == board.PlayerValue && score[2] < bestMove[2])
                {
                    bestMove = score;
                }
            }

            return(bestMove);
        }