![]() Results$`One Group of Three Consecutive Os` = 1) Results$`At Least One Group of Three Consecutive Xs` = 1)) For example, what’s the probability of X winning on one of the diagonals? This lets us compare the simulation to some analytic solutions. O_loses <- sapply(1:sims, function(s) sum(o_row_win, o_col_win, o_diag1_win, o_diag2_win) < 1) O_diag2_win <- sapply(1:sims, function(s) sum(tictactoe] = "O" & tictactoe] = "O" & tictactoe] = "O")) O_diag1_win <- sapply(1:sims, function(s) sum(tictactoe] = "O" & tictactoe] = "O" & tictactoe] = "O")) Now that we have each of the eight different ways that there could be three consecutive Xs, we only need to check whether at least one of these occurred (recall from the discussion above that there could be two, though no more than two, given the restriction of the number of pieces). The problem does not specify that the pieces should be placed in a particular order, nor that the game should be stopped once a single player wins, so I’m considering these cases as valid. Notice how in this case (it’s just an example), the Xs player won in two ways. In the second case, there are consecutive Xs in the antidiagonal: In the first case, the Xs are aligned along what’s known in linear algebra as the main diagonal: ![]() X_diag2_win <- sum(tictactoe = "X" & tictactoe = "X" & tictactoe = "X") X_diag1_win <- sum(tictactoe = "X" & tictactoe = "X" & tictactoe = "X") We can use sum to add up the TRUE values of that logical vector to get the number of times that there were three consecutive Xs in each row (of course, since there are only 5 total Xs, it’s impossible for there to be more than one row with three consecutive Xs, but we’ll return to that later). This is done over the three possible rows of the matrix, which returns another logical vector of length three that is representing each of the rows, where TRUE means there were three Xs in a row for that row. When the sum of that vector (values of TRUE are equal to one) is three, then it means one of the rows contains three Xs in a row. Essentially we’re checking to see whether each row is exactly equal to a vector c("X", "X", "X")), or three Xs in a row by checking if the sum of the logical vector produced by testing some random row of Xs and Os.įor example, if the row generated is X-X-O, then x = c("X", "X", "X") for that particular row will be TRUE, TRUE, FALSE. The function that is being applied is a dummy function. In this case, 1 means that the function will be applied over the rows of the matrix of interest, tictactoe. Starting from the middle of this next line, apply applies a function over the rows or columns of a matrix. Tictactoe <- matrix(sample(pieces, 9), nrow = 3, ncol = 3) I started with function in R to model the game.įirst, we create a vector of each of our pieces. You can see a few different scenarios at the end of this post.Īnyways, I wanted to share the way I approached this. There are actually quite a few situations in this perverse version of tic-tac-toe where you’ll have both Xs and Os have three pieces in a row. This might seem counterintuitive given that there are more X pieces than O pieces, but the key is that we are searching for the probability that there will be at least one occurrence of three Xs in a row AND no occurrences of three Os. The answer, interestingly, is around 49%. ![]() If he randomly places all nine pieces in the nine slots on the tic-tac-toe board (with one piece in each slot), what’s the probability that X wins? That is, what’s the probability that there will be at least one occurrence of three Xs in a row at the same time there are no occurrences of three Os in a row? When I took my two-year-old with me, he wasn’t particularly interested in the game itself, but rather in the placement of the pieces. One of the games is a tic-tac-toe board, which comes with nine pieces that you and your opponent can place: five Xs and four Os. Here’s the full puzzle:Ī local cafe has board games on a shelf, designed to keep kids (and some adults) entertained while they wait on their food. This weekend’s Riddler Express asks solvers to model a peculiar game of tic-tac-toe where all nine pieces (five Xs and four Os) are placed into the nine slots on the board randomly.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |