Files
Brian Holman d412e35c4e Implement CoCo 3 Quadrapassel and deliver build 01
Add gameplay, keyboard/joystick controls, disk-backed high scores and settings,
standard CMOC effects, and the approved C graphics renderer. Include the clearer
attributed font, title/credits updates, source-verified Modern and Classic (40.2)
scoring, meaningful regression fixtures, and recorded guest validation.

Deliver QUADPSL01.DSK with fresh high scores. Retain source, licenses and selected
guest evidence; exclude personal disks, private connections and build scratch.

Host rules/input/persistence/video checks and memory guards pass. Official
512 KB playtests pass the recorded checks; actual 128 KB/physical joystick
qualification and the normal-window buzzing retest remain pending.
2026-11-11 03:14:14 -07:00

246 lines
9.2 KiB
C

/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "rules.h"
#include "reference_cases.h"
static QOptions defaults = {0,Q_MODERN,0,1};
static QGame game;
static void pieces(void)
{
unsigned t,r,x,y,n;
int minx,maxx,miny;
QPiece p;
QBoard empty = {{{0}}};
for (t=0;t<7;++t) for (r=0;r<4;++r) {
assert(q_masks[t][r]==reference_masks[t][r]);
n=0;minx=miny=4;maxx=-1;
for (y=0;y<4;++y) for (x=0;x<4;++x) if (q_occupied(t,r,x,y)) {
++n;
if ((int)x<minx) minx=x;
if ((int)x>maxx) maxx=x;
if ((int)y<miny) miny=y;
}
assert(n==4);
p=q_spawn(t,r);
assert(p.x+minx==(10-(maxx-minx+1))/2);
assert(p.y+miny==0);
assert(q_fits(&empty,&p));
}
assert(!q_occupied(7,0,0,0));
assert(!q_occupied(0,4,0,0));
assert(!q_occupied(0,0,4,0));
}
static void rotation_kicks(void)
{
static const int expected[5]={0,1,-1,2,-2};
static const unsigned blockers[6][5]={
{99,99,99,99,99},{4,99,99,99,99},{4,5,99,99,99},
{4,5,3,99,99},{4,5,3,6,99},{4,5,3,6,2}
};
unsigned k,i;
QPiece previous;
for (k=0;k<6;++k) {
q_start(&game,&defaults);
memset(&game.board,0,sizeof(game.board));
game.active=q_spawn(5,0);game.active.x=3;game.active.y=5;
for (i=0;i<5;++i) if (blockers[k][i]<10) game.board.cells[5][blockers[k][i]]=1;
previous=game.active;
assert(q_fits(&game.board,&previous));
if (k<5) {
assert(q_rotate(&game,0));
assert(game.active.x==3+expected[k]);
assert(game.active.rotation==1 && game.active.y==5);
} else {
assert(!q_rotate(&game,0));
assert(memcmp(&previous,&game.active,sizeof(previous))==0);
}
}
memset(&game.board,0,sizeof(game.board));
game.active=q_spawn(5,0);assert(game.active.y==-1);
assert(q_rotate(&game,0));assert(game.active.y==0);
game.active=q_spawn(0,0);game.active.y=5;
assert(q_rotate(&game,1));assert(game.active.rotation==3);
}
static void movement_and_lock(void)
{
QPiece p,landed;
QBoard before;
unsigned x,y;
q_start(&game,&defaults);
game.active=q_spawn(5,0);
while(q_move(&game,-1,0)) {}
assert(game.active.x==0);
while(q_move(&game,1,0)) {}
assert(game.active.x==6);
p=game.active;
assert(!q_move(&game,1,0));assert(memcmp(&p,&game.active,sizeof(p))==0);
landed=q_shadow(&game);assert(landed.y==18);
assert(memcmp(&p,&game.active,sizeof(p))==0);
assert(q_drop(&game));assert(game.score==0 && game.lines==0);
for(x=6;x<10;++x) assert(game.board.cells[19][x]==6);
q_start(&game,&defaults);
before=game.board;
game.active=q_spawn(5,1);game.active.y=-2;
assert(q_fits(&game.board,&game.active));
assert(!q_lock(&game) && game.over);
assert(memcmp(&before,&game.board,sizeof(before))==0);
q_start(&game,&defaults);
game.active=q_spawn(6,0);
while(q_move(&game,0,1)) {}
p=game.active;assert(!q_move(&game,0,1));
assert(memcmp(&p,&game.active,sizeof(p))==0);
assert(q_lock(&game));assert(!game.hold_used);
/* A pending preview whose spawn is occupied must block out. */
q_start(&game,&defaults);
game.next.type=5;game.next.rotation=0;
for(x=3;x<=6;++x) game.board.cells[0][x]=2;
game.active=q_spawn(6,0);game.active.x=0;game.active.y=17;
assert(q_lock(&game));assert(game.over);
for(y=0;y<20;++y) for(x=0;x<10;++x) assert(game.board.cells[y][x]<=7);
}
static void simultaneous_rows(void)
{
QBoard b={{{0}}};
unsigned x;Q8 bottom;
for(x=0;x<10;++x) b.cells[19][x]=b.cells[17][x]=1;
b.cells[18][2]=5;b.cells[16][1]=7;b.cells[0][0]=2;
assert(q_compact(&b,&bottom)==2 && bottom);
assert(b.cells[19][2]==5 && b.cells[19][1]==0);
assert(b.cells[18][1]==7 && b.cells[18][2]==0);
assert(b.cells[2][0]==2 && b.cells[0][0]==0 && b.cells[1][0]==0);
memset(&b,0,sizeof(b));
for(x=0;x<10;++x)b.cells[18][x]=1;
b.cells[19][0]=4;
assert(q_compact(&b,&bottom)==1 && !bottom && b.cells[19][0]==4);
}
static void scoring(void)
{
unsigned i;
for(i=0;i<sizeof(score_cases)/sizeof(score_cases[0]);++i) {
const ScoreCase *c=&score_cases[i];
q_start(&game,&defaults);
game.options.difficulty=c->difficulty;game.options.scoring=c->rule;
game.lines=c->prior;game.score=0;
q_score_clear(&game,c->rows,c->bottom);
assert(game.level==c->level && game.score==c->points);
}
game.options.difficulty=0;
game.score=Q_SCORE_MAX-5;game.lines=Q_SCORE_MAX-1;
q_score_clear(&game,4,1);
assert(game.lines==Q_SCORE_MAX && game.score==Q_SCORE_MAX && game.level==65535);
for(i=1;i<=100;++i) assert(q_gravity_ms(i)==reference_gravity[i-1]);
}
/* Adapted from Brian Holman's GPL-3.0-or-later Mac test-scoring.vala:
* drop an actual vertical I piece through one-cell gaps in nearly full rows.
* This also checks bottom-row detection and level changes, not just arithmetic.
*/
static void scoring_through_drop(void)
{
unsigned i,x;int y,bottom;
for(i=0;i<sizeof(score_cases)/sizeof(score_cases[0]);++i){
const ScoreCase *c=&score_cases[i];
q_start(&game,&defaults);
game.options.difficulty=c->difficulty;game.options.scoring=c->rule;
game.lines=c->prior;game.level=q_level(c->difficulty,c->prior);
bottom=c->bottom?19:18;
for(y=bottom-(int)c->rows+1;y<=bottom;++y)
for(x=0;x<10;++x)if(x!=4)game.board.cells[y][x]=1;
if(!c->bottom)game.board.cells[19][4]=1;
game.active=q_spawn(5,1);game.active.x=3;game.active.y=0;
assert(q_drop(&game));
assert(game.last_clear==c->rows);
if(c->rows)assert(game.last_bottom==c->bottom);
assert(game.lines==c->prior+c->rows);
assert(game.level==c->level&&game.score==c->points);
}
}
static void hold(void)
{
QChoice preview;
Q8 original,held;
QRandom random;
q_start(&game,&defaults);
original=game.active.type;preview=game.next;
assert(q_hold(&game));
assert(game.held==original && game.active.type==preview.type && game.active.rotation==0);
random=game.random;preview=game.next;
assert(!q_hold(&game));assert(memcmp(&random,&game.random,sizeof(random))==0);
assert(q_drop(&game));assert(!game.hold_used);
held=game.held;original=game.active.type;preview=game.next;random=game.random;
assert(q_hold(&game));
assert(game.active.type==held && game.active.rotation==0 && game.held==original);
assert(memcmp(&preview,&game.next,sizeof(preview))==0);
assert(memcmp(&random,&game.random,sizeof(random))==0);
assert(!q_hold(&game));
}
static void randomness(void)
{
QRandom a,b;
QOptions options=defaults;
QGame other;
unsigned i,j,x,y;unsigned selected[7]={0};Q16 lo,hi;
q_random_init(&a,1);
assert(q_random_next(&a)==270369UL);
assert(q_random_next(&a)==67634689UL);
assert(q_random_next(&a)==2647435461UL);
q_random_init(&a,0);q_random_init(&b,0);
for(i=0;i<100;++i)assert(q_weighted(&a)==q_weighted(&b));
q_random_init(&a,1234567);
for(i=0;i<20000;++i)++selected[q_weighted(&a)];
lo=hi=selected[0];
for(i=0;i<7;++i){if(selected[i]<lo)lo=selected[i];if(selected[i]>hi)hi=selected[i];}
assert(hi-lo<20);
/* Fixed count vector: underrepresented types must be much likelier. */
memset(selected,0,sizeof(selected));q_random_init(&a,0x12345678UL);
for(i=0;i<20000;++i){for(j=0;j<7;++j)a.counts[j]=0;a.counts[0]=10;++selected[q_weighted(&a)];}
assert(selected[0]>0 && selected[0]<100);
for(i=1;i<7;++i)assert(selected[i]>2500);
for(i=0;i<32;++i) for(j=0;j<=16;++j) {
options.seed=i;options.difficulty=(int)j-1;
q_start(&game,&options);q_start(&other,&options);
assert(memcmp(&game.board,&other.board,sizeof(game.board))==0);
assert(game.active.type==other.active.type && game.next.type==other.next.type);
for(y=0;y<20;++y){unsigned count=0;for(x=0;x<10;++x){assert(game.board.cells[y][x]<=7);count+=game.board.cells[y][x]!=0;}assert(count<10);}
}
}
static void difficult(void)
{
static const int empty_metric[7]={-20,-20,-20,-25,-25,-20,-20};
QBoard b={{{0}}},before;
QRandom a,c;
QChoice choice,other;
unsigned i;
before=b;
for(i=0;i<7;++i)assert(q_best_metric(&b,i)==empty_metric[i]);
assert(memcmp(&b,&before,sizeof(b))==0);
for(i=0;i<10;++i)if(i<3||i>6)b.cells[19][i]=1;
before=b;
assert(q_best_metric(&b,5)==5000);
assert(memcmp(&b,&before,sizeof(b))==0);
q_random_init(&a,47);q_random_init(&c,47);
choice=q_difficult(&b,&a);other=q_difficult(&b,&c);
assert(choice.type<7 && choice.rotation<4);
assert(choice.type==other.type && choice.rotation==other.rotation);
assert(memcmp(&b,&before,sizeof(b))==0);
memset(&b,1,sizeof(b));
for(i=0;i<7;++i)assert(q_best_metric(&b,i)==-30000L);
}
int main(void)
{
pieces();rotation_kicks();movement_and_lock();simultaneous_rows();
scoring();scoring_through_drop();hold();randomness();difficult();
printf("PASS: 28 states, all kicks, bounds/top-out, compaction, %u score fixtures plus actual-drop scoring, gravity, hold, RNG/prefill, difficult metrics\n",(unsigned)(sizeof(score_cases)/sizeof(score_cases[0])));
return 0;
}