Connect 4 PvP

In-game screen.

Program listing on 48K Spectrum.

Text Listing

1 LET n=23620:
 LET p=2:
 POKE n+73,8:
 CLS :
 DIM t(65):
 FOR x=1 TO 6:
 FOR y=2 TO 8:
 PRINT AT 20,y*3;y-1; PAPER t(x*8+y); FLASH t(x*8+y)=8;AT x*3-1,y*3;"  ";AT x*3,y*3;"  ":
 NEXT y:
 NEXT x:
 IF 49=x=t(5) THEN BORDER p:
 INPUT ':
 LET b=CODE INKEY$-47:
 POKE (b<2 OR b>8)*n,14:
 POKE (t(b+8)>0)*n,14:
 LET a=0:
 BEEP .1,12-a:
 PRINT PAPER a=0;AT a-1,b*3;"  ";AT a,b*3;"  "; PAPER p;AT a+2,b*3;"  ";AT a+3,b*3;"  ":
 LET a=a+3:
 LET k=a*8/3+b:
 POKE (t(k+8)=0)*(a<18)*n,18:
 LET t(k)=p:
 FOR j=1 TO 4:
 LET l=0:
 LET d=j+5*(j>1):
 FOR e=0 TO 1:
 LET m=k:
 LET l=l+1:
 LET t(l)=m:
 LET m=m-d+2*d*e:
 POKE (t(m)=p)*n,29:
 NEXT e:
 FOR f=1 TO l*(l>4):
 LET t(t(f))=8:
 BEEP .1,f*3:
 NEXT f:
 NEXT j:
 LET p=8-p:
 POKE n,6+4*(t(5)=0)

Download the game

About the game

This game is a one-liner implementation of the well known Connect 4 board game for two human players. The game was developed in 2008 by Digital Prawn and Einar Saukas with additional suggestions from Dr BEEP. Digital Prawn coded the initial game version. Einar Saukas provided a huge amount of optimizations, allowing all of the game features to fit within a one-liner. Dr BEEP provided additional optimizing suggestions.

Players Red and Yellow take alternate turns selecting a number in the range 1-7 in order to drop coloured tokens into the columns on the board. Once a token is in place, it cannot be moved. The object of the game is to try to get four tokens of your own colour in a row, winning the game. Horizontal, vertical and diagonal rows are allowed. When this happens, the winning four-in-a-row will FLASH, and the game will end. If the board completely fills with tokens, and neither player has achieved a four-in-a-row, then the game ends in a draw.

This game borrows a lot of ideas from the previous Conexão one-liner game by the same authors. The board printing routine works in the same way, except that we now have seven columns instead of six. In this game though, the board printing routine is not executed on every turn, rather only at the beginning of the game to draw the initial empty board, and again at the end to FLASH the winning tokens. This speeds the game up considerably. Instead during each turn, the drawing of each individual token is handled by a separate "falling token animation" routine in the main game loop.
The board data structure is similar to that of Conexão, in that it is flattened into a one-dimensional array to save space in the one-liner program. However, in this game, the first "row" of the array is also used to store the position of winning pieces so they can be FLASHed by the board printing routine after the winning move has been made.

Back to Sinclair stuff index