Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Player p1 = new Player("David", SpaceType.Player1, PlayerColor.Red);
            Player p2 = new Player("Paul", SpaceType.Player2, PlayerColor.Blue);
            Game plop = new Game(p1,p2);

            Position pos1 = new Position();
            pos1.row = 3;
            pos1.col = 4;

            Position pos2 = new Position();
            pos2.row = 6;
            pos2.col = 4;

            plop.setPieceOnGrid(plop.player2Pieces[2], pos1);
            plop.setPieceOnGrid(plop.player1Pieces[0], pos2);

            plop.start();

            plop.initialGrid.displayGrid();

            for (int i = 1; i <8;i++ )
            {
                Position nextPos = new Position();
                nextPos.row = pos1.row+1;
                nextPos.col = pos1.col;
                switch (plop.movePiece(pos1, nextPos))
                {
                    case 1: Console.WriteLine("Piece MOVED !");
                        break;
                    case 10: Console.WriteLine("WIN !!!");
                        break;
                    case 20: Console.WriteLine("TIE !");
                        break;
                    case 30: Console.WriteLine("LOST !!!");
                        break;
                    case 50: Console.WriteLine("You found the flag !");
                        break;
                    default: Console.WriteLine("Move not allowed !");
                        break;
                }
                pos1.row++;
                plop.initialGrid.displayGrid();
            }
            Console.ReadLine();
        }
Exemplo n.º 2
0
        public List<Position> getMoves(Position pos, out int returnCode)
        {
            List<Position> listPos = new List<Position>();
            GridSpace gs = new GridSpace();
            gs=initialGrid.mainGrid[pos.row,pos.col];

            //check if it is not an unplayed grid space and that there is a piece on the space
            if (gs._piece != null && gs._isPlayable)
            {
                //check if the piece can move
                if (gs._piece.pieceCanMove)
                {
                    int max = 1;
                    if (gs._piece.pieceCanRun) max = 10;

                    //find below positions
                    for (int i = pos.row + 1; i < pos.row + max + 1; i++)
                    {
                        if (i > 9 || !initialGrid.mainGrid[i, pos.col]._isPlayable) break;
                        //make sure to stop before if same-team piece
                        if (initialGrid.mainGrid[i, pos.col]._piece != null && initialGrid.mainGrid[i, pos.col]._piece.piecePlayer.playerType == initialGrid.mainGrid[pos.row, pos.col]._piece.piecePlayer.playerType) break;
                        Position p = new Position();
                        p.row = i;
                        p.col = pos.col;
                        listPos.Add(p);
                        if (initialGrid.mainGrid[i, pos.col]._piece != null) break;
                    }

                    //find up positions
                    for (int i = pos.row - 1; i > pos.row - max-1; i--)
                    {
                        if (i < 0 || !initialGrid.mainGrid[i, pos.col]._isPlayable) break;
                        if (initialGrid.mainGrid[i, pos.col]._piece != null && initialGrid.mainGrid[i, pos.col]._piece.piecePlayer.playerType == initialGrid.mainGrid[pos.row, pos.col]._piece.piecePlayer.playerType) break;
                        Position p = new Position();
                        p.row = i;
                        p.col = pos.col;
                        listPos.Add(p);
                        if (initialGrid.mainGrid[i, pos.col]._piece != null) break;

                    }

                    //find left positions
                    for (int i = pos.col - 1; i > pos.col - max - 1; i--)
                    {
                        if (i < 0 || !initialGrid.mainGrid[pos.row, i]._isPlayable) break;
                        if (initialGrid.mainGrid[pos.row, i]._piece != null && initialGrid.mainGrid[pos.row, i]._piece.piecePlayer.playerType == initialGrid.mainGrid[pos.row, pos.col]._piece.piecePlayer.playerType) break;
                        Position p = new Position();
                        p.row = pos.row;
                        p.col = i;
                        listPos.Add(p);
                        if (initialGrid.mainGrid[pos.row, i]._piece != null) break;

                    }

                    //find right positions
                    for (int i = pos.col + 1; i < pos.col + max + 1; i++)
                    {
                        if (i > 9 || !initialGrid.mainGrid[pos.row, i]._isPlayable) break;
                        if (initialGrid.mainGrid[pos.row, i]._piece != null && initialGrid.mainGrid[pos.row, i]._piece.piecePlayer.playerType == initialGrid.mainGrid[pos.row, pos.col]._piece.piecePlayer.playerType) break;
                        Position p = new Position();
                        p.row = pos.row;
                        p.col = i;
                        listPos.Add(p);
                        if (initialGrid.mainGrid[pos.row, i]._piece != null) break;

                    }

                    returnCode = 1;

                }
                else
                {
                    //piece can't move
                    returnCode = 20;
                }
            }
            else
            {
                //not a piece
                returnCode = 30;
            }
            return listPos;
        }
Exemplo n.º 3
0
 private void updateWinGrid(Position p1, Position p2)
 {
     //add piece to lost pieces
     switch(initialGrid.mainGrid[p2.row, p2.col]._piece.piecePlayer.playerType)
     {
         case SpaceType.Player1: break;
         case SpaceType.Player2: break;
         default: break;
     }
     initialGrid.mainGrid[p2.row, p2.col]._piece = initialGrid.mainGrid[p1.row, p1.col]._piece;
     initialGrid.mainGrid[p1.row, p1.col]._piece = null;
 }
Exemplo n.º 4
0
 private void updateMoveGrid(Position p1, Position p2)
 {
     initialGrid.mainGrid[p2.row, p2.col]._piece = initialGrid.mainGrid[p1.row, p1.col]._piece;
     initialGrid.mainGrid[p1.row, p1.col]._piece = null;
 }
Exemplo n.º 5
0
 //method used by players to set up the game
 //they can only set up their pieces in spaces defined in initGrid().
 public bool setPieceOnGrid(Piece p,Position pos)
 {
     if (p.piecePlayer.playerType != initialGrid.mainGrid[pos.row,pos.col]._type) return false;
     initialGrid.mainGrid[pos.row, pos.col]._piece = p;
     return true;
 }
Exemplo n.º 6
0
        public int movePiece(Position now,Position next)
        {
            bool isAllowed=false;
            int returnValue;
            Piece nowPiece=initialGrid.mainGrid[now.row, now.col]._piece;
            Piece nextPiece=initialGrid.mainGrid[next.row, next.col]._piece;

            //check the piece is moving to an allowed location
            foreach(Position p in getMoves(now,out returnValue))
            {
                if (p == next) isAllowed = true;
            }
               if(!isAllowed)return 0;

               //1. empty space
            if(nextPiece==null)
            {
                updateMoveGrid(now,next);
                return 1;
            }
            else
            {
                if(nextPiece.piecePlayer.playerType!=nowPiece.piecePlayer.playerType)
                {
                    //opponent piece
                    //if flag -> win
                    if(nextPiece.pieceName==piecesTypes.Flag)return 50;

                    //in case bomb vs miner
                    if(nextPiece.pieceName==piecesTypes.Bomb && nowPiece.pieceName==piecesTypes.Miner)
                    {
                        updateWinGrid(now,next);
                        return 10;
                    }

                    //in case spy vs Marshal
                    if (nextPiece.pieceName == piecesTypes.Marshal && nowPiece.pieceName == piecesTypes.Spy)
                    {
                        updateWinGrid(now, next);
                        return 10;
                    }

                    //in case of a tie
                    if ((int)nextPiece.pieceName == (int)nowPiece.pieceName)
                    {
                        updateTieGrid(now, next);
                        return 20;
                    }

                    //if opponent piece better
                    if((int)nextPiece.pieceName>(int)nowPiece.pieceName)
                    {
                        updateLoseGrid(now, next);
                        return 30;
                    }
                    else
                    {
                        updateWinGrid(now, next);
                        return 10;
                    }
                }
                else
                {
                    //both pieces are in the same team (shouldn't happen tho...)
                    return 4;
                }
            }
        }