1: / ttt -- learning tic-tac-toe
   2: 
   3:         jsr     r5,mesg; <Tic-Tac-Toe\n\0>; .even
   4:         jsr     r5,mesg; <Accumulated knowledge? \0>; .even
   5:         jsr     r5,quest
   6:                 br 1f
   7: .globl  _open
   8: .globl  _read
   9: .globl  _close
  10:         mov     $0,-(sp)
  11:         mov     $ttt.k,-(sp)
  12:         jsr     pc,_open
  13:         cmp     (sp)+,(sp)+
  14:         bes     1f
  15:         mov     r0,r1
  16:         mov     $1000.,-(sp)
  17:         mov     $badbuf,-(sp)
  18:         mov     r0,-(sp)
  19:         jsr     pc,_read
  20:         add     $6,sp
  21:         mov     r1,-(sp)
  22:         jsr     pc,_close
  23:         tst     (sp)+
  24: 1:
  25:         mov     $badbuf,r0
  26: 1:
  27: .globl  _signal
  28:         tst     (r0)+
  29:         bne     1b
  30:         tst     -(r0)
  31:         mov     r0,badp
  32:         sub     $badbuf,r0
  33:         asr     r0
  34:         jsr     pc,decml
  35:         jsr     r5,mesg; < 'bits' of knowledge\n\0>; .even
  36:         mov     $addno,-(sp)
  37:         mov     $2,-(sp)
  38:         jsr     pc,_signal
  39:         cmp     (sp)+,(sp)+
  40: 
  41: game:
  42:         jsr     r5,mesg; <new game\n\0>; .even
  43: 
  44: / initialize board
  45: 
  46:         mov     $singbuf,singp
  47:         mov     $board,r0
  48: 1:
  49:         clrb    (r0)+
  50:         cmp     r0,$board+9
  51:         blo     1b
  52: 
  53: loop:
  54: 
  55: / print board
  56: 
  57:         mov     $board,r1
  58: 1:
  59:         movb    (r1)+,r0
  60:         beq     3f
  61:         dec     r0
  62:         beq     2f
  63:         mov     $'X,r0
  64:         br      4f
  65: 2:
  66:         mov     $'O,r0
  67:         br      4f
  68: 3:
  69:         mov     r1,r0
  70:         sub     $board-'0,r0
  71: 4:
  72:         jsr     pc,putc
  73:         cmp     r1,$board+3
  74:         beq     2f
  75:         cmp     r1,$board+6
  76:         beq     2f
  77:         cmp     r1,$board+9
  78:         bne     1b
  79: 2:
  80:         jsr     pc,nline
  81:         cmp     r1,$board+9
  82:         blo     1b
  83:         jsr     pc,nline
  84: 
  85: / get bad move
  86: 
  87:         jsr     r5,mesg; <? \0>; .even
  88:         jsr     pc,getc
  89:         cmp     r0,$'\n
  90:         beq     1f
  91:         sub     $'1,r0
  92:         cmp     r0,$8
  93:         bhi     badmov
  94:         tstb    board(r0)
  95:         bne     badmov
  96:         mov     r0,r1
  97:         jsr     pc,getc
  98:         cmp     r0,$'\n
  99:         bne     badmov
 100:         movb    $2,board(r1)
 101: 1:
 102: 
 103: / check if he won
 104: 
 105:         jsr     r5,check; 6.
 106:                 br loose
 107: 
 108: / check if cat game
 109: 
 110:         jsr     pc,catgam
 111: 
 112: / select good move
 113: 
 114:         clr     ntrial
 115:         clr     trial
 116:         mov     $board,r4
 117: 1:
 118:         tstb    (r4)
 119:         bne     2f
 120:         movb    $1,(r4)
 121:         jsr     r5,check; 3
 122:                 br win
 123:         jsr     pc,getsit
 124:         clrb    (r4)
 125:         mov     $badbuf,r1
 126: 3:
 127:         cmp     r1,badp
 128:         bhis    3f
 129:         cmp     r0,(r1)+
 130:         bne     3b
 131:         br      2f
 132: 3:
 133:         cmp     r0,trial
 134:         beq     2f
 135:         blo     3f
 136:         mov     r0,trial
 137:         mov     r4,move
 138: 3:
 139:         inc     ntrial
 140: 2:
 141:         inc     r4
 142:         cmp     r4,$board+9
 143:         blo     1b
 144: 
 145: / install move
 146: 
 147:         tst     ntrial
 148:         beq     conced
 149: 1:
 150:         cmp     ntrial,$1
 151:         beq     1f
 152:         mov     $singbuf,singp
 153: 1:
 154:         mov     singp,r0
 155:         mov     trial,(r0)+
 156:         mov     r0,singp
 157:         mov     move,r0
 158:         movb    $1,(r0)
 159: 
 160:         jmp     loop
 161: 
 162: badmov:
 163:         jsr     r5,mesg; <Illegal move\n\0>; .even
 164:         jsr     pc,flush
 165:         jmp     loop
 166: 
 167: loose:
 168:         jsr     r5,mesg; <You win\n\0>; .even
 169:         br      1f
 170: 
 171: conced:
 172:         jsr     r5,mesg; <I concede\n\0>; .even
 173: 1:
 174:         mov     badp,r1
 175:         mov     $singbuf,r2
 176: 1:
 177:         cmp     r2,singp
 178:         bhis    1f
 179:         mov     (r2)+,(r1)+
 180:         br      1b
 181: 1:
 182:         mov     r1,badp
 183:         jmp     game
 184: 
 185: win:
 186:         jsr     r5,mesg; <I win\n\0>; .even
 187:         jmp     game
 188: 
 189: getsit:
 190:         mov     $maps,r2
 191:         clr     r3
 192: 1:
 193:         mov     $9,r1
 194:         mov     r5,-(sp)
 195:         clr     r5
 196: 2:
 197:         mul     $3,r5
 198:         movb    (r2)+,r0
 199:         movb    board(r0),r0
 200:         add     r0,r5
 201:         sob     r1,2b
 202:         mov     r5,r0
 203:         mov     (sp)+,r5
 204:         cmp     r0,r3
 205:         blo     2f
 206:         mov     r0,r3
 207: 2:
 208:         cmp     r2,$maps+[9*8]
 209:         blo     1b
 210:         mov     r3,r0
 211:         rts     pc
 212: 
 213: catgam:
 214:         mov     $board,r0
 215: 1:
 216:         tstb    (r0)+
 217:         beq     1f
 218:         cmp     r0,$board+9
 219:         blo     1b
 220:         jsr     r5,mesg; <Draw\n\0>; .even
 221:         jmp     game
 222: 1:
 223:         rts     pc
 224: 
 225: check:
 226:         mov     $wins,r1
 227: 1:
 228:         jsr     pc,1f
 229:         mov     r0,r2
 230:         jsr     pc,1f
 231:         add     r0,r2
 232:         jsr     pc,1f
 233:         add     r0,r2
 234:         cmp     r2,(r5)
 235:         beq     2f
 236:         cmp     r1,$wins+[8*3]
 237:         blo     1b
 238:         tst     (r5)+
 239: 2:
 240:         tst     (r5)+
 241:         rts     r5
 242: 1:
 243:         movb    (r1)+,r0
 244:         movb    board(r0),r0
 245:         bne     1f
 246:         mov     $10.,r0
 247: 1:
 248:         rts     pc
 249: 
 250: addno:
 251: .globl  _open
 252: .globl  _read
 253: .globl  _close
 254:         mov     $0,-(sp)
 255:         mov     $ttt.k,-(sp)
 256:         jsr     pc,_open
 257:         cmp     (sp)+,(sp)+
 258:         bes     1f
 259:         mov     r0,r1
 260:         mov     $1000.,-(sp)
 261:         mov     badp,-(sp)
 262:         mov     r0,-(sp)
 263:         jsr     pc,_read
 264:         add     $6,sp
 265:         mov     r1,-(sp)
 266:         jsr     pc,_close
 267:         tst     (sp)+
 268: 1:
 269:         mov     $badbuf,r1
 270:         mov     r1,r2
 271: 1:
 272:         mov     (r2)+,r0
 273:         beq     1f
 274:         blt     1b
 275:         mov     $badbuf,r3
 276: 2:
 277:         tst     (r3)
 278:         beq     2f
 279:         cmp     r0,(r3)+
 280:         bne     2b
 281:         mov     $-1,-2(r3)
 282:         br      2b
 283: 2:
 284:         mov     r0,(r1)+
 285:         br      1b
 286: 1:
 287: .globl  _creat
 288: .globl  _write
 289:         sub     $badbuf,r1
 290:         mov     r1,0f
 291:         jsr     pc,nline
 292:         mov     r1,r0
 293:         asr     r0
 294:         jsr     pc,decml
 295:         jsr     r5,mesg; < 'bits' returned\n\0>; .even
 296:         mov     $644,-(sp)
 297:         mov     $ttt.k,-(sp)
 298:         jsr     pc,_creat
 299:         cmp     (sp)+,(sp)+
 300:         mov     0f,-(sp)
 301:         mov     $badbuf,-(sp)
 302:         mov     r0,-(sp)
 303:         jsr     pc,_write
 304:         add     $6,sp
 305:         jmp     done
 306: 
 307: 0:      ..
 308: 
 309: maps:
 310:         .byte   0,1,2,3,4,5,6,7,8
 311:         .byte   6,3,0,7,4,1,8,5,2
 312:         .byte   8,7,6,5,4,3,2,1,0
 313:         .byte   2,5,8,1,4,7,0,3,6
 314:         .byte   2,1,0,5,4,3,8,7,6
 315:         .byte   8,5,2,7,4,1,6,3,0
 316:         .byte   6,7,8,3,4,5,0,1,2
 317:         .byte   0,3,6,1,4,7,2,5,8
 318: 
 319: wins:
 320:         .byte   0,1,2
 321:         .byte   3,4,5
 322:         .byte   6,7,8
 323:         .byte   0,3,6
 324:         .byte   1,4,7
 325:         .byte   2,5,8
 326:         .byte   0,4,8
 327:         .byte   2,4,6
 328: 
 329: ttt.k:  <ttt.k\0>
 330: 
 331: board:  .=.+9
 332:         .even
 333: singp:  .=.+2
 334: singbuf:.=.+18.
 335: badp:   .=.+2
 336: badbuf: .=.+1000.
 337: ntrial: .=.+2
 338: trial:  .=.+2
 339: move:   .=.+2

Defined functions

addno defined in line 250; used 1 times
  • in line 36
badbuf defined in line 336; used 8 times
badmov defined in line 162; used 3 times
badp defined in line 335; used 5 times
board defined in line 331; used 16 times
catgam defined in line 213; used 1 times
check defined in line 225; used 2 times
conced defined in line 171; used 1 times
game defined in line 41; used 3 times
getsit defined in line 189; used 1 times
loop defined in line 53; used 2 times
loose defined in line 167; used 1 times
maps defined in line 309; used 2 times
move defined in line 339; used 2 times
ntrial defined in line 337; used 4 times
singbuf defined in line 334; used 3 times
singp defined in line 333; used 5 times
trial defined in line 338; used 4 times
ttt.k defined in line 329; used 3 times
win defined in line 185; used 1 times
wins defined in line 319; used 2 times
Last modified: 1987-08-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3363
Valid CSS Valid XHTML 1.0 Strict