Esempio n. 1
0
        public bool MakeMove(User user, string movestr)
        {
            int            playerid = this.users.IndexOf(user);
            MorabarabaMove move     = board.ParseAndValidateMove(playerid, movestr);

            if (move != null)
            {
                bool playerWon = board.MakeMove(playerid, move);
                board.addToHistory(movestr);
                if (playerWon)
                {
                    //Player won
                    MorabarabaController.EndGame(user);
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
 public MorabarabaMove ParseAndValidateMove(int playerid, string move)
 {
     if (playerid < 0 || playerid > 1)
     {
         return(null);                              //invalid player id
     }
     if (turn != playerid)
     {
         return(null);                  //not player's turn
     }
     if (move.Length == 2)
     {
         //Place a cow (no shooting)
         if (this.playerContexts[playerid].phase == MorabarabaPhase.PLACING)
         {
             if (this.playerContexts[playerid].placingCowsLeft > 0)
             {
                 if (this.fields.ContainsKey(move))
                 {
                     if (this.fields[move].value == -1)
                     {
                         MorabarabaMove moveObj = new MorabarabaMove();
                         moveObj.moveType    = MoveType.PLACE;
                         moveObj.destination = this.fields[move];
                         return(moveObj);
                     }
                 }
             }
         }
     }
     else if (move.Length == 5 && move.Contains('-'))
     {
         //Move a cow (no shooting)
         if (this.playerContexts[playerid].phase == MorabarabaPhase.MOVING || this.playerContexts[playerid].phase == MorabarabaPhase.FLYING)
         {
             var moveFields = move.Split('-');
             if (this.fields.ContainsKey(moveFields[0]) && this.fields.ContainsKey(moveFields[1]))
             {
                 if (this.fields[moveFields[0]].value == playerid && this.fields[moveFields[1]].value == -1)
                 {
                     if (this.fieldGraphMatrix[sortFieldnames(moveFields[0], moveFields[1])] || this.playerContexts[playerid].phase == MorabarabaPhase.FLYING) //are the fields connected?
                     {
                         MorabarabaMove moveObj = new MorabarabaMove();
                         moveObj.moveType    = MoveType.MOVE;
                         moveObj.source      = this.fields[moveFields[0]];
                         moveObj.destination = this.fields[moveFields[1]];
                         return(moveObj);
                     }
                 }
             }
         }
     }
     else if (move.Length == 5 && move.Contains('x'))
     {
         //Place a cow and shoot
         if (this.playerContexts[playerid].phase == MorabarabaPhase.PLACING)
         {
             if (this.playerContexts[playerid].placingCowsLeft > 0)
             {
                 var shotspl = move.Split('x');
                 if (this.fields.ContainsKey(shotspl[0]) && this.fields.ContainsKey(shotspl[1]))
                 {
                     if (this.fields[shotspl[0]].value == -1 && this.fields[shotspl[1]].value != playerid && this.fields[shotspl[1]].value != -1)
                     {
                         if (isPotentialMill(this.fields[shotspl[0]], playerid)) //does the placed cow form a mill?
                         {
                             bool canPlaceShoot = false;
                             if (!isPotentialMill(this.fields[shotspl[1]], GetEnemyId(playerid))) //make sure that the enemy cow is not a part of a mill
                             {
                                 canPlaceShoot = true;
                             }
                             else
                             {
                                 //check if all enemy cows are in mills
                                 var enemyId = GetEnemyId(playerid);
                                 canPlaceShoot = true;
                                 foreach (var boardField in this.fields.Values)
                                 {
                                     if (boardField.value == enemyId)
                                     {
                                         if (!isPotentialMill(boardField, enemyId))
                                         {
                                             canPlaceShoot = false;
                                             break;
                                         }
                                     }
                                 }
                             }
                             if (canPlaceShoot)
                             {
                                 MorabarabaMove moveObj = new MorabarabaMove();
                                 moveObj.moveType    = MoveType.PLACESHOOT;
                                 moveObj.destination = this.fields[shotspl[0]];
                                 moveObj.target      = this.fields[shotspl[1]];
                                 return(moveObj);
                             }
                         }
                     }
                 }
             }
         }
     }
     else if (move.Length == 8 && move.Contains('-') && move.Contains('x'))
     {
         //Move a cow and shoot
         if (this.playerContexts[playerid].phase == MorabarabaPhase.MOVING || this.playerContexts[playerid].phase == MorabarabaPhase.FLYING)
         {
             var shotspl    = move.Split('x');
             var moveFields = shotspl[0].Split('-');
             if (this.fields.ContainsKey(moveFields[0]) && this.fields.ContainsKey(moveFields[1]) && this.fields.ContainsKey(shotspl[1]))
             {
                 if (this.fields[moveFields[0]].value == playerid && this.fields[moveFields[1]].value == -1 && this.fields[shotspl[1]].value != playerid && this.fields[shotspl[1]].value != -1)
                 {
                     if (this.fieldGraphMatrix[sortFieldnames(moveFields[0], moveFields[1])] || this.playerContexts[playerid].phase == MorabarabaPhase.FLYING) //are the fields connected?
                     {
                         if (isPotentialMill(this.fields[moveFields[1]], playerid))                                                                            //does the placed cow form a mill?
                         {
                             bool canMoveShoot = false;
                             if (!isPotentialMill(this.fields[shotspl[1]], GetEnemyId(playerid)))//make sure that the enemy cow is not a part of a mill
                             {
                                 canMoveShoot = true;
                             }
                             else
                             {
                                 //check if all enemy cows are in mills
                                 var enemyId = GetEnemyId(playerid);
                                 canMoveShoot = true;
                                 foreach (var boardField in this.fields.Values)
                                 {
                                     if (boardField.value == enemyId)
                                     {
                                         if (!isPotentialMill(boardField, enemyId))
                                         {
                                             canMoveShoot = false;
                                             break;
                                         }
                                     }
                                 }
                             }
                             if (canMoveShoot)
                             {
                                 MorabarabaMove moveObj = new MorabarabaMove();
                                 moveObj.moveType    = MoveType.MOVESHOOT;
                                 moveObj.source      = this.fields[moveFields[0]];
                                 moveObj.destination = this.fields[moveFields[1]];
                                 moveObj.target      = this.fields[shotspl[1]];
                                 return(moveObj);
                             }
                         }
                     }
                 }
             }
         }
     }
     return(null);
 }
        public bool MakeMove(int playerid, MorabarabaMove move)
        {
            int enemyid = GetEnemyId(playerid);

            if (move.moveType == MoveType.PLACE)
            {
                move.destination.value = playerid;
            }
            else if (move.moveType == MoveType.MOVE)
            {
                move.source.value      = -1;
                move.destination.value = playerid;
            }
            else if (move.moveType == MoveType.PLACESHOOT)
            {
                move.destination.value = playerid;
                move.target.value      = -1;
            }
            else if (move.moveType == MoveType.MOVESHOOT)
            {
                move.source.value      = -1;
                move.destination.value = playerid;
                move.target.value      = -1;
            }
            if (move.moveType == MoveType.PLACE || move.moveType == MoveType.PLACESHOOT)
            {
                this.playerContexts[playerid].placingCowsLeft--;
                if (this.playerContexts[playerid].placingCowsLeft == 0)
                {
                    this.playerContexts[playerid].phase = MorabarabaPhase.MOVING;
                }
                this.playerContexts[playerid].cows++;
            }
            if (move.moveType == MoveType.PLACESHOOT || move.moveType == MoveType.MOVESHOOT)
            {
                this.playerContexts[playerid].mills++;
                this.playerContexts[enemyid].cows--;
                if (this.playerContexts[enemyid].cows <= 3 && this.playerContexts[enemyid].phase == MorabarabaPhase.MOVING)
                {
                    this.playerContexts[enemyid].phase = MorabarabaPhase.FLYING;
                }
            }
            if (this.turn == 0)
            {
                this.turn = 1;
            }
            else
            {
                this.turn = 0;
            }
            if (this.playerContexts[enemyid].cows <= 2 && this.playerContexts[enemyid].placingCowsLeft == 0)
            {
                //Player won
                return(true);
            }
            if (this.playerContexts[enemyid].phase == MorabarabaPhase.MOVING)
            {
                bool enemyHasNowhereToMove = true;
                foreach (var boardField in this.fields.Values)
                {
                    if (boardField.value == enemyid)
                    {
                        foreach (var kvp in this.fieldGraphMatrix)
                        {
                            if (kvp.Key.Contains(boardField.name) && kvp.Value)
                            {
                                var adjField = this.fields[kvp.Key.Replace(boardField.name, "")];
                                if (adjField.value == -1)
                                {
                                    enemyHasNowhereToMove = false;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (enemyHasNowhereToMove)
                {
                    return(true);
                }
            }
            return(false);
        }