Well, I'm trying to make Scrabble in C++. I thought it'd be a nice challenge, but it's more challenging then I expected.
-When a player enters a word, how can I make the program check to see if it's in my dictionary( A text file).
-How can I make it so that when players go to input words onto the board, that they can only build off of other words?
-How can I calculate score off the letters used in a word? (each letter gives a score, how do I make it read the letters then add the score?)
If I could just get some help with these 3 problems, I think i'd be able to finish it and do some testing. I can also share my code so far(it's mainly just the board at the moment) if that would help at all.
For the first one, you could load in the text file to a vector of strings, and use the std::find() algorithm to find the word. If the word isn't found, you know what to do.
For the second two, that would depend on how you're doing all this...is this a console app? (I hope not)
First: I'd recommend reading your file into a vector or something similar, then use a map of first letter combos(two letters probably) to use later. Then when someone uses a word go to that number in your dictionary vector, and iterate through that letter section to check for valid moves.
Or you could just put the whole thing in an stl::list... If your dictionary is too large to store on your ram, then you can use the first map method to store the iterator of that particular letter, then you can read in as needed.
Second: Mark each tile as it's put down, and check to make sure it's a valid move. When it's a valid move, check the word using some sort of store of your words (see above)
When it's a valid move, apply the individual letter scores (from a simple array of structs, or a map, or whatever the heck you want), then check for special tiles, and apply the specials.
For the existing tiles, it's a simple matter, you can just prepopulate your array for checking with the existing tiles, thereby also giving yourself a simple method of error checking if the user tries to place a tile on a predestined board tile.
Third: To calc the score. Store every letter and value in some sort of container. (be it struct, vector, map, array, or heck even individual variables if your crazy) then perhaps have a score variable and add all of the current letters (before adding your special modifiers eg: 3x letter, 3x word, etc), then add your mods. Then permanently increase the score of that person, if it's a valid move of course.
That's a rough outline of a rough course I'd roughly take on rough days... roughly.
Ok, can I get some quick help? I'm having a really hard time with assigning random letters to each player's tile set. I have an array for each player (2 players), and I need to assign a random letter to each value of that array, while taking away from my letter counters(cause you eventually run out of letters). I'm having a bad day, and getting really frustrated. I just can't get it to work, and i think i was doing it a really difficult way that I couldn't even get to work. If someone could just show me some example code (would be the best), or just explain in detail of what I should do I would greatly appreciate it.
for(int i = 0; i < 12;i++)
{
letter.push_back("E");
}
for(int i = 0; i < 9;i++)
{
letter.push_back("A");
letter.push_back("I");
}
for(int i = 0; i < 8;i++)
{
letter.push_back("O");
}
for(int i = 0; i < 6;i++)
{
letter.push_back("N");
letter.push_back("R");
letter.push_back("T");
}
for(int i = 0; i < 4;i++)
{
letter.push_back("L");
letter.push_back("S");
letter.push_back("U");
letter.push_back("D");
}
for(int i = 0; i < 3;i++)
{
letter.push_back("G");
}
for(int i = 0; i < 2;i++)
{
letter.push_back("B");
letter.push_back("C");
letter.push_back("M");
letter.push_back("P");
letter.push_back("F");
letter.push_back("H");
letter.push_back("V");
letter.push_back("W");
letter.push_back("Y");
}
letter.push_back("K");
letter.push_back("J");
letter.push_back("X");
letter.push_back("Q");
letter.push_back("Z");
random_shuffle( letter.begin(), letter.end() );
random_shuffle( letter.begin(), letter.end() );
random_shuffle( letter.begin(), letter.end() );
random_shuffle( letter.begin(), letter.end() );
So I'm now trying to use this, after seeing something similar. But now i'm now sure how to use it, like grabbing a letter and assigning it into my array.
p2tile[0]=letter.pop_back(); Nope, I feel really clueless today.
What data type is stored in the vector? A character? vector<char> letter; ?
If so, then use single quotes when pushing back. eg. letter.push_back('A');
Is p2tile an array of characters? If so your assignment ought to work.
You're going with a vector of characters then? Interesting...
I've been pushing this exercise forward myself!
I also made the players tile hands into vector<char> instead of an array.
It looks like you are trying to initialize the letter pool in a function.
This is what I have for that now:
void INITtilePool( vector<char>& pool )
{
int i = 0;// for looping
for( i = 0; i < 12;i++)
pool.push_back('E');
for( i = 0; i < 9;i++)
{
pool.push_back('A');
pool.push_back('I');
}
for( i = 0; i < 8;i++)
pool.push_back('O');
for( i = 0; i < 6;i++)
{
pool.push_back('N');
pool.push_back('R');
pool.push_back('T');
}
for( i = 0; i < 4;i++)
{
pool.push_back('L');
pool.push_back('S');
pool.push_back('U');
pool.push_back('D');
}
for( i = 0; i < 3;i++)
pool.push_back('G');
for( i = 0; i < 2;i++)
{
pool.push_back('B');
pool.push_back('C');
pool.push_back('M');
pool.push_back('P');
pool.push_back('F');
pool.push_back('H');
pool.push_back('V');
pool.push_back('W');
pool.push_back('Y');
}
pool.push_back('K');
pool.push_back('J');
pool.push_back('X');
pool.push_back('Q');
pool.push_back('Z');
random_shuffle( pool.begin(), pool.end() );
return;
}
Then I can just do this in main(). I'm now working on having a player make a play.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
const size_t handSize = 8;// Aren't there only 7 tiles in a hand?
int main()
{
vector<char> letter;// tile pool
vector<char> p1tile;// players hands
vector<char> p2tile;
INITgame(letter, p1tile, p2tile);// this calls INITtilePool() and fills and displays both hands.
//player 1 plays 2 tiles - then refills hand from pool. New hand is displayed.
cout << "\n p1 plays: " << playTile( p1tile, 0 ) << " and " << playTile( p1tile, 2 ) << endl;// 1st and 3rd tiles in hand
cout << "These tiles are left: "; showHand(p1tile);
fillHand(letter, p1tile);
cout << "p1 new hand is: "; showHand(p1tile);
return 0;
}
Jeez dude, your really good at this. I would give you my board, but it doesn't fit xD
Right now im trying to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
string player1t()
{
cout << "Use your letters and spell a word:";
cin >> input;
vector<string> words;
ifstream file("dictionary.txt");
string word;
while ( file >> word )
words.insert( word );
if ( words.find( "acreage" ) != words.end() )
cout << "The word 'acreage' is in the dictionary!";
}
Thank you. Are you placing the entire dictionary list of words into one vector?
Shouldn't be a problem in terms of memory used. Even 50,000 words with average length 8 characters (say) requires 50,000*8 Bytes = 400KB < 0.5MB which isn't much.
For your line 11 I think you want words.push_back( word );
I made a scrabble game for windows about 10 years ago (user vs. computer - it plays a little better than me). I knew little of the language then and did it all in terms of global arrays, simple structures and global functions so I may try to "modernize" it.
Of course I would not make it a console game, though a console program is useful for working out the mechanics.
I found it worked very well to organize the dictionary by word length.
Searches are always done by word length first.
I'd use separate vectors for 2 letter words, 3 letter words and so on.
An array of vector<string> might work well since there is a maximum word length of 15 letters (due to board dimensions). Perhaps vector<string>words[13]; where words[0] would be for 2 letter words and so on.
You don't want to put the dictionary into a vector.
Use the standard set container, or better yet use unordored_set. Checking for a word match using a lookup in unordered_set<string> will run in constant time. Walking through an entire vector will be much slower.
Thanks. I'll look into that.
I do want to use the best container for the job.
In my old game version I actually performed each search on the text file (never loading it into program memory). I used an array of file pointers for searching by length.
Even this method is fast enough that plays appear to be processed "instantly".
Hi guys. Can you help me with the scrabble game. I dont have idea where to start. Im not that familiar in c++ codes. Please i really need your help. the game is for our finals. thanks