当前位置:Gxlcms > PHP教程 > 让你永远赢得Popcapbookworm

让你永远赢得Popcapbookworm

时间:2021-07-01 10:21:17 帮助过:27人阅读

Bookworm是个很不错的游戏,如果你不知道它,那么可以在app store里搜索一下。我用php写了一个永远会拼出最高分词汇的程序,嘿嘿,我好坏

fullwordlist.txt 就是词典,文件太长,就不贴出来了,感兴趣的朋友可以网上随便搜一个。


  1. // author: huangfeng爱吴佳旻
  2. if(empty($_POST)) die;
  3. // 接收并处理原始输入数据,w代表字符,v代表对应的分数
  4. $data = array();
  5. for($i=1;$i<=24;$i++){
  6. array_push($data, array('w' => strtoupper(trim($_POST['w'.$i])), 'v' => trim($_POST['v'.$i])));
  7. }
  8. // 去重以便计算求和的分数
  9. $data_unduplicated = array();
  10. foreach($data as $key => $value){
  11. $data_unduplicated[] = implode(',', $value);
  12. }
  13. $data_unduplicated = array_unique($data_unduplicated);
  14. // 求每个字符的出现频次
  15. $data_frequency = array();
  16. foreach ($data as $arr) {
  17. $data_frequency[] = $arr['w'];
  18. }
  19. $data_frequency_result = array_count_values($data_frequency);
  20. // 将词汇表文件读入数组
  21. $words = file('./fullwordlist.txt', FILE_IGNORE_NEW_LINES);
  22. // 将词汇表全部转成大写英文
  23. $words = array_map(function ($word){
  24. return strtoupper($word);
  25. }, $words);
  26. // 对每个单词进行频次判断
  27. $result = array();
  28. foreach ($words as $k => $word) {
  29. // 遇到含有'的单词,就略过
  30. if(strpos($word, "'") > 0) continue;
  31. // 对$word做字母拆解,判断每个字母的出现频次
  32. $word_arr = str_split($word);
  33. $word_arr_frequency = array_count_values($word_arr);
  34. $pass = true;
  35. foreach ($word_arr_frequency as $character => $frequency) {
  36. if(!array_key_exists($character, $data_frequency_result) || ($data_frequency_result[$character] < $frequency)) {
  37. $pass = false;
  38. }
  39. }
  40. // 对筛选出的$word进行求分数操作
  41. if($pass) {
  42. $word_value = 0;
  43. foreach ($word_arr as $wkey => $wchar) {
  44. foreach($data_unduplicated as $data_item) {
  45. $data_item = explode(',', $data_item);
  46. // var_dump($data_item); array 0 => string 'A' (length=1) 1 => string '3' (length=1)
  47. if($wchar != $data_item[0]) continue;
  48. $word_value += $data_item[1];
  49. }
  50. }
  51. $result[] = array('w' => $word, 'v' => $word_value);
  52. }
  53. }
  54. // 最后只选出分数最高的
  55. $k = 0;
  56. $highest = 0;
  57. foreach ($result as $key => $item) {
  58. if(intval($item['v']) >= $highest){
  59. $highest = $item['v'];
  60. $k = $key;
  61. }
  62. }
  63. echo '最高分的词是:' . $result[$k]['w'] . '('.$result[$k]['v'].')';;

人气教程排行