MagicforestA 2022-06-08 12:35 采纳率: 100%
浏览 53
已结题

井字棋代码中遇到的问题

这是一个tic-tac-toe(井字棋,大叉或圆圈连成一直线的算赢)的代码,我的问题在enter_move函数的第5行,make_list_of_free_fields
函数的第6行


from random import randrange


def display_board(board):
    print("+-------" * 3,"+",sep="")
    for row in range(3):
        print("|       " * 3,"|",sep="")

        for col in range(3):
            print("|   " + str(board[row][col]) + "   ", end="")

        print("|")
        print("|       " * 3,"|",sep="")
        print("+-------" * 3,"+",sep="")

def enter_move(board):
    ok = False# fake assumption - we need it to enter the loop
    while not ok:
        move = input("Enter your move: ")
        ok = len(move) == 1 and move >= '1' and move <= '9'
"""
这里为什么不用integer来约定条件而要用str,譬如move<=9和>=1,我尝试了一下改成      "ok=move<=9 and move>=1"  , 发现问题出在对角线,当对角线是同一个"X"或“O”,系统识别不出已经赢了这盘棋,但我不清楚为什么改成integer后会引起这个状况,请解答
"""
        if not ok:
            print("Bad move - repeat your input!")
            continue
        move = int(move) - 1     # cell's number from 0 to 8
        row = move // 3     # cell's row
        col = move % 3        # cell's column
        sign = board[row][col]    # check the selected square
        ok = sign not in ['O','X'] 
        if not ok:    # it's occupied - to the input again
            print("Field already occupied - repeat your input!")
            continue
  board[row][col] = 'O'# set '0' at the selected square

def make_list_of_free_fields(board):
    free = []    # the list is empty initially
    for row in range(3): # iterate through rows
        for col in range(3): # iterate through columns
            if board[row][col] not in ['O','X']: # is the cell free?
                free.append((row,col)) # yes, it is - append new tuple to the list ,这里可以把元组(row,col)改成board[row][col]吗
    return free


def victory_for(board,sgn):
    if sgn == "X":    # are we looking for X?
        who = 'me'    # yes - it's computer's side
    elif sgn == "O": # ... or for O?
        who = 'you'    # yes - it's our side
    else:
        who = None    # we should not fall here!
    cross1 = cross2 = True  # for diagonals
    for rc in range(3):
        if board[rc][0] == sgn and board[rc][1] == sgn and board[rc][2] == sgn:    # check row rc
            return who
        if board[0][rc] == sgn and board[1][rc] == sgn and board[2][rc] == sgn: # check column rc
            return who
        if board[rc][rc] != sgn: # check 1st diagonal
            cross1 = False
        if board[2 - rc][2 - rc] != sgn: # check 2nd diagonal
            cross2 = False
    if cross1 or cross2:
        return who
    return None


def draw_move(board):
    free = make_list_of_free_fields(board) # make a list of free fields
    cnt = len(free)
    if cnt > 0:    # if the list is not empty, choose a place for 'X' and set it
        this = randrange(cnt)
        row, col = free[this]
        board[row][col] = 'X'



board = [ [3 * j + i + 1 for i in range(3)] for j in range(3) ] # make an empty board

board[1][1] = 'X' # set first 'X' in the middle
free = make_list_of_free_fields(board)
human_turn = True # which turn is it now?

while len(free):
        display_board(board)
        if human_turn:
                enter_move(board)
                victor = victory_for(board,'O')
        else:    
                draw_move(board)
                victor = victory_for(board,'X')
        if victor != None:   
                break

        human_turn = not human_turn
        free = make_list_of_free_fields(board)

display_board(board)

if victor == 'you':
    print("You won!")
elif victor == 'me':
    print("I won")
else:
    print("Tie!")

还有就是里面那么多def函数,为什么有的没return一个值,像第2,5个函数就没有return一个值,应该要加一个return吗

  • 写回答

4条回答 默认 最新

  • 关注

    有的直接输出一些值了没必要返回,不用加return
    在enter_move函数的第5行
    move = input("Enter your move: ")默认输入的是字符串类型的,你想用integer来约定条件需要先把move转成int类型才有意义

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 6月16日
  • 已采纳回答 6月8日
  • 修改了问题 6月8日
  • 修改了问题 6月8日
  • 展开全部

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。