2006-04-23

Mu Torere, moving the pieces II

Step 11.
Now the User and the Computer needs to move. An enumerated type lSelection needs to be created with Invalid, Valid, ComputerWon, and UserWon as values to allow for CGame to signal the Document.
AND, might as well do this now, a boolean flag needs to be added to the CGame class to denote that there are no more moves to do. This flag will need to be added to the various copying functions.

For the User the CGame adds the function
CGame::lSelection CGame::selectMove(const CPoint& point)
If the finished flag is true, then return a CGame::Invalid signal.

Get a pointer to the CBoard instance.
If this pointer is NULL, return CGame::Invalid.

If there is no valid selection from CBoard's whatIndex(), return CGame Invalid.
Otherwise,
Test the move. If not possible to move, return CGame::Invalid.
If moving is not possible, return CGame::Invalid.

If the User won, set the finished flag to true and return CGame::UserWon.

Get the move from the computer Move function.
ASSERT that this move is not CBoard::OFF_BOARD.

Move the Computer's piece.
ASSERT that this Move was possible.

If the Computer won, set the finished flag to true and return CGame::ComputerWon.

If nothing else, CGame::Valid.
I should point out the issue with the move. (As if you had not noticed.) If the User's move work but the Computer's did not, at this poin the only reasonable action is to exit the program, preffereably with some message to the user. Even then there must be a better way to handle this situation.
The only thing that I can think of is to use the Undo functionality to restore the game, and try again. (Either ask the user to re-do the move, or to loop a few times and THEN ask the user.)

The function for testing who won;
COwner::Player CGame::whoWon(void) const
If the emptyLoc is the center location (8), return COwner::playN.

Get the COwner::Player value of the side that owns the center.

Switch on the value in emptyLoc,
{
If either neighbouring array element (element 7 is next to element 0) is owned by the same side of the center, return that COwner::Player value.
}

Otherwise, just return COnwer::playN.


The function for testing the move;
bool CGame::canMove(const COwner::Player& tag, const int& fromLoc)const
Get the pointer to the CBoard instance.
Pre-Conditions:
That tag is either COwner::playC or COwner::playU.
That fromLoc is greater than or equal to 0 and less than CBoard::NUM_PLACES.
And that fromLoc is NOT equal to emptyLoc.
Get a pointer to the CBoard instance.

If the move from fromLoc to emptyLoc is NOT possible, return false.

Get the COwner::Player value of the opposing side.

If emptyLoc is the center location, test to see if one and / or the other neighbour of fromLoc is owned by the opposing side.
If this is the case, return true.
Otherwise, return false.

And if emptyLoc is NOT the center, return true.


And the function for the computer's move;
int CGame::computerMove(void) const
Get a pointer to the CMoveGenerator instance.
Allocate a CMoveGenerator instance.
Test pointer to see if it is valid.

Setup the MoveGenerator instance with the current CGame.

Get the move.
ASSERT that the move will not be CBoard::OFF_BOARD. (Actually done AFTER releasing the CMoveGenerator instance.)

Release Delete the CMoveGenerator instance.

Return the move


Update: Changed CMoveGenerator instance from Singleton instance to straight pointer allocation.

No comments: