示例#1
0
        public Grid Play(Grid currentState, Move move)
        {
            //0-Game already finished, no more move
            if (currentState.CurrentMessage.Id == (int)ClassicMessage.GameFinished)
            {
                return currentState;
            }
            //1-Not Your turn
            if (move.PlayerNumber != currentState.NextPlayerToPlay)
            {
                currentState.CurrentMessage = Message.GetMessage(ClassicMessage.NotYourTurn);
                return currentState;
            }

            var choosenX = move.Positions[0].Coord.x;
            var indice = maxPawnInColumns - currentState.PawnLocations.Where(o => o.Coord.x == choosenX).Count();

            //2-Column full
            if (indice == 0)
            {
                currentState.CurrentMessage = Message.GetMessage(ConnectFourMessage.ColumnFull);
                return currentState;
            }

            //3-Case Ok,
            var PawnToAdd = new Pawn(move.PlayerNumber == 1 ? "R" : "Y", choosenX, indice);
            currentState.PawnLocations.Add(PawnToAdd);
            currentState.LastMove = move;
            if (IsFinished(currentState, PawnToAdd))
            {
                //3-1 Case Ok + finished
                currentState.NextPlayerToPlay = 0;
                currentState.CurrentMessage = Message.GetMessage(ClassicMessage.GameFinished);
            }
            else
            {
                //3-1 Case Ok + Not finished
                currentState.NextPlayerToPlay = (currentState.NextPlayerToPlay == 1) ? 2 : 1;
                currentState.CurrentMessage = Message.GetMessage((currentState.NextPlayerToPlay == 2) ? ConnectFourMessage.J2 : ConnectFourMessage.J1);
            }
            return currentState;
        }
示例#2
0
        public Grid Play(Grid currentState, Move move)
        {
            //0-Game already finished, no more move
            if (currentState.CurrentMessage.Id == (int)ClassicMessage.GameFinished)
            {
                return currentState;
            }
            //1-Not Your turn
            if (move.PlayerNumber != currentState.NextPlayerToPlay)
            {
                currentState.CurrentMessage = Message.GetMessage(ClassicMessage.NotYourTurn);
                return currentState;
            }

            var choosenX = move.Positions[0].Coord.x;
            var choosenY = move.Positions[0].Coord.y;
            //2-Case already taken
            if (currentState.PawnLocations.Where(o => o.Coord.x == choosenX && o.Coord.y == choosenY).Any())
            {
                currentState.CurrentMessage = Message.GetMessage(ClassicMessage.NotEmptyPosition);
                return currentState;
            }
            //3-Case Ok,
            var PawnToAdd = new Pawn(move.PlayerNumber == 1 ? "B" : "W", choosenX, choosenY);
            currentState.PawnLocations.Add(PawnToAdd);
            currentState.LastMove = move;
            if (IsFinished(currentState, PawnToAdd))
            {
                //3-1 Case Ok + finished
                currentState.NextPlayerToPlay = 0;
                currentState.CurrentMessage = Message.GetMessage(ClassicMessage.GameFinished);
            }
            else
            {
                //3-1 Case Ok + Not finished
                currentState.NextPlayerToPlay = (currentState.NextPlayerToPlay == 1) ? 2 : 1;
                currentState.CurrentMessage = Message.GetMessage((currentState.NextPlayerToPlay == 2) ? MorpionMessage.J2 : MorpionMessage.J1);
            }
            return currentState;
        }
示例#3
0
        private bool IsFinished(Grid currentState, Pawn pawnToAdd)
        {
            var allPawns = currentState.PawnLocations;

            for (int x = -1; x < 2; x++)
            {
                for (int y = -1; y < 2; y++)
                {
                    if (x == 0 && y == 0)
                    {
                        //We have to do a move at least
                        continue;
                    }

                    int newx, newy;
                    for (int i = 1; i < 4; i++)
                    {
                        newx = pawnToAdd.Coord.x + i*x;
                        newy = pawnToAdd.Coord.y + i*y;
                        if (!isLocationCorrect(newx, newy))
                        {
                            break;
                        }
                        var currentPawn = allPawns.Where(o => o.Coord.x == newx && o.Coord.y == newy).FirstOrDefault();
                        if (currentPawn == null || currentPawn.Name != pawnToAdd.Name) { break; }
                        if (i == 3)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
示例#4
0
 private bool IsFinished(Grid currentState, Pawn newPawn)
 {
     bool ligneFull = currentState.PawnLocations.Where(o => o.Coord.y == newPawn.Coord.y && o.Name == newPawn.Name).Count() == 3;
     bool colonneFull = currentState.PawnLocations.Where(o => o.Coord.x == newPawn.Coord.x && o.Name == newPawn.Name).Count() == 3;
     bool firstDiag = currentState.PawnLocations.Where(o => o.Coord.IsInFirstDiag() && o.Name == newPawn.Name).Count() == 3;
     bool secondDiag = currentState.PawnLocations.Where(o => o.Coord.IsInSecondDiag() && o.Name == newPawn.Name).Count() == 3;
     return ligneFull || colonneFull || firstDiag || secondDiag;
 }