计算双字母和双字的拼字游戏单词分数MATLAB [英] Calculate Scrabble word scores for double letters and double words MATLAB

查看:158
本文介绍了计算双字母和双字的拼字游戏单词分数MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里的目标是创建代码,以将字符串中的字母作为字母并给出一个分数,就像在Scrabble中一样.它必须考虑双字得分(由!指示)和双字母得分(由#指示).

My goal here is to create code that takes the letters in a string and gives out a score like in Scrabble. It has to account for double words scores (dictated by a !) and double letter score (dictated by a #).

Example: If you input was 'Hel#lo!', then the first 'l' was played on a
double letter score space, and the final 'o' was played on a            
double word score space. The output score would be 18.

感谢rayryeng的帮助,如果没有双字或双字母,我终于可以读取分数值,但是现在我似乎无法弄清这些值.如果我能提供一些指导,我将不胜感激.

Thanks to help from rayryeng I finally got it to read off the score value if there were no double words or double letters, but now I can't seem to get it to figure out those. If I could have some guidance in how to go about it, I would appreciate it.

doubleword = '#'; %// Here I labeled some things
doubleletter = '!';
doublew = strfind(word, doubleword-1); %// I want to find where the double words are
doublewb = 2;
trouble = strfind(word, doubleletter-1); %// I want to find where double letters are
troubleb = letterPoints*2;
Convert = word;
stringToConvert = lower(Convert);
ASCII = double(stringToConvert) - 96;
Origscore = (sum(values(ASCII)));
score = doublewb*(sum(Origscore)+troubleb);

最后一行给我带来麻烦.我得到一个数组,现在只是一个干净的数字.此外,当我尝试运行简单的字符串"matlab"时,它给了我一个错误.还是一个很奇怪的数字,而不是一个简单的数字.我想我需要做一些进一步的索引编制,但是我有些困惑.我知道这应该是最困难的问题,尽管我一直在努力解决所有问题. 'Q#uar!tz#ifer#ous!'应该给220."Ramblin!gwreck"应该给54.

The last line gives me trouble. I get an array and now just a nice clean number. Furthermore, when I try to run my simple string 'matlab', it gives me an error. Or a really weird, not a simple number. I think I need to do some further indexing, but I'm a little confused. I know this is supposed to be the hardest problem, though I've been struggling with them all. 'Q#uar!tz#ifer#ous!' should give 220. 'Ramblin!gwreck' should give 54.

推荐答案

为什么再次问好!让我们重新创建在之前的帖子中中创建的查找:

Why hello again! Let's recreate the lookup that we created in our previous post:

string1point = 'aeionrtlsu';
string2point = 'dg';
string3point = 'bcmp';
string4point = 'fhvwy';
string5point = 'k';
string8point = 'jx';
string10point = 'qz';
lookup = zeros(1,26);
lookup(double(string1point) - 96) = 1;
lookup(double(string2point) - 96) = 2;
lookup(double(string3point) - 96) = 3;
lookup(double(string4point) - 96) = 4;
lookup(double(string5point) - 96) = 5;
lookup(double(string8point) - 96) = 8;
lookup(double(string10point) - 96) = 10;

在执行其他任何操作之前,我们需要将所有字母都小写以访问查找表.

Before we do anything else, we need to lowercase all of the letters in order to access the lookup table.

lowerWord = lower(word);

完成此操作后,让我们找出双字母和双字的位置.在要查找哪些字母是双字母(#)和/或哪些磁贴击中双字磁贴(!)的地方,您几乎完全正确了.您需要将1移到方括号之外,以便找出实际的字母是什么,而不是符号所在的位置.换句话说:

Once we do this, let's figure out where the double letters and double words are located. You've almost got it right where you want to find which letters are double letters (#) and/or which tiles hit the double words tiles (!). You need to move the 1 outside of the brackets so you can figure out what the actual letter is, not where the symbol is located. In other words:

doublew = strfind(lowerWord, '!') - 1;
doublel = strfind(lowerWord, '#') - 1;

我们现在需要做的是提取没有任何符号的原始字符串.我们可以通过以下方式做到这一点:

What we need to do now is extract the original string, without any symbols. We can do this by:

originalWord = lowerWord;
originalWord([doublew + 1 doublel + 1]) = [];

我必须在每个位置加1,因为我想访问符号所在的位置.我使用符号所在的两个位置,然后使用[]消除这些符号.这应该给我们我们最初的话.现在,我们要做的是在查询表中访问每个字母的点值并汇总点.换句话说:

I have to add 1 to each of those locations because I want to access where the symbols are located. I use both locations of where the symbols are, then use [] to eliminate those symbols. This should give us our original word. Now, what we're going to do is access what each letter is worth in points in our lookup table and sum up the points. In other words:

ASCII = double(originalWord) - 96;
basePoints = sum(lookup(ASCII));

现在,我们的积分是:

basePoints =

 8

8分是我们分数调整前的得分.现在,让我们一次解决一个案例:

8 points is how many points we have before any adjustment in score. Now, let's tackle each case one at a time:

如果您想起以前的话,doublewdoublel将包含字母的位置,这些位置会随着分数的增加而增加.

If you remember from before, doublew and doublel will contain the locations of the letters that are subject to the increase in score.

首先,让我们找出哪些字母的点值加倍,然后找出它们在查找表中的位置,然后累加该分数是多少:

First, let's figure out what letters are subject to doubling their point value, and figure out where these are in the lookup table and add up how much this score is:

doublelettersInd = double(lowerWord(doublel)) - 96;
sumLetters = sum(lookup(doublelettersInd));

sumLetters将包含所有这些字母的总分,其值将加倍.现在,这里变得棘手.我接下来要做的是将此分数存储在sumLetters中,并添加在基本分数(本例中为8)的基础上.这将有效地将要加倍的那些字母加倍.请记住,如果您向自身添加一些东西,那么您将获得两倍的价值.因为在基本分数中,我已经添加了仅作为基本分数的那些字母,如果我随后找到要加倍的那些字母并获得它们的分数并将它们添加到我们的原始分数之上,我将将相应字母的分数加倍:

sumLetters will contain the total score of all of these letters subject to be doubled in value. Now here's where it gets tricky. What I'm going to do next is take this score stored in sumLetters and add this on top of the base score (which is 8 in our case). This will effectively take those letters that are to be doubled and double them. Remember, if you add something to itself, you are doubling the value. Because in the base score, I've already added those letters that are just the base score, if I then find those letters that are to be doubled and get the scores of those and add them on top of our original score, I will be doubling the score of the corresponding letters:

doublePoints = basePoints + sumLetters;

双词

您需要做的最后一件事是计算我们遇到双字图块的次数.然后,您将使用该数字,并且必须将其用作2的幂的指数,因为这将有效显示需要翻倍的次数.如果您一次击中双倍图块,则分数会翻倍(即2^1 = 2).如果您两次按下双字图块,则您的得分将翻两番(即2^2 = 4),依此类推.换句话说:

Double Words

The last thing you need to do is count how many times we have encountered the double word tile. You would then take this number, and you'd have to use this as the exponent to a power of two as this would effectively show how many times you would need to double. If you hit the double tile once, you double your score (i.e. 2^1 = 2). If you hit the double word tile twice, you would quadruple your score (i.e. 2^2 = 4) , and so on. In other words:

finalPoints = (2^numel(doublew))*doublePoints;

numel 计算数组中有多少个元素或矩阵.这样,doublew的长度将告诉我们遇到双字图块的次数.

numel counts how many elements are in an array or matrix. As such, the length of doublew will tell us how many times we have encountered a double word tile.

^很重要,因为它将正确加倍. ^代表幂,它处理了我们在正确加倍您的分数时所谈论的内容.这样就可以处理我们没有遇到任何双字拼贴的情况,如2^0 = 1.

The ^ is important, as this will double correctly. The ^ stands for exponentiation, and this handles what we talked about when correctly doubling your score. This will be able to handle the case where we don't encounter any double word tiles, as 2^0 = 1.

一旦完成所有这些操作,当您使用上面的代码执行word = 'Hel#lo!';时,我们将得到:

Once we do all this, when you do word = 'Hel#lo!';, with the above code, we get:

finalPoints =

 18

通过其他示例,我得到:

With your other examples, I get:

....酷吗? :)

.... cool? :)

另外,在您使用上述测试用例的情况下,对于Ramblin!gwreck,我得到:

Also, with your above test cases, for Ramblin!gwreck, I get:

finalPoints = 

54

对于Q#uar!tz#ifer#ous!,我得到:

finalPoints = 

220


要使这一切都好,以便可以将其复制并粘贴到编辑器中以便可以运行它,这是单个代码块中的代码:


To make this all nice so that you can copy and paste this into your editor so you can run it, here's the code in a single code block:

%// Define word here
word = 'Ramblin!gwreck';
%word = 'Q#uar!tz#ifer#ous!';

%// Convert word to lower case
lowerWord = lower(word);

%// Create look up table
string1point = 'aeionrtlsu';
string2point = 'dg';
string3point = 'bcmp';
string4point = 'fhvwy';
string5point = 'k';
string8point = 'jx';
string10point = 'qz';
lookup = zeros(1,26);
lookup(double(string1point) - 96) = 1;
lookup(double(string2point) - 96) = 2;
lookup(double(string3point) - 96) = 3;
lookup(double(string4point) - 96) = 4;
lookup(double(string5point) - 96) = 5;
lookup(double(string8point) - 96) = 8;
lookup(double(string10point) - 96) = 10;

%// Find letters that are either double word or double letter
doublew = strfind(lowerWord, '!') - 1;
doublel = strfind(lowerWord, '#') - 1;

%// Get original word without symbols
originalWord = lowerWord;
originalWord([doublew + 1 doublel + 1]) = [];

%// Get the base points of the word
ASCII = double(originalWord) - 96;
basePoints = sum(lookup(ASCII));

%// Find those letters that need to be doubled in point value
%// and double their score
doublelettersInd = double(lowerWord(doublel)) - 96;
sumLetters = sum(lookup(doublelettersInd));
doublePoints = basePoints + sumLetters;

%// Finally, if we need to double the word score, do it
finalPoints = (2^numel(doublew))*doublePoints;

这篇关于计算双字母和双字的拼字游戏单词分数MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆