Data update

This commit is contained in:
Ingy döt Net 2024-10-16 18:07:41 -07:00
parent 81fd053722
commit 52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions

View file

@ -0,0 +1,100 @@
use rand::seq::SliceRandom;
use std::collections::HashSet;
use std::fs;
use std::io::Write;
fn request_player_names(wanted: i32) -> Vec<String> {
let mut player_names = vec![];
for i in 0..wanted {
let mut buf = String::new();
print!("\nPlease enter the player's name for player {}: ", i + 1);
let _ = std::io::stdout().flush();
let _ = std::io::stdin().read_line(&mut buf);
player_names.push(buf.trim_end().to_owned());
}
return player_names;
}
fn is_letter_removed(previous_word: &str, current_word: &str) -> bool {
for i in 0..previous_word.len() {
if current_word == previous_word[..i].to_owned() + &previous_word[i + 1..] {
return true;
}
}
return false;
}
fn is_letter_added(previous_word: &str, current_word: &str) -> bool {
return is_letter_removed(current_word, previous_word);
}
fn is_letter_changed(previous_word: &str, current_word: &str) -> bool {
if previous_word.len() != current_word.len() {
return false;
}
let mut difference_count = 0;
for i in 0..current_word.len() {
difference_count += (current_word[i..=i] != previous_word[i..=i]) as usize;
}
return difference_count == 1;
}
fn is_wordiff(current_word: &str, words_used: &Vec<String>, dictionary: &HashSet<&str>) -> bool {
if !dictionary.contains(current_word) || words_used.contains(&current_word.to_string()) {
return false;
}
let previous_word = words_used.last().unwrap();
return is_letter_changed(previous_word, current_word)
|| is_letter_removed(previous_word, current_word)
|| is_letter_added(previous_word, current_word);
}
fn could_have_entered(words_used: &Vec<String>, dictionary: &HashSet<&str>) -> Vec<String> {
let mut result: Vec<String> = vec![];
for word in dictionary {
if is_wordiff(word, words_used, dictionary) {
result.push(word.to_string());
}
}
return result;
}
fn main() {
let mut rng = rand::thread_rng();
let wordsfile = fs::read_to_string("unixdict.txt").unwrap().to_lowercase();
let words = wordsfile.split_whitespace().collect::<Vec<&str>>();
let dictionary: &HashSet<&str> = &words.iter().cloned().collect();
let mut starters = words
.clone()
.into_iter()
.filter(|w| w.len() == 3 || w.len() == 4)
.collect::<Vec<&str>>();
starters.shuffle(&mut rng);
let mut words_used = vec![starters[0].to_string(); 1];
let player_names = request_player_names(2);
let mut playing = true;
let mut player_index = 0_usize;
let mut current_word = words_used.last().unwrap().to_string();
println!("\nThe first word is: {}", current_word);
let _ = std::io::stdout().flush();
while playing {
print!("{}, enter your word: ", player_names[player_index]);
let _ = std::io::stdout().flush();
current_word.clear();
let _ = std::io::stdin().read_line(&mut current_word);
current_word = current_word.trim_end().to_owned();
println!("You entered {}.", current_word);
if is_wordiff(&current_word, &words_used, dictionary) {
words_used.push(current_word.clone());
player_index = (player_index + 1) % player_names.len();
} else {
println!("You have lost the game, {}.", player_names[player_index]);
let missed_words = could_have_entered(&words_used, dictionary);
println!("You could have entered: {:?}", missed_words);
playing = false;
}
}
}

View file

@ -5,11 +5,11 @@ import arrays
fn is_wordiff(guesses []string, word string, dict []string) bool {
if word !in dict {
println('That word is not in the dictionary')
println("That word is not in the dictionary")
return false
}
if word in guesses {
println('That word has already been used')
println("That word has already been used")
return false
}
if word.len < guesses[guesses.len-1].len {
@ -25,16 +25,16 @@ fn is_wordiff_removal(new_word string, last_word string) bool {
return true
}
}
println('Word is not derived from previous by removal of one letter')
println("Word is not derived from previous by removal of one letter")
return false
}
fn is_wordiff_insertion(new_word string, last_word string) bool {
if new_word.len > last_word.len+1 {
println('More than one character insertion difference')
println("More than one character insertion difference")
return false
}
mut a := new_word.split('')
b := last_word.split('')
mut a := new_word.split("")
b := last_word.split("")
for c in b {
idx := a.index(c)
if idx >=0 {
@ -42,7 +42,7 @@ fn is_wordiff_insertion(new_word string, last_word string) bool {
}
}
if a.len >1 {
println('Word is not derived from previous by insertion of one letter')
println("Word is not derived from previous by insertion of one letter")
return false
}
return true
@ -55,16 +55,16 @@ fn is_wordiff_change(new_word string, last_word string) bool {
}
}
if diff != 1 {
println('More or less than exactly one character changed')
println("More or less than exactly one character changed")
return false
}
return true
}
fn main() {
words := os.read_lines('unixdict.txt')?
time_limit := os.input('Time limit (sec) or 0 for none: ').int()
players := os.input('Please enter player names, separated by commas: ').split(',')
words := os.read_lines("unixdict.txt")?
time_limit := os.input("Time limit (sec) or 0 for none: ").int()
players := os.input("Please enter player names, separated by commas: ").split(",")
dic_3_4 := words.filter(it.len in [3,4])
mut wordiffs := rand.choose<string>(dic_3_4,1)?
@ -73,25 +73,25 @@ fn main() {
mut turn_count := 0
for {
turn_start := time.now()
word := os.input('${players[turn_count%players.len]}: Input a wordiff from ${wordiffs[wordiffs.len-1]}: ')
word := os.input("${players[turn_count%players.len]}: Input a wordiff from ${wordiffs[wordiffs.len-1]}: ")
if time_limit != 0.0 && time.since(start).seconds()>time_limit{
println('TIMES UP ${players[turn_count%players.len]}')
println("TIMES UP ${players[turn_count%players.len]}")
break
} else {
if is_wordiff(wordiffs, word, words) {
wordiffs<<word
}else{
timing[turn_count%players.len] << time.since(turn_start).seconds()
println('YOU HAVE LOST ${players[turn_count%players.len]}')
println("YOU HAVE LOST ${players[turn_count%players.len]}")
break
}
}
timing[turn_count%players.len] << time.since(turn_start).seconds()
turn_count++
}
println('Timing ranks:')
println("Timing ranks:")
for i,p in timing {
sum := arrays.sum<f64>(p) or {0}
println(' ${players[i]}: ${sum/p.len:10.3 f} seconds average')
println(" ${players[i]}: ${sum/p.len:10.3 f} seconds average")
}
}