Wordiff: Difference between revisions

7,404 bytes added ,  2 months ago
Added FreeBASIC
(New post.)
(Added FreeBASIC)
 
(4 intermediate revisions by 2 users not shown)
Line 211:
Bill, what is the next word? famous
Not a correct wordiff. Try again: fame</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#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;
}
}
}
</syntaxhighlight>
{{ out }}
<pre>
Please enter the player's name: Alice
Please enter the player's name: Bob
The first word is: her
Alice enter your word: hem
Bob enter your word: hemp
Alice enter your word: temp
You have lost the game, Alice
You could have entered: [heap, help, hump, kemp]
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">REM Wordiff ' 10 march 2024 '
 
'--- Declaración de variables globales ---
Dim Shared As String words()
Dim Shared As String used()
Dim Shared As String player1
Dim Shared As String player2
Dim Shared As String player
Dim Shared As String prevWord
Dim Shared As Integer prevLen
 
'--- SUBrutinas y FUNCiones ---
Sub loadWords
Dim As Integer i, j, numLines
Dim As String linea
Open "unixdict.txt" For Input As #1
While Not Eof(1)
Line Input #1, linea
If Len(linea) = 3 Or Len(linea) = 4 Then
Redim Preserve words(numLines)
words(numLines) = linea
numLines += 1
End If
Wend
Close #1
End Sub
 
Sub Intro
Cls
Color 10, 0 '10, black
Locate 10, 30 : Print "---WORDIFF---"
Locate 12, 5 : Print "Por turnos, teclear nuevas palabras del "
Locate 13, 5 : Print "diccionario de tres o mas caracteres que "
Locate 14, 5 : Print "se diferencien de la anterior en una letra."
Color 14
Locate 16, 5 : Input "Player 1, please enter your name: ", player1
Locate 17, 5 : Input "Player 2, please enter your name: ", player2
If player2 = player1 Then player2 &= "2"
Color 7
End Sub
 
 
Function wordExists(word As String) As Boolean
For i As Integer = 0 To Ubound(words)
If words(i) = word Then Return True
Next i
Return False
End Function
 
Function wordUsed(word As String) As Boolean
For i As Integer = 0 To Ubound(used)
If used(i) = word Then Return True
Next i
Return False
End Function
 
Sub addUsedWord(word As String)
Redim Preserve used(Ubound(used) + 1)
used(Ubound(used)) = word
End Sub
 
Sub MenuPrincipal
Dim As String word
Dim As Integer i, changes, longi
Dim As Boolean ok
Cls
prevWord = words(Int(Rnd * Ubound(words)))
prevLen = Len(prevWord)
player = player1
Print "The first word is ";
Color 15 : Print prevWord : Color 7
Do
Color 7 : Print player; ", plays:";
Color 15 : Input " ", word
word = Lcase(word)
longi = Len(word)
ok = False
Color 12
If longi < 3 Then
Print "Words must be at least 3 letters long."
Elseif wordExists(word) = 0 Then
Print "Not in dictionary."
Elseif wordUsed(word) <> 0 Then
Print "Word has been used before."
Elseif word = prevWord Then
Print "You must change the previous word."
Elseif longi = prevLen Then
changes = 0
For i = 1 To longi
If Mid(word, i, 1) <> Mid(prevWord, i, 1) Then changes += 1
Next i
If changes > 1 Then
Print "Only one letter can be changed."
Else
ok = True
End If
Else
Print "Invalid change."
End If
If ok Then
prevLen = longi
prevWord = word
addUsedWord(word)
player = Iif(player = player1, player2, player1)
Else
Print "So, sorry "; player; ", you've lost!"
Dim As String KBD
Do: KBD = Inkey: Loop While KBD = ""
Exit Do
End If
Loop
End Sub
 
'--- Programa Principal ---
Randomize Timer
Intro
loadWords
MenuPrincipal
End
'--------------------------</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
Line 630 ⟶ 881:
List<String> playerNames = new ArrayList<String>();
for ( int i = 0; i < 2; i++ ) {
System.out.print("Please enter the first player's name: ");
String playerName = scanner.nextLine().trim();
playerNames.add(playerName);
Line 641 ⟶ 892:
{{ out }}
<pre>
Please enter the first player's name: Alice
Please enter the first player's name: Bob
The first word is: tnt
Alice enter your word:
Line 1,852 ⟶ 2,103:
{{libheader|Wren-sort}}
Due to a bug in the System.clock method (basically it gets suspended whilst waiting for user input), it is not currently possible to add timings.
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./ioutil" for File, Input
import "./str" for Str
import "./sort" for Find
 
var rand = Random.new()
2,122

edits