Ejemplo n.º 1
0
        private bool ProcessSequence(int x, int y, IActor actedPlayer, ref C4Actor lastTrackedPlayer, ref int sequenceLength, out EEndGameStatus result)
        {
            result = EEndGameStatus.Draw;
            C4Actor gridActor = Grid[x, y] as C4Actor;

            if (gridActor == null)
            {
                lastTrackedPlayer = null;
                sequenceLength    = 0;
                return(false);
            }

            if (lastTrackedPlayer == null)
            {
                lastTrackedPlayer = gridActor;
                sequenceLength    = 1;
            }
            else if (lastTrackedPlayer != gridActor)
            {
                lastTrackedPlayer = gridActor;
                sequenceLength    = 1;
            }
            else // lastTrackedPlayer == gridActor
            {
                if (++sequenceLength >= WIN_SEQUENCE_LENGTH)
                {
                    result = lastTrackedPlayer == (C4Actor)actedPlayer ? EEndGameStatus.Win : EEndGameStatus.Loss;
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
 public C4GameState(string aPlayerA, string aPlayerB, C4Actor aJustActed) : base(aPlayerA, aPlayerB, aJustActed, GRID_WIDTH, GRID_HEIGHT)
 {
     for (int x = 0; x < GridWidth; ++x)
     {
         for (int y = 0; y < GridHeight; ++y)
         {
             Grid[x, y] = null;
         }
     }
 }
Ejemplo n.º 3
0
        public override IEnumerable <IAction> GetAllMoves()
        {
            List <IAction> actions = new List <IAction>();

            C4Actor nowActing = new C4Actor(ActorJustActed.Name.Equals(PlayerAName) ? PlayerBName : PlayerAName);

            for (int x = 0; x < GridWidth; ++x)
            {
                for (int y = 0; y < GridHeight; ++y)
                {
                    if (Grid[x, y] == null)
                    {
                        IAction newAction = new C4Action(nowActing, this, x, y);
                        actions.Add(newAction);
                        // only one action is possible per column
                        break;
                    }
                }
            }

            return(actions);
        }
Ejemplo n.º 4
0
 public C4Action(C4Actor aActing, C4GameState aState, int x, int y) : base(aActing)
 {
     this._incomingState = aState;
     this._x             = x;
     this._y             = y;
 }
Ejemplo n.º 5
0
 public C4GameState(C4GameState aOtherGameState, C4Actor aJustActed, AbstractActor[,] aGrid) : base(aOtherGameState.PlayerAName, aOtherGameState.PlayerBName, aJustActed, aGrid)
 {
 }
Ejemplo n.º 6
0
 public C4GameState(string aPlayerA, string aPlayerB, C4Actor aJustActed, AbstractActor[,] aGrid) : base(aPlayerA, aPlayerB, aJustActed, aGrid)
 {
 }
Ejemplo n.º 7
0
        public override EEndGameStatus GetResult(IActor player)
        {
            // check verticals for victory
            for (int x = 0; x < GridWidth; ++x)
            {
                int     sequenceLength    = 0;
                C4Actor lastTrackedPlayer = null;
                for (int y = 0; y < GridHeight; ++y)
                {
                    if (ProcessSequence(x, y, player, ref lastTrackedPlayer, ref sequenceLength, out EEndGameStatus result))
                    {
                        return(result);
                    }
                }
            }

            // check horizontals for victory
            for (int y = 0; y < GridHeight; ++y)
            {
                int     sequenceLength    = 0;
                C4Actor lastTrackedPlayer = null;
                for (int x = 0; x < GridWidth; ++x)
                {
                    if (ProcessSequence(x, y, player, ref lastTrackedPlayer, ref sequenceLength, out EEndGameStatus result))
                    {
                        return(result);
                    }
                }
            }

            // check diagonals for victory
            for (int x = 0; x < GridWidth; ++x)
            {
                for (int y = 0; y < GridHeight; ++y)
                {
                    int     sequenceURLength    = 0;
                    C4Actor lastTrackedPlayerUR = null;

                    int     sequenceULLength    = 0;
                    C4Actor lastTrackedPlayerUL = null;

                    for (int i = 0; i < WIN_SEQUENCE_LENGTH; i++)
                    {
                        try
                        {
                            // diagonally up-right
                            if (ProcessSequence(x + i, y + i, player, ref lastTrackedPlayerUR, ref sequenceURLength, out EEndGameStatus urResult))
                            {
                                return(urResult);
                            }
                        }
                        catch (System.IndexOutOfRangeException e)
                        {
                            // out of range, break
                            break;
                        }

                        try
                        {
                            // diagonally up-left
                            if (ProcessSequence(x - i, y + i, player, ref lastTrackedPlayerUL, ref sequenceULLength, out EEndGameStatus ulResult))
                            {
                                return(ulResult);
                            }
                        }
                        catch (System.IndexOutOfRangeException e)
                        {
                            // out of range, break
                            break;
                        }
                    }
                }
            }

            return(EEndGameStatus.Draw);
        }