为Python中已经正常工作的程序创建一个类

时间:2016-12-06 13:05:43

标签: python

我最近开始使用Python并且得到了一个赋值,我们应该在没有类的情况下使用现有代码并使用类和self.example变量重写它。

我决定使用在线发现的以下代码:

 1. """A command line version of Minesweeper""" import random import re
import time from string import ascii_lowercase


def setupgrid(gridsize, start, numberofmines):
    emptygrid = [['0' for i in range(gridsize)] for i in range(gridsize)]

    mines = getmines(emptygrid, start, numberofmines)

    for i, j in mines:
        emptygrid[i][j] = 'X'

    grid = getnumbers(emptygrid)

    return (grid, mines)


def showgrid(grid):
    gridsize = len(grid)

    horizontal = '   ' + (4 * gridsize * '-') + '-'

    # Print top column letters
    toplabel = '     '

    for i in ascii_lowercase[:gridsize]:
        toplabel = toplabel + i + '   '

    print(toplabel + '\n' + horizontal)

    # Print left row numbers
    for idx, i in enumerate(grid):
        row = '{0:2} |'.format(idx + 1)

        for j in i:
            row = row + ' ' + j + ' |'

        print(row + '\n' + horizontal)

    print('')


def getrandomcell(grid):
    gridsize = len(grid)

    a = random.randint(0, gridsize - 1)
    b = random.randint(0, gridsize - 1)

    return (a, b)


def getneighbors(grid, rowno, colno):
    gridsize = len(grid)
    neighbors = []

    for i in range(-1, 2):
        for j in range(-1, 2):
            if i == 0 and j == 0:
                continue
            elif -1 < (rowno + i) < gridsize and -1 < (colno + j) < gridsize:
                neighbors.append((rowno + i, colno + j))

    return neighbors


def getmines(grid, start, numberofmines):
    mines = []
    neighbors = getneighbors(grid, *start)

    for i in range(numberofmines):
        cell = getrandomcell(grid)
        while cell == start or cell in mines or cell in neighbors:
            cell = getrandomcell(grid)
        mines.append(cell)

    return mines


def getnumbers(grid):
    for rowno, row in enumerate(grid):
        for colno, cell in enumerate(row):
            if cell != 'X':
                # Gets the values of the neighbors
                values = [grid[r][c] for r, c in getneighbors(grid,
                                                              rowno, colno)]

                # Counts how many are mines
                grid[rowno][colno] = str(values.count('X'))

    return grid


def showcells(grid, currgrid, rowno, colno):
    # Exit function if the cell was already shown
    if currgrid[rowno][colno] != ' ':
        return

    # Show current cell
    currgrid[rowno][colno] = grid[rowno][colno]

    # Get the neighbors if the cell is empty
    if grid[rowno][colno] == '0':
        for r, c in getneighbors(grid, rowno, colno):
            # Repeat function for each neighbor that doesn't have a flag
            if currgrid[r][c] != 'F':
                showcells(grid, currgrid, r, c)


def playagain():
    choice = input('Play again? (y/n): ')

    return choice.lower() == 'y'


def parseinput(inputstring, gridsize, helpmessage):
    cell = ()
    flag = False
    message = "Invalid cell. " + helpmessage

    pattern = r'([a-{}])([0-9]+)(f?)'.format(ascii_lowercase[gridsize - 1])
    validinput = re.match(pattern, inputstring)

    if inputstring == 'help':
        message = helpmessage

    elif validinput:
        rowno = int(validinput.group(2)) - 1
        colno = ascii_lowercase.index(validinput.group(1))
        flag = bool(validinput.group(3))

        if -1 < rowno < gridsize:
            cell = (rowno, colno)
            message = ''

    return {'cell': cell, 'flag': flag, 'message': message}


def playgame():
    gridsize = 9
    numberofmines = 10

    currgrid = [[' ' for i in range(gridsize)] for i in range(gridsize)]

    grid = []
    flags = []
    starttime = 0

    helpmessage = ("Type the column followed by the row (eg. a5). "
                   "To put or remove a flag, add 'f' to the cell (eg. a5f).")

    showgrid(currgrid)
    print(helpmessage + " Type 'help' to show this message again.\n")

    while True:
        minesleft = numberofmines - len(flags)
        prompt = input('Enter the cell ({} mines left): '.format(minesleft))
        result = parseinput(prompt, gridsize, helpmessage + '\n')

        message = result['message']
        cell = result['cell']

        if cell:
            print('\n\n')
            rowno, colno = cell
            currcell = currgrid[rowno][colno]
            flag = result['flag']

            if not grid:
                grid, mines = setupgrid(gridsize, cell, numberofmines)
            if not starttime:
                starttime = time.time()

            if flag:
                # Add a flag if the cell is empty
                if currcell == ' ':
                    currgrid[rowno][colno] = 'F'
                    flags.append(cell)
                # Remove the flag if there is one
                elif currcell == 'F':
                    currgrid[rowno][colno] = ' '
                    flags.remove(cell)
                else:
                    message = 'Cannot put a flag there'

            # If there is a flag there, show a message
            elif cell in flags:
                message = 'There is a flag there'

            elif grid[rowno][colno] == 'X':
                print('Game Over\n')
                showgrid(grid)
                if playagain():
                    playgame()
                return

            elif currcell == ' ':
                showcells(grid, currgrid, rowno, colno)

            else:
                message = "That cell is already shown"

            if set(flags) == set(mines):
                minutes, seconds = divmod(int(time.time() - starttime), 60)
                print(
                    'You Win. '
                    'It took you {} minutes and {} seconds.\n'.format(minutes,
                                                                      seconds))
                showgrid(grid)
                if playagain():
                    playgame()
                return

        showgrid(currgrid)
        print(message)

playgame()

这段代码就像它应该的那样工作。这就是我设法改变它(我很可能不理解一些基本部分,这就是为什么它不起作用)。

 1. import random import re import time from string import
ascii_lowercase

class Minesweeper:


    def __init__(self, numberofbombs, gridsize):
        self.numberofbombs = numberofbombs
        self.gridsize = gridsize

    def setupgrid(self, start):
        emptygrid = [['0' for i in range(self.gridsize)] for i in range(self.gridsize)]

        mines = Minesweeper.getmines(self, emptygrid, start)

        for i, j in mines:
            emptygrid[i][j] = 'X'

        grid = Minesweeper.getnumbers(emptygrid)

        return (grid, mines)


    def showgrid(self, grid):
        self.gridsize = len(grid)

        horizontal = '   ' + (4 * self.gridsize * '-') + '-'

        # Skriver ut bokstäverna för kolumnerna
        toplabel = '     '

        for i in ascii_lowercase[:self.gridsize]:
            toplabel = toplabel + i + '   '

        print(toplabel + '\n' + horizontal)

        # Skriver ut radnummer
        for idx, i in enumerate(grid):
            row = '{0:2} |'.format(idx + 1)

            for j in i:
                row = row + ' ' + j + ' |'

            print(row + '\n' + horizontal)

        print('')


    def getrandomcell(self, grid):
        self.gridsize = len(grid)

        a = random.randint(0, self.gridsize - 1)
        b = random.randint(0, self.gridsize - 1)

        return (a, b)


    def getneighbors(self, grid, rowno, colno):
        self.gridsize = len(grid)
        neighbors = []

        for i in range(-1, 2):
            for j in range(-1, 2):
                if i == 0 and j == 0:
                    continue
                elif -1 < (rowno + i) < self.gridsize and -1 < (colno + j) < self.gridsize:
                    neighbors.append((rowno + i, colno + j))

        return neighbors


    def getmines(grid, start, self):
        mines = []
        neighbors = Minesweeper.getneighbors(grid, *start)

        for i in range(self.numberofmines):
            cell = Minesweeper.getrandomcell(grid)
            while cell == start or cell in mines or cell in neighbors:
                cell = Minesweeper.getrandomcell(grid)
            mines.append(cell)

        return mines


    def getnumbers(grid):
        for rowno, row in enumerate(grid):
            for colno, cell in enumerate(row):
                if cell != 'X':
                    # Hämtar värdet av närliggande bomber
                    values = [grid[r][c] for r, c in Minesweeper.getneighbors(grid,
                                                                  rowno, colno)]

                    # Räknar hur många som är minor
                    grid[rowno][colno] = str(values.count('X'))

        return grid


    def showcells(grid, currgrid, rowno, colno):
        # Exit function if the cell was already shown
        if currgrid[rowno][colno] != ' ':
            return

        # Show current cell
        currgrid[rowno][colno] = grid[rowno][colno]

        # Get the neighbors if the cell is empty
        if grid[rowno][colno] == '0':
            for r, c in Minesweeper.getneighbors(grid, rowno, colno):
                # Repeat function for each neighbor that doesn't have a flag
                if currgrid[r][c] != 'F':
                    Minesweeper.showcells(grid, currgrid, r, c)


    def playagain():
        choice = input("Vill du testa igen?? (y/n): ")

        return choice.lower() == 'y'


    def parseinput(self, inputstring, helpmessage):
        cell = ()
        flag = False
        message = "Denna ruta existerar inte. " + helpmessage

        pattern = r'([a-{}])([0-9]+)(f?)'.format(ascii_lowercase[self.gridsize - 1])
        validinput = re.match(pattern, inputstring)

        if inputstring == 'hjälp':
            message = helpmessage

        elif validinput:
            rowno = int(validinput.group(2)) - 1
            colno = ascii_lowercase.index(validinput.group(1))
            flag = bool(validinput.group(3))

            if -1 < rowno < self.gridsize:
                cell = (rowno, colno)
                message = ''

        return {'cell': cell, 'flag': flag, 'message': message}


def playgame():
    gridsize = int(input("Hur står vill du att spelplanen ska vara? Skriv t.ex 9 för 9x9: "))
    numberofmines = int(input("Hur många bomber vill du ha på spelplanen?: "))
    information = Minesweeper(gridsize, numberofmines)

    currgrid = [[' ' for i in range(gridsize)] for i in range(gridsize)]

    grid = []
    flags = []
    starttime = 0

    helpmessage = ("Skriv kolumnen förljd av raden (t.ex a5). "
                       "För att placera eller ta bort en flagga, lägg till 'f' (t.ex a5f).")

    information.showgrid(currgrid)
    print(helpmessage + " Skriv 'hjälp' för att visa detta  meddelande igen\n")

    while True:
        minesleft = numberofmines - len(flags)
        prompt = input('Välj ruta ({} mines left): '.format(minesleft))
        result = information.parseinput(prompt, helpmessage + '\n')

        message = result['message']
        cell = result['cell']


        if cell:
            print('\n\n')
            rowno, colno = cell
            currcell = currgrid[rowno][colno]
            flag = result['flag']

            if not grid:
                grid, mines = information.setupgrid(cell)
            if not starttime:
                starttime = time.time()

            # Kontrolerar placeringen av flaggor
            if flag:
                # Placerar en flagga om platsen är tom
                if currcell == ' ':
                    currgrid[rowno][colno] = 'F'
                    flags.append(cell)
                # Tar bort flaggan om det redan finns en där
                elif currcell == 'F':
                    currgrid[rowno][colno] = ' '
                    flags.remove(cell)
                else:
                    message = 'Du kan inte placera en flagga här'

            # Skapar en utskrift om det redan är en flagga där
            elif cell in flags:
                message = 'Det är en flagga här'

            elif grid[rowno][colno] == 'X':
                print('Du förlorade\n')
                information.showgrid(grid)
                if information.playagain():
                    information.playgame()
                return

            elif currcell == ' ':
                information.showcells(grid, currgrid, rowno, colno)

            else:
                message = "Denna ruta är redan synlig"

            if set(flags) == set(mines):
                minutes, seconds = divmod(int(time.time() - starttime), 60)
                print(
                    'Du vann! '                        'Det tog dig {} minuter och {} sekunder.\n'.format(minutes,
                                                                      seconds))
                information.showgrid(grid)
                if information.playagain():
                    information.playgame()
                return

        information.showgrid(currgrid)
        print(message)

playgame()

基本上我所做的就是创建班级&#34;扫雷&#34;并制作&#34; numberofbombs&#34;和&#34; gridsize&#34;进入自我。参数。 起初我遇到的问题是每个&#34;例子(网格等)&#34;没看到他们现在在班里怎么样。在我将它们更改为&#34; Minesweeper.example(grid等)&#34;代码现在开始,因为它应该。

我得到的错误如下:

  1. Traceback(最近一次调用最后一次):文件 &#34; C:\用户\马库斯\应用程序数据\本地\程序\的Python \ Python35-32 \ minesweeper2.py&#34 ;, 第227行,在     playgame()文件&#34; C:\ Users \ Marcus \ AppData \ Local \ Programs \ Python \ Python35-32 \ minesweeper2.py&#34;, 第180行,在游戏中     grid,mines = information.setupgrid(cell)File&#34; C:\ Users \ Marcus \ AppData \ Local \ Programs \ Python \ Python35-32 \ minesweeper2.py&#34;, 第17行,在setupgrid中     mines = Minesweeper.getmines(self,emptygrid,start)文件&#34; C:\ Users \ Marcus \ AppData \ Local \ Programs \ Python \ Python35-32 \ minesweeper2.py&#34;, 第77行,在getmines     neighbors = Minesweeper.getneighbors(grid,* start)TypeError:getneighbors()需要4个位置参数但是10个被赋予
  2. 每当我放置&#34; Minesweeper。&#34;进入&#34; getneighbors(网格,*开始)&#34;它增加了6个位置参数,我似乎无法绕过它。

    有谁知道如何解决这个问题,或者我是否从一开始就采取了错误的方式,以及为了修复代码需要做些什么? 期待大家的帮助!

1 个答案:

答案 0 :(得分:0)

你给它一个参数加上你解压缩大小为9的列表,总共10个

def f(*args):
    print('received the following arguments:')
    for arg in args:
        print(arg)

foo = [1, 2, 5, 8]
f('sauce', *foo) # 5 arguments

输出:

received the following arguments:
sauce
1
2
5
8