3

For a self-assigned project, I decided to try and create every possible game of tic-tac-toe. To store and represent each of these games, I decided to use a matrix with 9 columns and 362880 rows. Each row is one game, where the odd columns are "X's" moves and the even columns are "O's" moves.

(1,2,3,4,5,6,7,NULL,NULL) represents a game where X wins.

enter image description here

This is why I want to generate every nine digit number that does not contain duplicate integers, as a duplicate integer would mean that a player tried to mark a position that is already occupied.

Below is the beginnings of one possible method

#create matrix that can contain all possible arrangements of moves on a tic-tac-toe board
tictactoematrix <- matrix(ncol = 9, nrow = 362880)

j = 1
k = 1

#create list of possible moves
move <- list(1,2,3,4,5,6,7,8,9)

#populate every row with numbers 1-9
for(i in 1:362880){
  tictactoematrix[i,1] <- move[[1]]
  move[1] <- NULL
  tictactoematrix[i,2] <- move[[1]]
  move[1] <- NULL
  tictactoematrix[i,3] <- move[[1]]
  move[1] <- NULL
  tictactoematrix[i,4] <- move[[1]]
  move[1] <- NULL
  tictactoematrix[i,5] <- move[[1]]
  move[1] <- NULL
  tictactoematrix[i,6] <- move[[1]]
  move[1] <- NULL
  tictactoematrix[i,7] <- move[[1]]
  move[1] <- NULL
  tictactoematrix[i,8] <- move[[1]]
  move[1] <- NULL
  tictactoematrix[i,9] <- move[[1]]
  move[1] <- NULL

  move <- list(1,2,3,4,5,6,7,8,9)
}

The output:

enter image description here

Now obviously the problem with is that every row is identical, while I want them to each be unique. And what I can't for the life of me figure out is how to rearrange every number in the

move <- list(1,2,3,4,5,6,7,8,9)

into every possible combination.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
user2533660
  • 151
  • 5
  • 12
  • 4
    You want to generate all permutations. This [SO answer](http://stackoverflow.com/questions/11095992/generating-all-distinct-permutations-of-a-list-in-r) will do. – aichao Aug 02 '16 at 19:06
  • 1
    A tree seems like a more natural structure, considering that games can end before the final move. Also, you might as well collapse all the rotational and flip symmetry: let the first move be "corner", "side" or "middle" and define other moves relative to that. – Frank Aug 02 '16 at 19:07
  • 1
    A good data structure would be a recursive tree that has some children and a value (matrix in your case). The branches would be the allowed moves from that position, or just all of the possible moves, which you then clean. – FisherDisinformation Aug 02 '16 at 19:14

2 Answers2

1

If you're willing to use another package, you can do this directly via:

library(combinat)

temp <- permn(c(1,2,3,4,5,6,7,8,9))
fullTable <- do.call("rbind", temp)
Frank
  • 66,179
  • 8
  • 96
  • 180
user1357015
  • 11,168
  • 22
  • 66
  • 111
  • 1
    They don't need the 0 since there are nine cells, I guess. – Frank Aug 02 '16 at 19:13
  • 1
    Yes, it should be `permn(seq_len(9))`. As is, it is generating all permutations for `10` elements and therefore you have `10!` rows and not the requested `9!` rows. I know the OP has already accepted your answer, but please fix. – aichao Aug 02 '16 at 19:16
  • 1
    This worked, but I did end up removing the "0." I tried to remove it from your answer myself, but the edit is too small for me to be allowed to make. – user2533660 Aug 02 '16 at 19:29
1

If you are only looking for the table:

library(permute)
all_games <- allPerms(1:9, how(maxperm=1e10))
Pierre L
  • 28,203
  • 6
  • 47
  • 69