In the game of Connect Four, two players take turns playing on an n x n board, which we represent as a 2d list (a list of strings). To win, a player needs to connect 4 consecutive pieces in a row or column, or diagonally (either direction is OK). The input board is guaranteed to be a square board. You will implement a function findWinner(B:List[str]) -> str that takes a board and returns the winner or - if no one has won. Specifically, the given board B will be a list of n strings, where each string has length n. There are two players R and G, so each position in the board B[i][j] is either the string R or G if it has been occupied---or . (dot) for an unoccupied spot. You are to return the string R if the R player has won; the string G if the G player has won; or the string - otherwise. Notice that the size of the board can be obtained by asking for the length of B. We guarantee that if there's a winner, that player will be the only winner. Here are a few examples (keep in mind, the board size doesn't have to be 4x4): board1 = ['RRRG', board2 = ['GRRG', '....', '.GGR', 'GRRG', 'GRGR', '.R..'] 'RGRG'] In these cases, findWinner(board1)=='-'' and findWinner(board2)=='G'. Performance Expectations: The largest input you will be tested is a 100-by-100 board, and your function must return within 2 seconds.
In the game of Connect Four, two players take turns playing on an n x n board, which we represent as a 2d list (a list of strings). To win, a player needs to connect 4 consecutive pieces in a row or column, or diagonally (either direction is OK). The input board is guaranteed to be a square board. You will implement a function findWinner(B:List[str]) -> str that takes a board and returns the winner or - if no one has won. Specifically, the given board B will be a list of n strings, where each string has length n. There are two players R and G, so each position in the board B[i][j] is either the string R or G if it has been occupied---or . (dot) for an unoccupied spot. You are to return the string R if the R player has won; the string G if the G player has won; or the string - otherwise. Notice that the size of the board can be obtained by asking for the length of B. We guarantee that if there's a winner, that player will be the only winner. Here are a few examples (keep in mind, the board size doesn't have to be 4x4):
board1 = ['RRRG', board2 = ['GRRG', '....', '.GGR', 'GRRG', 'GRGR', '.R..'] 'RGRG'] In these cases, findWinner(board1)=='-'' and findWinner(board2)=='G'.
Performance Expectations: The largest input you will be tested is a 100-by-100 board, and your function must return within 2 seconds.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images