上QQ阅读APP看书,第一时间看更新
- Add public methods to the GameBoard class to interact with
GamePiece
:public void RotatePiece(int x, int y, bool clockwise) { boardSquares[x, y].RotatePiece(clockwise); } public Rectangle GetSourceRect(int x, int y) { return boardSquares[x, y].GetSourceRect(); } public string GetSquare(int x, int y) { return boardSquares[x, y].PieceType; } public void SetSquare(int x, int y, string pieceName) { boardSquares[x, y].SetPiece(pieceName); } public bool HasConnector(int x, int y, string direction) { return boardSquares[x, y].HasConnector(direction); } public void RandomPiece(int x, int y) { boardSquares[x, y].SetPiece(GamePiece.PieceTypes[rand.Next(0, GamePiece.MaxPlayablePieceIndex+1)]); }
RotatePiece()
, GetSourceRect()
, GetSquare()
, SetSquare()
, and HasConnector()
methods simply locate the appropriate GamePiece
within the boardSquares
array and pass on the function request to the piece.
The RandomPiece()
method uses the rand
object to get a random value from the PieceTypes
array and assign it to a GamePiece
. It is important to remember that with the Random.Next()
method overload used here, the second parameter is non-inclusive. In order to generate a random number from 0 through 5, the second parameter needs to be 6.