2017-10-29 40 views
-1

我是澳大利亞學習Digitronics的學生,我不確定如何獲取每行之間的行。 (我可以用列而不是行來做)如何在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() 

回答

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 
--------- 
相關問題