Time for action - GamePiece class methods part 1 updating

  1. Add the following methods to the GamePiece class:
    public void SetPiece(string type, string suffix)
    {
    pieceType = type;
    pieceSuffix = suffix;
    }
    public void SetPiece(string type)
    {
    SetPiece(type,"");
    }
    public void AddSuffix(string suffix)
    {
    if (!pieceSuffix.Contains(suffix))
    pieceSuffix += suffix;
    }
    public void RemoveSuffix(string suffix)
    {
    pieceSuffix = pieceSuffix.Replace(suffix, "");
    }
    

The first two methods are overloads with the same name, but different parameter lists. In a manner similar to the GamePiece constructors, code that wishes to update a GamePiece can pass it a piece type, and optionally a suffix.

Additional methods have been added to modify suffixes without changing the pieceType associated with the piece. The AddSuffix() method first checks to see if the piece already contains the suffix. If it does, nothing happens. If it does not, the suffix value passed to the method is added to the pieceSuffix member variable.

The RemoveSuffix() method uses the Replace() method of the string class to remove the passed suffix from the pieceSuffix variable.

Rotating pieces

The heart of the Flood Control play mechanic is the ability of the player to rotate pieces on the game board to form continuous pipes. In order to accomplish this, we can build a table that, given an existing piece type and a rotation direction, supplies the name of the piece type after rotation. We can then implement this code as a switch statement:

Rotating pieces
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.67.166