首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python中的TicTacToe游戏

Python中的TicTacToe游戏
EN

Code Review用户
提问于 2016-02-07 19:52:30
回答 1查看 109关注 0票数 2

我只想问一问,是否有办法使函数play_Game()is_move_valid()变得更加高效。例如,使用某种类型的循环,我一直无法弄清楚如何并希望得到任何帮助,以使这些函数更短。

代码语言:javascript
复制
#TicTacToe
import sys
rows=[[0,1,2],[3,4,5],[6,7,8]]
columns =[[0,3,6],[1,4,7],[2,5,8]]
diagonals=[[0,4,8],[2,4,6]]
count=0

def display_Grid():
    print("\n")
    print(grid[0][0], "|" , grid[0][1], "|" , grid[0][2])
    print("---------")
    print(grid[1][0], "|", grid[1][1], "|", grid[1][2])
    print("---------")
    print(grid[2][0], "|", grid[2][1], "|", grid[2][2])

def check_turn():
    if count%2 == 0:
        player_1()
    else:
        player_2()

def game_over(shape):
    try:
        sys.exit()

    except SystemExit:
        print(shape," won")
        win = True
        quit()
def player_1():
    p_number = ("Player one")
    p_shape = p1
    play_Game(p_number, p_shape)

def player_2():
    p_number = ("Player two")
    p_shape = p2
    play_Game(p_number, p_shape)

def checkDraw():
    try:
        if count==10:
            sys.exit()

    except SystemExit:
        print("Draw")
        quit()

def check_win(shape):
    directions=[rows,columns,diagonals]
    for i in directions:
        for j in i:
            status = map(lambda val: True if grid[int(val/3)][val%3] == shape else False, j)
            if all(status):
                return True

    return False


def play_Game(p_number, p_shape):
    global count
    checkDraw()
    print(p_number, ",enter where you would like to move on the grid: ")
    move = int(input(""))
    if move in (1, 2, 0):
        valid = is_move_valid(move)
        if valid:
            count+=1
            grid[0][move] = p_shape
            display_Grid()
        else:
            check_turn()
    elif move in (3, 4, 5):
        valid = is_move_valid(move)
        if valid:
            count+=1
            move2 = move-3
            grid[1][move2] = p_shape
            display_Grid()
        else:
            check_turn()
    elif move in (6, 7, 8):
        valid = is_move_valid(move)
        if valid:
            count+=1
            move2 = move-6
            grid[2][move2] = p_shape
            display_Grid()
        else:
            check_turn()

    else:
        print("Enter one of the numbers visible on the grid: ")

    if check_win(p_shape):
        game_over(p_shape)
    else:
        None
    if count%2 == 0:
        player_1()
    else:
        player_2()

def is_move_valid(move):
    print(move)
    if move in(0, 1, 2):
        if grid[0][move] in ("X", "O"):
            print("This place has already been picked...")
            return False
        else:
            None
    elif move in(3, 4, 5):
        move2 = move-3
        if grid[1][move2] in ("X", "O"):
            print("This place has already been picked...")
            return False
        else:
              None
    elif move in(6, 7, 8):
        move2 = move-6
        if grid[2][move2] in ("X", "O"):
            print("This place has already been picked...")
            return False
        else:
              None

    return True

win = False    
grid = [[0,1,2],[3,4,5],[6,7,8]]
p_number = (" ")
display_Grid()
choose = input("Player one, would you like to be 'X' or 'O'?: ")
while win != True:
    try:
         if choose == "X":
             p2 = "O"
             p1 = choose
             player_1()
         elif choose == "O":
             p1 = "O"
             p2 = "X"
             player_1()
         else:
             print("Enter x or o: ")
             choose = input("Player 1, would you like to be 'X' or 'O'?: ")
    except ValueError:
        print("Please enter X OR O")
EN

回答 1

Code Review用户

发布于 2016-02-07 21:11:33

划分逻辑与用户交互

为什么要用一个函数来决定移动到屏幕上的print是否有效?如果你想改变这条信息呢?您会在逻辑函数中查看以更改用户界面消息吗?

我喜欢尽可能多地保持我的函数的“纯”,这样它们只会进行计算并返回结果,并在其上构建一个薄的交互皮肤。

长话短说,is_move_valid不应该打印任何东西。

Simplification

就简化而言,您只需将板压平,功能就变成:

代码语言:javascript
复制
def is_move_valid(move):
    return flatten(board)[move] not in ("X", "O")
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/119214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档