c23456-------------------------------------------5---------6--------7--!
	include 'common.for'
        real bingo,bingo1
	real bet,money,btinit,btinit1,money1,cbc,w,ret,totret,sc
	integer choice,bj,ind,bt,dk(10),s,x1(4,10),sx1(9,10)
	integer sp1(10,10),su1(9,10),sh1(4,10),bpm1(9,10)
	common /block6/ bpm1,x1,sx1,sp1,su1,sh1
	open(8,file='bet',status='old')
	open(10,file='bjcom',status='old')
	open(6,file='money',status='old')
        open(80,file='stat',status='unknown')
	data s /0/,ind /0/
	ret=0.
	totret=0
c
c query for number of decks
c
1	write(0,*)'How many decks?  (1-8)'
c	read(*,'(i1)')ndeck
        ndeck=6
	if(ndeck.gt.8 .or. ndeck.lt.1)then
	  write(0,*)'Invalid number of decks -- try again'
	  goto 1
	endif
	ncards=ndeck*52
	call mkdeck(ndeck,ncards,card)
c
c query for interactive
c
7	write(0,*)'Enter 1 to play or 0 to watch'
c	read(*,*)m10z
        m10z=0
c
c if computer plays, get totret from file stat to continue it
c
        if(m10z.eq.0)then
           do 6721 mdm=1,1000000
              read(80,*,end=6722)bif1,bif2,bif3,bif4,bif5,totret
 6721      continue
        endif
c   
        if(m10z.eq.1)close(80,status='delete')
	if(m10z.ne.1 .and. m10z.ne.0)then
	  write(0,*)'Invalid entry -- try again'
	  goto 7
	endif

c 
c begin play loop
c
6722    next=1
	do 8 bt=1,1000000
	  read(8,*,end=11)btinit
	  btinit1=btinit
8	continue
11	do 3 mon=1,1000000
	  read(6,*,end=50)money
	  money1=money
3	continue
50	ind=0
	if(money.le.0.0)then
	   write(0,250)1000.+abs(money)
	   goto 736
	endif
250	format(1x,'You ran out of money.  You lost',1x,f7.1,1x,
     &	       'dollars.',//,'Better luck next time!!')
	if(float(next)/float(ncards).gt.(2./3.))then
258	  write(0,*)'Time to shuffle.  You have ',money,' dollars.'
 4        write(0,293)money1
293	format(18x,'You started with',1x,f7.2,1x,'dollars.')
          bingo=((money-money1)/(btinit-btinit1))
          bingo1=((money-1000.)/btinit)
	  write(0,730)bingo
	  write(0,732)bingo1
730	format(1x,'Local % =',1x,f9.6)
732	format(1x,'Total % =',1x,f9.6)
	  write(6,255)money
	  write(8,255)btinit
	  write(10,307)'$exit'
255	format(f7.1)
736	  stop
	endif
	write(0,300)money
300	format(1x,'You have',1x,f7.1,1x,'dollars')

c-----BETTING

	write(0,*)'Place your bet (5-100, 0 for status, -1 to exit)'
	if(m10z.eq.0)then
	  if(lra.eq.0)then
	    bet=0.
	    lra=1
	  elseif(lra.eq.1)then
	    bet=101.
	    lra=2
	  elseif(lra.eq.2)then
	    write(0,*)w
	    call pyb(cbc,sc,w,bet)
	    lra=0
	  else
	    write(0,*)'lra ne 0,1,2 in Z'
	  endif
	else
	  read(*,*)bet
	endif

c-----END BETTING

c
c calculate and write bet,counts, etc.
c
	if(m10z.eq.0)then
	  if(int(bet).ge.5 .and. int(bet).le.100)then
	    ret=w*bet
	    totret=totret+ret
	    write(80,803)w,sc,cbc,int(bet),ret,totret
803	format(1x,f6.4,2x,f5.1,2x,f6.2,2x,i3,2x,f7.4,2x,f9.4)
	  endif
	endif
c
	if(int(bet).eq.0)then
	  call count(next-1,card,ncards,sc,cbc)
	  goto 50
	elseif(int(bet).lt.5)then
	  if(int(bet).lt.0)then
	    write(10,307)'$exit'
	    goto 258
	  endif
307	format(A5)
	  write(0,*)'Insufficient bet -- try again'
	  goto 50
c
c if bet=101 then write remaining deck
c
	elseif(int(bet).eq.101)then
	  do 372 m1=next,ncards
	    dk(card(m1))=dk(card(m1))+1
372	  continue
	  write(10,373)ncards-next+1,(dk(m2),m2=1,10)
	  write(0,*)'Deck written to bjcom'
	  call deal(ncards-next+1,dk)
	  call play
	  call soft(sh1)
	  call doub(x1)
	  call f(w)
          call sleep(1)
c
c read in and print strategy data after f is run
c
	  do 53 kz=1,10
	    write(0,1111)(bpm1(k,kz),k=1,9)
53	  continue
         call sleep(1)
	  do 1053 kz=1,10
	    write(0,1112)(x1(k,kz),k=1,4)
1053	  continue
          call sleep(1)
	  do 1054 kz=1,10	   
	    write(0,1111)(sx1(k,kz),k=1,9)
1054	  continue
         call sleep(1)
	  do 1055 kz=1,10	   
	    write(0,1113)(sp1(k,kz),k=1,10)
1055	  continue
          call sleep(1)
	  do 1056 kz=1,10	   
	    write(0,1111)(su1(k,kz),k=1,9)
1056	  continue
          call sleep(1)
	  do 1057 kz=1,10	   
	    write(0,1112)(sh1(k,kz),k=1,4)
1057	  continue
1111	format(9(1x,i1))
1112	format(4(1x,i1))
1113	format(10(1x,i1))
c
c now close the scratch files containing the data
c
	  close(71)
	  close(72)
	  close(73)
	  close(74)
	  close(75)
	  close(76)
c          close(41,status='delete')
          close(20,status='delete')
          close(21,status='delete')
	  do 374 m3=1,10
	    dk(m3)=0
374	  continue
373	format('$run [aeromech.els]deal',/,11(i3,/),
     &  '$run [aeromech.els]play',/,'$run [aeromech.els]soft',/,
     &  '$run [aeromech.els]doub',/,'$run [aeromech.els]f')
c     &  ,'$exit')
	  goto 50
c
c if bet=102 then set to auto and go
c
	elseif(int(bet).eq.102)then
	  m10z=0
	  goto 50
	elseif(int(bet).gt.100)then
	  write(0,*)'Bet exceeds maximum -- try again'
	  goto 50
	endif

	btinit=btinit+bet
c
c deal 1st 4 cards
c
	player(1)=card(next)
	next=next+1
	player(2)=card(next)
	next=next+1
	dealer(1)=card(next)
	next=next+1
	dealer(2)=card(next)
	next=next+1
	np=2
	nd=2

60	write(0,1000)player(1),player(2),dealer(1)
	if(ind.eq.1.or.ind.eq.2)then
	  if(player(1).eq.1)goto 77
	endif
	if(ind.eq.0)call bjcheck(player(1),player(2),dealer(1),
     &		dealer(2),bet,money,bj,*50)
1000	format(1x,'Player has',1x,i2,1x,i2,/,1x,'Dealer has',1x,i2)
	write(0,*)
c
c send player choice menu
c
	if(player(1).eq.1 .or. player(2).eq.1)s=1
	write(0,*)'1=hit'
	write(0,*)'2=stand'
	write(0,*)'3=double'
	if(player(1).eq.player(2) .and. ind.eq.0)write(0,*)'4=split'
	if(ind.eq.0)write(0,*)'5=surrender'
c
c determine softness
c
	if(player(1).eq.1 .or. player(2).eq.1)s=1
c
	if(m10z.eq.0)then
	  call chooser(np,dealer(1),player,s,ind,choice)
	else
	  read(*,400)choice
	endif
400	format(i1)
	if(choice.eq.1)then
	  call hit(bet,money,ind,*60,m10z)
	elseif(choice.eq.2)then
77	  call tot(player,np,ptot)
	  if(ind.eq.1.or.ind.eq.2)then
	    call split(bet,money,ind,*60,ptot)
	    goto 50
	  endif
	  call stand(md)
	  dtot=md
	  call pay(bet,dtot,ptot,money,dealer)
	elseif(choice.eq.3)then
	  btinit=btinit+bet
	  call dbl(bet,money,ind,*60)
	elseif(choice.eq.4 .and. player(1).eq.player(2))then
	  if(ind.eq.0)then
	    btinit=btinit+bet
	    call split(bet,money,ind,*60,ptot)
	  else
	    write(0,*)'Not allowed to split now -- try again'
	    goto 60
	  endif
	elseif(choice.eq.5)then
	  if(ind.eq.0)then
	    call sur(bet,money,dealer(1),dealer(2))
	  else
	    write(0,*)'Not allowed to surrender now -- try again'
	    goto 60
	  endif
	else
	  write(0,*)'Invalid choice -- try again'
	  goto 60
	endif
	goto 50
	end


	subroutine bjcheck(p1,p2,d1,d2,bet,money,bj,*)
c23456-------------------------------------------5---------6---------7--!
	integer p1,p2,d1,d2,dbj,pbj,bj
	real bet,money

	bj=0
	dbj=0
	pbj=0
c
c set bj flags
c
	if((d1.eq.1 .and. d2.eq.10).or.(d1.eq.10 .and. d2.eq.1))dbj=1
	if((p1.eq.1 .and. p2.eq.10).or.(p1.eq.10 .and. p2.eq.1))pbj=1
	if(dbj.eq.1 .or. pbj.eq.1)bj=1
c
c evaluate results
c
	if(pbj.eq.1 .and. dbj.ne.1)then
	  bet=bet*1.5
	  write(0,1000)bet
	  money=money+bet
	elseif(pbj.eq.1 .and. dbj.eq.1)then
	  write(0,1010)
	elseif(pbj.ne.1 .and. dbj.eq.1)then
	  write(0,1020)bet
	  money=money-bet
	endif
	if(bj.eq.1)return 1
1000	format(1x,'Player has BJ and wins',1x,f6.1,1x,'dollars')
1010	format(1x,'Standoff at BJ')
1020	format(1x,'Dealer has BJ and player loses',1x,f6.1,1x,'dollars')
	return
	end


	subroutine count(nc,card,ncards,sc,cbc)
c23456-------------------------------------------5---------6---------7--!
	integer nc,card(417),ncards,ace,bingo3
	real cbc,nde,sc,bingo2
	sc=0.
	ace=0
	cbc=0.
	nde=float(ncards-nc)/52.
c
c begin counting the deck
c
	do 10 i=1,nc
	  if(card(i).eq.1)then
	    ace=ace+1
	    cbc=cbc-1.2
	  elseif(card(i).eq.2 .or. card(i).eq.9)then
	    if(card(i).eq.2)then
	      sc=sc+1.
	      cbc=cbc+1.
	    endif
	  elseif(card(i).eq.3 .or. card(i).eq.8)then
	    if(card(i).eq.3)then
	      sc=sc+1.
	      cbc=cbc+1.
	    endif
	  elseif(card(i).eq.4)then
	    sc=sc+1.
	    cbc=cbc+1.
	  elseif(card(i).eq.5)then
	    sc=sc+2.
	    cbc=cbc+2.
	  elseif(card(i).eq.6)then
	    sc=sc+1.
	    cbc=cbc+1.
	  elseif(card(i).eq.7)then
	    cbc=cbc
	  elseif(card(i).eq.10)then
	    sc=sc-1.5
	    cbc=cbc-1.2
	  else
	    write(0,*)'Failure in COUNT card-scan'
	    stop
	  endif
10	continue
	cbc=cbc/nde
	if(ace.eq.0)goto 25
        bingo2=(float(nc)/(13.*float(ace))-1.)/nde
	write(0,1000)sc,bingo2
1000	format(1x,'Simple count =',1x,f5.1,1x,'(',f6.2,')')
25	write(0,1010)cbc
1010	format(1x,'Complex BET count =',1x,f7.2)
        bingo3=ncards-nc
	write(0,1030)bingo3
1030	format(1x,'There are',1x,i3,1x,'cards left')
	return
	end


	subroutine pay(bet,dtot,ptot,money,dealer)
c23456-------------------------------------------5---------6---------7--!
	real bet,money
	integer dtot,ptot,dealer(12)
c
c check for player bust then dealer bust
c
	if(ptot.gt.21)then
	  write(0,1000)bet
	  money=money-bet
	  write(0,1005)dealer(1),dealer(2)
	elseif(dtot.gt.21)then
	  write(0,1010)bet
	  money=money+bet
	else
	  write(0,1020)dtot
	  write(0,1030)ptot
c if no bust, evaluate result
	  if(ptot.gt.dtot)then
	    write(0,1040)bet
	    money=money+bet
	  elseif(ptot.eq.dtot)then
	    write(0,1050)
	  else
	    write(0,1060)bet
	    money=money-bet
	  endif
	endif
1000 	format(1x,'Player busts and loses',1x,f6.1,1x,'dollars')
1005	format(1x,'Dealer had',1x,i2,1x,i2)
1010	format(1x,'Dealer busts and player wins',1x,f6.1,1x,'dollars')
1020	format(1x,'Dealer stands at',1x,i2)
1030	format(1x,'Player stands at',1x,i2)
1040	format(1x,'Player wins',1x,f6.1,1x,'dollars')
1050	format(1x,'Standoff')
1060	format(1x,'Player loses',1x,f6.1,1x,'dollars')
	return
	end


	subroutine tot(array,numb,total)
c23456-------------------------------------------5---------6---------7--!
	integer array(12),numb,total,soft,no

c
c change first ace to 11
c
	soft=0
	no=0
5 	do 10 i=1,numb
	  if(array(i).eq.1 .and. no.ne.1)then
	    array(i)=11
	    soft=1
	    mark=i
	    goto 15
	  endif
10	continue
c
c sum total
c
15	total=0
	do 20 j=1,numb
	  total=total+array(j)
20	continue
c
c evaluate result
c
	if(total.gt.21 .and. soft.eq.1)then
	  array(mark)=1
	  soft=0
	  no=1
	  goto 5
	endif
c
c change 11's back to 1's
c
	do 50 m=1,numb
	  if(array(m).eq.11)array(m)=1
50	continue
	return
	end


	subroutine dbl(bet,money,ind,*)
c23456-------------------------------------------5---------6---------7--!
	include 'common.for'
	real bet,money
	integer ind
c
c get 1 card, dbl bet, total, stand, and pay
c
	np=np+1
	player(np)=card(next)
	next=next+1
	bet=bet*2.
	write(0,1000)(player(jj),jj=1,np)
1000	format(1x,'Player has',3(1x,i2))
	call tot(player,np,ptot)
	if(ind.eq.1.or.ind.eq.2)then
	  call split(bet,money,ind,*20,ptot)
	  bet=.5*bet
	  goto 10000
	endif
	if(ptot.gt.21)goto 10
	call stand(md)
	dtot=md
10	if(ind.eq.1.or.ind.eq.2)then
	  call split(bet,money,ind,*20,ptot)
	  bet=.5*bet
	  goto 10000
	endif
	call pay(bet,dtot,ptot,money,dealer)
10000	return
20	bet=.5*bet
	return 1
	end


	subroutine hit(bet,money,ind,*,m10z)
c23456-------------------------------------------5---------6---------7--!
	include 'common.for'
	real bet,money
	integer choice,ind,s,m10z
	s=0
c
c check total and bust, if necessary
c
	call tot(player,np,ptot)
c 
c now give card and iterate
c
5	np=np+1
	player(np)=card(next)
	next=next+1
	call tot(player,np,ptot)
        if(np.eq.3)then
	  write(0,1000)(player(jj),jj=1,np),dealer(1)
        elseif(np.eq.4)then
          write(0,1003)(player(jj),jj=1,np),dealer(1)
        elseif(np.eq.5)then
          write(0,1006)(player(jj),jj=1,np),dealer(1)
        elseif(np.eq.6)then
          write(0,1009)(player(jj),jj=1,np),dealer(1)
        elseif(np.eq.7)then
          write(0,1012)(player(jj),jj=1,np),dealer(1)
        elseif(np.eq.8)then
          write(0,1015)(player(jj),jj=1,np),dealer(1)
        elseif(np.eq.9)then
          write(0,1018)(player(jj),jj=1,np),dealer(1)
        endif
	if(ptot.lt.21)then
1000	format(1x,'Player has',3(1x,i2),/,1x,'Dealer has a',1x,i2)
1003    format(1x,'Player has',4(1x,i2),/,1x,'Dealer has a',1x,i2)
1006    format(1x,'Player has',5(1x,i2),/,1x,'Dealer has a',1x,i2)
1009    format(1x,'Player has',6(1x,i2),/,1x,'Dealer has a',1x,i2)
1012    format(1x,'Player has',7(1x,i2),/,1x,'Dealer has a',1x,i2)
1015    format(1x,'Player has',8(1x,i2),/,1x,'Dealer has a',1x,i2)
1018    format(1x,'Player has',9(1x,i2),/,1x,'Dealer has a',1x,i2)
10	  write(0,*)'1=hit'
	  write(0,*)'2=stand'
c
c determine softness
c
	mtot=0
	do 29 i=1,np
	  mtot=mtot+player(i)
	  if(player(i).eq.1)s=1
29	continue
	if(mtot.ge.11)s=0
c	
	if(m10z.eq.1)then
	  read(*,300)choice
	else
	  call chooser(np,dealer(1),player,s,ind,choice)
	endif
300	format(i1)
	  if(choice.eq.1)then
	    goto 5
	  else
	    if(ind.eq.1)then
	      call split(bet,money,ind,*20,ptot)
	      goto 20
	    elseif(ind.eq.2)then
	      call split(bet,money,ind,*20,ptot)
	      goto 10000
	    endif  
	    call stand(md)
	    dtot=md
	    call pay(bet,dtot,ptot,money,dealer)
	    goto 10000
	  endif
	elseif(ptot.eq.21)then
	  if(ind.eq.1 .or. ind.eq.2)goto 35
	  call stand(md)
	  dtot=md
	endif
35	if(ind.eq.1)then
	  call split(bet,money,ind,*20,ptot)
	  goto 20
	elseif(ind.eq.2)then
	  call split(bet,money,ind,*20,ptot)
	  goto 10000
	endif
	call pay(bet,dtot,ptot,money,dealer)
10000	return
20	return 1
	end


	subroutine stand(md)
c23456-------------------------------------------5---------6---------7--!
	include 'common.for'
	integer md
c
c get dealer next card and sum til busted or 17
c
        ptot=ptot
5	call tot(dealer,nd,dtot)
	write(0,1000)dealer(nd),dtot
        call sleep(1)
1000	format(1x,'Dealer flips a',1x,i2,1x,'for',1x,i2)
	if(dtot.lt.17)then
	  nd=nd+1
	  dealer(nd)=card(next)
	  next=next+1
	  goto 5
	endif
	md=dtot
	return
	end


	subroutine mkdeck(ndeck,ncards,card)


c23456------------------------------------------------------------------!

	integer numar,deck(417),num,ndeck,card(417),seed
	real rnd
	open(3,file='seed',status='old')
c
c make standard deck array from which to choose rnd cards
c
	do 5 i=0,12
	  do 10 i1=1,4*ndeck
	    deck(i*4*ndeck+i1)=min0(i+1,10)
10	  continue
5	continue

c
c make the deck by generating the rnd's and picking
c
c now read the real rnd's
c
100	numar=ncards
c        write(0,*)'Enter the integer seed'
        do 17 mmj=1,1000000
          read(3,*,end=19)seed
17      continue      
19      nseed=seed+1
        write(3,*)nseed
        do 20 j=1,ncards
          if(j.eq.1)then
	   rnd=random(seed)
          else
           rnd=random(0)
          endif
	  num=int(rnd*float(numar)+1.)
	  card(j)=deck(num)
c alter std deck by shifting everything forward
	  do 30 k=num,numar
	    deck(k)=deck(k+1)
30	  continue
	  numar=numar-1
20	continue
	return
	end


	subroutine sur(bet,money,d1,d2)
c23456-------------------------------------------5---------6---------7--!
	real bet,money,a
	integer d1,d2
	a=bet/2.
	money=money-a
	write(0,1000)a
	write(0,1010)d1,d2
1000	format(1x,'Player surrenders',1x,f6.1,1x,'dollars')
1010	format(1x,'Dealer had',1x,i2,1x,i2)
	return
	end


c	common /block3/ card,player,dealer,next,np,nd,ptot
c    &                  dtot,ncards,b1,b2,pt1,pt2

c	integer card(417),player(12),dealer(12),next,np,nd
c       integer ptot,dtot,ncards,pt1,pt2
c	real b1,b2

	subroutine split(bet,money,ind,*,pt)
c23456-------------------------------------------5---------6---------7--!
	include 'common.for'
	integer pt,ind,md
	real bet,money
c
c this is hand 1
c
        ptot=ptot
	if(ind.eq.0)then
	  do 10 i=2,np
	    player(i)=0
10	  continue
	  np=2
	  player(2)=card(next)
	  next=next+1
	  ind=1
	  write(0,*)'First hand...'
	  goto 10000
c
c this is hand 2
c
	elseif(ind.eq.1)then
	  pt1=pt
	  b1=bet
	  ind=2
	  do 20 j=2,np
	    player(j)=0
20	  continue
	  np=2
	  player(2)=card(next)
	  next=next+1
	  write(0,*)'Second hand...'
	  goto 10000
c
c now evaluate results
c
	elseif(ind.eq.2)then
	  pt2=pt
	  b2=bet
	  call stand(md)
	  dtot=md
	  call pay(b1,dtot,pt1,money,dealer)
	  call pay(b2,dtot,pt2,money,dealer)
	else
	  write(0,*)'ind ne 0,1,2 -- error in SPLIT'
	endif
	return
10000	return 1
	end
	



	SUBROUTINE DEAL(NUM,ARRAY)

	 REAL*8 PROB(10,7)
	 INTEGER CARD(12),ACE,TOTAL,PACK,DECK(10),COMBO,N
	 INTEGER PACK1,DECK1(10),HDATA,SUB,ARRAY(10),NUM
	 COMMON /BLOCK1/ CARD,PACK,DECK,COMBO,PACK1,DECK1
	 OPEN(31,FILE='BJOUT',STATUS='NEW')
	 OPEN(41,FILE='BJDATA',STATUS='NEW')
	 DATA PROB /70*0./, ACE /0/
C
C READ IN PACK AND DECK DATA
C
         SUB=SUB
C	 WRITE(0,*)'ENTER # OF CARDS IN PACK'
C	 READ(*,*)PACK1
	 PACK1=NUM
	 WRITE(31,*)PACK1
	 PACK=PACK1
	 DO 2 HDATA=1,10
C	   WRITE(0,*)'ENTER # OF ',HDATA
C	   READ(*,*)DECK1(HDATA)
	   DECK1(HDATA)=ARRAY(HDATA)
	 WRITE(31,*)DECK1(HDATA)
	   DECK(HDATA)=DECK1(HDATA)
2 	 CONTINUE
	 COMBO=0
C
C SET 1ST CARD TO ACE AND SECOND CARD TO ACE
C
	 CARD(1)=1
	 CARD(2)=1
	 N=2
C
C CHECK ACES AND MODIFY CARD VARIABLE IF NECESSARY
C
18	 CALL ACER(ACE,N)
C
C CHECK TOTAL AND ACT ACCORDINGLY
C
19	 TOTAL=0
	 DO 10 I=1,N
	   TOTAL=TOTAL+CARD(I)
10 	 CONTINUE
C
	 IF(TOTAL.GT.21 .AND. ACE.EQ.0) THEN
	   CALL P(N,PROB(CARD(1),6))
	 ELSEIF(TOTAL.GT.21 .AND. ACE.NE.0) THEN
	   CARD(ACE)=1
	   ACE=0
	   GOTO 19
	 ELSEIF(TOTAL.EQ.17) THEN
	   IF(CARD(1).EQ.11)CARD(1)=1
	   CALL P(N,PROB(CARD(1),1))
	 ELSEIF(TOTAL.EQ.18) THEN
	   IF(CARD(1).EQ.11)CARD(1)=1
	   CALL P(N,PROB(CARD(1),2))
	 ELSEIF(TOTAL.EQ.19) THEN
	   IF(CARD(1).EQ.11)CARD(1)=1
	   CALL P(N,PROB(CARD(1),3))
	 ELSEIF(TOTAL.EQ.20) THEN
	   IF(CARD(1).EQ.11)CARD(1)=1
	   CALL P(N,PROB(CARD(1),4))
	 ELSEIF(TOTAL.EQ.21) THEN
	   IF(CARD(1).EQ.11)CARD(1)=1
	   IF(N.EQ.2) THEN
	     CALL P(N,PROB(CARD(1),7))
	   ELSE
          CALL P(N,PROB(CARD(1),5))
	   ENDIF
	 ELSEIF(TOTAL.LE.16) THEN
	   N=N+1
	   CARD(N)=1
	   GOTO 18
	 ENDIF
C
C IS LASTCARD A 10? IF SO, THEN KILL IT AND ITERATE PREVIOUS
C
30	 IF(CARD(N).EQ.10) THEN
	   CARD(N)=0
	   N=N-1
	   IF(N.EQ.0) THEN
	     CALL OUT(PROB)
          GOTO 100
	   ENDIF
	   GOTO 30
	 ELSE
	   IF(CARD(N).EQ.11)CARD(N)=1
	   CARD(N)=CARD(N)+1
	   GOTO 18
	 ENDIF
100	 do 71 jj=1,10
	    do 73 jj1=1,7
	      prob(jj,jj1)=0.
73	    continue
71	 continue
	 CLOSE(31)
	 CLOSE(41)
	 RETURN
	 END
	 



	 SUBROUTINE ACER(ACE,N)
C
C THIS SUBROUTINE CHANGES THE FIRST ACE TO AN 11
C
	 INTEGER CARD(12),ACE,N,PACK,PACK1,DECK(10),DECK1(10),COMBO
	 COMMON /BLOCK1/ CARD,PACK,DECK,COMBO,PACK1,DECK1
	 ACE=0
	 DO 10 I=1,N
	   IF(CARD(I).EQ.1 .OR. CARD(I).EQ.11) THEN
	     CARD(I)=11
	     ACE=I
	     GOTO 20
	   ENDIF
10	 CONTINUE
20    RETURN
	 END



	 SUBROUTINE P(N,PROB)
C
C THIS SUBROUTINE CALCULATES LINE PROBABILITIES
C
	 INTEGER N,CARD(12),DECK(10),PACK,PACK1,DECK1(10),COMBO
	 REAL*8 PROB,LINE
	 COMMON /BLOCK1/ CARD,PACK,DECK,COMBO,PACK1,DECK1
	 LINE=1.0
C
C CALCULATE LINE PROB'S AND ALTER DECK
C
	 DO 10 I=1,N
	   DO 20 I1=1,10
	     IF(CARD(I).EQ.I1 .OR. CARD(I).EQ.11) THEN
	       IF(I.EQ.1) THEN
	         LINE=1.0
	       ELSE
	         SUB=DECK(I1)
	         IF(SUB.LE.0)SUB=0
	         IF(SUB.LE.0)GOTO 15
	         LINE=LINE*DBLE(SUB)/DBLE(PACK)
	       ENDIF
	       PACK=PACK-1
	       DECK(I1)=DECK(I1)-1
	       GOTO 10
	     ENDIF
20	   CONTINUE
	   WRITE(0,*)'FAILURE--CARD SCAN IN SUBROUTINE P'
10	 CONTINUE
	 PROB=PROB+LINE
	 COMBO=COMBO+1
C	 IF(MOD(COMBO,1000).EQ.0)WRITE(0,*)COMBO
15	 PACK=PACK1
	 DO 30 I3=1,10
	   DECK(I3)=DECK1(I3)
30	 CONTINUE
	 RETURN
	 END



	 SUBROUTINE OUT(PROB)
C
C THIS SUBROUTINE WRITES THE OUTPUT
C
	 REAL*8 PROB(10,7),TOT(7)
	 INTEGER CARD(12),PACK,DECK(10),COMBO,DECK1(10),PACK1
	 COMMON /BLOCK1/ CARD,PACK,DECK,COMBO,PACK1,DECK1
	 DATA TOT /7*0.0/
	 WRITE(31,*)'THE TOTAL NUMBER OF COMBINATIONS IS',COMBO
	 WRITE(31,*)
	 DO 10 I=1,10
	   WRITE(31,1000)I
	   WRITE(41,*)I
	   DO 20 I1=17,22
	     IF(I1.NE.22) THEN
	       WRITE(31,1010)I1,PROB(I,I1-16)*100.
	     ELSE
	       WRITE(31,1020)PROB(I,6)*100.
	     ENDIF
	     WRITE(41,*)PROB(I,I1-16)
20	   CONTINUE
	   WRITE(41,*)PROB(I,7)
	   WRITE(31,*)
10	 CONTINUE
1000	 FORMAT('DEALER''S UP CARD:',2X,I2)
1010	 FORMAT(5X,'CHANCE TO STAND ON ',I2,' IS ',F11.7)
1020  FORMAT(5X,'CHANCE TO BUST IS  ',6X,F11.7)
	 WRITE(31,*)
	 DO 50 J=1,6
	   DO 60 J1=1,10
	     IF(J1.NE.10) THEN
	       TOT(J)=PROB(J1,J)*DBLE(DECK(J1))/DBLE(PACK1)+TOT(J)
	     ELSE
	       TOT(J)=PROB(10,J)*DBLE(DECK(10))/DBLE(PACK1)+TOT(J)
	     ENDIF
60	   CONTINUE
50	 CONTINUE
	 DO 70 K=17,22
	   IF(K.NE.22) THEN
	     WRITE(31,1100)K,TOT(K-16)*100.
	     tot(k-16)=0.
	   ELSE
	     WRITE(31,1110)TOT(6)*100.
	     tot(6)=0.
	   ENDIF
70	 CONTINUE
1100	 FORMAT('DEALER TOTAL CHANCE TO STAND AT ',I2,' IS ',F11.7)
1110	 FORMAT('DEALER TOTAL CHANCE TO BUST IS ',7X,F11.7)
	 END


	SUBROUTINE PLAY
C23456--------------------------------------------------------|
	 REAL WASTE
	 REAL*8 PROB(10,7),SPM(10,10),WIN,LOSE,HPM(10,10),B
	 INTEGER PACK,DECK(10),CB
	 INTEGER PLAY1,HIT(10,10),NNBUST,NBUST,NRE
	 OPEN(22,FILE='BJDATA',STATUS='OLD')
c	 OPEN(32,FILE='ODDSOUT',STATUS='NEW')
	 OPEN(42,FILE='ODDSDATA',STATUS='NEW')
	 OPEN(52,FILE='BJOUT',STATUS='OLD')
	 DATA SPM /100*0./,HPM /100*0./,HIT /100*0/
C
C READ IN PACK AND DECK
C
	 READ(52,*)PACK
	 DO 1 NRE=1,10
	   READ(52,*)DECK(NRE)
1	 CONTINUE
c	 WRITE(32,'(11(I4,1X),//)')PACK,(DECK(L),L=1,10)
C
C READ IN DEALER ODDS CHART
C
	 DO 10 I=1,10
	   DO 20 I1=1,8
	     IF(I1.NE.1) THEN
	       READ(22,*)PROB(I,I1-1)
	     ELSE
	       READ(22,*)WASTE
	     ENDIF
20	   CONTINUE
10	 CONTINUE
C
C CALCULATE STANDING PAYOFF MATRIX (SPM),NO DEALER BLACKJACK
C
	 DO 40 J=10,1,-1
	   PLAY1=11+J
	   DO 50 J1=1,10
	     IF(PLAY1.EQ.21) THEN
	       WIN=PROB(J1,1)+PROB(J1,2)+PROB(J1,3)+PROB(J1,4)
     &          +PROB(J1,6)
	       LOSE=PROB(J1,7)
	     ELSEIF(PLAY1.EQ.20) THEN
	       WIN=PROB(J1,1)+PROB(J1,2)+PROB(J1,3)+PROB(J1,6)
	       LOSE=PROB(J1,7)+PROB(J1,5)
	     ELSEIF(PLAY1.EQ.19) THEN
	       WIN=PROB(J1,1)+PROB(J1,2)+PROB(J1,6)
	       LOSE=PROB(J1,7)+PROB(J1,5)+PROB(J1,4)
	     ELSEIF(PLAY1.EQ.18) THEN
	       WIN=PROB(J1,1)+PROB(J1,6)
	       LOSE=PROB(J1,7)+PROB(J1,5)+PROB(J1,4)+PROB(J1,3)
	     ELSEIF(PLAY1.EQ.17) THEN
	       WIN=PROB(J1,6)
	       LOSE=PROB(J1,7)+PROB(J1,5)+PROB(J1,4)+PROB(J1,3)
     &           +PROB(J1,2)            
	     ELSE
	       WIN=PROB(J1,6)
	       LOSE=PROB(J1,7)+PROB(J1,5)+PROB(J1,4)+PROB(J1,3)
     &           +PROB(J1,2)+PROB(J1,1)
	     ENDIF
	     SPM(J,J1)=WIN-LOSE
50	   CONTINUE
40	 CONTINUE
C
C SET '21' HITTING PAYOFF MATRIX (HPM) AND HIT MATRIX
C
	 DO 55 I5=1,10
	   HPM(10,I5)=SPM(10,I5)
	   HIT(10,I5)=0
55	 CONTINUE
C
C BEGIN PLAYER CARD LOOP
C
	 DO 60 K=10,1,-1
	   PLAY1=11+K
	   B=0.
	   DO 65 KK=1,(PLAY1-11)
	     CB=11-KK
	     B=B+FLOAT(DECK(CB))/FLOAT(PACK)
65	   CONTINUE
	   NBUST=PLAY1-11
 	   NNBUST=10-NBUST
C
C BEGIN DEALER CARD LOOP
C
	   DO 70 K1=1,10
	     HPM(K,K1)=-B
C
C BEGIN HPM RECURRENCE LOOP
C
	     DO 80 K2=1,NNBUST
	       IF(HIT(K+K2,K1).EQ.0) THEN
	         HPM(K,K1)=HPM(K,K1)+FLOAT(DECK(K2))/FLOAT(PACK)
     &		           *SPM(K+K2,K1)
	       ELSE
	         HPM(K,K1)=HPM(K,K1)+FLOAT(DECK(K2))/FLOAT(PACK)
     &		           *HPM(K+K2,K1)
	       ENDIF
80	     CONTINUE
C
C SET HIT MATRIX ACCORDING TO HPM AND SPM
C
	     IF(HPM(K,K1).GT.SPM(K,K1)) THEN
	       HIT(K,K1)=1
	     ELSEIF(HPM(K,K1).LT.SPM(K,K1)) THEN
	       HIT(K,K1)=0
	     ELSE
   	       HIT(K,K1)=2
	     ENDIF
70	   CONTINUE
60    CONTINUE
C
C NOW PRINT OUT ALL MATRICES
C
c	 WRITE(32,*)'PLAYER COUNT ON TOP; DEALER CARD ON SIDE'
c	 WRITE(32,*)
c	 WRITE(32,*)
c	 WRITE(32,'(8X,A)')'HARD HIT MATRIX'
c	 WRITE(32,*)
c	 WRITE(32,1020)
1020	 FORMAT(6X,'12     13     14     15     16     17'
     &         ,'     18     19     20     21')
	 DO 100 L=2,11
	   LL=L
	   IF(LL.EQ.11)LL=1
c	   WRITE(32,1000)LL,(HIT(M,LL),M=1,10)
	   WRITE(42,1000)LL,(HIT(M,LL),M=1,10)
100	 CONTINUE
1000  FORMAT(I2,10(6X,I1))
c	 WRITE(32,*)
c	 WRITE(32,*)
c	 WRITE(32,'(8X,A)')'STANDING PAYOFF MATRIX'
c	 WRITE(32,*)
c	 WRITE(32,1020)
	 DO 110 L1=2,11
	   LL1=L1
	   IF(LL1.EQ.11)LL1=1
c	   WRITE(32,1030)LL1,(SPM(M,LL1),M=1,10)
	   WRITE(42,1030)LL1,(SPM(M,LL1),M=1,10)
110	 CONTINUE
1030	 FORMAT(I2,3X,9(F6.4,1X),F6.4)
1040	 FORMAT(I2,3X,9(F6.4,1X),F6.3)
c	 WRITE(32,*)
c	 WRITE(32,*)
c	 WRITE(32,'(8X,A)')'HITTING PAYOFF MATRIX'
c	 WRITE(32,*)
c	 WRITE(32,1020)
	 DO 120 L2=2,11
	   LL2=L2
	   IF(LL2.EQ.11)LL2=1
c	   WRITE(32,1040)LL2,(HPM(M,LL2),M=1,10)
	   WRITE(42,1040)LL2,(HPM(M,LL2),M=1,10)
120	 CONTINUE
	 CLOSE(22,status='delete')
c	 CLOSE(32)
	 CLOSE(42)
	 CLOSE(52)
	 RETURN
	 END


	SUBROUTINE SOFT(sh1)
	 REAL SPM(10,10),HPM(10,10),DOUBLE(4,10)
	 INTEGER HIT(4,10),DECK(10),PACK,IND,HDATA
	 integer sh1(4,10)
	 OPEN(33,FILE='ODDSDATA',STATUS='OLD')
	 OPEN(43,FILE='SOFTHIT',STATUS='NEW')
	 open(76,file='SHIT1',status='scratch')
	 OPEN(15,FILE='BJOUT',STATUS='OLD')
500	 FORMAT('PLAYER   DEALER    SOFT   PAYOFF     STAND PAYOFF')
	 DATA HIT /40*0/,DOUBLE /40*0./
C
C	 READ PACK/DECK INFO
C
	 READ(15,*)PACK
	 DO 2 HDATA=1,10
	   READ(15,*)DECK(HDATA)
2	 CONTINUE
C
C	 READ SPM/HPM DATA
C
	 DO 3 J=1,10
	   READ(33,*)NOT,(SPM(L,J),L=1,10)
3	 CONTINUE
	 DO 10 I=1,10
	   READ(33,*)NOT,(SPM(L,I),L=1,10)
10	 CONTINUE
	 DO 20 I1=1,10
	   READ(33,*)NOT,(HPM(L,I1),L=1,10)
20	 CONTINUE
	 DECK(1)=DECK(1)-1
	 DO 30 K=4,1,-1
	 DECK(K+5)=DECK(K+5)-1
	   KK=16+K
	   DO 40 K1=1,10
	     IF(K1.EQ.10)THEN
	       DECK(1)=DECK(1)-1
	       GOTO 25
	     ENDIF
	     DECK(K1+1)=DECK(K1+1)-1
25        DO 50 K2=1,10
	       IND=21-KK+K2
	       IF(IND.GT.10)IND=IND-10
	       DOUBLE(K,K1)=DOUBLE(K,K1)+AMAX1(SPM(K2,K1),HPM(K2,K1))
     &        *FLOAT(DECK(IND))/FLOAT(PACK-3)
50	     CONTINUE
C	    WRITE(53,501)KK,K1+1,DOUBLE(K,K1),SPM(K+5,K1)
	     WRITE(43,'(F10.7,1X,F10.7)')DOUBLE(K,K1),SPM(K+5,K1)
501	 FORMAT(2X,I2,6X,I2,9X,F10.7,7X,F10.7)
	     IF(DOUBLE(K,K1).GE.SPM(K+5,K1)) THEN
	       HIT(K,K1)=1
	       sh1(k,k1)=1
	     ELSE
	       HIT(K,K1)=0
	       sh1(k,k1)=0
	     ENDIF
	     IF(K1.EQ.10)THEN
	       DECK(1)=DECK(1)+1
	       GOTO 40
	     ENDIF
	     DECK(K1+1)=DECK(K1+1)+1
40	   CONTINUE
	   DECK(K+5)=DECK(K+5)+1
30	 CONTINUE
C
C PRINT OUT HIT TABLE AND STORE IN SOFTHIT
C
c	 WRITE(53,'(8X,A)')'SOFT HIT MATRIX'
c	 WRITE(53,*)
c	 WRITE(53,'(6X,''17   18   19   20'')')
	 DO 100 L=1,10
	   LL=L+1
	   IF(LL.EQ.11)LL=1
c	   WRITE(53,1000)LL,(HIT(M,L),M=1,4)
	   write(76,'(4(1x,i1))')(hit(m,l),m=1,4)
	   WRITE(43,'(4(I1,1X))')(HIT(M,L),M=1,4)
100	 CONTINUE
1000	 FORMAT(I2,4(4X,I1))
	 CLOSE(33)
	 CLOSE(43)
c	 CLOSE(53)
	 CLOSE(15)
	 do 27 jj=1,4
	   do 29 jj1=1,10
	     double(jj,jj1)=0.0
	     hit(jj,jj1)=0
29	   continue
27	 continue
	 RETURN
	 END


	SUBROUTINE DOUB(x1)
	 REAL SPM(10,10),HPM(10,10),HIT1(4,10),HIT2(4,10),BPM(10,10)
	 REAL A,B
	 INTEGER DOUBLE1(4,10),HDATA,IND,PACK,DECK(10),TOT,KEY,Z,P
	 integer x1(4,10)
	 OPEN(34,FILE='ODDSDATA',STATUS='OLD')
	 OPEN(44,FILE='DBBL',STATUS='new')
	 OPEN(16,FILE='BJOUT',STATUS='OLD')
	 open(72,file='HDOUB1',status='scratch')
	 DATA HIT1 /40*0./,HIT2 /40*0./,DOUBLE1 /40*0/
C
C	 READ IN CARD/DECK INFO
C
	 READ(16,*)PACK
	 DO 2 HDATA=1,10
	   READ(16,*)DECK(HDATA)
2	 CONTINUE
C
C	 READ IN SPM,HPM 
C
C     WRITE(54,500)
	 DO 3 J=1,10
	   READ(34,*)NOT,(SPM(L,J),L=1,10)
3	 CONTINUE
	 DO 10 I=1,10
	   READ(34,*)NOT,(SPM(L,I),L=1,10)
10	 CONTINUE
	 DO 20 I1=1,10
	   READ(34,*)NOT,(HPM(L,I1),L=1,10)
20	 CONTINUE
	 DO 21 I2=1,10
	   DO 22 I3=1,10
	     IF(HPM(I2,I3).GE.SPM(I2,I3)) THEN
	       BPM(I2,I3)=HPM(I2,I3)
	     ELSE
	       BPM(I2,I3)=SPM(I2,I3)
	     ENDIF
22	   CONTINUE
21	 CONTINUE
	 DO 15 KEY=11,8,-1
	   Z=12-KEY
	 DO 30 K=2,9
	   DECK(K)=DECK(K)-1
	   DO 40 JK=2,9
	     TOT=K+JK
	     DECK(JK)=DECK(JK)-1
        IF(TOT.EQ.KEY)THEN
	     IF(TOT.EQ.8)THEN
	       N=8
	     ELSEIF(TOT.EQ.9)THEN
	       N=9
	     ELSEIF(TOT.GE.10)THEN
	       N=10
	     ENDIF
	   ELSE
	       GOTO 39
        ENDIF
	     DO 45 K1=1,10
	       IF(K1.EQ.10)THEN
	         DECK(1)=DECK(1)-1
	         GOTO 48
	       ENDIF
	       DECK(K1+1)=DECK(K1+1)-1
48	     DO 50 K2=1,N
	       IND=K2+11-TOT
	       IF(IND.EQ.11)IND=1
	       HIT1(Z,K1)=HIT1(Z,K1)+SPM(K2,K1)*FLOAT(DECK(IND))/FLOAT
     &                 (PACK-3)
	       HIT2(Z,K1)=HIT2(Z,K1)+BPM(K2,K1)*FLOAT(DECK(IND))/FLOAT
     &                 (PACK-3)
50	     CONTINUE
	         A=HIT2(2,K1)
	         B=HIT2(1,K1)
	     IF(TOT.EQ.9)THEN
	       HIT1(Z,K1)=HIT1(Z,K1)+SPM(2,K1)*FLOAT(DECK(2))/FLOAT
     &          (PACK-3)
	       HIT2(Z,K1)=HIT2(Z,K1)+B*FLOAT(DECK(2))/FLOAT(PACK-3)
	     ELSEIF(TOT.EQ.8)THEN
	       HIT1(Z,K1)=HIT1(Z,K1)+SPM(2,K1)*FLOAT(DECK(3)+DECK(2))/
     &          FLOAT(PACK-3)
	       HIT2(Z,K1)=HIT2(Z,K1)+(A*FLOAT(DECK(2))+B*FLOAT(DECK(3)
     &       ))/FLOAT(PACK-3)
	     ENDIF
500	 FORMAT('PLAYER  DEALER    DOUBLE PAYOFF    HITTING PAYOFF')
501	 FORMAT(2X,I2,6X,I2,9X,F10.7,7X,F10.7)
	     IF(K1.EQ.10)THEN
	       DECK(1)=DECK(1)+1
	       GOTO 45
	     ENDIF
	     DECK(K1+1)=DECK(K1+1)+1
45	   CONTINUE
39  	   DECK(JK)=DECK(JK)+1
40	 CONTINUE
	 DECK(K)=DECK(K)+1
30	 CONTINUE
	 DO 800 P=1,10
	   HIT1(Z,P)=HIT1(Z,P)/FLOAT(KEY-3)
	   HIT2(Z,P)=HIT2(Z,P)/FLOAT(KEY-3)
	   IF(P.EQ.10)THEN
	     HIT1(Z,P)=HIT1(Z,P)*2.+FLOAT(DECK(10))/FLOAT(PACK-3)
	   ELSEIF(P.EQ.9)THEN
	     HIT1(Z,P)=HIT1(Z,P)*2.+FLOAT(DECK(1))/FLOAT(PACK-3)
	   ELSE
	     HIT1(Z,P)=HIT1(Z,P)*2.
	   ENDIF
C	   WRITE(54,501)KEY,P+1,HIT1(Z,P),HIT2(Z,P)
	   WRITE(44,'(F10.7,1X,F10.7)')HIT1(Z,P),HIT2(Z,P)
	   IF(HIT1(Z,P).GE.HIT2(Z,P) .AND. HIT1(Z,P).GE.0.)THEN
	     DOUBLE1(Z,P)=1
	     x1(5-z,p)=1
	   ELSE
	     DOUBLE1(Z,P)=0
	     x1(5-z,p)=0
	   ENDIF
800	 CONTINUE
15	 CONTINUE
C
C PRINT OUT DOUBLE TABLE AND STORE IN DOUBLE
C
c	 WRITE(54,'(8X,A)')'DOUBLE MATRIX'
c	 WRITE(54,*)
c	 WRITE(54,'(6X,''11   10   9   8'')')
	 DO 100 L=1,10
	   LL=L+1
	   IF(LL.EQ.11)LL=1
c	   WRITE(54,1000)LL,(DOUBLE1(M,L),M=1,4)
	   write(72,'(4(1x,i1))')(double1(m,l),m=4,1,-1)
	   WRITE(44,'(4(I1,1X))')(DOUBLE1(M,L),M=1,4)
100	 CONTINUE
1000	 FORMAT(I2,4(4X,I1))
	 CLOSE(34)
	 CLOSE(44)
	 CLOSE(16)
	 do 23 jj=1,4
	   do 27 jj1=1,10
	     double1(jj,jj1)=0.0
	     hit1(jj,jj1)=0
	     hit2(jj,jj1)=0
27	   continue
23	 continue
	 RETURN
	 END


	subroutine pyb(cbc,sc,win,bet)
c23456-------------2---------3---------4---------5---------6---------7--!
	real bet,cbc,win,sc
	if(sc.le.-8.0 .or. cbc.lt.-2.0)then
c	if(win.le.-0.010 .or. sc.le.-13.0)then
	  bet=-1.
	  goto 100
	elseif(cbc.lt.1.5)then
c	elseif(win.le.0.004)then
	  bet=1.
	elseif(cbc.lt.3.0)then
c	elseif(win.le.0.010)then
	  bet=2.
	elseif(cbc.lt.4.5)then
c	elseif(win.le.0.015)then
	  bet=3.
	elseif(cbc.lt.6.0)then
c	elseif(win.le.0.020)then
	  bet=4.
	elseif(cbc.lt.7.5)then
c	elseif(win.le.0.025)then
	  bet=5.
	elseif(cbc.lt.9.0)then
c	elseif(win.le.0.035)then
	  bet=7.
	elseif(cbc.lt.11.0)then
c	elseif(win.le.0.050)then
	  bet=10.
	elseif(cbc.lt.13.0)then
c	elseif(win.le.0.065)then
	  bet=15.
	else
	  bet=20.
	endif
	bet=bet*5.
100	write(0,'(1x,i3)')int(bet)
	return
	end


	SUBROUTINE F(awin)
c23456-------------2---------3---------4---------5---------6---------7--!
	 REAL*8 PROB(10,10,10),WIN,WINNS,CA,CB,CC,PROB7(10,10,10)
	 REAL HPM(10,10),SPM(10,10),BPM(10,10),A,B,SOFTPAY(4,10)
	 REAL DOUBLE(4,10),D(9,10),SPLIT(10,10),awin
	 INTEGER CARD(12),N,DECK(10),PACK,PACK1,DECK1(10),TOTAL
	 INTEGER SOFT,HD(9,10),HS(10,10),HDATA
	 integer x1(4,10),sh1(4,10)
	 integer su1(9,10),sp1(10,10),bpm1(9,10),sx1(9,10)
	 common /block6/ bpm1,x1,sx1,sp1,su1,sh1
	 COMMON /BLOCK2/ CARD,PACK,DECK,PACK1,DECK1
	 OPEN (35,FILE='ODDSDATA',STATUS='OLD')
	 OPEN (45,FILE='SOFTHIT',STATUS='OLD')
	 OPEN (55,FILE='DBBL',STATUS='OLD')
	 OPEN (20,FILE='FINOUT',STATUS='NEW')
	 OPEN (21,FILE='FINDATA',STATUS='NEW')
	 OPEN (23,FILE='SOFTDOUB',STATUS='NEW')
	 OPEN (24,FILE='SPLIT',STATUS='NEW')
         OPEN(25,FILE='BJOUT',STATUS='OLD')
	 open(71,file='BPAYM1',status='scratch')
	 open(73,file='SDOB1',status='scratch')
	 open(74,file='SPLAT1',status='scratch')
	 open(75,file='SUR1',status='scratch')
	 DATA SPLIT /100*0./,D /90*0./,HD /90*0/,HS /100*0/
	 DATA WINNS /0./, su1 /90*0/, bpm1 /90*0/
	 WIN=0.
C
C READ IN PACK/DECK INFO
C
	 READ(25,*)PACK1
	 PACK=PACK1
	 DO 2 HDATA=1,10
	   READ(25,*)DECK1(HDATA)
	   DECK(HDATA)=DECK1(HDATA)
2	 CONTINUE
C
C READ HPM,SPM AND CONVERT TO BPM
C
	 DO 100 J=1,10
	    READ(35,*)NOT,(SPM(L,J),L=1,10)
100	 CONTINUE
	 DO 110 J1=1,10
	   READ(35,*)NOT,(SPM(L,J1),L=1,10)
110	 CONTINUE
	 DO 120 J2=1,10
	   READ(35,*)NOT,(HPM(L,J2),L=1,10)
120	 CONTINUE
	 DO 130 J3=1,10
	   DO 140 J4=1,10
	     BPM(J3,J4)=AMAX1(HPM(J3,J4),SPM(J3,J4))
	     if(j3.eq.10)goto 140
	     if(hpm(j3,j4).ge.spm(j3,j4))then
	       bpm1(j3,j4)=1
	     else
	       bpm1(j3,j4)=0
	     endif
140	   CONTINUE
130	 CONTINUE
	 do 135 j8=1,10
	   write(71,'(9(1x,i1))')(bpm1(i,j8),i=1,9)
135	 continue
C
C READ IN SOFT PAYOFFS AND CONVERT
C
	 DO 200 K=4,1,-1
	   DO 210 K1=1,10
	     READ(45,*)A,B
	     SOFTPAY(K,K1)=AMAX1(A,B)
210	   CONTINUE
200	 CONTINUE
C
C READ DOUBLE PAYOFFS AND CONVERT
C
	 DO 300 K2=4,1,-1
	   DO 310 K3=1,10
	     READ(55,*)A,B
	     DOUBLE(K2,K3)=AMAX1(A,B)
310	   CONTINUE
300 	 CONTINUE
C
C
C
	 CARD(1)=1
	 CARD(2)=1
	 N=2
C
10    TOTAL=0
	 DO 20 I1=1,N
	   TOTAL=TOTAL+CARD(I1)
20	 CONTINUE
	 IF((N.EQ.2.AND.TOTAL.EQ.11).AND.(CARD(1).EQ.1.OR
     &   .CARD(1).EQ.10))GOTO 70
30	 SOFT=0
	 DO 40 I2=1,N
	   IF(CARD(I2).EQ.1.AND.TOTAL.LE.11) THEN
	     SOFT=1
	     TOTAL=TOTAL+10
	     GOTO 45
	   ENDIF
40	 CONTINUE
C
45	 DO 50 I3=1,10
60	   IF(TOTAL.GE.12.AND.SOFT.EQ.0) THEN
	     CALL PZ(N,PROB(CARD(1),CARD(2),I3),TOTAL,
     &           BPM(TOTAL-11,I3))
	   ELSEIF(TOTAL.LT.12.AND.SOFT.EQ.0) THEN
	     IF(N.EQ.2.AND.TOTAL.GE.8) THEN
	       CALL PZ(N,PROB(CARD(1),CARD(2),I3),TOTAL,
     &             DOUBLE(TOTAL-7,I3))
	     ELSE
	       N=N+1
	       CARD(N)=1
	       GOTO 10
	     ENDIF
	   ELSEIF(TOTAL.GE.17.AND.TOTAL.LT.21.AND.SOFT.EQ.1) THEN
	     CALL PZ(N,PROB(CARD(1),CARD(2),I3),TOTAL,
     &           SOFTPAY(TOTAL-16,I3))
	   ELSEIF(TOTAL.LT.17.AND.SOFT.EQ.1) THEN
	     N=N+1
	     CARD(N)=1
	     GOTO 10
	   ELSEIF(TOTAL.EQ.21) THEN
	     CALL PZ(N,PROB(CARD(1),CARD(2),I3),TOTAL,
     &           BPM(10,I3))
	   ELSEIF(TOTAL.GT.21) THEN
	     TOTAL=TOTAL-10
	     SOFT=0
	     GOTO 60
	   ELSE
	     WRITE(0,*)'FAILURE IN CHOOSER LOOP'
	   ENDIF
50	 CONTINUE
70	 IF(CARD(N).NE.10) THEN
	   CARD(N)=CARD(N)+1
	   GOTO 10
	 ELSE
	   CARD(N)=0
	   N=N-1
	   IF(N.NE.0)GOTO 70
	 ENDIF
C
C CALCULATE SOFT DOUBLING PAYOFFS
C
	 DO 150 M1=1,9
	   DECK(1)=DECK(1)-1
	   DECK(M1)=DECK(M1)-1
	   DO 160 M2=1,10
	      DECK(M2)=DECK(M2)-1
	      DO 170 M3=1,10
	         IF(M3.GT.M1)IN=M3-M1
		 IF(M3.LE.M1)IN=10+(M3-M1)
	         D(M1,M2)=D(M1,M2)+DBLE(DECK(IN))/DBLE(PACK-3)
     &                     *SPM(M3,M2)
170	     CONTINUE
	     D(M1,M2)=2.*D(M1,M2)
	     IF(M2.EQ.10) THEN
	       D(M1,M2)=D(M1,M2)+DBLE(DECK(10))/DBLE(PACK-3)
	     ELSEIF(M2.EQ.9) THEN
	       D(M1,M2)=D(M1,M2)+DBLE(DECK(1))/DBLE(PACK-3)
	     ENDIF
	     DECK(M2)=DECK(M2)+1
160	   CONTINUE
	   DECK(1)=DECK(1)+1
	   DECK(M1)=DECK(M1)+1
150	 CONTINUE
C
C CALCULATE AND PRINT SOFT DOUBLE MATRIX
C
	 WRITE(23,*)'SOFT DOUBLE MATRIX'
	 WRITE(23,*)
	 WRITE(23,173)
	 DO 172 M6=1,9
	   DO 174 M7=1,10
	     IF(D(M6,M7).GE.PROB(1,M6,M7)) THEN
	       HD(M6,M7)=1
	       sx1(m6,m7)=1
	     ELSE
	       HD(M6,M7)=0
	       sx1(m6,m7)=0
	     ENDIF
174	   CONTINUE
172	 CONTINUE
	 DO 176 M8=1,9
           WRITE(23,177)M8,(D(M8,L),L=1,10)
	   WRITE(23,178)(PROB(1,M8,L),L=1,10)
	   WRITE(23,1790)M8,(HD(M8,L),L=1,10)
176	 CONTINUE
	 do 185 kd=1,10
	   write(73,'(9(1x,i1))')(hd(kb,kd),kb=1,9)
185	 continue
173	 FORMAT(7X,'2      3      4      5      6      7      8',
     &       '      9      10     ACE')
177	 FORMAT('1',1X,I1,1X,F6.4,9(1X,F6.4))
178	 FORMAT(4X,F6.4,9(1X,F6.4))
1790  FORMAT('1',1X,I1,4X,I1,9(6X,I1))
C
C COMPARE TO PROB TABLES AND CONVERT, IF NECESSARY
C
	 DO 180 M4=1,9
	   DO 190 M5=1,10
	     A=PROB(1,M4,M5)
	     PROB(1,M4,M5)=AMAX1(A,D(M4,M5))
190	   CONTINUE
180	 CONTINUE
C
C CALCULATE SPLIT PAYOFFS
C
	 DO 219 L=1,10
	   DECK(L)=DECK(L)-2
	   DO 220 L1=1,10
	     DECK(L1)=DECK(L1)-1
	     DO 230 L2=1,10
	       IF(L.EQ.1) THEN
	         SPLIT(L,L1)=SPLIT(L,L1)+DBLE(DECK(L2))/DBLE(PACK-3)
     &                    *SPM(L2,L1)
	         ELSE
	           SPLIT(L,L1)=SPLIT(L,L1)+DBLE(DECK(L2))/DBLE(PACK-3)
     &                      *PROB(L,L2,L1)
	         ENDIF
230	     CONTINUE
	     SPLIT(L,L1)=2.*SPLIT(L,L1)
	     IF(L1.EQ.10) THEN
	       SPLIT(L,L1)=SPLIT(L,L1)+DBLE(DECK(10))/DBLE(PACK-3)
	     ELSEIF(L1.EQ.9) THEN
	       SPLIT(L,L1)=SPLIT(L,L1)+DBLE(DECK(1))/DBLE(PACK-3)
	     ENDIF
	     DECK(L1)=DECK(L1)+1
220	   CONTINUE
	   DECK(L)=DECK(L)+2
219	 CONTINUE
C
C CALCULATE AND WRITE SPLIT PAYOFFS AND MATRIX
C
	 WRITE(24,*)'SPLIT MATRIX'
	 WRITE(24,*)
	 WRITE(24,211)
	 DO 232 L3=1,10
	   DO 234 L4=1,10
	     IF(SPLIT(L3,L4).GE.PROB(L3,L3,L4)) THEN
	       HS(L3,L4)=1
	       sp1(l3,l4)=1
	     ELSE
	       HS(L3,L4)=0
	       sp1(l3,l4)=0
	     ENDIF
234	   CONTINUE
232	 CONTINUE
	 DO 236 L5=1,10
	  DO 237 LLMM=1,10
	     VLA=SPLIT(L5,LLMM)
	     SPLIT(L5,LLMM)=AMAX1(VLA,-.9998)
237	  CONTINUE
	  WRITE(24,212)L5,L5,(SPLIT(L5,L),L=1,10)
	  WRITE(24,213)(PROB(L5,L5,L),L=1,10)
	   WRITE(24,2140)L5,L5,(HS(L5,L),L=1,10)
236	 CONTINUE
	 do 235 ke=1,10
	   write(74,'(10(1x,i1))')(hs(kb,ke),kb=1,10)
235	 continue
211	 FORMAT(9X,'2      3      4      5      6      7      8'
     &       ,'      9      10     ACE')
212	 FORMAT(I2,1X,I2,1X,F6.4,9(1X,F6.4))
213	 FORMAT(6X,F6.4,9(1X,F6.4))
214	 FORMAT(9X,I1,9(6X,I1),/)
2140	 FORMAT(I2,1X,I2,4X,I1,9(6X,I1))
C
C ALTER PROB MATRIX LAST TIME INCLUDING SURRENDER AND PLAYER BJ
C BUT FIRST COPY PROB() INTO PROB7()
C
	DO 1520 LLM=1,10
	  DO 1525 LLM1=1,10
	    DO 1530 LLM2=1,10
	      PROB7(LLM,LLM1,LLM2)=PROB(LLM,LLM1,LLM2)
1530	    CONTINUE
1525	  CONTINUE
1520	CONTINUE

	 WRITE(24,*)
	 WRITE(24,*)
	 DO 242 L6=1,10
	   DECK(L6)=DECK(L6)-1
	   DO 244 L7=1,10
	     DECK(L7)=DECK(L7)-1
	     DO 246 L8=1,10
	       A=PROB(L6,L6,L8)
	       PROB(L6,L6,L8)=AMAX1(A,SPLIT(L6,L8))
	       PROB7(L6,L6,L8)=AMAX1(A,SPLIT(L6,L8))
	       DECK(L8)=DECK(L8)-1
	       L81=L8
	       IF(L81.EQ.1)L81=11
	       B=PROB(L6,L7,L81-1)
	       IF(L81-1.EQ.9) THEN
	         C=-.5*(1+DBLE(DECK(1))/DBLE(PACK-3))
	       ELSEIF(L81-1.EQ.10) THEN
		 C=-.5*(1+DBLE(DECK(10))/DBLE(PACK-3))
	       ELSE
		 C=-.5000
	       ENDIF
	       IF(C.GT.PROB(L6,L7,L81-1))then
	         WRITE(24,*)'SURRENDER A ',L6,L7,' ON A ',L81
	         su1(L6+L7-11,L81-1)=1
	       ELSE
	         SU1(L6+L7-11,L81-1)=0
	       ENDIF
	       PROB(L6,L7,L81-1)=AMAX1(B,C)
	       DECK(L8)=DECK(L8)+1
246	     CONTINUE
	     DECK(L7)=DECK(L7)+1
244	   CONTINUE
	   DECK(L6)=DECK(L6)+1
242	 CONTINUE
	 do 303 kf=1,10
	   write(75,'(9(1x,i1))')(su1(kb,kf),kb=1,9)
303	 continue
C THAT WAS SURRENDER, THIS IS BJ
	 DO 245 JB=1,10
	   C=1.5
	   IF(JB.EQ.9)C=1.5*(1.-DBLE(DECK(1)-1)/DBLE(PACK-3))
	   IF(JB.EQ.10)C=1.5*(1.-DBLE(DECK(10)-1)/DBLE(PACK-3))
	   PROB7(1,10,JB)=C
	   PROB7(10,1,JB)=C
	   PROB(1,10,JB)=C
	   PROB(10,1,JB)=C
245	 CONTINUE
C
C NOW CALCULATE TOTAL ODDS
C
	 DO 3000 MA=1,10
	   CA=DBLE(DECK(MA))/DBLE(PACK)
	   IF(DECK(MA).LT.0.)CA=0.
	   DECK(MA)=DECK(MA)-1
	   DO 3100 MB=1,10
	     CB=DBLE(DECK(MB))/DBLE(PACK-1)
	     IF(DECK(MB).LT.0.)CB=0.
	     DECK(MB)=DECK(MB)-1
	     DO 3200 MC=1,10
	       mc1=mc
	       CC=DBLE(DECK(MC1))/DBLE(PACK-2)
	       IF(DECK(MC1).LT.0.)CC=0.
	       if(mc1.eq.1)mc1=11
	       WIN=WIN+(CA*CB*CC*PROB(MA,MB,MC1-1))
	       WINNS=WINNS+(CA*CB*CC*PROB7(MA,MB,MC1-1))
3200      CONTINUE
	     DECK(MB)=DECK(MB)+1
3100    CONTINUE
	  DECK(MA)=DECK(MA)+1
3000  CONTINUE
	 awin=WIN
	 IF(WIN.GE.0. .AND. WINNS.GE.0.)THEN
	   WRITE(0,3050)WIN,WINNS
	   WRITE(20,3050)WIN,WINNS
	 ELSEIF(WIN.GE.0. .AND. WINNS.LT.0.)THEN
	   WRITE(0,3055)WIN,WINNS
	   WRITE(20,3055)WIN,WINNS
	 ELSE
	   WRITE(0,3059)WIN,WINNS
	   WRITE(20,3059)WIN,WINNS
	 ENDIF
	 WRITE(20,*)
3050	FORMAT(1X,'RETURN W/  SURRENDER IS +',F9.5,/,
     &         1X,'RETURN W/O SURRENDER IS +',F9.5)
3055	FORMAT(1X,'RETURN W/  SURRENDER IS +',F9.5,/,
     &         1X,'RETURN W/O SURRENDER IS  ',F9.5)
3059	FORMAT(1X,'RETURN W/  SURRENDER IS  ',F9.5,/,
     &         1X,'RETURN W/O SURRENDER IS  ',F9.5)
C
C PRINT OUT RESULTS AS FUNCTION OF 1ST 2 CARDS AND DEALER'S CARD
C
	 WRITE(20,*)'DEALER''S CARD ON TOP; PLAYER''S CARDS ON SIDE'
	 WRITE(20,*)
	 WRITE(20,'(''#1 #2'',5X,''2      3      4      5      6'',
     &           ''      7      8      9      10     ACE'')')
	 DO 500 M=1,10
	   DO 510 M1=M,10
	     WRITE(20,1000)M,M1,(PROB(M,M1,L),L=1,10)
	     WRITE(21,1010)(PROB(M,M1,L),L=1,10)
510	   CONTINUE
500	 CONTINUE
1000	 FORMAT(I2,1X,I2,2X,F6.4,9(1X,F6.4))
1010	 FORMAT(F6.4,9(1X,F6.4))
	 CLOSE(35,status='delete')
	 CLOSE(45,status='delete')
	 CLOSE(55,status='delete')
	 CLOSE(20,status='delete')
	 CLOSE(21,status='delete')
	 CLOSE(23,status='delete')
	 CLOSE(24,status='delete')
	 CLOSE(25,status='delete')
         CLOSE(41,status='delete')
	 do 1701 jz=1,10
	   do 1702 jz1=1,4
	     softpay(jz1,jz)=0.0
	     double(jz1,jz)=0.0
1702	   continue
	   do 1703 jz2=1,9
	     d(jz2,jz)=0.0
1703	   continue
	   do 1704 jz3=1,10
	     split(jz3,jz)=0.0
	     do 1705 jz4=1,10
	       prob(jz4,jz3,jz)=0.0
	       prob7(jz4,jz3,jz)=0.0
1705	     continue
1704	   continue
1701	 continue
	 win=0.0
	 winns=0.0
	 RETURN
	 END
C
C
C
	 SUBROUTINE PZ(N,PROB,TOTAL,BEST)
C
C THIS SUBROUTINE CALCULATES LINE PROBABILITIES
C
	 INTEGER N,CARD(12),DECK(10),PACK,PACK1,DECK1(10),TOTAL
	 REAL*8 PROB,LINE
	 REAL BEST
	 COMMON /BLOCK2/ CARD,PACK,DECK,PACK1,DECK1
	 LINE=1.0
C
C CALCULATE LINE PROB'S AND ALTER DECK
C
	 DO 10 I=1,N
	   IF(I.EQ.1 .OR. I.EQ.2) THEN
	     LINE=1.0
	     GOTO 5
	   ENDIF
	   DO 20 I1=1,10
	     IF(CARD(I).EQ.I1) THEN
	         IF(DECK(I1).LE.0)GOTO 40
	         LINE=LINE*DBLE(DECK(I1))/DBLE(PACK)
	         GOTO 5
	     ENDIF
20	   CONTINUE
	   WRITE(0,*)'FAILURE--CARD SCAN IN SUBROUTINE P'
5	   PACK=PACK-1
	   DECK(I1)=DECK(I1)-1
10	 CONTINUE
	 PROB=PROB+LINE*BEST
40    PACK=PACK1
	 DO 30 I3=1,10
	   DECK(I3)=DECK1(I3)
30	 CONTINUE
	 RETURN
	 END

	subroutine chooser(np,d,p,s,ind,choice)
c23456-------------2---------3---------4---------5---------6---------7--!
	common /block6/ bpm1,x1,sx1,sp1,su1,sh1
	integer x1(4,10),sx1(9,10),sp1(10,10),su1(9,10),sh1(4,10)
	integer p16,p11,d1,s,d,p(12),ind,np,pt,choice,bpm1(9,10)

	call tot(p,np,pt)
	if(d.eq.1)d=11
	p16=pt-16
	p11=pt-11
	d1=d-1

	if(np.ne.2)then
	  if(s.eq.0)then
100	    if(pt.le.11)then
	      choice=1
	    elseif(bpm1(p11,d1).eq.1)then
	      choice=1
	    else
	      choice=2
	    endif
	  elseif(s.eq.1)then
120	    if(pt.le.16)then
	      choice=1
	    else
110	      if(sh1(p16,d1).eq.1)then
	        choice=1
	      else
	        choice=2
	      endif
	    endif
	  endif
	elseif(np.eq.2)then
	  if(ind.eq.1 .or. ind.eq.2)then
	    if(s.eq.0)then
	      if(pt.ge.8 .and. pt.le.11)then
130	        if(x1(pt-7,d1).eq.1)then
	          choice=3
	        else
	          choice=1
	        endif
	      elseif(pt.le.7)then
	        choice=1
	      else
	        if(bpm1(p11,d1).eq.1)then
	          choice=1
	        else
	          choice=2
	        endif
	      endif
	    elseif(s.eq.1)then
	      if(sx1(p11,d1).eq.1)then
	        choice=3
	      else
	        if(pt.le.16)then
	          choice=1
	        elseif(sh1(p16,d1).eq.1)then
		  choice=1
	        else
		  choice=2
	        endif
	      endif
	    endif
	  elseif(ind.eq.0)then
	    if(s.eq.0)then
	      if(su1(p11,d1).eq.1 .and. pt.ge.12)then
	        choice=5
	      elseif(p(1).eq.p(2) .and. sp1(p(1),d1).eq.1)then
	        choice=4
	      elseif(pt.ge.8 .and. pt.le.11)then
		if(x1(pt-7,d1).eq.1)then
		  choice=3
		else
	          choice=1
		endif
	      elseif(pt.le.7)then
	        choice=1
	      else
		if(bpm1(p11,d1).eq.1)then
		  choice=1
		else
		  choice=2
	        endif
	      endif
	    elseif(s.eq.1)then
	      if(p(1).eq.p(2) .and. sp1(p(1),d1).eq.1)then
	        choice=4
	      elseif(sx1(p11,d1).eq.1)then
	        choice=3
	      else
		if(pt.le.16)then
		  choice=1
		else
		  if(sh1(p16,d1).eq.1)then
		    choice=1
		  else
		    choice=2
		  endif
		endif
	      endif
	    endif
	  endif
	endif
	if(d.eq.11)d=1
	s=0
	return
	end

