Display a Connect Four game
Python 2, 91 88 bytes
w,h,s=input()
i,x=0,['']*w
for c in s:x[c]+='XO'[i%2];i+=1
for c in x:print(c+'.'*h)[:h]
Since transposed is fine, this prints the board sitting on its right hand side.
repl.it
Vim, 34 bytes
@wO-<Esc><C-V>(d@hpqq<C-O>+D@"G+bs<C-R>=!@.<CR><Esc>@qq@q
Input is width in the "w
register, height in the "h
register, and the 1-indexed moves in the buffer, one per line. Output is a transposed grid with -
as blank, 1
as player 1, and 0
as player 2 (with a bunch of newlines at the end).
To clarify, if you want Height 6, Width 7: type qh6qqw7q
before starting.
@wO-<Esc><C-V>(d@hp
: Use the height and width in the registers to make a transposed grid of-
s. Note that(
is run from the last line of the grid, one above the moves. This sets up the jump list.<C-O>+D@"G
: The<C-O>
uses the jump list to move back to before the lastG
, always to the line above the next number.@"G
moves to the line number indicated by the deleted text.+
not only moves the cursor, but it's the failure point; when it runs on the last line, the macro dies.+b
: This maneuver goes to the first-
in the line, even if it's the first character. (f-
would go to the second character if the line was still all-
s.)s<C-R>=!@.
: Replaces a-
with the last insert@.
, but NOTted. First time through, the previous insert of-
will NOT to1
because that's the way Vim is. After that, it will alternate between0
and1
.
Example
Start with the moves in the buffer:
4
4
5
5
6
7
3
Make sure you're on line 1 with gg
, clear "q
with qqq
, set up the registers for width 7, height 6 with qw7qqh6q
. Then run the solution above and get the following output (with newlines at the end):
------
------
1-----
10----
10----
1-----
0-----