让你永远赢得Popcapbookworm
时间:2021-07-01 10:21:17
帮助过:27人阅读
Bookworm是个很不错的游戏,如果你不知道它,那么可以在app store里搜索一下。我用php写了一个永远会拼出最高分词汇的程序,嘿嘿,我好坏
fullwordlist.txt 就是词典,文件太长,就不贴出来了,感兴趣的朋友可以网上随便搜一个。
-
- // author: huangfeng爱吴佳旻
- if(empty($_POST)) die;
- // 接收并处理原始输入数据,w代表字符,v代表对应的分数
- $data = array();
- for($i=1;$i<=24;$i++){
- array_push($data, array('w' => strtoupper(trim($_POST['w'.$i])), 'v' => trim($_POST['v'.$i])));
- }
- // 去重以便计算求和的分数
- $data_unduplicated = array();
- foreach($data as $key => $value){
- $data_unduplicated[] = implode(',', $value);
- }
- $data_unduplicated = array_unique($data_unduplicated);
- // 求每个字符的出现频次
- $data_frequency = array();
- foreach ($data as $arr) {
- $data_frequency[] = $arr['w'];
- }
- $data_frequency_result = array_count_values($data_frequency);
- // 将词汇表文件读入数组
- $words = file('./fullwordlist.txt', FILE_IGNORE_NEW_LINES);
- // 将词汇表全部转成大写英文
- $words = array_map(function ($word){
- return strtoupper($word);
- }, $words);
- // 对每个单词进行频次判断
- $result = array();
- foreach ($words as $k => $word) {
- // 遇到含有'的单词,就略过
- if(strpos($word, "'") > 0) continue;
- // 对$word做字母拆解,判断每个字母的出现频次
- $word_arr = str_split($word);
- $word_arr_frequency = array_count_values($word_arr);
- $pass = true;
- foreach ($word_arr_frequency as $character => $frequency) {
- if(!array_key_exists($character, $data_frequency_result) || ($data_frequency_result[$character] < $frequency)) {
- $pass = false;
- }
- }
- // 对筛选出的$word进行求分数操作
- if($pass) {
- $word_value = 0;
- foreach ($word_arr as $wkey => $wchar) {
- foreach($data_unduplicated as $data_item) {
- $data_item = explode(',', $data_item);
- // var_dump($data_item); array 0 => string 'A' (length=1) 1 => string '3' (length=1)
- if($wchar != $data_item[0]) continue;
- $word_value += $data_item[1];
- }
- }
- $result[] = array('w' => $word, 'v' => $word_value);
- }
- }
- // 最后只选出分数最高的
- $k = 0;
- $highest = 0;
- foreach ($result as $key => $item) {
- if(intval($item['v']) >= $highest){
- $highest = $item['v'];
- $k = $key;
- }
- }
- echo '最高分的词是:' . $result[$k]['w'] . '('.$result[$k]['v'].')';;
|