Hướng dẫn word search game in python - trò chơi tìm kiếm từ trong python

Chương trình được đưa ra dưới đây lấy một danh sách các từ và cố gắng để phù hợp với chúng vào một lưới với các kích thước đã cho để tạo một câu đố tìm kiếm từ. Ví dụ: sử dụng tệp planets.txt:

python make_wordsearch.py planets.txt 7 7

python make_wordsearch.py states.txt 11 11 circle

Các câu đố lớn, chẳng hạn như câu này chứa tất cả các tên phần tử, là có thể [nhấp vào hình ảnh lớn hơn, có thể in]:

# make_wordsearch.py
import os
import sys
import random
from copy import deepcopy

# Maximum number of rows and columns.
NMAX = 32
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def circle_mask[grid]:
    """A circular mask to shape the grid."""
    r2 = min[ncols, nrows]**2 // 4
    cx, cy = ncols//2, nrows // 2
    for irow in range[nrows]:
        for icol in range[ncols]:
            if [irow - cy]**2 + [icol - cx]**2 > r2:
                grid[irow][icol] = '*'

def squares_mask[grid]:
    """A mask of overlapping squares to shape the grid."""
    a = int[0.38 * min[ncols, nrows]]
    cy = nrows // 2
    cx = ncols // 2
    for irow in range[nrows]:
        for icol in range[ncols]:
            if a = 0 else nrows - 1
            if colmax - colmin {}'
                                .format[x, y, letter]]
            x += letter_width
        y += letter_height

    # We return the last y-coord used, to decide where to put the word list.
    return y, '\n'.join[s]

def wordlist_svg[wordlist, width, height, y0]:
    """Return a list of the words to find as a sequence of  elements."""

    # Use two columns of words to save [some] space.
    n = len[wordlist]
    col1, col2 = wordlist[:n//2], wordlist[n//2:]

    def word_at[x, y, word]:
        """The SVG element for word centred at [x, y]."""
        return [ ''
                 '{}'.format[x, y, word] ]

    s = []
    x = width * 0.25
    # Build the list of  elements for each column of words.
    y0 += 25
    for i, word in enumerate[col1]:
       s.append[word_at[x, y0 + 25*i, word]]
    x = width * 0.75
    for i, word in enumerate[col2]:
       s.append[word_at[x, y0 + 25*i, word]]
    return '\n'.join[s]

def write_wordsearch_svg[filename, grid, wordlist]:
    """Save the wordsearch grid as an SVG file to filename."""

    width, height = 1000, 1414
    with open[filename, 'w'] as fo:
        svg_preamble[fo, width, height]
        y0, svg_grid = grid_as_svg[grid, width, height]
        print[svg_grid, file=fo]
        # If there's room print the word list.
        if y0 + 25 * len[wordlist] // 2  4:
    mask = sys.argv[4]
if nrows > NMAX or ncols > NMAX:
    sys.exit['Maximum number of rows and columns is {}'.format[NMAX]]
wordlist = sorted[get_wordlist[wordlist_filename], key=lambda w: len[w],
                  reverse=True]
# Obviously, no word can be longer than the maximum dimension.
max_word_len = max[nrows, ncols]
if max[len[word] for word in wordlist] > max_word_len:
    raise ValueError['Word list contains a word with too many letters.'
                     'The maximum is {}'.format[max[nrows, ncols]]]

# This flag determines whether words can be fitted backwards into the grid
# [which makes the puzzle a bit harder].
allow_backwards_words = False
# If using a mask, specify it by a key to the apply_mask dictionary.
grid, solution = make_wordsearch[nrows, ncols, wordlist, allow_backwards_words,
                                 mask]

# If we fitted the words to the grid, show it in text format and save SVG files
# of the grid and its solution.
if grid:
    show_wordsearch_text[grid, wordlist]
    filename = os.path.splitext[wordlist_filename][0] + '.svg'
    write_wordsearch_svg[filename, grid, wordlist]
    filename = os.path.splitext[wordlist_filename][0] + '-solution.svg'
    write_wordsearch_svg[filename, solution, []]

Trò chơi từ trong Python là gì?

Người dùng trước tiên phải nhập tên của họ và sau đó, sẽ được yêu cầu đoán bất kỳ bảng chữ cái nào.Nếu từ ngẫu nhiên chứa bảng chữ cái đó, nó sẽ được hiển thị dưới dạng đầu ra [với vị trí chính xác] khác, chương trình sẽ yêu cầu bạn đoán bảng chữ cái khác.. If the random word contains that alphabet, it will be shown as the output[with correct placement] else the program will ask you to guess another alphabet.

Có một trò chơi tìm kiếm từ nào không?

Tìm kiếm từ là một trò chơi bao gồm các chữ cái của các từ được định dạng trong một lưới.Nói chung, lưới trò chơi Word là hình chữ nhật hoặc hình vuông trong tự nhiên.Mục tiêu là tìm và làm nổi bật tất cả các từ ẩn trong câu đố.

Làm thế nào để bạn chơi trò chơi tìm kiếm từ?

Bắt đầu trò chơi: Từ này phải được người chơi đọc to.Tất cả những người chơi bây giờ tìm kiếm từ trong lĩnh vực này.Từ ẩn có thể được định vị theo tất cả các hướng, được viết từ trái sang phải từ phải sang trái, theo chiều ngang, chiều dọc và đường chéo, nhưng luôn ở một đường thẳng.[Xem hình.

Bài Viết Liên Quan

Chủ Đề