python - Printing a two dimensional list -


To represent the four connecting console game I am trying to print a two-dimensional list. Should look like:

  1 2 3 4 5 6 7. . . . . . . . . . . . . . . . . . . . . . . R. . . . . Y r . . RR Y Y   

It is far from me, and I can not seem to work for it, I am saying "index error", "Trouble index out of range". Keep in mind, in the module Connect_for, BOARD_ROWS is equal to 6 and BOARD_COLUMNS is 7. None is red, 'R', and Yellow is 'Y' In addition, the game_state is a named object object with two fields, which is the first time Is the game board (list of strings).

  def print_board (game_state) - & gt; None: For printing in "range" (connect_four.BOARD_COLUMNS): "Print of the game board given game game" for one (connect_four.BOARD_ROWS) in "" "" print ("1 2 3 4 5 6 7") range. : If the game_state [b] [a] == connect_four.NONE: print ('.', End = '') elif game_state [b] [a] == connect_four.RED: print ('r', end = '' ) Elif code_state [b] [a] == connect_four.YELLOW: print ('y', end = '') Other: print ('\ n', end = '')   

Code for Namedtuple

  def _new_game_board () - & gt; [[ARR]]: "" "A new game board is created. Initially, the size of a game board is BOARD_COLUMNS x BOARD_ROWS and does not have any value" "board = [] call to limit (BOARD_COLUMNS Board: [[]]: Board [-1] .Appendix (none) Returns Board ConnectFireGameState = Collections: 'ConnectFireGameState', ['Board', 'Turn' ]) def new_game_state () - & gt; ConnectFourGameState: "" "A ConnectorGamstate is a new one Game shows in which no tricks have been done yet. "" "ConnectFire Game Stats (board = _new_game_boot =) =,) =    

After adding the rest of the code, I'm a bit confused in my way of representing rows and columns, so I will write code (if I was having problems like you); It helps:

  def _new_game_board () -> [[Str]]: "" "A new game board creates a game board initially The car is BOARD_COLUMNS X BOARD_ROWS and contains only the wire with no value. "" "[[None] * to return to BOARD_COLUMNS_RANGE (BOARD_ROWS)] ConnectFourGameState = namedTuple ('ConnectFourGameState', ['Board', ' 'Turn']) def new_game_state () - & gt; ConnectFourGameState: "" "A ConnectorGamestrate represents a brand new game in which no tricks have been made."  
    def print_board (game_state): "Game" prints the game game to the current game state "" ("1 2 3 4 5 6 7") Range (row in BOARD_ROWS): if game_state.board [row] [col ] == connect_four.NONE: '.' Print (, end = '') elif in range (BOARD_COLUMNS) game_state.board [row] [col] == connect_four.RED: print ('r', end = '') elif game_state.board [row] [col] == connect_four.YELLOW: print ('Y', end = '') print ()    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -