algorithm to check a connect four field

前端 未结 3 1234
礼貌的吻别
礼貌的吻别 2020-11-30 00:43

I\'m wondering what\'s the best way to check for a winner on a connect four field.

I\'m interested in what you guys think and whether there is some \"well-known\" al

3条回答
  •  天命终不由人
    2020-11-30 01:28

    This is related to this question: How to find the winner of a tic-tac-toe game of any size?

    The twist is the 7x6 board with 4 in a row winning rather than a NxN board with N in a row winning. But it is trivial to adapt the solution to NxN tic tac toe to connect 4.

    EDIT: Actually, it's not quite trivial to adapt the other solution to this one. But you can get there with a little bit of extra work.

    Store a count for each player for every row, column, diagonal and anti-diagonal that could ever have 4 pieces in a row. When that count hits 4 or more for either player, check to see if that row/column/diagonal/anti-diagonal has the four pieces in a row. If it does, that player wins!

提交回复
热议问题