简体   繁体   English

Python Connect 4游戏

[英]Python Connect 4 Game

COLUMNS = 5
ROWS = 4

def empty_board():
    board_rows =["|   \t\t  |"] * ROWS
    board_columns = [" ___ "] * COLUMNS
    for i in range (ROWS):
        print(board_rows[i])
    print("+", end= " " )   
    for i in range(COLUMNS):
        print(board_columns[i], end="")
        print 
    print("+")
    for i in range(COLUMNS):
        column_number = i+1
        print("   " + str(column_number), end = " ")

def column_choice():
    player = 1
    if player % 2 != 0 :
        column = input("Player 1, please choose a column to make your next move ")
    else:
        column = input("Player 2, please choose a column to make your next move ")
    player += 1
    return column 


def new_board(column):
    moves = 0
    column = int(column) - 1
    if moves % 2 == 0:
        key = "X"
    else:
        key = "O"
    board_rows = ["|   \t\t  |"] * ROWS
    board_columns = [" ___ "] * COLUMNS
    for i in range (ROWS):
        print(board_rows[i])
        board_rows.pop(column)
        board_rows = board_rows.insert(column, key)
        print(board_rows)
    print("+", end= " " )   
    for i in range(COLUMNS):
        print(board_columns[i], end="")
    print("+")
    for i in range(COLUMNS):
        column_number = i+1
        print("   " + str(column_number), end = " ")    

if __name__ == "__main__":
    print()
    print("Welcome to Connect 4")
    print()
    new_board(column_choice())

I have to create a connect 4 board with the given column and row dimensions (for now). 我必须创建具有给定的列和行尺寸的connect 4板(目前)。 I currently have the board created, but I can't quite figure out how to get the X or O to go in the correct spot. 我目前已经创建了木板,但我还不太清楚如何使XO进入正确的位置。 For instance, right now when I run the program, the X will go in the whole column. 例如,现在当我运行程序时, X将出现在整个列中。 If you could offer any help I would appreciate it! 如果您能提供任何帮助,我将不胜感激!

board_rows =["|   \t\t  |"] * ROWS
board_columns = [" ___ "] * COLUMNS

points each entry/item in the list to the same memory address, so changing one column changes them all since they each point to the same place. 将列表中的每个条目/项目都指向相同的内存地址,因此更改一列会更改它们,因为它们都指向同一位置。

for row in board_rows:
    print id(row)

Use list comprehension or a for loop instead to get different items (memory location) in the list. 请使用列表理解或for循环来获取列表中的其他项(内存位置)。

board_rows =["|   \t\t  |" for row in ROWS]
for row in board_rows:
    print id(row)

Honestly, that is a very complicated way to represent a textual board. 老实说,这是表示文本面板的非常复杂的方式。 I would recommend, if possible, using an object-oriented approach, such as making the board into a list of lists, where every entry in the list is an list of strings that represents a row, with a __repr__ method to help visualize it more easily. 如果可能的话,我建议使用面向对象的方法,例如将开发板制作为列表列表,其中列表中的每个条目都是代表一行的字符串列表,并使用__repr__方法来帮助使其可视化容易。 Here is an example I wrote. 这是我写的一个例子。

class Board(object):

def __init__(self, rows, columns):
    self.rows = rows
    self.columns = columns
    self.board = [['_' for i in range(self.columns)] for i in range(self.rows)]
def __repr__(self):
    board = ''
    for row in self.board:
        board += ''.join(row) + '\n'
    return board

       f = Board(5, 5)
=> None
   f
=> _____
   _____
   _____
   _____
   _____

This way, a blank board is represented as a dual list-comprehension, where your rows parameter is the vertical length, and your columns parameter is the horizontal length. 这样,空白板就表示为双重列表理解,其中,行参数是垂直长度,列参数是水平长度。

With this code, try writing a method to add a piece onto the board, by finding the lowest point in a column that isn't the opponents piece. 使用此代码,尝试编写一种方法,通过找到不是对手棋子的最低点,在棋盘上添加棋子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM