【Python小游戏】五子棋

第一次用Python写一个游戏什么的。。。感觉还是挺高级的一门语言

不过还没有怎么熟悉语言的情况下代码风格好丑的说……里面的else我都是用的if判断的……

函数名的话应该会很好理解吧……

最开始的话check函数写的非常的愚蠢。。后来发现只需要从当前点往两边拓展就可以啦Orz

代码贴上。。。下一步应该是开始图形界面了QWQ

#encoding:utf-8
map=[['' for i in range (20)]for j in range (20)]
def print_map():
    for i in range (20):
        for j in range (20):
            print map[i][j],
        print ''
def make_map():
    
    for i in range (1,20):
        map[0][i]=''
        map[i][0]=''
        map[19][i]=''
        map[i][19]=''
    map[0][0]=''
    map[0][19]=''
    map[19][0]=''
    map[19][19]=''
    i=j=0
def check_illegal(x,y):
    if(x>20):
        return 0
    if(x<0):
        return 0
    if(y>20):
        return 0
    if(y<0):
        return 0
    if(map[x][y]==''):
        return 0
    if(map[x][y]==''):
        return 0
    return 1
def put_chess(x,y,color):#0代表白色,1代表黑色
    if(color==0):
        map[x][y]=''
    if(color==1):
        map[x][y]=''
def check_win_row(x,y,color):
    cnt=1
    tempy=y+1
    chess=''
    if(color==0):
        chess=''
    while(map[x][tempy]==chess):
        cnt=cnt+1
        tempy=tempy+1
    tempy=y-1
    while(map[x][tempy]==chess):
        cnt=cnt+1
        tempy=tempy-1
    if(cnt>=5):
        return 1
    return 0
def check_win_line(x,y,color):
    cnt=1
    tempx=x+1
    chess=''
    if(color==0):
        chess=''
    while(map[tempx][y]==chess):
        cnt=cnt+1
        tempx=tempx+1
    tempx=x-1
    while(map[tempx][y]==chess):
        cnt=cnt+1
        tempx=tempx-1
    if(cnt>=5):
        return 1
    return 0
def check_win_left_diagonal(x,y,color):
    cnt=1
    tempx=x+1
    tempy=y+1
    chess=''
    if(color==0):
        chess=''
    while(map[tempx][tempy]==chess):
        cnt=cnt+1
        tempx=tempx+1
        tempy=tempy+1
    tempx=x-1
    tempy=y-1
    while(map[tempx][tempy]==chess):
        cnt=cnt+1
        tempx=tempx-1
        tempy=tempy-1
    if(cnt>=5):
        return 1
    return 0
def check_win_right_diagonal(x,y,color):
    cnt=1
    tempx=x+1
    tempy=y-1
    chess=''
    if(color==0):
        chess=''
    while(map[tempx][tempy]==chess):
        cnt=cnt+1
        tempx=tempx+1
        tempy=tempy-1
    tempx=x-1
    tempy=y+1
    while(map[tempx][tempy]==chess):
        cnt=cnt+1
        tempx=tempx-1
        tempy=tempy+1
    if(cnt>=5):
        return 1
    return 0
def check_win(x,y,color):
    if(check_win_row(x,y,color)==1):
        return 1
    if(check_win_line(x,y,color)==1):
        return 1
    if(check_win_left_diagonal(x,y,color)==1):
        return 1
    if(check_win_right_diagonal(x,y,color)==1):
        return 1
    return 0
make_map()
print_map()
f=0
while(1):
    f=1-f
    if(f==0):
        print 'White turn.Choose a place to set your chess:'
    if(f==1):
        print 'Black turn.Choose a place to set your chess:'
    print 'Position'
    x=int(raw_input('X :'))
    y=int(raw_input('Y :'))
    print check_illegal(x,y)
    while(check_illegal(x,y)==0):
        print 'Illegal.Press again'
        x=int(raw_input('X :'))
        y=int(raw_input('Y :'))
    put_chess(x,y,f)
    print_map()
    if(check_win(x,y,f)):
        if(f==0):
            print 'White Win!'
        if(f==1):
            print 'Black Win!'
        break

 

posted @ 2016-12-20 14:46  BeyondW  阅读(777)  评论(0编辑  收藏  举报