简体   繁体   中英

matching chips in Connect Four Printing the match function (python)

I have a Connect Four program which runs appropriately but i would like to print my match_in_direction() method onto the screen...My code is as follows

class ConnectFour(object):

This initializes the board:

    def __init__(self):
        self.board = [[None for i in range(7)] for j in range(8)]

This gets a position of each play spot:

    def get_position(self, row, column):
        assert row >= 0 and row < 6 and column >= 0 and column < 7
        return self.board[row][column]

This is supposed to check if the chips played are matching:

    def match_in_direction(self, row, column, step_row, step_col):

    assert row >= 0 and row < 6 and column >= 0 and column < 7
    assert step_row != 0 or step_col != 0 # (0,0) gives an infinite loop

    match = 1

    while True:
        nrow = row + step_row
        ncolumn = column + step_col
        if nrow >=0 and nrow <6 and ncolumn >=0 and ncolumn <7:
            if self.board[row][column] == self.board[nrow][ncolumn]:
                match == match+1
                row = nrow
                column = ncolumn
            else:
                return match
        else:
            return match
    print match

This will be a play based on user input

    def play_turn(self, player, column):
    """ Updates the board so that player plays in the given column.

    player: either 1 or 2
    column: an integer between 0 and 6
    """
    assert player == 1 or player == 2
    assert column >= 0 and column < 7

    for row in xrange(6):
        if self.board[row][column] == None:
            self.board[row][column] = player
            return

Prints the Board:

    def print_board(self):
        print "-" * 29
        print "| 0 | 1 | 2 | 3 | 4 | 5 | 6 |"
        print "-" * 29
        for row in range(5,-1,-1):
            s = "|"
            for col in range(7):
                p = self.get_position(row, col)
                if p == None:
                    s += "   |"
                elif p == 1:
                    s += " x |"
                elif p == 2:
                    s += " o |"
                else:
                    # This is impossible if the code is correct, should never occur.
                    s += " ! |"
            print s
        print "-" * 29

And my usage:

b = ConnectFour()

b.play_turn(1, 3)

b.play_turn(1, 3)

b.play_turn(1, 4)

b.match_in_direction(0,3,0,2)

b.print_board()

My current Output gives me the positions just fine...However it doesn't print the match_in_direction(0,3,0,2) which should be 2 because that is how many chips are matching....Any Help will be greatly appreciated.

从match_in_direction快速浏览一下,看起来你match == match+1而不是match = match + 1 (或“更好”但match += 1

add this to your test code:

f = b.match_in_direction(0,3,0,2) 
print(m)

you don't need "print match" in the match_in_direction function because of the return statements, and print board doesn't utilize (for lack of a better word) the match in direction function so you have to print it out separately.

The formatting of your match_in_direction function is a little confusing, but I think it is because of the typo here:

if self.board[row][column] == self.board[nrow][ncolumn]:
    match == match+1

It should be

match += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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