Ejemplo n.º 1
0
/*--------------------------------------
 * Function: SolveMaze()
 * Parameters: pt         Den punkt utifrån vilken vi vill lösa labyrinten.
 *             num_calls  Räknare för antal anrop.
 *             unmark     Indikerar huruvida UnmarkSquare()-funktionen ska
 *                        anropas.
 *
 * Description:
 *   Löser labyrinten (om möjligt) och räknar antalet anrop.
 *------------------------------------*/
static bool SolveMaze(pointT pt, int* num_calls, bool unmark) {
    ++(*num_calls);

    if (OutsideMaze(pt))
        return TRUE;

    if (IsMarked(pt))
        return FALSE;

    MarkSquare(pt);

    for (directionT dir = North; dir <= West; dir++) {
        // Om det är en vägg ivägen går vi vidare till nästa riktning.
        if (WallExists(pt, dir))
            continue;

        if (SolveMaze(AdjacentPoint(pt, dir), num_calls, unmark)) {
            // Vi måste avmarkera även här så att inte lösningen ligger kvar
            // som markerad i labyrinten.
            if (unmark)
                UnmarkSquare(pt);

            return TRUE;
        }
    }

    if (unmark)
        UnmarkSquare(pt);

    return FALSE;
}
Ejemplo n.º 2
0
/*
 * Function: InputGuesses
 * -------------------------
 * Allows the user to input guesses. Runs checks on user guess to see if it meets all boggle requirements.
 */
void InputGuesses(Grid<char> boggleBoard, Lexicon wordList, Lexicon &usedWords, Grid<bool> usedDice)
{	
	cout << endl << "Ok, take all the time you want and find all the words you can!" << endl;
	cout << "Signal that you're finished by entering an empty line." << endl << endl;
	while (true) {
		int row= 0, col = 0;
		string userGuess = CheckUserGuess(wordList, usedWords); //checks if format rules are met
		if (userGuess == "") break; //checks if sentinel of user hitting return has been occured
		while (true) {
			if (FindUserGuessOnBoard(boggleBoard, row, col, "", userGuess, usedDice)) { 
                //checks if user guess can be created from boggleBoard
				usedWords.add(userGuess); //records user guess in lexicon of used words
				RecordWordForPlayer(userGuess, Human);
				UnhighlightBoard(boggleBoard);
				PlayNamedSound("excellent.wav");
				break;
			}
			if (!AdjacentPoint(boggleBoard.numRows(), boggleBoard.numCols(), row, col)) { 
                //checks if end of board has been reached without finding appropriate dice config
				cout << "You can't make that word! " << RandomizeResponse() << endl;
				PlayNamedSound("whoops.wav");
				break;
			}
		}
	}
}
Ejemplo n.º 3
0
/*
 * Function: FindRemainingWords
 * ------------------------------
 * Goes through all of the dice on boggleBoard, calling FindWordsOnBoard to search for all remaining
 * words that be created from the dice configuration of boggleBoard starting from the specific dice location.
 */
void FindRemainingWords(Grid<char> boggleBoard, Lexicon wordList, Lexicon &usedWords, Grid<bool> usedDice)
{
	int row = 0, col = 0;
	while (true) {
		FindWordsOnBoard(boggleBoard, row, col, "", usedDice, wordList, usedWords);
		if (!AdjacentPoint(boggleBoard.numRows(), boggleBoard.numCols(), row, col)) break;
	}
}
Ejemplo n.º 4
0
/*
 * Function: CreateMarker
 * ---------------------------
 * Initializes all locations in usedDice to false. Is used in other functions as a marker of
 * whether or not the specific dice has been used in the char arrangement.
 */
void CreateMarker(Grid<bool> &usedDice)
{
	int row = 0, col = 0;
	while (true) {
		usedDice.setAt(row, col, false);
		if (!AdjacentPoint(usedDice.numRows(), usedDice.numCols(), row, col)) break;
	}
}
Ejemplo n.º 5
0
/*
 * Function: DrawBoggleBoard
 * ---------------------------
 * Draws the graphic display of the boggle board, reflecting the char configuration of the boggleBoard.
 */
void DrawBoggleBoard(Grid<char> boggleBoard)
{
	int row = 0, col = 0;
	while (true) {
		LabelCube(row, col, boggleBoard.getAt(row, col));	
		if (!AdjacentPoint(boggleBoard.numRows(), boggleBoard.numCols(), row, col)) break;
	}
}
Ejemplo n.º 6
0
/*
 * UnhighlightBoard
 * --------------------
 * Unhighlights the graphic display of the boggle board. Ensures the highlighted word is 
 * returned to normal.
 */
void UnhighlightBoard(Grid<char> boggleBoard)
{
	int row = 0, col = 0;
	Pause(pauseTime); //creates highlighting animation of word
	while (true) {
		HighlightCube(row, col, false);
		if (!AdjacentPoint(boggleBoard.numRows(), boggleBoard.numCols(), row, col)) break;
	}
}
Ejemplo n.º 7
0
/*
 * Function: SetDiceConfiguration
 * ----------------------------------
 * Sets up the boggleBoard configuration of the dice according to the user-specified string of chars.
 */
void SetDiceConfiguration(Grid<char> &boggleBoard, string diceConfig)
{
	int stringIndex = 0, row = 0, col = 0;
	while (true) {
		boggleBoard.setAt(row, col, diceConfig.at(stringIndex));
		stringIndex++;
		if (!AdjacentPoint(boggleBoard.numRows(), boggleBoard.numCols(), row, col)) break;
	}
}
Ejemplo n.º 8
0
/*
 * Function: ShuffleDice
 * --------------------------
 * Randomly shuffles the dice in the boggleBoard by swapping the contents of 2 random locations in boggleBoard. 
 * Ensures the same dice does not always appear in the same location.
 */
void ShuffleDice(Grid<char> &boggleBoard)
{
	int row = 0, col = 0;
	while (true) {
		int randomRow = RandomInteger(0, boggleBoard.numRows() - 1);
		int randomCol = RandomInteger(0, boggleBoard.numCols() - 1);
		char dice = boggleBoard.getAt(row, col);
		boggleBoard.setAt(row, col, boggleBoard.getAt(randomRow, randomCol));
		boggleBoard.setAt(randomRow, randomCol, dice);
		if (!AdjacentPoint(boggleBoard.numRows(), boggleBoard.numCols(), row, col)) break;
	}
}
Ejemplo n.º 9
0
/*
 * Function: InitializeRandomBoard
 * ----------------------------------
 * Randomly initializes boggleBoard and picks a random side of the dice to face up by randomly selecting
 * a char from the const dice configuration. Then, calls on ShuffleDice to randomize the positions of the dice.
 */
void InitializeRandomBoard (Grid<char> &boggleBoard)
{
	PlayNamedSound("dice rattle.wav");
	int diceCount = 0, row = 0, col = 0;
	char randomChar;
	while (true) {
		if (boggleBoard.numRows() == 4) { //distinguishes between the 5x5 and 4x4 dice configs
			randomChar = StandardCubes[diceCount].at(RandomInteger(0, 5));
		} else {
			randomChar = BigBoggleCubes[diceCount].at(RandomInteger(0, 5));
		}
		boggleBoard.setAt(row, col, randomChar);
		diceCount++;
		if (!AdjacentPoint(boggleBoard.numRows(), boggleBoard.numCols(), row, col)) break;
	}
	ShuffleDice(boggleBoard);
}
Ejemplo n.º 10
0
bool SolveMaze(pointT pt)
{
 if (OutsideMaze(pt))
 return true;
 if (IsMarked(pt))
 return false;
 MarkSquare(pt);
 for (directionT dir = North; dir <= West; dir=directionT(dir + 1))
 {
 if (!WallExists(pt, dir) && SolveMaze(AdjacentPoint(pt, dir)))
 {
 return true;
 }
 }
 UnmarkSquare(pt);
 return false;
}