Beispiel #1
0
        public IGameMove[] GetPossibleMoves(IGameState gameState, GamePlayer player)
        {
            if (null == gameState)
            {
                throw new ArgumentNullException("gameState");
            }

            FourInARowState state = (FourInARowState)gameState;

            List <IGameMove> moves = new List <IGameMove>();

            for (Int32 q = 0, mq = FourInARowState.ColumnCount; q < mq; q++)
            {
                if (state.Get(q, FourInARowState.RowCount - 1) == FourInARowFieldState.Empty)
                {
                    moves.Add(new FourInARowMove
                    {
                        Column = q,
                        State  = player == GamePlayer.PlayerMax ? FourInARowFieldState.Cross : FourInARowFieldState.Circle
                    });
                }
            }

            return(moves.ToArray());
        }
Beispiel #2
0
        private static PartialResults CheckDiagonals(FourInARowState state)
        {
            PartialResults summary = new PartialResults();

            //// from left bottom to right top

            for (Int32 startY = 0, mStartY = FourInARowState.RowCount - WinningCount + 1; startY < mStartY; startY++)
            {
                FieldContext context = new FieldContext();

                for (Int32 x = 0, y = startY, my = FourInARowState.RowCount, mx = FourInARowState.ColumnCount; y < my && x < mx /* && y < state.LastEmptyRow */; x++, y++)
                {
                    ProcessField(state, x, y, context);
                }

                summary.Add(context.PartialResults);
            }

            for (Int32 startX = 1, mStartX = FourInARowState.ColumnCount - WinningCount + 1; startX < mStartX; startX++)
            {
                FieldContext context = new FieldContext();

                for (Int32 x = startX, y = 0, my = FourInARowState.RowCount, mx = FourInARowState.ColumnCount; y < my && x < mx /* && y < state.LastEmptyRow */; x++, y++)
                {
                    ProcessField(state, x, y, context);
                }

                summary.Add(context.PartialResults);
            }

            //// from right bottom to left top

            for (Int32 startY = 0, mStartY = FourInARowState.RowCount - WinningCount + 1; startY < mStartY; startY++)
            {
                FieldContext context = new FieldContext();

                for (Int32 x = FourInARowState.ColumnCount - 1, y = startY, my = FourInARowState.RowCount; y < my && x >= 0 /* && y < state.LastEmptyRow */; x--, y++)
                {
                    ProcessField(state, x, y, context);
                }

                summary.Add(context.PartialResults);
            }

            for (Int32 sStartX = FourInARowState.ColumnCount - 2, mStartX = WinningCount - 1; sStartX >= mStartX; sStartX--)
            {
                FieldContext context = new FieldContext();

                for (Int32 x = sStartX, y = 0, my = FourInARowState.RowCount; y < my && x >= 0 /* && y < state.LastEmptyRow */; x--, y++)
                {
                    ProcessField(state, x, y, context);
                }

                summary.Add(context.PartialResults);
            }

            return(summary);
        }
Beispiel #3
0
        private static PartialResults CheckColumns(FourInARowState state)
        {
            PartialResults summary = new PartialResults();

            for (Int32 x = 0, mx = FourInARowState.ColumnCount; x < mx; x++)
            {
                FieldContext context = new FieldContext();

                for (Int32 y = 0, my = FourInARowState.RowCount; y < my /* && y < state.LastEmptyRow */; y++)
                {
                    ProcessField(state, x, y, context);
                }

                summary.Add(context.PartialResults);
            }

            return(summary);
        }
Beispiel #4
0
        internal FourInARowState(FourInARowState source)
        {
            if (null == source)
            {
                throw new ArgumentNullException("source");
            }

            _nextMoveIndexes = (Int32[])source._nextMoveIndexes.Clone();

            _fields = new FourInARowFieldState[ColumnCount][];

            for (Int32 q = 0; q < _fields.Length; q++)
            {
                _fields[q] = (FourInARowFieldState[])source._fields[q].Clone();
            }

            _stateDesc = (Char[])source._stateDesc.Clone();

            _lastEmptyRow = source._lastEmptyRow;
        }
Beispiel #5
0
        public Int32 Evaluate(IGameState gameState, GamePlayer player)
        {
            FourInARowState state = (FourInARowState)gameState;

            PartialResults summary = new PartialResults();

            PartialResults result = CheckColumns(state);

            summary.Add(result);

            result = CheckRows(state);

            summary.Add(result);

            result = CheckDiagonals(state);

            summary.Add(result);

            return(CalculateScore(summary, player));
        }
Beispiel #6
0
        public IGameState MakeMove(IGameMove gameMove, IGameState gameState)
        {
            if (null == gameState)
            {
                throw new ArgumentNullException("gameState");
            }

            if (null == gameMove)
            {
                throw new ArgumentNullException("gameMove");
            }

            FourInARowMove move = (FourInARowMove)gameMove;

            FourInARowState state = (FourInARowState)gameState;

            if (move.Column < 0 || move.Column >= FourInARowState.ColumnCount)
            {
                throw new ArgumentOutOfRangeException("column " + move.Column);
            }

            Int32 nextField = state.Indexes[move.Column];

            if (nextField < FourInARowState.RowCount &&
                state.Get(move.Column, nextField) == FourInARowFieldState.Empty)
            {
                FourInARowState newState = new FourInARowState(state);

                newState.Set(move.Column, nextField, move.State);

                return(newState);
            }
            else
            {
                throw new InvalidOperationException(String.Format("Move not allowed {0} {1}", move.Column, nextField));
            }
        }
Beispiel #7
0
        private static void ProcessField(FourInARowState state, Int32 x, Int32 y, FieldContext context)
        {
            FourInARowFieldState field = state.Get(x, y);

            context.LastFourItems.Add(field);

            if (field == FourInARowFieldState.Empty)
            {
                context.EmptyInRow++;
            }

            if (field == FourInARowFieldState.Circle)
            {
                context.CirclesInRow++;
            }

            if (field == FourInARowFieldState.Cross)
            {
                context.CrossesInRow++;
            }

            if (context.LastFourItems.Count >= 4)
            {
                if (context.LastFourItems.Count > 4)
                {
                    FourInARowFieldState first = context.LastFourItems[0];

                    context.LastFourItems.RemoveAt(0);

                    if (first == FourInARowFieldState.Empty)
                    {
                        context.EmptyInRow--;
                    }

                    if (first == FourInARowFieldState.Circle)
                    {
                        context.CirclesInRow--;
                    }

                    if (first == FourInARowFieldState.Cross)
                    {
                        context.CrossesInRow--;
                    }
                }

                if (context.CirclesInRow == 0)
                {
                    if (context.CrossesInRow == 4)
                    {
                        context.PartialResults.FourCrossesInRow++;
                    }
                    else if (context.CrossesInRow == 2 && context.EmptyInRow == 2)
                    {
                        context.PartialResults.TwoCrossesInRow++;
                    }
                    else if (context.CrossesInRow == 3 && context.EmptyInRow == 1)
                    {
                        context.PartialResults.ThreeCrossesInRow++;
                    }
                    else if (context.CrossesInRow == 1 && context.EmptyInRow == 3)
                    {
                        context.PartialResults.OneCrossInRow++;
                    }
                }

                if (context.CrossesInRow == 0)
                {
                    if (context.CirclesInRow == 4)
                    {
                        context.PartialResults.FourCirclesInRow++;
                    }
                    else if (context.CirclesInRow == 2 && context.EmptyInRow == 2)
                    {
                        context.PartialResults.TwoCirclesInRow++;
                    }
                    else if (context.CirclesInRow == 3 && context.EmptyInRow == 1)
                    {
                        context.PartialResults.ThreeCirclesInRow++;
                    }
                    else if (context.CirclesInRow == 1 && context.EmptyInRow == 3)
                    {
                        context.PartialResults.OneCircleInRow++;
                    }
                }
            }
        }