I have the following code, for making an AI in tic tac toe (or knots and crosses).
Pre: I've written a program to make every possible configuration to a text file. I've loaded the text file and loaded into lm as a list.
I found out that using eval is not very safe to use. So I thought, no problem, let's make a multidimensional array! It should start out as a nine by two list. Name the first column after the moves and if it doesn't fit, make the array larger by adding columns.
So here's the thing... python doesn't do multidimensional arrays or lists... It only does do lists in lists...
So I thinking of making a list like this:
That's fine by me, one reason is that the lists of moves1 and moves9 are 3 strings long, moves5 on the other hand is way larger, anyway. How do I access those lists easily like I did with the eval function?
1. How do I itterate through a 'lists in list'-list?
2. How do I index a list in a list? So I need in move 4, the second listed string, how do I acces that?
Pre: I've written a program to make every possible configuration to a text file. I've loaded the text file and loaded into lm as a list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Set the value of "o" equal from 0 to 4. for o in range ( 0 , 5 ): # Set the value of "x" equal from 1 to 5. for x in range ( 1 , 6 ): # For each board configuration in the list lm. for board in lm: # If the number of o's and x's found in the board configuration is equal to the value o and x. if board.count( "o" ) = = o and board.count( "x" ) = = x: # Set i equal to the amount of moves made: the value of x plus the value of o. i = x + o # Replace the empty spaces with the number of moves left. board = board.replace( " " , str ( 9 - i)) # Insert the board configuration to the move list #i. eval ( "moves" + str (i)).insert( 0 , board) |
So here's the thing... python doesn't do multidimensional arrays or lists... It only does do lists in lists...
So I thinking of making a list like this:
1 |
moves = [[moves1],[moves2],[moves3],[moves4],[moves5],[moves6],[moves7],[moves8],[moves9]] |
1. How do I itterate through a 'lists in list'-list?
2. How do I index a list in a list? So I need in move 4, the second listed string, how do I acces that?