首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Python连接四个

用Python连接四个
EN

Code Review用户
提问于 2016-02-19 18:55:47
回答 1查看 5.5K关注 0票数 4

我用Python制作了Connect 4的工作版本。我对Python非常陌生,所以它非常原始。请看一看,让我知道你的想法!

代码语言:javascript
复制
import random

board = [["." for x in range(7)] for x in range(6)]
symbol = ""
winCondition = ""
fullCols = []

def boardLogic(pick, user):
    if user == "user":
        symbol = "0"
    else:
        symbol = "X"
    if board[0][pick] != ".":
        if board[1][pick] != ".":
            if board[2][pick] != ".":
                if board[3][pick] != ".":
                    if board[4][pick] != ".":
                        if board[5][pick] != ".":
                            print("That column is full.")
                            if pick not in fullCols:
                                fullCols.append(pick)
                            fullCols.sort()
                            print fullCols
                        else:
                            board[5][pick] = symbol
                    else:
                        board[4][pick] = symbol
                else:
                    board[3][pick] = symbol
            else:
                board[2][pick] = symbol
        else:
            board[1][pick] = symbol
    else:
        board[0][pick] = symbol

def printBoard():
    for row in board:
        for val in row:
            print '{:4}'.format(val),
        print

def does_square_contain_win(i, j):
    #right_diag:  [[i,j], [i-1,j+1], [i-2,j+2], [i-3,j+3]]
    if i-3 in range(0, 6) and j+3 in range(0,5):
        if board[i][j] == "0" and board[i-1][j+1] == "0" and board[i-2][j+2] == "0" and board[i-3][j+3] == "0":
            print("******************************\nPlayer Wins\n******************************")
            quit()
    if i-3 in range(0, 6) and j+3 in range(0,5):
        if board[i][j] == "X" and board[i-1][j+1] == "X" and board[i-2][j+2] == "X" and board[i-3][j+3] == "X":
            print("******************************\nComputer Wins\n******************************")
            quit()
    #left_diag:  [[i,j], [i-1,j-1], [i-2,j-2], [i-3,j-3]]
    if i-3 in range(0, 6) and j-3 in range(0,5):
        if board[i][j] == "0" and board[i-1][j-1] == "0" and board[i-2][j-2] == "0" and board[i-3][j-3] == "0":
            print("******************************\nPlayer Wins\n******************************")
            quit()
    if i-3 in range(0, 6) and j-3 in range(0,5):
        if board[i][j] == "X" and board[i-1][j-1] == "X" and board[i-2][j-2] == "X" and board[i-3][j-3] == "X":
            print("******************************\nComputer Wins\n******************************")
            quit()
    #right: [[i,j], [i,j+1], [i,j+2], [i,j+3]]
    if j+3 in range(0, 5):
        if board[i][j] == "0" and board[i][j+1] == "0" and board[i][j+2] == "0" and board[i][j+3] == "0":
            print("******************************\nPlayer Wins\n******************************")
            quit()
    if j+3 in range(0, 5):
        if board[i][j] == "X" and board[i][j+1] == "X" and board[i][j+2] == "X" and board[i][j+3] == "X":
            print("******************************\nComputer Wins\n******************************")
            quit()
    #down:  [[i,j], [i-1,j], [i-2,j], [i-3,j]]
    if i-3 in range(0, 6):
        if board[i][j] == "0" and board[i-1][j] == "0" and board[i-2][j] == "0" and board[i-3][j] == "0":
            print("******************************\nPlayer Wins\n******************************")
            quit()
    if i-3 in range(0, 6):
        if board[i][j] == "X" and board[i-1][j] == "X" and board[i-2][j] == "X" and board[i-3][j] == "X":
            print("******************************\nComputer Wins\n******************************")
            quit()

while True:
    printBoard()

    #pick a column
    while True:
        try:
            yourPick = int(raw_input('Pick a column 0 - 6: '))
        except ValueError:
            print 'That\'s not a number!'
        else:
            if 0 <= yourPick < 6 and yourPick not in fullCols:
                break
            else:
                print 'Out of range or column full. Try again.'
    print(yourPick)
    #column logic
    boardLogic(yourPick, "user")

    #opponent randomly picks a column
    while True:
        try:
            theirPick = random.randint(0, 6)
        except ValueError:
            print 'That\'s not a number!'
        else:
            if theirPick not in fullCols:
                break
            else:
                print 'Column full!'
    print(theirPick)
    #column logic
    boardLogic(theirPick, "notUser")

    #check win conditions
    for i in range(0, 6):
        for j in range(0, 5):
            does_square_contain_win(i, j)
EN

回答 1

Code Review用户

发布于 2016-02-19 19:32:18

提高抽象

代码语言:javascript
复制
if board[0][pick] != ".":
    if board[1][pick] != ".":
        if board[2][pick] != ".":
            if board[3][pick] != ".":
                if board[4][pick] != ".":
                    if board[5][pick] != ".":
                        print("That column is full.")
                        if pick not in fullCols:
                            fullCols.append(pick)
                        fullCols.sort()
                        print fullCols
                    else:
                        board[5][pick] = symbol
                else:
                    board[4][pick] = symbol
            else:
                board[3][pick] = symbol
        else:
            board[2][pick] = symbol
    else:
        board[1][pick] = symbol
else:
    board[0][pick] = symbol

因此,如果第一个是空的,则分配第一个,如果第二个是空的,则分配第二个.

换句话说,您分配第一个空,让我写一些伪代码:

代码语言:javascript
复制
def boardLogic(pick, user):
    # ... Logic before (this should maybe be moved elsewhere)

    try:
        first_empty = first(lambda i: board[i][pick] == EMPTY, range(5+1))
        board[first_empty][pick] = symbol
    except StopIteration:
         print("That column is full.")
         # ... No empty space

哪里

代码语言:javascript
复制
EMPTY = '.'

first很容易在StackOverflow上找到

在高级语言中,您应该尝试编写类似于问题的高级描述的代码,并避免使用许多链式条件。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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