Hi please I have a scrabble project in c++ and I want to solve it . I want to ma
ID: 3874002 • Letter: H
Question
Hi please I have a scrabble project in c++ and I want to solve it . I want to make a scrabble game in c++ .
there are two players , they want to make words from these caracters :ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQ
RSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRST
these 2 players have to make words from these caracters till these caracters done .
at the end all the words that the 2 players made by (cin) must be viewed on the screen by (cout ).
every time a player enter a word, the characters of this word must be eliminated from the list of characters .
I want to use iostream and string library only
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
#include <ctype.h>
using namespace std;
/*
This function is used to check if all the characters are used or not.
returns 1 if used completely else 0.
*/
int allCharactersOver(int a[], int size) {
for(int i = 0; i<size; i++){
//if any of the characters are not used completely, it means its value would not be 0
if(a[i] != 0)
return 0;
}
return 1;
}
/*
This function is used to check if a word can be accepted or not.
returns 1 if accepted else 0.
*/
int wordAccepted(string s, int a[], int size) {
int temp[size];
//creating temp array
for(int i=0;i<size;i++)
temp[i] = a[i];
int accepted = 1; //to determine whether the word can be accepted or not
string::iterator it;
for (it=s.begin(); it!=s.end(); it++){
temp[*it - 'A'] --;
if(temp[*it - 'A'] < 0){
accepted = 0;
break;
}
}
return accepted;
}
int main() {
//first store all the characters
string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRST";
//now we need to store the count of each character in an array
int size = 26;
int count[size];
//intitialize count array to 0
for (int i=0; i<26; i++)
count[i] = 0;
string::iterator it;
for (it=characters.begin(); it!=characters.end(); it++){
count[*it - 'A'] ++;
}
//array of all possible words
vector<string> v;
while(true){
//first check whether all characters have been used?
if(allCharactersOver(count, size) == 1){
cout<<" All characters have been used :)";
break;
}
cout<<"Character available (with count) are: ";
cout<<"-------------------------------------- ";
for(int i=0; i<26; i++){
if(count[i] > 0){
char ch = 'A'+i;
cout<<ch<<"("<<count[i]<<") ";
}
}
cout<<endl;
while(true){
string input = "";
cout<<"Player 1 (Enter word) : ";
getline (cin, input);
if(wordAccepted(input, count, size) == 0){
cout<<"Sorry. The word "<<input<<" cannot be accepted as all the characters in it are not available. ";
cout<<"Please try again. ";
} else {
cout<<"Word accepted ";
//push the accepted word to the store
v.push_back(input);
string::iterator it;
for (it=input.begin(); it!=input.end(); it++){
count[*it - 'A'] --;
}
break;
}
}
//check whether all characters have been used?
if(allCharactersOver(count, size) == 1){
cout<<" All characters have been used :)";
break;
}
cout<<"Character available (with count) are: ";
cout<<"---------------------------------------- ";
for(int i=0; i<26; i++){
if(count[i] > 0){
char ch = 'A'+i;
cout<<ch<<"("<<count[i]<<") ";
}
}
cout<<endl;
while(true){
string input = "";
cout<<"Player 2 (Enter word) : ";
getline (cin, input);
if(wordAccepted(input, count, size) == 0){
cout<<"Sorry. The word "<<input<<" cannot be accepted as all the characters in it are not available. ";
cout<<"Please try again. ";
} else {
//push the accepted word to the store
cout<<"Word accepted. ";
v.push_back(input);
for (it=input.begin(); it!=input.end(); it++){
count[*it - 'A'] --;
}
break;
}
}
}
cout<<" LIST OF ACCEPTED WORDS ";
cout<<"----------------------- ";
vector<string>::iterator vector_it;
for(vector_it = v.begin(); vector_it != v.end(); vector_it++)
cout<<*vector_it<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.