如何简化这个很长的 if 语句?

发布于 2024-09-30 14:42:04 字数 534 浏览 7 评论 0原文

如何简化这个 if 语句?它产生一个加号: https://i.sstatic.net/PtHO1.png

如果语句完成,则块设置在 x 和 y 坐标处。

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        if (x%5 == 2 or x%5 == 3 or x%5 == 4) and \
            (y%5 == 2 or y%5 == 3 or y%5 == 4) and \
            not(x%5 == 2 and y%5 == 2) and \
            not(x%5 == 4 and y%5 == 2) and \
            not(x%5 == 2 and y%5 == 4) and \
            not(x%5 == 4 and y%5 == 4):
            ...

How can this if-statement be simplified? It makes a plus sign:
https://i.sstatic.net/PtHO1.png

If the statement is completed, then a block is set at the x and y coordinates.

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        if (x%5 == 2 or x%5 == 3 or x%5 == 4) and \
            (y%5 == 2 or y%5 == 3 or y%5 == 4) and \
            not(x%5 == 2 and y%5 == 2) and \
            not(x%5 == 4 and y%5 == 2) and \
            not(x%5 == 2 and y%5 == 4) and \
            not(x%5 == 4 and y%5 == 4):
            ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

妳是的陽光 2024-10-07 14:42:04

这是一样的:

if (x % 5 == 3 and y % 5 > 1) or (y % 5 == 3 and x % 5 > 1): 

This is the same:

if (x % 5 == 3 and y % 5 > 1) or (y % 5 == 3 and x % 5 > 1): 
慢慢从新开始 2024-10-07 14:42:04

基本上你正在平铺一个 5x5 的二​​进制图案。这是一个清晰的表达:

pattern = [[0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 0, 0, 1, 0],
           [0, 0, 1, 1, 1],
           [0, 0, 0, 1, 0]]

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        if pattern[x%5][y%5]:
           ...

这是一种非常简单且通用的方法,可以轻松修改模式。

Basically you're tiling a 5x5 binary pattern. Here's a clear expression of that:

pattern = [[0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 0, 0, 1, 0],
           [0, 0, 1, 1, 1],
           [0, 0, 0, 1, 0]]

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        if pattern[x%5][y%5]:
           ...

This is a very simple and general approach which would allow the pattern to be easily modified.

烂人 2024-10-07 14:42:04

有两个简单的修复:

  • 缓存 x % 5y % 5 的结果
  • 使用 in 或链接 < > 来测试这些值:

此外,对 <= 4(或 <5)的测试实际上是多余的,因为 每个 值code>lx 和 ly 将是 lxly 将是 lxly 5.

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        lx = x % 5 # for local-x
        ly = y % 5 # for local-y
        if lx > 1 and y > 1 and \
           not (lx == 2 and ly == 2) and \
           not (lx == 4 and ly == 2) and \
           not (lx == 2 and ly == 4) and \
           not (lx == 4 and ly == 4):

或者您可以只保留实际允许的元组的列表:

cross_fields = [(2, 3), (3, 2), (3, 3), (3, 4), (4, 3)]

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        if (x % 5, y % 5) in cross_fields:

There are two trivial fixes:

  • Cache the result of x % 5 and y % 5
  • Use in or chained < to test the values:

Additionally, the test for <= 4 (or < 5) is actually redundant because every value of lx and ly will be < 5.

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        lx = x % 5 # for local-x
        ly = y % 5 # for local-y
        if lx > 1 and y > 1 and \
           not (lx == 2 and ly == 2) and \
           not (lx == 4 and ly == 2) and \
           not (lx == 2 and ly == 4) and \
           not (lx == 4 and ly == 4):

Or you may just keep a list of the actually allowed tuples:

cross_fields = [(2, 3), (3, 2), (3, 3), (3, 4), (4, 3)]

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        if (x % 5, y % 5) in cross_fields:
我不会写诗 2024-10-07 14:42:04

基于康拉德的答案,您可以进一步简化它:

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        lx = x % 5 # for local-x
        ly = y % 5 # for local-y
        if (1 < lx < 5 and 1 < y < 5 and 
           (lx, ly) not in ((2, 2), (4, 2), (2, 4), (4, 2))):

Building on Konrad's answer, you can simplify it further:

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        lx = x % 5 # for local-x
        ly = y % 5 # for local-y
        if (1 < lx < 5 and 1 < y < 5 and 
           (lx, ly) not in ((2, 2), (4, 2), (2, 4), (4, 2))):
意中人 2024-10-07 14:42:04

康拉德的第二个答案:-

cross_fields = [(2, 3), (3, 2), (3, 3), (3, 4), (4, 3)]

for y in range(MAP_HEIGHT):
  for x in range(MAP_WIDTH):
    if (x % 5, y % 5) in cross_fields:

可能是最好的一个。

不过,我会贡献:-

for y in range(MAP_HEIGHT):
  for x in range(MAP_WIDTH):
    lx = x % 5
    ly = y % 5
    if (lx > 1 and ly == 3) or (ly > 1 and lx == 3):

Konrad's second answer:-

cross_fields = [(2, 3), (3, 2), (3, 3), (3, 4), (4, 3)]

for y in range(MAP_HEIGHT):
  for x in range(MAP_WIDTH):
    if (x % 5, y % 5) in cross_fields:

is probably the best one.

However, I'll contribute:-

for y in range(MAP_HEIGHT):
  for x in range(MAP_WIDTH):
    lx = x % 5
    ly = y % 5
    if (lx > 1 and ly == 3) or (ly > 1 and lx == 3):
猫瑾少女 2024-10-07 14:42:04

优化此类逻辑函数的一般解决方案是卡诺图。您的真值表将是您想要的文字加形状,其中行和列是您的模块化测试。

The general solution to optimizing a logic function like this is a Karnaugh map. Your truth table would be the literal plus shape you want with rows and columns being your modular tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文