Instructions:

There are 8 pairs of coloured shapes in a 4 x 4 grid, initially unrevealed. You need to reveal them and match the pairs.

Each player reveals 2 coloured shapes. If they match then the player gets another go else the shapes disappear and it's the turn of the other player.

Once all of the pairs of coloured shapes have been matched the winner is the player who has matched the most pairs.

Keys: A to P (as indicated by the grid shown on screen)

The emulator that I used is an online one: JSBeeb - https://bbc.godbolt.org/

Simply copy and paste the source code into it and then type RUN

Technical information:

The program stucture (referring to the expanded version) is as follows:

lines 100 to 211 is the initialisation

lines 212 to 301 is the main game loop

lines 302 to 400 (when z=0) is the end of game

line 400 (when z<>0) "remembers" a shape and erases the previously drawn shape from the screen

lines 500 to 506 is the first half of the shape drawing routine

lines 600 to 800 is the computer player turn

line 800 (when k=0) displays the ABCD grid which is called in the initialisation

lines 900 to 1000 is the human player turn

line 1000 (when k<>1) is the second half of the shape drawing routine

The RETURN for the 400, 500, 600 and 900 subroutines is the same one for all of them, the one on line 1007.

This is because the 400 subroutine drops into 500 and both the 600 and 900 subroutines are exited with a GOTO 500.

Variables used:

a(15) - array for the 4 x 4 game grid. The shapes are represented by a number 1 to 8. 0 means that the position is empty.

c(8,1) - the computer player's "memory". c(x,0) will contain the grid position+1 of the 1st occurence of shape x, c(x,1) will contain the grid position+1 of the 2nd occurence of shape x.

d(1) - the player types. d(0) is the player type for player 1. 90 is for a human player, 60 is the computer (these are 900 and 600 in the expanded version). d(1) is the player type for player 2.

You can set them both to 90 (or 900) for a 2 player game or set them both to 60 (or 600) to make the computer play itself.

s(1) - the player scores. s(0) is player 1's score. s(1) is player 2's score.

l - the skill level (0-10). 10 is the highest skill level where the computer has perfect memory, 0 is the lowest skill level where the computer has no memory; it makes a random guess each time.

l is the probability (out of 10) that the computer "remembers" a shape when it is revealed. So when l is 5, for example, the probability of it remembering a shape is 5/10 or 50%.

i,j,t,g - general purpose variables. The 600 and 900 subroutines (computer and human turns) set i and j to the grid positions of the 1st and 2nd shapes selected.

w - how long the revealed shapes are displayed for before disappearing (in 1/100th's of a second, so 100 = 1 second)

p - the current player, 0 = player 1, 1 = player 2

z - the number of unmatched pairs. This is set to 8 initially. The game ends when this is 0.

u - the colour scheme

x,y - used in the shape drawing routine. The x and y coordinates of the shape being drawn.

r,q,f - used in the shape drawing routine.

e - used in the human player turn routine, e=0 when selecting the 1st shape and e=1 when selecting the 2nd.

k - is always set to 1 apart from in 3 places:

when calling subroutine 800 in the initialisation (k=0)

in subroutine 400 when "remembering" a shape and then k is set to 0 to erase the shape (i.e. k=0 when it drops into the shape drawing routine).

later in the shape drawing routine k is set to the number of sides that the shape being drawn has.

it gets reset back to 1 at the end of the shape drawing routine.

Computer player strategy:

The strategy used by the computer player is simple, but effective, it will beat you most of the time (at the highest skill level).

First of all it remembers all revealed shapes (at the highest skill level anyway).

If there any known pairs then select a known pair.

Else reveal an unknown shape, if you know where it's partner is then select it.

Else reveal a 2nd unknown shape (there's still a chance that you'll guess a pair).

```Full listing with comments:
initialisation
100MODE1
101VDU23,1,0;0;0;0;
numeric arrays are initialised to 0
102DIMa(15),c(8,1),d(1),s(1)
set player 1 type to human
103d(0)=900
set player 2 type to computer
104d(1)=600
set skill level to 10 (highest)
105l=10
1 second delay after displaying shapes
106w=100
put the 8 shapes in the grid
107FORi=0TO15
108a(i)=i DIV2+1
109NEXT
seed the pseudo random number generator
110j=RND(-TIME)
now shuffle the grid
200FORi=0TO14
201j=16-RND(16-i)
202t=a(j)
203a(j)=a(i)
204a(i)=t
205NEXT
current player = player 1
206p=0
number of unmatched pairs = 8
207z=8
set the colour scheme
208u=RND(6)
209k=0
210GOSUB800
211k=1
main game loop
212REPEAT
call computer/human turn routine depending on player type of current player
213GOSUB(d(p))
214g=INKEY(w)
if the 2 shapes match then increment the current player's score, decrement the number of unmatched pairs, "forget" the pair and remove the 2 shapes from the grid.
if the 2 shapes do not match then remember/erase the 2 shapes and set the current player the other player.
300IFa(i)=a(j)THENs(p)=s(p)+1:z=z-1:c(a(i),1)=0:a(i)=0:a(j)=0 ELSEt=j:j=i:GOSUB400:j=t:GOSUB400:p=1-p
301UNTILz=0
end of game (VDU30 positions the cursor at the top left of the screen)
302VDU30
remember/erase shape (when z<>0) and end of game continued (when z=0)
when RND(10)>l the shape isn't remembered; it will get updated into c(0,0) or c(0,1) which aren't checked for as there is no shape 0.
400IFz THENk=c(a(j),0):c(a(j)*-(RND(10)<=l),(k<>0)*(k<>j+1))=j+1:k=0 ELSEPRINT"P1:";s(0);" P2:";s(1):VDU23,1,1;0;0;0;:END
shape drawing routine - part 1
change palette to use colour scheme
500VDU19,1,u;0;19,2,(u+1)MOD6+1;0;
501x=256+j MOD4*256
502y=896-j DIV4*256
set graphics colour, shapes will be erased (i.e. drawn in black) when k=0
503GCOL0,(2-a(j)MOD2)*k
set k to number of sides (3 to 6)
504k=(a(j)+5)DIV2
505r=360/k
506GOTO1000
computer player turn
display the current player in top left
600VDU30,80,p+49
look for known pairs
601t=9
602REPEAT
603t=t-1
604UNTILt=0ORc(t,1)<>0
if found a known pair then select and display the 2 shapes
605IFt THENj=c(t,0)-1:GOSUB500:g=INKEY(w):i=j:j=c(t,1)-1:GOTO500
else select and display an unknown shape
700REPEAT
701j=RND(16)-1
702UNTILa(j)<>0ANDc(a(j),0)<>j+1
703GOSUB500
704g=INKEY(w)
705i=j
if you know where it's partner is then select and display it
706IFc(a(i),0)<>0THENj=c(a(i),0)-1:GOTO500
else select and display a 2nd unknown shape (when k<>0)
800IFk THENREPEAT:j=RND(16)-1:UNTILa(j)<>0ANDc(a(j),0)<>j+1ANDj<>i:GOTO500 ELSEPRINT''"ABCD"'"EFGH"'"IJKL"'"MNOP":RETURN
human player turn
display the current player in top left
900VDU30,80,p+49
901i=-1
902FORe=0TO1
select a valid shape (2nd shape must not be same as 1st)
903REPEAT
904j=(GETOR32)-97
905IFj<0ORj>15ORj=i THENUNTIL0 ELSEUNTILa(j)<>0
display 1st shape
906IFe=0THENGOSUB500:i=j
2nd part of human player turn (when k=1) and 2nd part of shape drawing routine (when k<>1)
1000IFk=1THENNEXT:GOTO500 ELSEMOVEx,y+100
1001FORq=1TOk
1003MOVEx,y
PLOT85 draws triangles
1004PLOT85,x+100*SIN(f),y+100*COS(f)
1005NEXT
1006k=1
1007RETURN```
 Status Released Author BASIC 10Liner Genre Puzzle Tags 8-Bit, basic, basic10liner, bbc-micro