如何在 Minewsweeper 中的每一行之间添加行,Python

How to add lines between each row in Minewsweeper, Python

我是一名在澳大利亚学习数字电子学的学生,我不确定如何在每行之间插入线条。 (我可以用列来做,但不能用行来做)

我还发现了其他几个问题,例如 1.玩家可以作弊倒计时, 通过一次又一次地输入相同的安全坐标。 2.如果玩家输入数字以外的数字,游戏会崩溃 董事会范围 3.还有一个小问题,我不能标记任何 炸弹,因为这样做所需的功能是方式 超出我的理解,虽然我很确定 我可以在更多时间(即几个月)内完成, 代码不允许,我必须到此为止 4. 还有一些你不知道的轻微并发症 在 Windows 参加扫雷游戏,然后 事实上,你在第一回合就死了。这是 常规游戏不可能做到这一点。

提前致谢

有什么方法可以添加标记功能,或者在丢失后显示面板的方法吗?

# Matthew
# Simple Minesweeper - but not so simple anymore
# Version 3.0
# 20/9/17 - 25/10/17

from random import randint      # so there is a random mine placement
import time     # so I can delay the game
import sys      # so I can quit the game at any time`enter code here`

# Starts the loop for the game
play = True
while play == True:

    ##############################
    #### Functions Start Here ####
    ##############################

    # opens the board for use
    board = [] 

    # opens the mines list, currently empty so it can be altered later. 
    mines = []

    # determines the rows on the board
    board_row = 0

    # determines the adjacent mines
    adj = 0

    # sets a variable to be used later in the check_ans() function
    wrong = 0

    #determines the amount of rows on the board
    while True:
        board_row = int(input("For a square board, how many rows and columns? (5 min, 10 max):"))
        if board_row > 10:
            print("That is too high. Please enter another!")
        elif board_row < 5:
            print("That is too small. Please enter another!")
        else:
            break

    # adds mines for a larger board
    if board_row >= 8:
        for i in range(15):
            mines.append([randint(0,board_row-1), randint(0, board_row-1)])

    # adds smaller mines for a smaller board
    elif board_row >= 5:
        for i in range(10):
            mines.append([randint(0,board_row-1), randint(0, board_row-1)])

    # creates rows
    for i in range(board_row):
        board.append(["x"] * board_row)

    # creates the rows
    def draw_board(board):
        for i in board:
            print("|".join(i))

    # check the answers
    def check_ans():
        if row >= board_row or col >= board_row:
            print("That number is too high. The order goes 0 to ", board_row)
            wrong = 1
        else:
            wrong = 0      

    # defines the adjacent mines, by checking each of the surrounding squares, one
    # by one
    def adj_mines(r, c, adj):
        adj = 0
        if [r+1, c] in mines:
            adj += 1
        if [r+1, c+1] in mines:
            adj += 1
        if [r+1, c-1] in mines:
            adj += 1
        if [r, c+1] in mines:
            adj += 1
        if [r, c-1] in mines:
            adj += 1
        if [r-1, c+1] in mines:
            adj += 1
        if [r-1, c] in mines:
            adj += 1
        if [r-1, c-1] in mines:
            adj += 1
        return adj

    def Intro_to_game():
        print('Hello. This is a game of Minesweeper - with a twist!')
        print('You cannot flag any bombs, because I can\'t figure out how to do that...')
        print('On each turn, select the row or column that you wish to ')
        print('disarm, and this program will do it. To let you know how')
        print('many tiles you have left to disarm, there will be a ')
        print('countdown before each turn. Enjoy, and good luck!')

    # defines number of moves required to beat the game, as
    # there is no flagging function. 
    moves = (((board_row) * (board_row) - int(len(mines))))


    ##################################
    #### Main Program Starts Here ####
    ##################################

    draw_board(board)

    # This uses a function to determine how many cells are left to
    # clear the board, as there is no way to flag. This makes the
    # game significantly harder, as you have to keep track in your
    # head of where the bombs are. 
    Intro_to_game()
    while True:
        print('===================================')
        print("Cells to clear: " + str(moves))

        # This part enters in the rows and columns. However, although
        # the lists typically start at 0, the program has to subtract
        # one from the different imputs to put them in the right place

        row = (int(input("Row: ")) - 1)

        # This part doesn't allow the player to enter
        # a number that is higher than the board range
        while row >= board_row + 1:
            print('That is not in the board range')
            row = (int(input("Row: ")) -1) 

        col = (int(input("Col: ")) - 1)

        # This part doesn't allow the player to enter
        # a number that is higher than the board range

        while col >= board_row + 1:
            print('That is not in the board range')
            col = (int(input("Col: ")) -1)              

        # checks to see if there is a bomb in the called field, if not,
        # then it repeats. If there is a bomb, it shows, "Sorry, but you
        # have blown up." Then it asks if they player would like to play
        # again. 
        check_ans()

        if wrong != 1:
            if [row, col] in mines:
                break

            else:
                board[row][col] = str(adj_mines(row,col,0))
                moves = moves - 1
        draw_board(board)
        if moves == 0:
            print("You have won!")
            time.sleep(2)
            sys.exit

    print("Sorry, but you have blown up :(")

    # draws the board again each time. 
    draw_board(board)

    # Although unconventional, this little bit processes the
    # request to play again. If yes, it breaks the loop, and
    # goes back to the start. If no, sys.exit quits the game

    print("Would you like to play again? (Yes or No)(Needs Capitals)")
    play_again = input()
    if play_again == 'Yes':
        continue
    if play_again == 'Y':
        continue
    if play_again == 'yes':
        continue
    if play_again == 'y':
        continue
    else:
        sys.exit()

How to add lines between each row?

目前,假设 board_row5,那么您的表示是

2|x|x|x|x
x|x|x|x|x
x|x|x|x|x
x|x|x|x|x
x|x|x|x|x

您可以添加以下行来添加水平行。

print("|".join(i))
print("-" * (board_row*2-1)) # add this line

董事会代表变成

x|x|x|x|x
---------
x|x|x|x|x
---------
x|x|x|x|x
---------
x|x|x|x|x
---------
x|x|x|x|x
---------