0

I need help setting up the write logic for the winning condition "The game end when either player's king reaches row 8, unless it is white's turn and black's king is in row 7, then black has one turn to reach row 8 to tie game."

I have tried this and it keeps giving me draws

def check_endgame():
    for i, row in enumerate(board):
        if 'WK' in row:
            if i == 7:
                return "White Wins!"
        if 'BK' in row:
            if i == 7:
                return "Black Wins!" if board[6].count('WK') == 0 else "Draw!"
    return None

I have also tried this but the game starts out with White winning.

def check_endgame():
    for i, row in enumerate(board):
        if 'WK' in row:
            if i == 7:
                if 'BK' in board[6]:
                    return "Draw!"
                else:
                    return "White Wins!"
        if 'BK' in row:
            if i == 7:
                return "Black Wins!"
    return None
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 1
    Why enumerate the rows? Just say `if 'WK' in board[7]:`. However, what you're describing is not a winning condition in any chess game I'm aware of. In your game, don't the kings start on opposite edges? If so, one of the kings STARTS in row 8. Perhaps you should describe your version of the game in more detail. – Tim Roberts Aug 13 '23 at 01:13
  • If the first function returns "Draw!", then it must be that the black king is on the eighth row and the white king is on the seventh row. – John Gordon Aug 13 '23 at 01:19
  • Basically white chess player start the game, black player follows, If white landed on row 8, black player still have one more turn to hit row 8. If black doesn't hit row 8 the following turn, white player win. If black player landed on row 8 before white player, black wins. – Kevin Mai Aug 13 '23 at 05:35
  • Do both kings start on the **same** row? That sounds more like a horse race. Or are you using a different row numbering for when talking about black? – trincot Aug 13 '23 at 12:05

1 Answers1

0

This does what you describe, although it doesn't check for white's turn vs black's turn. You haven't told us about that.

def check_endgame():
    if 'WK' in board[7]:
        if 'BK' in board[6]:
            return "Draw!"
        else:
            return "White Wins!"
    if 'BK' in board[7]:
        return "Black Wins!"
    return None
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Thanks so much for reaching out!!!! In such a quick manner, thanks so much, I will implement that into my code. Thanks! – Kevin Mai Aug 13 '23 at 05:09