如何生成板游戏一样惊奇 [英] How to generate board for game like boggle

查看:103
本文介绍了如何生成板游戏一样惊奇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多惊奇像谷歌玩游戏,苹果商店和Facebook平台。如何生成一个可玩板像那些游戏?

There are many Boggle like games on Google Play, Apple Store and Facebook platform. How do I generate a playable board like those games?

推荐答案

最简单的方法是产生均匀选择随机字符网格。但这样做不会给你很多的话:

the simplest way is to generate a grid of random characters selected uniformly. but that will not give you many words:

from random import randint

N = 4

def display(letters):
    for row in letters:
        print('+%s+' % '+'.join('-' * N))
        print('|%s|' % '|'.join(row))
    print('+%s+' % '+'.join('-' * N))

def uniform_char():
    return chr(ord('A') + randint(0, 25))

def to_array(fn):
    return [[fn() for _ in range(N)] for _ in range(N)]

display(to_array(uniform_char))

+-+-+-+-+
|B|G|C|Z|
+-+-+-+-+
|G|B|T|K|
+-+-+-+-+
|I|R|O|Q|
+-+-+-+-+
|G|A|S|W|
+-+-+-+-+

上的改进是将重量信件由频率则发生在英语(假设这是你想要的语言):

an improvement on that would be to weight letters by how often then occur in english (assuming that is the language you want):

from collections import Counter

def letters():
    with open('/usr/share/dict/words', 'r') as words:
        for word in words:
            for letter in word.upper():
                if letter >= 'A' and letter <= 'Z':
                    yield letter

letter_scores = Counter(letters())
print(letter_scores)

# http://stackoverflow.com/questions/14992521/python-weighted-random/14992648
def weighted_random(pairs):
    total = sum(pair[1] for pair in pairs)
    r = randint(1, total)
    for (value, weight) in pairs:
        r -= weight
        if r <= 0: return value

display(to_array(lambda: weighted_random(letter_scores.items())))

Counter({'E': 301968, 'S': 274630, 'I': 241084, 'A': 225091, 'R': 191386,
'N': 191320, 'O': 184143, 'T': 177237, 'L': 151341, 'C': 111066, 
'U': 90838, 'D': 89014, 'M': 80645, 'P': 79507, 'G': 71689, 'H': 71423, 
'B': 52921, 'Y': 47941, 'F': 32114, 'V': 27918, 'K': 26797, 'W': 22635,  
'Z': 14022, 'X': 7861, 'J': 5130, 'Q': 4722})

+-+-+-+-+
|L|E|S|T|
+-+-+-+-+
|O|A|C|P|
+-+-+-+-+
|A|I|L|L|
+-+-+-+-+
|N|G|S|I|
+-+-+-+-+

更好的仍然是使用正克(如常见的字母对)和马尔可夫链,或只是某种随机抽样。在这里,我开始用概率加权(如上)的字母,然后设置几个邻居流行对(在组合我随机选择一个点,找到一个字母通常遵循信则有,并设置相邻方来表示):

better still would be to use n-grams (eg common letter pairs) and a markov chain or just some kind of random sampling. here i start with letters weighted by probability (as above) and then set a few neighbours to popular pairs (in mix i choose a random point, find a letter that commonly follows the letter there, and set a neighbouring square to that):

def pairs():
    with open('/usr/share/dict/words', 'r') as words:
        for word in words:
            prev = None
            for letter in word.upper():
                if letter >= 'A' and letter <= 'Z':
                    if prev: yield (prev, letter)
                    prev = letter

pair_scores = Counter(pairs())
#print(pair_scores)                                                             
start = to_array(lambda: weighted_random(letter_scores.items()))

def mix(array):
    x, y = randint(0, N-1), randint(0, N-1)
    a = array[y][x]
    neighbours = [(pair[1], score)
                  for (pair, score) in pair_scores.items()
                  if pair[0] == a]
    if neighbours:
        b = weighted_random(neighbours)
        # print(a, b, neighbours)                                               
        array[(y+randint(-1,1))%N][(x+randint(-1,1))%N] = b
    else:
        print('no neighbours for', a)

for _ in range(N*(N-1)//2): mix(start)
display(start)

+-+-+-+-+
|L|T|H|P|
+-+-+-+-+
|S|S|S|O|
+-+-+-+-+
|S|O|O|L|
+-+-+-+-+
|E|S|A|E|
+-+-+-+-+

不知道这是一个很大的进步,但要注意双S,TH等。

not sure it's a big improvement, but note double S, TH, etc.

最后,当然,你可以只注意在惊奇中使用的骰子的字母和从每次随机的选择,完全模拟游戏。

finally, of course, you could just note the letters on the dice used in boggle and select from each of those at random, exactly emulating the game.

在Linux上的所有code蟒蛇3。

all code python 3 on linux.

这篇关于如何生成板游戏一样惊奇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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