# Shift Tac Toe - Code Golf Challenge

## Shift Tac Toe

Shift Tac Toe is a game that combines Tic Tac Toe and Connect 4 together. In this game, you start with a 3 by 3 board, and each row is connected to a slider that you can move left and right. At the start, the sliders all start to the very right(this means that you can't move the slider to the right on the first turn). Each slider can hold a total of 5 pieces. Each turn, the player can drop an O or a X in one of the 3 columns of the Tic Tac Toe grid depending on which turn it is, or the player can move one of the sliders one spot to the left or to the right. All pieces fall to the bottom most space that is unoccupied. The pieces can also fall from one slider to another outside the 3 by 3 grid. If a piece is outside the 3 by 3 grid and doesn't fall into the bottom slider, then the piece is taken out. If it does reach the bottom slider, it will stay in play. A notable example of this is shown in the following grid:

     --- --- --- --- ---
|   |   |   |   - O -
--- --- --- --- --- ---
-   |   |   |   |   -
--- --- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- ---
In the grid above, the dashes(-) indicate the part of the sliders that are outside of the 3 by 3 grid and the vertical bars(|) indicate the 3 by 3 grid.
As you can see, this is the starting board except that the middle slider is one spot over to the left, and that there is an O at the very top right.
What happens in this scenario? There is nothing immediately underneath it, so does it go out of play?
No. This is because it still falls into the bottom slider, which means that it is still in play.

The final grid is this:
--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- --- ---
-   |   |   |   |   -
--- --- --- --- --- ---
|   |   |   |   - O -
--- --- --- --- ---


Pieces can also stack outside of the 3 by 3 grid. Players will alternate between O and X, with the O player going first.

## Example game:

Start with 3 by 3 grid with sliders all the way to the right:

--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- ---

The O player places an O in the middle column of the 3 by 3 grid and it falls to the bottom:

--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- ---
|   | O |   |   -   -
--- --- --- --- ---

The X player then places an X in the middle column:

--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- ---
|   | X |   |   -   -
--- --- --- --- ---
|   | O |   |   -   -
--- --- --- --- ---

The O player then pushes the middle row slider one space to the left.
Notice that after the slider moves, there is nothing under the X anymore, so it falls down.
Also note that the slider has moved one space to the right as indicated below:

--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- --- ---
-   |   |   |   |   -
--- --- --- --- --- ---
| X | O |   |   -   -
--- --- --- --- ---

The X player places a X in the rightmost column:

--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- --- ---
-   |   |   |   |   -
--- --- --- --- --- ---
| X | O | X |   -   -
--- --- --- --- ---

The O player then moves the bottom slider one spot to the left.
Notice that all the pieces shift one place to the left, and the leftmost X is now out of the playing field:

--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- --- ---
-   |   |   |   |   -
--- --- --- --- ---
- X | O | X |   |   -
--- --- --- --- ---

The X player places a X in the leftmost column:

--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- --- ---
-   | X |   |   |   -
--- --- --- --- ---
- X | O | X |   |   -
--- --- --- --- ---

The O player places an O in the leftmost column:

--- --- --- --- ---
| O |   |   |   -   -
--- --- --- --- --- ---
-   | X |   |   |   -
--- --- --- --- ---
- X | O | X |   |   -
--- --- --- --- ---

The X player shifts the top slider one place to the left. Notice that the O falls one place down because there is nothing beneath it:

--- --- --- --- ---
-   |   |   |   |   -
--- --- --- --- ---
- O | X |   |   |   -
--- --- --- --- ---
- X | O | X |   |   -
--- --- --- --- ---

The O player is not very good at this game, so he shifts the middle slider one place to the right.
This shifts all the pieces in the middle row one place to the right:

--- --- --- --- ---
-   |   |   |   |   -
--- --- --- --- --- ---
| O | X |   |   -   -
--- --- --- --- --- ---
- X | O | X |   |   -
--- --- --- --- ---

The X player wins the game by placing a X in the middle column:

--- --- --- --- ---
-   |   | X |   |   -
--- --- --- --- --- ---
| O | X |   |   -   -
--- --- --- --- --- ---
- X | O | X |   |   -
--- --- --- --- ---


Your job is to take in a string or array of any length that only consists of 9 unique characters(you choose the characters). Three of the characters will choose which column you place the X or O(depending on whose turn it is), three of them will choose which slider to move right, and the last three will choose which slider to move left. You can assume that the input only has these 9 characters. The output should be a 3 by 3 matrix or some kind of list/string that clearly shows the final position of the grid upon following the instructions of the input. You can assume that all inputs are valid. Each character takes up a turn. Also, if any move results in a winning move(forms 3 in a row in the 3 by 3 grid like regular Tic-Tac-Toe), then ignore the rest of the input. Note that the pieces that form the winning 3 in a row all have to be in the 3 by 3 grid. The two example grids below are NOT winning positions:

Grid #1:
--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- ---
|   |   |   |   -   -
--- --- --- --- ---
|   |   | O | O - O -
--- --- --- --- ---
This is not a winning move because two of the O's are outside the playing field, despite the fact that it forms a 3 in a row.
Using the character assignment stated below, this grid pattern can be achieved with 99372467643.

Grid #2:
--- --- --- --- ---
|   |   |   |   - O -
--- --- --- --- ---
|   |   |   | O - X -
--- --- --- --- ---
|   |   | O | X - X -
--- --- --- --- ---
This is not a winning position because two of the O's are outside the playing field.
Using the character assignment below, this grid pattern can be achieved with 939318836537734654


In the examples below, 1, 2, and 3 mean drop in the leftmost, middle, and rightmost column respectively. 4, 5, and 6 mean to move the top, middle, and bottom slider to the right respectively, and 7, 8, and 9 mean to move the top, middle, and bottom slider to the left respectively.

## Examples

Input will be in the form of a string
Output will be a list of lists, with each sub-list representing a row(I'm Python programmer so this list format might not be compatible with all languages).
The first, second, and third sub-list correspond to the top, middle, and bottom row of the 3 by 3 grid respectively.
The output will have 'O' for the O pieces, 'X' for the X pieces, and an empty string for empty spaces.

Input: 123332
Output:
[['','','O'],
['','X','X'],
['O','X','O']]

Input: 33387741347
Output:
[['','',''],
['','','O'],
['X','O','X']]

Input: 2283911752
Output:
[['','X',''],
['O','X',''],
['O','X','']]

Input: 228374739
Output:
[['','',''],
['','',''],
['X','X','X']]

Input: 8873334917349
Output:
[['','',''],
['','','O'],
['X','X','O']]

Input: 799333466
Output:
[['','',''],
['','',''],
['','','']]

Input: 99372467643
Output:
[['','',''],
['','',''],
['','','O']]

Input: 939318836537734654
Output:
[['','',''],
['','',''],
['','','O']]



This is , so shortest code wins!

• This looks like a nice first challenge! How flexible is the output format? May we just return a 3x3 matrix in any convenient format? (I would recommend to allow that, as the main point of the challenge is obviously to simulate the game.) Aug 1, 2020 at 21:41
• @Arnauld I was hoping that the code would actually print out the 3 by 3 grid like shown in the examples but I could implement your suggestion if you think that's the best thing to do. Aug 1, 2020 at 21:45
• Let's see what others have to say about that, but we usually discourage cumbersome I/O formats unless that's the very point of the challenge, or any peripheral task that may turn the challenge into a chameleon. Aug 1, 2020 at 21:53
• It's not a good idea to mix a challenge that's going to require some processing to get the correct answer along with ascii-art output. Usually a challenge is just one or the other, Aug 1, 2020 at 22:16
• The problem you have now is that it's quite a complex challenge just to calculate the final grid and you've added error handling into the mix. Usually challenges assume only valid input unless the challenge itself is just to verify if the input is or isn't valid. Aug 2, 2020 at 12:12

# MATLAB, 727 Bytes

Try it online

b=nan(3,7);m=num2str(input(''));a=@(B)any(all(B)|all(B,2)'|all(diag(B))|all(diag(flip(B))));A=@(x,y)x(:,3:5)==y;n=@(x)isnan(x);
o=[1;1;1];
for i=1:numel(m)
t=1+mod(i,2);M=str2num(m(i));
switch M
case{1,2,3}
c=2+M;b(sum(n(b(:,c))),c)=t;case{4,5,6}
r=M-3;o(r)=o(r)+1;b(r,2:7)=b(r,1:6);b(r,1)=nan;case{7,8,9}
r=M-6;o(r)=o(r)-1;b(r,1:6)=b(r,2:7);b(r,7)=nan;end
r=o(3);
if r==1
b(:,[1,2])=nan;
elseif r==0
b(:,[1,7])=nan;
else
b(:,[6,7])=nan;
end
for j=1:7
d=n(b(:,j));e=sum(d);b(:,j)=[nan(e,1); b(~d,j)];
end
if r==1
b(:,[1,2])=nan;
elseif r==0
b(:,[1,7])=nan;
else
b(:,[6,7])=nan;
end
B=A(b,1);if a(B)
break
end
B=A(b,2);if a(B)
break
end
end
b(b==1)=79;b(b==2)=88;b(n(b))=0;disp(char(b(:,3:5)));


Explanation:

This challenge is a bit of a beast. First the user runs the program and inputs the moveset as either a single large number or a character array. For large numbers, due to numerical precision, a character array is required. I define a few often used functions that take up a lot of space so that they can be called with one character rather than a whole lot. Then I loop through each character in the input and use a switch statement to determine the move. 1-3 places the piece at the lowest available spot in the right column. 4-6 shift the row left and 7-9 shift it right. After, all invalid positions (outside the sliders) are removed automatically, as nothing could possibly stay there. Then, all columns are shifted so that things are settled as far down as they can go after 'falling'. Invalid positions are removed again in case a token fell onto another but was still outside a slider. The last thing to do is check the victory condition and break if it is found.

Overall, I had to chip my way out of a few sandtraps but I golfed as best I could. I'll take a triple bogey if no one else is fighting me for it.