在游戏中创造灯光

时间:2015-10-12 04:51:43

标签: python python-3.x recursion

我正在为一款名为Lights On的游戏编写代码,其中游戏的想法是制作一块随机点亮的方块并赢得所有方格必须点亮(1是0开关)。单击一个正方形可反转该正方形以及与其相对的上方,下方,左侧或右侧的任何方块。但是,我无法使用我的代码正确反转电路板

import time # provides time.sleep(0.5)
from csplot import choice
from random import * # provides choice( [0,1] ), etc.
import sys  # larger recursive stack
sys.setrecursionlimit(100000) # 100,000 deep

def runGenerations2d(L , x = 0,y=0):
    show(L)
    print( L )           # display the list, L
    time.sleep(.1)      # pause a bit
    newL = evolve2d( L )   # evolve L into newL
    print(newL)
    if min(L) == 1:
    #I like read outs to be explained so I added an extra print command.
        if x<=1: # Takes into account the possibility of a 1 click completition.
            print ('BaseCase Reached!... it took %i click to complete' % (x))
            print (x)
            done()#removes the need to input done() into the shell
        else:
            print ('BaseCase Reached!... it took %i clicks to complete' % (x))
            print (x)
            done()#removes the need to input done() into the shell
        return   
    x = x+1 # add 1 to x before every recusion
    runGenerations2d( newL , x,y )  # recurse

def evolve2d( L ):    
    N = len(L)  
    x,y = sqinput2()
    return [[ setNewElement2d( L, i, j,x,y ) for i in range(N)]for j in range(N) ]

def setNewElement2d( L, i, j, x=0, y=0 ):
    if y==j and (i == x-1 or i == x+1 or i ==x):
        return 1-L[i][j] 
    elif x==i and (j == y-1 or j == y+1):  
        return 1-L[i][j] 
    else:
        return L[i][j] 

我认为问题在于我的setNewElement2d函数,但我似乎无法弄明白。

1 个答案:

答案 0 :(得分:0)

简短回答:您已在evolve2d()中换了i和j。

长答案:当evolve2d()创建新矩阵时,i需要位于外循环中,而不是内循环。否则,交换ij。例如,如果我将呼叫更改为

return [[ setNewElement( L, j, i, x, y ) for i in range(N) ] for j in range(N) ]

然后我得到了你的例子的正确答案。

这与二维数组的组织有关。在Python中,L[i][j]查找第i行(垂直倒数)和第j列(水平计数右)。但是您希望ix值进行比较,而x值在您的脑海中是水平的。因此,您要测试的重点是将其坐标与您认为正在测试的点进行交换。

上次编辑,我发誓:基本上,该程序完全按照预期的方式完成。但是当你看到它时,你想象yL[x][y]具有传统意义(分别是水平和垂直),当它们在数组中具有相反的含义时:x看起来向下 y行和向右 if( is_wp_error( $tmp ) ){ echo "error in downloading ".$tmp; } // Set variables for storage // fix file name for query strings preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|bmp|BMP|png|PNG)/', $thumb_url, $matches); //$matches[0]=$tmp; $file_array['name'] = basename($matches[0]); $file_array['tmp_name'] = $tmp; // If error storing temporarily, unlink if ( is_wp_error( $tmp ) ) { @unlink($file_array['tmp_name']); $file_array['tmp_name'] = ''; if ($debug) { echo 'Error storing temp file! <br />'; } } //use media_handle_sideload to upload img: $thumbid = media_handle_sideload( $file_array, $post_id, $post_title ); 列。