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

时间:2017-10-29 01:34:56

标签: python game-development

我是澳大利亚留学Digitronics的学生,我不确定如何在每行之间划线。 (我可以使用列,但不能使用行)

我还发现了其他几个问题,例如 1.玩家可以欺骗倒数计时器, 通过一次又一次地输入相同的安全坐标。 如果玩家输入一个数字,游戏就会崩溃 板范围 还有一个小问题,我无法举旗 炸弹,因为这样做的功能就是这样 超出我的理解,虽然我确定 我可以在更长的时间内(即几个月)做到这一点, 代码不会允许它,我必须在此停止 你也没有轻微的并发症 进入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()

1 个答案:

答案 0 :(得分:0)

  

如何在每一行之间添加行?

目前,假设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
---------