Example #1
0
 public Grid GetNewGame()
 {
     Grid toReturn = new Grid()
     {
         CurrentMessage = Message.GetMessage(ConnectFourMessage.NewGame),
         MoveNumber = 1,
         NextPlayerToPlay = 1,
         NumberColumns = 7,
         NumberLines = maxPawnInColumns
     };
     return toReturn;
 }
Example #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 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;
        }
Example #3
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;
        }
Example #4
0
        public void PawnPromoted_GoodWorkflow_Success()
        {
            LocalChessManager = new ChessManager();
            CurrentGid = LocalChessManager.GetNewGame();
            int nextPlayer;
            nextPlayer = Play(1, 1, 2, 1, 3).NextPlayerToPlay;
            nextPlayer = Play(2, 1, 7, 1, 5).NextPlayerToPlay;
            nextPlayer = Play(1, 2, 1, 3, 3).NextPlayerToPlay;
            nextPlayer = Play(2, 1, 5, 1, 4).NextPlayerToPlay;
            nextPlayer = Play(1, 3, 3, 1, 4).NextPlayerToPlay;
            nextPlayer = Play(2, 1, 8, 1, 5).NextPlayerToPlay;
            nextPlayer = Play(1, 1, 4, 3, 3).NextPlayerToPlay;
            nextPlayer = Play(2, 1, 5, 2, 5).NextPlayerToPlay;
            nextPlayer = Play(1, 1, 3, 1, 4).NextPlayerToPlay;
            nextPlayer = Play(2, 2, 5, 3, 5).NextPlayerToPlay;
            nextPlayer = Play(1, 1, 4, 1, 5).NextPlayerToPlay;
            nextPlayer = Play(2, 3, 5, 2, 5).NextPlayerToPlay;
            nextPlayer = Play(1, 1, 5, 1, 6).NextPlayerToPlay;
            nextPlayer = Play(2, 2, 5, 3, 5).NextPlayerToPlay;
            nextPlayer = Play(1, 1, 6, 1, 7).NextPlayerToPlay;
            nextPlayer = Play(2, 3, 5, 2, 5).NextPlayerToPlay;
            var grid = Play(1, 1, 7, 1, 8);
            Assert.AreEqual(9, grid.NumberLines);
            var extraPawn = grid.PawnLocations.Where(p => p.Coord.y == 9);
            Assert.AreEqual(4, extraPawn.Count());
            Assert.AreEqual(1, grid.MoveNumber);
            Assert.AreEqual(1, grid.NextPlayerToPlay);
            //Choice of taking ... A queen!
            Grid ToReturn = LocalChessManager.Play(CurrentGid, new Move() { PlayerNumber = 1, Positions = new List<Pawn> { new Pawn("", 1, 9)} });
            CurrentGid = ToReturn;

            nextPlayer = Play(2, 2, 5, 3, 5).NextPlayerToPlay;
            Assert.AreEqual(1, grid.NextPlayerToPlay);
            grid = Play(1, 1, 8, 1, 2);
            Assert.AreEqual(2, grid.NextPlayerToPlay);
            var newQueen = grid.PawnLocations.Where(p => p.Coord.x == 1 && p.Coord.y == 2).FirstOrDefault();
            Assert.IsNotNull(newQueen);
        }
Example #5
0
 public Grid Play(int playerNumber, int x, int y, int x2, int y2)
 {
     Grid ToReturn = LocalChessManager.Play(CurrentGid, new Move() { PlayerNumber = playerNumber, Positions = new List<Pawn> { new Pawn("", x, y), new Pawn("", x2, y2) } });
     CurrentGid = ToReturn;
     return ToReturn;
 }
Example #6
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;
        }
Example #7
0
        public Grid Play(Grid currentState, Move move)
        {
            //0-Game already finished, no more move
            if (currentState.CurrentMessage.Id == (int)ClassicMessage.GameFinished)
            {
                return currentState;
            }

            //0-Not Your turn
            if (move.PlayerNumber != currentState.NextPlayerToPlay)
            {
                currentState.CurrentMessage = Message.GetMessage(ClassicMessage.NotYourTurn);
                return currentState;
            }

            //Not enough information for doing a move!
            if (move.Positions.Count < 1)
            {
                currentState.CurrentMessage = Message.GetMessage(ChessMessage.NotEnoughMove);
                return currentState;
            }

            int x_init = move.Positions[0].Coord.x;
            int y_init = move.Positions[0].Coord.y;
            Board b = GenerateBoardFromGrid(currentState);

            //Choice for promoted pawn is done
            if (currentState.NumberLines == 9 && move.Positions.Count == 1)
            {
                b.SetPawnPromoted(x_init);
                var finalState = GetGridFromBoard(b);
                finalState.NextPlayerToPlay = (currentState.NextPlayerToPlay == 1) ? 2 : 1;
                finalState.LastMove = move;
                return finalState;
            }

            //Not enough information for doing a move!
            if (move.Positions.Count != 2)
            {
                currentState.CurrentMessage = Message.GetMessage(ChessMessage.NotEnoughMove);
                return currentState;
            }

            int x_end = move.Positions[1].Coord.x;
            int y_end = move.Positions[1].Coord.y;
            Piece p = b.GetPiece(x_init, y_init);

            if (p == null)
            {
                currentState.CurrentMessage = Message.GetMessage(ChessMessage.NoPawnHere);
                return currentState;
            }

            if (!IsGoodColor(p.ChessColor, move.PlayerNumber))
            {
                currentState.CurrentMessage = Message.GetMessage(ChessMessage.NotYourPawn);
                return currentState;
            }

            var result = b.MovePiece(x_init, y_init, x_end, y_end);
            var newState = GetGridFromBoard(b);
            newState.NextPlayerToPlay = currentState.NextPlayerToPlay;
            if (result.IsSuccess)
            {
                if (newState.NumberLines == 8)
                {
                    newState.NextPlayerToPlay = (currentState.NextPlayerToPlay == 1) ? 2 : 1;
                    newState.LastMove = move;
                    newState.CurrentMessage = Message.GetMessage((newState.NextPlayerToPlay == 2) ? ChessMessage.J2 : ChessMessage.J1);
                }
                else
                {
                    //If a pawn is at his last position, we can change it by what we want.
                    //The 9th column is the choose.
                    newState.NextPlayerToPlay = currentState.NextPlayerToPlay;
                    newState.LastMove = move;
                    newState.CurrentMessage = currentState.CurrentMessage;
                }
            }
            else
            {
                newState.CurrentMessage = Message.GetMessage(ChessMessage.InvalidMove);
                newState.CurrentMessage.Information = result.Description;
            }
            return newState;
        }
Example #8
0
        private Grid GetGridFromBoard(Board b)
        {
            Grid toReturn = new Grid()
            {
                CurrentMessage = Message.GetMessage(ChessMessage.NewGame),
                IsGridShifted = false,
                MoveNumber = 2,
                NextPlayerToPlay = 1,
                NumberColumns = 8,
                NumberLines = 8
            };

            toReturn.PawnLocations = GetAllPieces(b);

            if (b.PawnPromoted)
            {
                //We list the propositions :
                toReturn.NumberLines = 9;
                toReturn.MoveNumber = 1;
            }

            return toReturn;
        }
Example #9
0
 private Board GenerateBoardFromGrid(Grid currentState)
 {
     Board b = new Board(false);
     List<Tuple<char, int>> HaveAlreadyMoved = new List<Tuple<char, int>>();
     //0 Special Pawn
     foreach (Pawn pawn in currentState.PawnLocations)
     {
         if(pawn.Coord.x < 1 || pawn.Coord.y < 1){
             //Special Pawn!
             if (pawn.Name == EnPassantSpecialName)
             {
                 b.EnPassant = new Tuple<char, int>(Board.Columns_Inv[Math.Abs(pawn.Coord.x)], Math.Abs(pawn.Coord.y));
             }
             else
             {
                 if (pawn.Name == HaveAlreadyMovedSpecialName)
                 {
                     var hasAlreadyMoved = new Tuple<char, int>(Board.Columns_Inv[Math.Abs(pawn.Coord.x)], Math.Abs(pawn.Coord.y));
                     HaveAlreadyMoved.Add(hasAlreadyMoved);
                 }
                 else
                 {
                     throw new Exception(String.Format("Piece Unknown ({0}) or badly located: [{1},{2}]", pawn.Name, pawn.Coord.x, pawn.Coord.y));
                 }
             }
         }
         else{
             if (pawn.Coord.x < 9 && pawn.Coord.y < 9)
             {
                 var p = GetPiece(pawn);
                 b.PutPiece(p, Board.Columns_Inv[pawn.Coord.x], pawn.Coord.y);
             }
         }
     }
     foreach (var hasAlreadyMoved in HaveAlreadyMoved)
     {
         var piece = b.GetPiece(hasAlreadyMoved.Item1, hasAlreadyMoved.Item2);
         ((IHasAlreadyMoved)piece).HasAlreadyMoved = true;
     }
     return b;
 }
Example #10
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;
 }