如何使用MATLAB计算Scrabble中的单词分数 [英] How to compute word scores in Scrabble using MATLAB

查看:230
本文介绍了如何使用MATLAB计算Scrabble中的单词分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个家庭作业计划我遇到了问题。我们基本上必须采用一个词(例如MATLAB)并使用Scrabble规则为函数提供正确的分数值。还有其他的事情,如双字和双点值,但我正在努力的是转换为ASCII。我需要将我的字符串转换为ASCII格式,然后总结这些值。我们只知道字符串的基本知识,我们的老师很没用。我已经尝试将字符串转换为数字,但这并不完全正常。有什么建议吗?

I have a homework program I have run into a problem with. We basically have to take a word (such as MATLAB) and have the function give us the correct score value for it using the rules of Scrabble. There are other things involved such as double word and double point values, but what I'm struggling with is converting to ASCII. I need to get my string into ASCII form and then sum up those values. We only know the bare basics of strings and our teacher is pretty useless. I've tried converting the string into numbers, but that's not exactly working out. Any suggestions?

function[score] = scrabble(word, letterPoints)

doubleword = '#';
doubleletter = '!';
doublew = [findstr(word, doubleword)]
trouble = [findstr(word, doubleletter)] 
word = char(word)
gameplay = word;
ASCII = double(gameplay)


score = lower(sum(ASCII));


推荐答案

基于弗朗西斯的帖子,我建议你做什么是创建一个查找数组。你当然可以将每个字符转换成它的ASCII等价物,但是我要做的是有一个数组,其中输入是你想要的字符的ASCII码(稍加修改),输出将是点值人物。一旦你找到了这个,你可以总结点数来得到你的最终得分。

Building on Francis's post, what I would recommend you do is create a lookup array. You can certainly convert each character into its ASCII equivalent, but then what I would do is have an array where the input is the ASCII code of the character you want (with a bit of modification), and the output will be the point value of the character. Once you find this, you can sum over the points to get your final point score.

我要省去双点,双字母,空格和那个现在,为了获得你想要的工作,Scrabble中有趣的东西。通过咨询维基百科,这是Scrabble中遇到的每个字母的点分布。

I'm going to leave out double points, double letters, blank tiles and that whole gamut of fun stuff in Scrabble for now in order to get what you want working. By consulting Wikipedia, this is the point distribution for each letter encountered in Scrabble.


  • 1分:A,E,I,O,N,R,T,L,S,U

  • 2分:D,G

  • 3分:B,C,M,P

  • 4分:F,H,V,W,是

  • 5分:K

  • 8分:J,X

  • 10分:Q,Z

  • 1 point: A, E, I, O, N, R, T, L, S, U
  • 2 points: D, G
  • 3 points: B, C, M, P
  • 4 points: F, H, V, W, Y
  • 5 points: K
  • 8 points: J, X
  • 10 points: Q, Z

我们要做的是将您的单词转换为小写以确保一致性。现在,如果您查看字母 a ,这对应于ASCII码97.您可以使用 double 我们之前谈过的功能:

What we're going to do is convert your word into lower case to ensure consistency. Now, if you take a look at the letter a, this corresponds to ASCII code 97. You can verify that by using the double function we talked about earlier:

>> double('a')

97

因为有26个字母在字母表,这意味着从 a z 应该从97到122.因为MATLAB开始索引数组1,我们可以做的是将每个字符减去96,这样我们就可以计算出这些字符从1到26的数字位置。

As there are 26 letters in the alphabet, this means that going from a to z should go from 97 to 122. Because MATLAB starts indexing arrays at 1, what we can do is subtract each of our characters by 96 so that we'll be able to figure out the numerical position of these characters from 1 to 26.

让我们从构建查找表开始。首先,我要定义一大堆字符串。每个字符串表示与Scrabble中每个点相关联的字母:

Let's start by building our lookup table. First, I'm going to define a whole bunch of strings. Each string denotes the letters that are associated with each point in Scrabble:

string1point = 'aeionrtlsu';
string2point = 'dg';
string3point = 'bcmp';
string4point = 'fhvwy';
string5point = 'k';
string8point = 'jx';
string10point = 'qz';

现在,我们可以使用每个字符串,转换为 double ,减去96然后将每个相应的位置分配给每个字母的点。让我们像这样创建我们的查找表:

Now, we can use each of the strings, convert to double, subtract by 96 then assign each of the corresponding locations to the points for each letter. Let's create our lookup table like so:

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;

我首先通过 zeros 功能。然后我找出每个字母的去向,并为每个字母分配它们的点值。

I first create an array of length 26 through the zeros function. I then figure out where each letter goes and assign to each letter their point values.

现在,你需要做的最后一件事就是取一个字符串,取小写字母可以肯定的是,然后将每个字符转换为 ASCII 等价物,减去96,然后总结这些值。如果我们给...说... MATLAB:

Now, the last thing you need to do is take a string, take the lower case to be sure, then convert each character into its ASCII equivalent, subtract by 96, then sum up the values. If we are given... say... MATLAB:

stringToConvert = 'MATLAB';
stringToConvert = lower(stringToConvert);
ASCII = double(stringToConvert) - 96;
value = sum(lookup(ASCII));

请注意......我们得到:

Lo and behold... we get:

value =

 10

上面代码的最后一行至关重要。基本上, ASCII 将包含一堆索引位置,其中每个数字对应于字母出现在字母表中的数字位置。我们使用这些位置来查找每个字母给我们的点/分数,并且我们总结所有这些值。

The last line of the above code is crucial. Basically, ASCII will contain a bunch of indexing locations where each number corresponds to the numerical position of where the letter occurs in the alphabet. We use these positions to look up what point / score each letter gives us, and we sum over all of these values.

可以在我的其他StackOverflow帖子中找到双点值和双字出现的下一部分:

The next part where double point values and double words come to play can be found in my other StackOverflow post here:

计算双字母和双字MATLAB的拼字游戏单词分数

这篇关于如何使用MATLAB计算Scrabble中的单词分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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