Thread: Programming Boggle in C++ using a time function

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    4

    Programming Boggle in C++ using a time function

    HI,
    I am currently working on a project shown below. I am trying to figure out how to create the function to determine the time from when the game begins and how to exit the game once the 3 minutes are up. I think I have figured out the first function that determines and stores the letters in the grid here :

    insert
    Code:
     string[] storeAndLetters()
    Code:
    {
    string words[16];
    words[0]="AAEEGN";
    words[1]="ABBJOO";
    words[2]="ACHOPS";
    words[3]="AFFKPS";
    words[4]="AOOTTW";
    words[5]="CIMOTU";
    words[6]="DEILRX";
    words[7]="DELRVY";
    words[8]="DISTTY";
    words[9]="EEGHNW";
    words[10]="EEINSU";
    words[11]="EHRTVW";
    words[12]="EIOSST";
    words[13]="ELRTTY";
    words[14]="HIMNUQu";
    words[15]="HLNNRZ";
    string[16] selectedWord;
    int k = 0;
    for(int i = 0; i < 6; i++)
    {
    int rndNo = rand() % 6 + 1;
    char word[4];
    for(int j =0; j< 16; j+=4)
    {
    word[0]=words[j][rndNo]
    word[1]=words[j][rndNo];
    word[2]=words[j][rndNo];
    word[3]=words[j][rndNo];
    selectedWord[k++]=word;
    }
    
    }
    return selectedWord;
    }
    void Print(string[] words, int length)
    {
    for(int i=0; i < lengt; i ++)h
    {
       cout<< word[i];
    }   
    }
    


    HERE IS THE PROJECT DESCRIPTION

    For this project, you will write a program in "C+" that will create the a set of 16 semi-random letters in a 4-by-4 grid and have the user enter in as many words as possible that can be made from these letters in a 3 minute time frame.
    This game has been marketed as Boggle. The wikipedia page for Boggle has a good discussion of the game as does the wikihow Boggle page. The official rules of Boggle can be found at the Hasbro Web Site:


    The letters used come from 16 6-sided cubes (or dice) with one letter per side. Each of the 16 locations in the 4-by-4 grid will use one letter from one of the dice. Since the letters on each of the dice are pre-determined, we can call this a "semi-random" distribution. We will use the following dice combinations:

    1. AAEEGN
    2. ABBJOO
    3. ACHOPS
    4. AFFKPS
    5. AOOTTW
    6. CIMOTU
    7. DEILRX
    8. DELRVY
    9. DISTTY
    10. EEGHNW
    11. EEINSU
    12. EHRTVW
    13. EIOSST
    14. ELRTTY
    15. HIMNUQu
    16. HLNNRZ

    Storing the above information as an array of 16 strings may be a good way to keep track of the information.
    Note that the 15th dice has a final letter as the combination Qu. Since the letter Q is almost always followed by the letter U in most English words, the game automatically allows this combination. Note, you may want to just use the letter Q
    Your program is to display the Boggle grid of 4-by-4 letter based on randomly placing the 16 dice into the 16 grid positions and randomly selecting one of the 6 letters from each dice.
    After the grid has been displayed your program is to allow the user to enter in as many words as possible until 3 minutes has elapsed. Since we are not making a truly interactive User Interface, we will only check for the time after the user enters in a work. When a user enters a word, check to see how long it have been since the letter grid was displayed. If it have been less than 3 minutes (or 180 seconds), loop and allow the user to enter another word. If it has been more than 3 minutes, display the number of words entered by the user and the end the program. This manner will allow the user to enter in one word after the 3 minutes has expired, but that will be OK for this program.
    Note: This program is not validating the words entered by the user nor keeping track of the points scored. That will be done in Phase 2 (Project 4).
    Your program must determine and store the letters in the grid first. Then it can display the grid after all 16 locations have been determined. The 4-by-4 grid of letters may best be stored in a 2 dimensional array. This is not a requirement for Phase 1, but will be a requirement for Phase 2.
    You should use functions as much as possible when writing this program. You are REQUIRED to have

    • a function to determine and store the letters in the grid
    • a function to print out the letters to user
    • a function to monitor the users input of words and keep track of the 3 minutes of time

    The array used to store the letters in the grid will be passed out of the first function and into the second function. For Phase 2, this array will also need to be passed into the third function.
    User Input

    At the start of your program, you should give the user some instructions on how the play the game. The exact verbiage is left up to you, but some points will be given for the appropriateness of this message.
    After the grid has been displayed, allow the user to enter in input. Reading this information as string is probably the easiest. We are not doing anything with this data except counting the number of words entered by the user and checking how long it has been since the grid was displayed.
    At the end of the program, you should thank the user for playing and give some information on the "development team" (i.e. your name, that this program was developed for CS 107, etc).
    Random Numbers and More on Time

    While there really is no such thing as a real random number, the sequences of numbers generated are complicated enough that to just about everyone, they seem random. A nice discussion of random numbers is given on the cplusplus.com web page.
    The primary library function for random numbers in C/C++ is rand() in the C library stdlib.h or in the C++ library cstdlib.
    The rand() library function returns an integer value from 0 to the largest integer usable on the machine. To get this value into a desired range the modulus operator % is normally used. The modulus operator will get the random value to be in the range from 0 to some number. Since we want to have our random number to be in the range from 1 to 6, we will want to add 1 to the random number after the modulus operation is performed.
    Note that rand() when used by itself will generate the same random numbers everytime a program runs. While this is useful for testing, if makes playing a game like codebreaker a bit boring. Thus we will also want to use the library function of srand() to make things become truly random. The most common way to use srand() is to use the library function of time(). All of the cplusplus.com web pages that discuss rand() show how to use srand() and time() to create truly random numbers.
    The time library also has a function called difftime(). This function will take two instances of the typetime_t and return the number of seconds between the two times. Consider the following example.
    // Example program

    insert
    Code:
    #include <iostream>
    #include <string>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
    time_t now, then;
    double seconds;
    // get the starting time
    time(&now);
    
    std::string name;
    std::cout << "What is your name? ";
    getline (std::cin, name);
    std::cout << "Hello, " << name << "!\n";
    
    // get the ending time
    time(&then);
    
    seconds = difftime(then, now);
    
    cout << "You took " << seconds << " seconds to type in your name." << endl;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Why are you dumping all of this here? Do you have a question or are you just venting?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-12-2013, 10:11 AM
  2. Boggle game.
    By Desolation in forum Game Programming
    Replies: 5
    Last Post: 08-03-2007, 01:52 AM
  3. Time and Date programming
    By houtram in forum C Programming
    Replies: 1
    Last Post: 09-14-2006, 11:59 PM
  4. Boggle board help
    By PuhFENDanT in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2003, 10:43 AM
  5. First-time Windows programming
    By Grayson_Peddie in forum Windows Programming
    Replies: 5
    Last Post: 06-05-2002, 11:09 PM

Tags for this Thread