Ejemplo n.º 1
0
 private void SendGameloopAction(sendMessages msg, object data)
 {
     if (msg == msgExpecting)
     {
         msgData = data;
         GameloopNext();
     }
 }
Ejemplo n.º 2
0
    private void GameloopNext()
    {
        switch (currEvent)
        {
        case GameLoop.RequestShot:
            msgExpecting = sendMessages.shotCoords;
            currEvent    = GameLoop.CheckShot;
            otherPlayerRecieveAction(recieveMessages.waiting, null);
            currPlayerRecieveAction(recieveMessages.shotCoords, null);
            break;

        case GameLoop.CheckShot:
            //try {
            Coordinate tempCoord = (Coordinate)msgData;
            // check if the player already tried that spot7
            if (otherPlayerBoard[tempCoord.col, tempCoord.row] != -2)
            {
                currPlayerRecieveAction(recieveMessages.alreadyShot, null);
                otherPlayerRecieveAction(recieveMessages.waiting, null);
                currEvent    = GameLoop.RequestShot;
                msgExpecting = sendMessages.shotCoords;
            }
            else
            {
                // the shot is good, checks if it hit a boat
                bool found     = false;
                int  index     = -1;
                int  boatIndex = -1;

                // goes through all the coords of the enemy to check if the shot hit any boat
                for (int i = 0; i < otherPlayerBoats.Count; i++)
                {
                    var boat = otherPlayerBoats[i];
                    foreach (var coord in boat.coords)
                    {
                        if (coord.Equals(tempCoord))
                        {
                            found     = true;
                            index     = i;
                            boatIndex = boat.index;
                            break;
                        }
                    }
                    if (found)
                    {
                        break;
                    }
                }

                // remove a section from the boat, and report back with the results
                if (found)
                {
                    otherPlayerBoard[tempCoord.col, tempCoord.row] = boatIndex;
                    otherPlayerBoats[index].removeSectionFromCoords(tempCoord);
                    if (otherPlayerBoats[index].destroyed)
                    {
                        // shot sunk a boat
                        currPlayerRecieveAction(recieveMessages.sunk, tempCoord);
                        otherPlayerRecieveAction(recieveMessages.opponentSunk, tempCoord);
                    }
                    else
                    {
                        // shot hit but not sunk
                        currPlayerRecieveAction(recieveMessages.hit, tempCoord);
                        otherPlayerRecieveAction(recieveMessages.opponentHit, tempCoord);
                    }
                }
                else
                {
                    // the shot missed
                    otherPlayerBoard[tempCoord.col, tempCoord.row] = -1;
                    currPlayerRecieveAction(recieveMessages.miss, tempCoord);
                    otherPlayerRecieveAction(recieveMessages.opponentMiss, tempCoord);
                }

                // go to next event
                msgExpecting = sendMessages.ok;
                currEvent    = GameLoop.CheckIfWon;
            }

            /*}
             * catch (Exception e) {
             *  // bad coords
             *  currPlayerRecieveAction(recieveMessages.badCoords, null);
             *  otherPlayerRecieveAction(recieveMessages.waiting, null);
             *  msgExpecting = sendMessages.shotCoords;
             *  break;
             * }*/

            GameloopNext();
            break;

        case GameLoop.CheckIfWon:
            bool won = true;
            // check if all of the enemy's boats are destroyed
            foreach (var boat in otherPlayerBoats)
            {
                if (!boat.destroyed)
                {
                    won = false;
                    break;
                }
            }

            if (won)
            {
                currPlayerRecieveAction(recieveMessages.won, null);
                otherPlayerRecieveAction(recieveMessages.lost, null);
                break;
            }

            msgExpecting = sendMessages.ok;
            currEvent    = GameLoop.SwitchPlayer;
            GameloopNext();

            break;

        case GameLoop.SwitchPlayer:
            if (currPlayer == 1)
            {
                currPlayer = 2;
            }
            else
            {
                currPlayer = 1;
            }

            currEvent    = GameLoop.RequestShot;
            msgExpecting = sendMessages.shotCoords;

            GameloopNext();

            break;

        default:
            break;
        }
    }