How to resolve the algorithm Wordiff step by step in the C++ programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Wordiff step by step in the C++ programming language
Table of Contents
Problem Statement
Wordiff is an original game in which contestants take turns spelling new dictionary words of three or more characters that only differ from the last by a change in one letter.
The change can be either:
Note:
Create a program to aid in the playing of the game by:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Wordiff step by step in the C++ programming language
This C++ code is a word game that follows the rules of "Wordiff", a simple word game where players can win a match by creating a new word from the previous word by adding, removing, or changing a single letter.
The program is designed to simulate a two-player Wordiff game. Here's a breakdown of the code:
- Initialization:
- The program begins by reading words from a dictionary file (unixdict.txt) and storing them in two vectors:
dictionary
: This vector contains all the words from the dictionary.starters
: This vector contains words with 3 or 4 characters that can be used as starting words.
- Player Details:
- The program requests player names from the console and stores them in a vector called
player_names
.
- Game Loop:
- The program starts a game loop that continues until a player loses.
- Each turn, the program prompts the current player to enter a word.
- The entered word is checked for validity using the
is_wordiff()
function, which ensures that the word is present in the dictionary, has not been used before, and differs from the previous word by only adding, removing, or changing a single letter.
- Turn Management:
- If the entered word is valid, it is added to the
words_used
vector, and the turn passes to the next player. - If the entered word is invalid, the game ends, and the program displays the player who lost the game and suggests words that could have been entered according to the rules of the game.
- Validation Function (
is_wordiff()
):
- This function checks if the given
current_word
is a valid Wordiff word based on the following rules:- The word must exist in the
dictionary
. - The word must not have been used before (not in the
words_used
vector). - The word must differ from the previous word (last word in
words_used
) by changing, adding, or removing only one letter.
- The word must exist in the
- The function uses
is_letter_changed()
,is_letter_removed()
, andis_letter_added()
helper functions to determine if the word meets these criteria.
- Game End:
- The game ends when a player enters an invalid word or when there are no valid words left to enter.
- The program displays the losing player and suggests words that could have been entered instead.
- Helper Functions:
is_letter_changed()
,is_letter_removed()
, andis_letter_added()
: These functions check if the difference between the current word and the previous word is adding, removing, or changing a single letter.
- Suggestions Function (
could_have_entered()
):
- When a player loses, this function is used to suggest words that could have been entered based on the game's rules.
- It checks all words in the dictionary that have not been used before and that meet the Wordiff criteria for the given
words_used
vector.
- Game Execution:
- The program starts by randomly selecting a starting word from the
starters
vector and adds it to thewords_used
vector. - The game loop continues, alternating between players until one of them enters an invalid word or until there are no more valid words left.
Source code in the cpp programming language
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>
std::vector<std::string> request_player_names() {
std::vector<std::string> player_names;
std::string player_name;
for ( uint32_t i = 0; i < 2; ++i ) {
std::cout << "Please enter the player's name: ";
std::getline(std::cin, player_name);
player_names.emplace_back(player_name);
}
return player_names;
}
bool is_letter_removed(const std::string& previous_word, const std::string& current_word) {
for ( uint64_t i = 0; i < previous_word.length(); ++i ) {
if ( current_word == previous_word.substr(0, i) + previous_word.substr(i + 1) ) {
return true;
}
}
return false;
}
bool is_letter_added(const std::string& previous_word, const std::string& current_word) {
return is_letter_removed(current_word, previous_word);
}
bool is_letter_changed(const std::string& previous_word, const std::string& current_word) {
if ( previous_word.length() != current_word.length() ) {
return false;
}
uint32_t difference_count = 0;
for ( uint64_t i = 0; i < current_word.length(); ++i ) {
difference_count += ( current_word[i] == previous_word[i] ) ? 0 : 1;
}
return difference_count == 1;
}
bool is_wordiff(const std::string& current_word,
const std::vector<std::string>& words_used,
const std::vector<std::string>& dictionary) {
if ( std::find(dictionary.begin(), dictionary.end(), current_word) == dictionary.end()
|| std::find(words_used.begin(), words_used.end(), current_word) != words_used.end() ) {
return false;
}
std::string previous_word = words_used.back();
return is_letter_changed(previous_word, current_word)
|| is_letter_removed(previous_word, current_word) || is_letter_added(previous_word, current_word);
}
std::vector<std::string> could_have_entered(const std::vector<std::string>& words_used,
const std::vector<std::string>& dictionary) {
std::vector<std::string> result;
for ( const std::string& word : dictionary ) {
if ( std::find(words_used.begin(), words_used.end(), word) == words_used.end()
&& is_wordiff(word, words_used, dictionary) ) {
result.emplace_back(word);
}
}
return result;
}
int main() {
std::vector<std::string> dictionary;
std::vector<std::string> starters;
std::fstream file_stream;
file_stream.open("../unixdict.txt");
std::string word;
while ( file_stream >> word ) {
dictionary.emplace_back(word);
if ( word.length() == 3 || word.length() == 4 ) {
starters.emplace_back(word);
}
}
std::random_device rand;
std::mt19937 mersenne_twister(rand());
std::shuffle(starters.begin(), starters.end(), mersenne_twister);
std::vector<std::string> words_used;
words_used.emplace_back(starters[0]);
std::vector<std::string> player_names = request_player_names();
bool playing = true;
uint32_t playerIndex = 0;
std::string current_word;
std::cout << "The first word is: " << words_used.back() << std::endl;
while ( playing ) {
std::cout << player_names[playerIndex] << " enter your word: ";
std::getline(std::cin, current_word);
if ( is_wordiff(current_word, words_used, dictionary) ) {
words_used.emplace_back(current_word);
playerIndex = ( playerIndex == 0 ) ? 1 : 0;
} else {
std::cout << "You have lost the game, " << player_names[playerIndex] << std::endl;
std::vector<std::string> missed_words = could_have_entered(words_used, dictionary);
std::cout << "You could have entered: [";
for ( uint64_t i = 0; i < missed_words.size() - 1; ++i ) {
std::cout << missed_words[i] << ", ";
}
std::cout << missed_words.back() << "]" << std::endl;
playing = false;
}
}
}
You may also check:How to resolve the algorithm File modification time step by step in the Elixir programming language
You may also check:How to resolve the algorithm Assertions step by step in the Lasso programming language
You may also check:How to resolve the algorithm Matrix digital rain step by step in the BASIC programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the Sidef programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the Plain English programming language