1D 生命游戏 - 我的范围应该是多少?

问题描述 投票:0回答:1

我们的任务是编写一个模拟一维生命游戏的程序。我们应该创建一个蜘蛛图案。我已经让它工作了,但是在第一个字符串初始化中需要有大量的空格才能打印整个数字,因为每行都会丢失 2 个空格。 无论初始化如何,我的 for 循环中的范围应该是多少才能打印整个图形?

注意:我们不应该使用数组、矩阵或任何其他聪明的方法或途径。我知道它们,但它们只期望 if 语句、while/for 循环和变量(主要)。

我写了这段代码:

def main():
    a="       ******       "
    print(a)
    empty=False
    while (empty==False):
        b=""
        for i in range(len(a)-2):
            alive_cells=0
            if (i==0):
                if(a[i+1]=="*"):
                    alive_cells+=1
                if(a[i+2]=="*"):
                    alive_cells+=1
            elif (i==1):
                if(a[i-1]=="*"):
                    alive_cells+=1
                if(a[i+1]=="*"):
                    alive_cells+=1
                if(a[i+2]=="*"):
                    alive_cells+=1
            else:
                if(a[i-2]=="*"):
                    alive_cells+=1
                if(a[i-1]=="*"):
                    alive_cells+=1
                if(a[i+1]=="*"):
                    alive_cells+=1
                if(a[i+2]=="*"):
                    alive_cells+=1
            if (a[i]==" " and (alive_cells==1 or alive_cells==4 or alive_cells==0)):
                b+=" "
            elif(a[i]==" " and (alive_cells==2 or alive_cells==3)):
                b+="*"
            elif(a[i]=="*" and (alive_cells==0 or alive_cells==1 or alive_cells==3)):
                b+=" "
            elif(a[i]=="*" and (alive_cells==2 or alive_cells==4)):
                b+="*"
        if (b.isspace()==True):
            empty=True
            break
        a=b
        print(a)
        
            
main()

打印以下输出:

   ******       
  ** ** **    
 * * ** * *
  ********
 ** ****
* *
 *

它应该打印这个:

    ******
   ** ** **
  * * ** * *
   ********
  ** **** **
 * *      * *
  *        *
python conways-game-of-life
1个回答
0
投票

我每次只附加三个空格。那么你也不需要额外的边缘情况区分:

def main():
    a="       ******       "
    print(a)
    empty=False
    while (empty==False):
        b=""
        a += "   "
        for i in range(len(a)-2):
            alive_cells=0
            if(a[i-2]=="*"):
                alive_cells+=1
            if(a[i-1]=="*"):
                alive_cells+=1
            if(a[i+1]=="*"):
                alive_cells+=1
            if(a[i+2]=="*"):
                    alive_cells+=1
            if (a[i]==" " and (alive_cells==1 or alive_cells==4 or alive_cells==0)):
                b+=" "
            elif(a[i]==" " and (alive_cells==2 or alive_cells==3)):
                b+="*"
            elif(a[i]=="*" and (alive_cells==0 or alive_cells==1 or alive_cells==3)):
                b+=" "
            elif(a[i]=="*" and (alive_cells==2 or alive_cells==4)):
                b+="*"
        if (b.isspace()==True):
            empty=True
            break
        a=b
        print(a)
        
            
main()

输出(在线尝试!):

       ******       
      ** ** **       
     * * ** * *       
      ********         
     ** **** **         
    * *      * *         
     *        *           
© www.soinside.com 2019 - 2024. All rights reserved.