/* TIC TAC TOE (TRIS) Game By Stefano Bodrato - August 2003 This version has lots of inline assembly code and must be compiled with Z88DK the algorithm is broken so you can win some time */ //#include char x; char board[9]; // = { // '1','2','3', // '4','5','6', // '7','8','9' //}; char threes[] = { 0,1,2, 3,4,5, 6,7,8, 0,4,8, 6,4,2, 0,3,6, 1,4,7, 2,5,8 }; void printline() { #asm ld a,13 ; NewLine rst 16 ld b,8 sl1a: ld a,32 rst 16 djnz sl1a ld b,17 lloop: ld a,146 rst 16 djnz lloop #endasm } void printspline() { #asm ld a,13 ; NewLine rst 16 ld b,8 sl1b: ld a,32 rst 16 djnz sl1b ld b,2 rl1: push bc ld b,5 sl1: ld a,32 rst 16 djnz sl1 ld a,146 rst 16 pop bc djnz rl1 #endasm } void printboard() { int a; #asm ld a,22 ; PRINT AT... rst 16 ld a,4 rst 16 ld a,0 ; ...(4,0) rst 16 #endasm printspline(); // a "blank" line #asm ld a,13 rst 16 ld b,10 sl1c: ld a,32 rst 16 djnz sl1c #endasm a=board[0]; #asm call putpiece #endasm a=board[1]; #asm call putpiece #endasm a=board[2]; #asm ld a,l rst 16 #endasm printspline(); printline(); printspline(); #asm ld a,13 rst 16 ld b,10 sl1d: ld a,32 rst 16 djnz sl1d #endasm a=board[3]; #asm call putpiece #endasm a=board[4]; #asm call putpiece #endasm a=board[5]; #asm ld a,l rst 16 #endasm printspline(); printline(); printspline(); #asm ld a,13 rst 16 ld b,10 sl1e: ld a,32 rst 16 djnz sl1e #endasm a=board[6]; #asm call putpiece #endasm a=board[7]; #asm call putpiece #endasm a=board[8]; #asm ld a,l rst 16 #endasm printspline(); #asm ld a,13 push af rst 16 pop af rst 16 #endasm /* printf("\n * *"); printf("\n %c * %c * %c", board[0], board[1], board[2]); printf("\n*****************"); printf("\n %c * %c * %c", board[3], board[4], board[5]); printf("\n*****************"); printf("\n %c * %c * %c ", board[6], board[7], board[8]); printf("\n * *"); printf("\n\n"); */ } #asm putpiece: ld a,l rst 16 ld a,32 push af rst 16 pop af rst 16 ld a,146 rst 16 ld a,32 push af rst 16 pop af rst 16 ret #endasm int domove(char player,char opponent) { // Is the center of board free ? if ( board[4] != player && board[4] != opponent ) { board[4] = player; return (0); } // Can I win ? for (x=0 ; x!=8; x++) { if ((board[threes[x*3]]==player && board[threes[x*3+1]]==player) && (board[threes[x*3+2]] != player && board[threes[x*3+2]] != opponent)) { board[threes[x*3+2]] = player; return (1); } else if ((board[threes[x*3+1]]==player && board[threes[x*3+2]]==player) && (board[threes[x*3]] != player && board[threes[x*3]] != opponent)) { board[threes[x*3]] = player; return (1); } } // Is the opponent going to win ? for (x=0 ; x!=8; x++) { if ((board[threes[x*3]]==opponent && board[threes[x*3+1]]==opponent) && (board[threes[x*3+2]] != player && board[threes[x*3+2]] != opponent)) { board[threes[x*3+2]] = player; return (0); } else if ((board[threes[x*3+1]]==opponent && board[threes[x*3+2]]==opponent) && (board[threes[x*3]] != player && board[threes[x*3]] != opponent)) { board[threes[x*3]] = player; return (0); } } // Put in the first free place for (x=0 ; x!=9; x++) { if ( board[x] != player && board[x] != opponent ) { board[x] = player; return (0); } } return(2); } int human(char player,char opponent) { char c; while (1) { c=10; while (c<0 || c>9) //c=(getch()-49); c=(getk()-49); if ( board[c] != player && board[c] != opponent ) { board[c]=player; return (1); } } } int ckwin(char player) { for (x=0 ; x!=8; x++) { if ( board[threes[x*3]]==player && board[threes[x*3+1]]==player && board[threes[x*3+2]]==player) return (1); } return(0); } int ckfree (char player1 , char player2) { for (x=0 ; x!=9; x++) { if ( board[x] != player1 && board[x] != player2 ) { return (1); } } return(0); } int ckgame (char player1 , char player2) { if (ckwin (player1)) { #asm ;ld de,youl ;ld bc,18 ;call $203C ;jr youo ;youl: ;defm "You are the winner" ; ;youo: ld a,96 ld (blen+1),a ld a,96-32 ld (blen1+1),a ld a,5 ld (bheight+1),a ld hl,msgtab1 ld (message+1),hl call banner #endasm //printk ("%c is the winner\n\n",player1); return (1); // "you win" banner #asm msgtab1: defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,16,56,56,16,56,56,16,16,56,56,16,56,56,16,56,56,56,56,56,56,32,56,56,56,32,56,32,56,32,56,56,56,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,16,56,56,16,56,16,56,56,16,56,16,56,56,16,56,56,56,56,56,56,32,56,56,56,32,56,32,56,32,32,56,56,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,16,16,16,56,16,56,56,16,56,16,56,56,16,56,56,56,56,56,56,32,56,32,56,32,56,32,56,32,56,32,56,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,16,56,16,56,56,16,56,16,56,56,16,56,56,56,56,56,56,32,32,56,32,32,56,32,56,32,56,56,32,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,16,16,16,56,56,56,16,16,56,56,56,16,16,56,56,56,56,56,56,56,32,56,56,56,32,56,32,56,32,56,56,56,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 #endasm } if (ckwin (player2)) { #asm ;ld de,spwl ;ld bc,13 ;call $203C ;jr spwo ;spwl: ;defm "Spectrum wins" ;spwo: ld a,126 ld (blen+1),a ld a,126-32 ld (blen1+1),a ld a,7 ld (bheight+1),a ld hl,msgtab ld (message+1),hl call banner #endasm return (2); #asm banner: message: ld hl,msgtab blen1: ld b,94 msgscrl: push bc push hl ld de,22528+(32*7) push de xor a ld ($5C78),a wsync: ld a,($5C78) and a jr z,wsync pop de bheight: ld b,7 clloop: push bc push hl ld bc,32 ldir pop hl blen: ld bc,126 add hl,bc pop bc djnz clloop pop hl inc hl ld bc,10000 wloop: dec bc ld de,2600 wlp1: dec de ld a,d or e jr nz,wlp1 ld a,b or c jr z,wloop pop bc ld a,(23560) and a jr nz,spwret djnz msgscrl jr banner spwret: ret msgtab: defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,8,8,8,56,8,8,8,56,56,8,8,8,8,56,56,8,8,8,56,8,8,8,8,8,56,8,8,8,56,56,8,56,56,56,8,56,8,56,56,56,8,56,56,56,32,56,56,56,32,56,32,56,32,56,56,56,32,56,56,32,32,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,8,56,56,56,56,8,56,56,8,56,8,56,56,56,56,8,56,56,56,56,56,56,8,56,56,56,8,56,56,8,56,8,56,56,56,8,56,8,8,56,8,8,56,56,56,32,56,56,56,32,56,32,56,32,32,56,56,32,56,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,16,56,56,56,56,16,56,56,16,56,16,56,56,56,56,16,56,56,56,56,56,56,16,56,56,56,16,56,56,16,56,16,56,56,56,16,56,16,56,16,56,16,56,56,56,32,56,56,56,32,56,32,56,32,56,32,56,32,56,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,16,16,56,56,16,16,16,56,56,16,16,16,16,56,16,56,56,56,56,56,56,16,56,56,56,16,16,16,56,56,16,56,56,56,16,56,16,56,56,56,16,56,56,56,32,56,56,56,32,56,32,56,32,56,56,32,32,56,56,32,32,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,24,56,24,56,56,56,56,24,56,56,56,56,24,56,56,56,56,56,56,24,56,56,56,24,56,56,24,56,24,56,56,56,24,56,24,56,56,56,24,56,56,56,32,56,32,56,32,56,32,56,32,56,56,56,32,56,56,56,56,56,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,40,56,40,56,56,56,56,40,56,56,56,56,40,56,56,56,56,56,56,40,56,56,56,40,56,56,40,56,40,56,56,56,40,56,40,56,56,56,40,56,56,56,32,32,56,32,32,56,32,56,32,56,56,56,32,56,56,56,56,56,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 defb 56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,48,48,48,56,56,48,56,56,56,56,48,48,48,48,56,56,48,48,48,56,56,56,48,56,56,56,48,56,56,48,56,56,48,48,48,56,56,48,56,56,56,48,56,56,56,32,56,56,56,32,56,32,56,32,56,56,56,32,56,32,32,32,32,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56 #endasm //printk ("%c is the winner\n\n",player2); } if (!ckfree (player1,player2)) { #asm ld de,nowl ld bc,10 call $203C jr nowo nowl: defm "No Winners" nowo: nowwait: ld a,(23560) and a jr z,nowwait #endasm return (3); } return (0); } int main() { #asm ;call $d6b ;cls ;ld hl,udggfx ;ld de,($5C7B) ;ld bc,24 ;ldir ld hl,udggfx ld ($5C7B),hl jr gfxloaded udggfx: ; 'X' defb @11000011 defb @11000011 defb @01100110 defb @00111100 defb @00111100 defb @01100110 defb @11000011 defb @11000011 ; 'O' defb @00111100 defb @01111110 defb @11100111 defb @11000011 defb @11000011 defb @11100111 defb @01111110 defb @00111100 ; square for board defb @00000000 defb @01111110 defb @01010110 defb @01010010 defb @01101010 defb @01010110 defb @01111110 defb @00000000 copyright: defb 16 defb 1 defm "By stefano Bodrato - Aug. 2003" defb 13 defb 16 defb 2 defm "http://z88dk.sf.net" defb 16 defb 0 gfxloaded: call $d6b ; clear screen ld de,copyright ld bc,56 call $203C ld a,2 call 5633 ; init channel #2 (print to screen) #endasm //for (x=0 ; x!=9; x++) board[x] = x; board[0]='1'; board[1]='2'; board[2]='3'; board[3]='4'; board[4]='5'; board[5]='6'; board[6]='7'; board[7]='8'; board[8]='9'; printboard(); // 144 is the 'X' symbol (UDG).. 145 is 'O' while (ckgame (144,145) == 0) { human (144,145); // BEEP effect #1 #asm ld b,50 bl2: ld c,200 bl1: ld a,c out (254),a dec c jr nz,bl1 djnz bl2 ld a,7 out (254),a #endasm printboard(); if (ckgame (144,145) != 0) return (0); domove (145,144); printboard(); // BEEP effect #2 #asm ld b,200 bl4: ld c,50 bl3: ld a,c out (254),a dec c jr nz,bl3 djnz bl4 ld a,7 out (254),a #endasm } #asm jp gfxloaded #endasm } int getk() { #asm ; Border effect (timed not to blurr) ld a,2 out (254),a ld a,4 out (254),a ld a,6 out (254),a ld a,(23672) ld a,1 out (254),a ld a,7 out (254),a ; Check keyboard ld h,0 ld a,(23560) ld l,a and a ret z xor a ld (23560),a #endasm }