How Do I make a regulation scrabble board in C++ that meets the following requir
ID: 3766348 • Letter: H
Question
How Do I make a regulation scrabble board in C++ that meets the following requirements:
1) Size board is 15 x 15 with one additional row to show the tiles on your rack, note the asterick is a wild card, the following 15 rows will contain 15 characters in each.
2) Capital Letters represent played Tiles
3) 'A' represents all empty spaces
4) 2 is a double letter
5) 3 is a triple letter
6) 4 is a double word
7) 9 is a triple word
Here is an example a board:
RIFLOX->(this should be at the center of first row) 9--2---9---2-NO -4---3---3JOLE- --4---2-2---I-- 2--4---2--MUD-2 ----4----MA---- -3---3-GROWTHS- --2-G-DIED--2U- 9--CAVES---2-QI --2-ZA2-2---T-N -3--EN---3-BULK -T--R-----TAR-- 2HUES--2--EYNE2 WAP---2-2---I-- IT---3---3--P4- G--2---9---2SIB
Explanation / Answer
// SCRABBLE GAME // by jephthah // // develops standard english scrabble board and allows user to 'place' // tiles on board ... checks validity of word placement according to // boundaries and existing tiles ... calculates word scores based on tile // values, word and letter multipliers, and existing tiles' face value // // to do: // score parallel tiles that incidentally form cross-words with placed tiles // allow variable number of players // assign random tiles to players per standard distribution // validate words to "official dictionary" // // #include #include #include #define FLUSH_STDIN(x) {if(x[strlen(x)-1]!=' '){do fgets(Junk,16,stdin);while(Junk[strlen(Junk)-1]!=' ');}} char Junk[16]; // buffer for discarding excessive user input, used by "FLUSH_STDIN" macro // Globals typedef struct { char tile; // current tile at board position int letter; // letter multiplier (2 or 3), 0 if none int word; // word multiplier (2 or 3), 0 if none } board_t; // board geometry 15x15 // add one extra column for print border board_t Board[15][16]; // tile values are A B C D E F G H I J K L M N O P Q R S T U V W X Y Z int TileScore[26]={1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10}; // Functions // initialize blank board with word and letter // multipliers for each space, if any void init_board(void) { int i, j; for (i=0; i 15) { printf("not valid, try again. "); return 0; } return 1; } // scores a word at the indicated board placement if word is a valid // placement according to existing letters // input word,row,col,dir previously indicated, plus 'accept' // argument which indicates whether word is written // output score of word // returns 1 if successful // 0 if unsuccessful, and output will be undefined // -1 if user opts to quit int score_word(char *word, int *score, int row, int col, int dir, int accept) { char *temp = word; int tempscore, valid=0, wordmult=1, r=row, c=col, index; tempscore = 0; *score = 0; while(*temp) { if (r == 7 && c == 7) // first valid move uses center square valid = 1; index = *temp - 0x41; // find zero-based index for alpha tile score if (Board[r][c].tile != ' ') // if letter was already placed... { if (Board[r][c].tile != *temp) // make sure it matches { printf("not valid, try again. "); return 0; } valid = 1; // valid moves use previously placed letter tempscore += TileScore[index]; // tile score has no multipliers } else { // new tiles get letter and/or word multiplier, if any tempscore += (TileScore[index] * ((Board[r][c].letter > 1)?Board[r][c].letter:1)); if (Board[r][c].word > 1) wordmult *= Board[r][c].word; } // increment row or col index appropriately if (dir) r++; else c++; // increment pointer for next letter temp++; } if (valid == 0) { printf("not valid, try again. "); return 0; } // calculate final word score *score = tempscore * wordmult; if (accept) // add word to board if accepted { r=row; c=col; temp=word; while (*temp) { Board[r][c].tile=*temp++; if (dir) r++; else c++; } } return 1; } int main() { int row, col, direction, player=1, score_p1=0, score_p2=0, tempscore, result; char word[17], prompt[8]; init_board(); while(1) { // display current board and scores draw_board(); printf("SCORE Player 1: %-4d Player 2: %d ",score_p1, score_p2); // indicate which player's turn printf(" -- PLAYER #%d -- ",player); // get location of where to start word insertion, or quit game do result = get_location(&row, &col); while (result == 0); if (result == -1) break; // get direction (Horiz, Vert) to place word, or restart location do result = get_direction(&direction); while (result == 0); if (result == -1) continue; // get word to be inserted, or restart location do result = get_word(word, row, col, direction); while (result == 0); if (result == -1) continue; // test validity and score word on board tempscore=0; while (1) { result = score_word(word, &tempscore, row, col, direction, 0); if (result == 1) break; do result = get_word(word, row, col, direction); while (result == 0); if (result == -1) break; } // restart new location if user opts out if (result == -1) continue; // display score for valid word allow accept or reject printf("score is %d. accept word? (Y/N): ",tempscore); fflush(stdout); fgets(prompt,sizeof(prompt),stdin); prompt[0] &= 0x5F; //converts alpha to UPPERCASE FLUSH_STDIN(prompt); // write word to memory, or allow new word/location if (prompt[0]=='Y') result = score_word(word, &tempscore, row, col, direction, 1); else continue; // unknown error, word was not written to board memory if (result == 0) printf("ERROR: word was not entered! "); // score word to player's total else if (player == 1) score_p1 += tempscore; else score_p2 += tempscore; player ^= 3; // XOR function toggles Player1 Player2 } return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.