Let you win Popcap bookworm forever

WBOY
Release: 2016-07-25 08:51:07
Original
1201 people have browsed it
Bookworm is a very good game. If you don’t know it, you can search for it in the app store. I used PHP to write a program that will always spell out the highest-scoring words. Hey, how am I?

fullwordlist.txt is a dictionary. The file is too long, so I won’t post it. Interested friends can search for one online.

  1. 字符:


  2. 占分:




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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!