you will write a program in \"C+\" that will create the a set of 16 semi-random
ID: 665266 • Letter: Y
Question
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:
http://www.hasbro.com/common/instruct/boggle.pdf
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:
AAEEGN
ABBJOO
ACHOPS
AFFKPS
AOOTTW
CIMOTU
DEILRX
DELRVY
DISTTY
EEGHNW
EEINSU
EHRTVW
EIOSST
ELRTTY
HIMNUQu
HLNNRZ
Storing the above information as an array of 16 strings may be a good way to keep track of the information.
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
Explanation / Answer
The below code uses the predefined set of dice and generates a boggle and displays to user and waits for 3 minutes for user to input words and stops after 3 mins. Displays the words input by user at the end. Please don't forget to rate the answer if it helped. Thank you very much.
Boggle.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <thread>
#include <chrono>
using namespace std;
//since we don't know how many words the user will input, we use a vector to store all
//the words input by user
vector<string> words;
//shuffle function shuffles the dices
void shuffle(string dices[])
{
int j;
string temp;
srand(time(NULL));
for(int i=0;i<16;i++)
{
j=rand() % 16;
temp = dices[i];
dices[i] = dices[j];
dices[j] = temp;
}
}
//generates a 4x4 grid with randomly choosen character from each of the 16 dice
//first shuffles all the dice and then randomly selects a character in the dice
void generateGrid(string grid[4][4])
{
int p;
string dices[]={"AAEEGN","ABBJOO","ACHOPS","AFFKPS",
"AOOTTW","CIMOTU","DEILRX","DELRVY",
"DISTTY","EEGHNW","EEINSU","EHRTVW",
"EIOSST","ELRTTY","HIMNUQu","HLNNRZ"};
shuffle(dices);
for(int i=0,k=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
p = rand() % 6; //choose one of the 6 charactesr in the kth dice
grid[i][j] = dices[k].at(p);
if(grid[i][j] == "Q") //if Q is selected , store is as Qu
grid[i][j] = "Qu";
k++;
}
}
}
//displays the grid to user
void displayGrid(string grid[4][4])
{
cout<<" Here is the Boggle.... ";
for(int i=0; i<4; i++)
{
cout<<" "<<setfill('_')<<setw(33)<<""<<endl;
cout<<"| ";
for(int j=0; j<4; j++)
{
cout<<setfill(' ')<<setw(4)<<grid[i][j]<<" |";
}
}
cout<<" "<<setfill('_')<<setw(33)<<""<<endl;
}
//this function is called by a thread and runs independently... this is used to only
//get user input words and store itin the global vector words.
void getUserInput( )
{
string str;
cout<<" Your time starts now !"<<endl;
while(true)
{
cin>>str;
words.push_back(str);
}
}
int main()
{
string grid[4][4];
generateGrid(grid);
displayGrid(grid);
//create a separate thread for user input and let it run independently
thread inputthread(getUserInput);
inputthread.detach();
//put the current thread to sleep for 3 minutes and when it wakes up, the times up
//we dont want to wait for the other thread
std::this_thread::sleep_for(std::chrono::minutes(3));
cout<<"Your time's up!";
cout<<"You have input "<<words.size()<< " words given below"<<endl;
for(int i=0;i<words.size();i++)
{
cout<<words[i]<<endl;
}
}
Sample output
Here is the Boggle....
_________________________________
| A | E | Qu | T |
_________________________________
| C | X | F | E |
_________________________________
| O | A | T | T |
_________________________________
| M | I | L | A |
_________________________________
Your time starts now !
ATE
TAIL
FAT
CAT
Your time's up!You have input 4 words given below
ATE
TAIL
FAT
CAT
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.