资源简介

没有用到GUI控件;python3.6版本;实现了五子棋双人对战,后面会尝试加入电脑对战的;希望你们可以喜欢

资源截图

代码片段和文件信息

#!/usr/env/bin python
#-*- coding:utf-8 -*-

#author:涓涓清泉
#date:2018.4.11.20:34
#description: 最简单的双人对战五子棋游戏


#产生棋盘
row = 10
clo = 10

#显示棋盘
def displaymap(board):
    #显示棋盘
    for i in range(row+1):
        print(“          “board[i])

#初始化
def initial():
    board = [[“*“ for i in range(row+1)] for j in range(clo+1)]
    for i in range(row+1):
        board[i][0]=“%s“%(i);
    for j in range(clo+1):
        board[0][j] =“%s“%(j);
    displaymap(board)
    print(“——————————————————————————————Let‘s begin!——————————————————————————————“)
    return board


#是否连成五子
def isFive(boardplayer):
    #连成五子的情况有:横五,竖五,斜五
    count = 1#记录连续子数

    # 横五
    for i in range(row+1):
        if i != 0 :
            for j in range(clo+1):
                if j!=0:
                    if j + 4<=clo:
                        if board[i][j]==player:
                            count = 1
                            temp = j+1
                            while(temp!=j+5):
                                if board[i][temp]==player:
                                    count += 1
                                    if count == 5:
                                        return True
                                temp += 1
                    if count == 5:
                        return True

    # 竖五
    for i in range(row + 1):
        if i != 0:
            for j in range(clo + 1):
                if j != 0:
                    if i + 4 <= row:
                        if board[i][j] == player:
                            count = 1
                            temp = i + 1
                            while (temp != i + 5):
                                if board[temp][j] == player:
                                    count += 1
                                    if count >= 5:
                                        return True
                                temp += 1
                    if count >= 5:
                        return True

    # 斜上五:
    for i in range(row + 1):
        if i != 0:
            for j in range(clo + 1):
                if j != 0:
                    if i - 4 >= 1 and j + 4 <= clo:
                        if board[i][j] == player:
                            count = 1
                            temp1 = i - 1
                            temp2 = j + 1
                            while (temp1 != i - 5 or temp2 != j + 5):
                                if board[temp1][temp2] == player:
                                    count += 1
                                    if count >= 5:
                                        return True
                                temp1 -= 1
                                temp2 += 1
                    if count >= 5:
                        return True

    #  斜下五:
    for i in range(row + 1):
        if i != 0:
            for j in range(clo + 1):
                if j != 0:
                    if i + 4 <= row a

评论

共有 条评论