制作简单的列表网格

时间:2017-11-21 20:07:36

标签: python list

大家好,我需要帮助做游戏的游戏网格男人不要生气。 我应该创建一个格式n*n this is how it look for n=9网格的函数 而且我需要为每个奇数执行此操作,武器的宽度为3,长度为n。它应该用列表列表完成,因为那时我必须制作一个图来绕过这个网格,所以我需要坐标。 我通过循环

制作了这个游戏网格
def gensachovnicu(n):
    matica='*'' '
    matica1='X '
    matica2='D '
    for j in range(n):
        for i in range(n):
            if i<((n-3)//2) and j<((n-3)//2):
                print('  ',end='')
            elif i<((n-3)//2) and j>(n-((n-3)//2)-1):
                print('  ',end='')
            elif i>(n-((n-3)//2)-1) and j<((n-3)//2):
                print('  ',end='')
            elif i>(n-((n-3)//2)-1) and j>(n-((n-3)//2)-1):
                print('  ',end='')
            elif i==(n//2) and j>0 and j<(n-1) and j!=(n//2):
                print(matica2,end='')
            elif i==(n//2) and j==(n//2):
                print(matica1,end='')
            elif j==(n//2) and i>0 and i<(n-1):
                print(matica2,end="")
            else:
                print(matica,end='')
        print()

但它太长了我无法处理他。

2 个答案:

答案 0 :(得分:0)

责任分工的例子:

# get a DF with a rownumber
lst=['QQ', '1', '2', '3', 'ZZ', 'b', 'QQ', '4', '5', '6', 'ZZ', 'a', 'QQ', '9', '8', '23']
df=sc.parallelize(lst).zipWithIndex()\
  .map(lambda (x,i): Row(**{'col': x, 'rownum': i})).toDF()

# hack to count cumulative occurrences of QQ
winspec=Window.partitionBy().orderBy('rownum')
df=df.withColumn('QQ_indicator', f.expr("case when col='QQ' then 1 else 0 end"))
df=df.withColumn('QQ_indicator_cum', f.sum('QQ_indicator').over(winspec))

# ditto for ZZ
df=df.withColumn('ZZ_indicator', f.expr("case when col='ZZ' then 1 else 0 end"))
df=df.withColumn('ZZ_indicator_cum', f.sum('ZZ_indicator').over(winspec))

df.filter("QQ_indicator_cum=ZZ_indicator_cum+1 and not(col='QQ')")\
  .groupby('QQ_indicator_cum')\
  .agg(f.collect_list('col').alias('result'))\
  .select('result')\
  .show(3)

输出:

from random import randint

def dice():
    return randint(1,6)

# true for all fields (including border ones and player) 
def IsField(n, x,y): 
    off = (n-3)//2 # offset to the sides of 3 spaces playing ground

    if (x >= off and x < n-off):
       return True
    if (y >= off and y < n-off):
        return True

    return False


# true only for border 
def IsBorder(n,x,y): 
    off = (n-3)//2
    mid = (n-1)//2 # the middle where player moves

    if (y >= n) or (x >=n) or (y<0) or (x<0):
        return False
    if (y == 0 or y == n-1) and x in [mid-1, mid, mid+1]:
        return True
    if (x == 0 or x == n-1) and y in [mid-1, mid, mid+1]:
        return True
    if (y > 0 or y < n-1) and x == mid:
        return False
    if (x > 0 or x < n-1) and y == mid:
        return False
    if ((x == off or x == off+2) and not x == mid):
       return True
    if ((y == off or y == off+2) and not y == mid):
        return True

    return False


# what to mark field with?
def GetMarker(n,x,y):
    if IsBorder(n,x,y):
        return '*' 
    else:
        return ' '


# repair wrong inputs to odd and minimal 9
def MakeOddMin9(n):
    if n % 2==0:
        n += 1 # make odd
    n = max(9,n) # min 9
    return n


# construct the board, player pos still missing
def MakeBoard(n):
    n = MakeOddMin9(n)
    base = [ [GetMarker(n,x,y) for x in range(n)] for y in range(n)]
    mid = (n-1)//2
    base[0][mid] = 'a' # add player      
    return (base,(mid,0))


# print the boards fields
def PrintBoard(board):
    print("")
    size = len (board) 
    for y in range(size):
        row = ""
        for x in range(size):
            row += board[y][x]

        print (row) 
    print ("")

def Move(b,x,y,nx,ny):
    b[y][x]='*'
    b[ny][nx]='a'


# moves player along 1 tile  
def MoveOne(board,player):
    actX = player[0]
    actY = player[1]
    mid = (len(board)-1)//2
    # right
    if IsBorder(len(board), actX+1, actY) and actY < mid:
        Move(board,actX,actY,actX+1,actY)
        player = (actX+1,actY)
    # down
    elif IsBorder(len(board), actX, actY+1) and actX > mid:
        Move(board,actX,actY,actX,actY+1)
        player = (actX,actY+1)
    #left
    elif IsBorder(len(board), actX-1, actY) and actY > mid:
        Move(board,actX,actY,actX-1,actY)
        player = (actX-1,actY)
    #up
    elif IsBorder(len(board), actX, actY-1) and actX < mid:
        Move(board,actX,actY,actX,actY-1)
        player = (actX,actY-1)

    return (board,player)


b, player = MakeBoard(19)
mid = (len(b)-1)//2

PrintBoard(b)
inputKey= ''
while(inputKey == ''):
    moves = dice()
    b[mid][mid] = str(moves)
    for n in range(0,moves):
        b,player = MoveOne(b,player)

    PrintBoard(b)        
    inputKey = input("\n Hit enter. Enter anything to quit.").lower()
    if (inputKey != ''):
        break

答案 1 :(得分:0)

import os
import time
import random
doska = []

n=int(input('Zadaj rozmer hracieho pola:'))
def create_sachovnica(n):
    global doska
    doska = [[' ' for i in range(n)] for j in range(n)]
    npol = n // 2
    for i in range(n):
        doska[npol - 1][i] ='*'
        doska[npol + 1][i] ='*'
        doska[i][npol - 1] ='*'
        doska[i][npol + 1] ='*'
    doska[npol][0] ='*'
    doska[npol][n - 1] ='*'                                                 
    doska[0][npol] ='*'
    doska[n - 1][npol] ='*'
    for i in range(1, npol):
        doska[npol][i] ='D'
        doska[npol][n - i - 1] ='D'
        doska[i][npol] ='D'
        doska[n - i - 1][npol] ='D'
    doska[npol][npol] ='X'


def print_sachovnica(n):
    for n in doska:                                                               
        print(' '.join(n))
    print()

def gensachovnicu(n):
    create_sachovnica(n)                                
    print_sachovnica(n)



def nextpos(doska, x, y):
size = len(doska)
if x >= size // 2:
    if y < size // 2 and doska[x - 1][y] == '*':
        return x - 1, y
else:
    if y <= size // 2 and doska[x][y + 1] == '*':
        return x, y + 1
    elif y <= size // 2 and doska[x-1][y] == '*':
        return x-1,y
if y >= size // 2:
    if x <= size // 2 and doska[x + 1][y] == '*':               
        return x + 1, y
    elif x < size // 2 and doska[x][y+1] == '*':
        return x,y+1
    elif x > size // 2 and doska[x][y-1] == '*':
        return x,y-1
    elif x > size // 2 and doska[x+1][y] == '*':
        return x+1,y
else:
    if x > size // 2 and doska[x][y - 1] == '*':
        return x, y - 1
    elif x > size // 2 and doska[x][y - 1] == '*':
        return x, y - 1
return x, y

def nextpos2(doska, s, r):
velkost = len(doska)
if s >= velkost // 2:
    if r < velkost // 2 and doska[s - 1][r] == '*':
        return s - 1, r
else:
    if r <= velkost // 2 and doska[s][r + 1] == '*':
        return s, r + 1
    elif r <= velkost // 2 and doska[s-1][r] == '*':
        return s-1,r
if r >= velkost // 2:
    if s <= velkost // 2 and doska[s + 1][r] == '*':  
        return s + 1, r
    elif s < velkost // 2 and doska[s][r+1] == '*':
        return s,r+1
    elif s > velkost // 2 and doska[s][r-1] == '*':
        return s,r-1
    elif s > velkost // 2 and doska[s+1][r] == '*':
        return s+1,r
else:
    if s > velkost // 2 and doska[s][r - 1] == '*':
        return s, r - 1
    elif s > velkost // 2 and doska[s][r - 1] == '*':
        return s, r - 1
return s, r



def pohyb(n, x, y, s ,r):
while True:
    create_sachovnica(n)
    doska[x][y] = 'A'
    doska[s][r] = 'B'
    hod=random.randint(1,6)
    hod1=random.randint(1,6)
    if hod ==1 :
        x, y = nextpos(doska, x, y)
    elif hod==2:
        for i in range(hod):
            x, y = nextpos(doska, x, y)
    elif hod ==3:
        for i in range(hod):
            x, y = nextpos(doska, x, y)               
    elif hod==4:
        for i in range(hod):
            x, y = nextpos(doska, x, y)
    elif hod == 5:
        for i in range(hod):
            x, y = nextpos(doska, x, y)
    elif hod ==6:
        rozhodnutie1 = str(input('Pohyb(P)/Nova figurka(N)'))
        if rozhodnutie1 == 'P':
            for i in range(hod):
                x, y = nextpos(doska, x, y)
        else:
            doska[n-1][((n - 3) // 2)] = 'A'
    if hod1==1:
        s, r = nextpos2(doska, s, r)
    elif hod1==2:
        for i in range(hod1):
            s, r = nextpos2(doska, s, r)
    elif hod1==3:
        for i in range(hod1):
            s, r = nextpos2(doska, s, r)
    elif hod1==4:
        for i in range(hod1):
            s, r = nextpos2(doska, s, r)
    elif hod1==5:
        for i in range(hod1):
            s, r = nextpos2(doska, s, r)
    elif hod1==6:
        rozhodnutie=str(input('Pohyb(P)/Nova figurka(N)'))
        if rozhodnutie=='P':
            for i in range(hod1):
                s, r = nextpos2(doska, s, r)
        else:
            doska[0][((n-3)//2)+2] = 'B'
    print_sachovnica(n)
    print(hod)
    print(hod1)
    time.sleep(1)

gensachovnicu(n)
pohyb(n,(n-1),((n-3)//2),0,((n-3)//2)+2)
print(figurky(n))