Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program blackjack
- !CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
- !C C
- !C PROGRAM NAME: SHUFFLE C
- !C C
- !C INPUTS: C
- !C C
- !C OUTPUTS: C
- !C C
- !CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
- IMPLICIT NONE
- character(len=11) cards(52), player_cards(13), dealer_cards(13)
- character(len=1) parse(572)
- logical player_busted, dealer_busted, bj
- integer player_score, dealer_score
- equivalence(parse(1),cards(1)),(player_cards(1),cards(1)),(dealer_cards(1),cards(13))
- data cards/' 2-hearts ',' 3-hearts ',' 4-hearts ',' 5-hearts ',&
- ' 6-hearts ',' 7-hearts ',' 8-hearts ',' 9-hearts ',&
- '10-hearts ',' J-hearts ',' Q-hearts ',' K-hearts ',&
- ' A-hearts ', &
- ' 2-clubs ',' 3-clubs ',' 4-clubs ',' 5-clubs ',&
- ' 6-clubs ',' 7-clubs ',' 8-clubs ',' 9-clubs ',&
- '10-clubs ',' J-clubs ',' Q-clubs ',' K-clubs ',&
- ' A-clubs ', &
- ' 2-spades ',' 3-spades ',' 4-spades ',' 5-spades ',&
- ' 6-spades ',' 7-spades ',' 8-spades ',' 9-spades ',&
- '10-spades ',' J-spades ',' Q-spades ',' K-spades ',&
- ' A-spades ', &
- ' 2-diamonds',' 3-diamonds',' 4-diamonds',' 5-diamonds',&
- ' 6-diamonds',' 7-diamonds',' 8-diamonds',' 9-diamonds',&
- '10-diamonds',' J-diamonds',' Q-diamonds',' K-diamonds',&
- ' A-diamonds'/
- player_busted = .false.
- dealer_busted = .false.
- call knuth_shuffle(cards)
- call player_play(player_cards,parse, player_busted, player_score)
- If (player_score .gt. 21) then
- print *, "Player has busted with a score of", player_score
- else
- call dealer_play(dealer_cards,parse, dealer_busted, dealer_score, bj)
- endif
- end program blackjack
- subroutine knuth_shuffle(cards)
- !CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
- !C C
- !C PROGRAM NAME: KNUTH_SHUFFLE C
- !C C
- !C INPUTS: CARDS C
- !C C
- !C OUTPUTS: CARDS C
- !C C
- !CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
- implicit none
- integer lo, card
- character(len=11) cards(52), temp
- real rn
- call random_seed()
- do lo=52,1,-1
- call random_number(rn)
- card=int(rn*(lo+1))
- if(card.le.0) card = 1
- if(card.ge.52) card = 52
- if(card.ne.lo) then
- temp = cards(card)
- cards(card) = cards(lo)
- cards(lo) = temp
- endif
- enddo
- return
- end subroutine knuth_shuffle
- subroutine dealer_play(dealer_cards,parse, dealer_busted, isum, bj)
- !CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
- !C C
- !C PROGRAM NAME: DEALER_PLAY C
- !C C
- !C INPUTS: DEALER_CARDS C
- !C C
- !C OUTPUTS: <none> C
- !C C
- !CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
- implicit none
- integer i,j,ival1, ival2, ace, isum,ipos1,ipos2
- character(len=11) dealer_cards(13)
- character(len=1) parse(572)
- logical dealer_busted, bj
- ace = 0
- isum = 0
- ipos1=133
- ipos2=134
- ! turn over first two cards
- write(*,'("DEALER IS DEALT: ",A11,5x,A11)') dealer_cards(1), dealer_cards(2)
- !
- ! Parse the cards
- !
- ! First card
- ! i and j are the ASCII representations of the numbers (1-9) or
- ! letters (J,Q,K,A)
- !
- i=ichar(parse(ipos1))
- j=ichar(parse(ipos2))
- !
- ! If the first character is "1" (ASCII 49), then
- ! the card must be a "ten"
- !
- If(i.eq.49) then
- ival1 = 10
- else
- if(j.lt.65) then ! numbers
- ival1=j-48 ! convert ASCII to base 10
- else
- if(j.eq.74) ival1=10 ! J
- if(j.eq.81) ival1=10 ! Q
- if(j.eq.75) ival1=10 ! K
- if(j.eq.65) then
- ival1=11 ! A
- ace = ace + 1
- endif
- endif
- endif
- !
- ! Second Card
- !
- ipos1=ipos1+11
- ipos2=ipos2+11
- i=ichar(parse(ipos1))
- j=ichar(parse(ipos2))
- If(i.eq.49) then
- ival2 = 10
- else
- if(j.lt.65) then
- ival2=j-48
- else
- if(j.eq.74) ival2=10
- if(j.eq.81) ival2=10
- if(j.eq.75) ival2=10
- if(j.eq.65) then
- ival2=11
- ace = ace + 1
- endif
- endif
- endif
- isum = ival1 + ival2
- print *,' sum of first dealer two cards = ',isum
- If(isum.eq.21) then
- Write(*,'(" DEALER HAS BLACKJACK - YOU LOSE ")')
- Return
- endif
- !
- ! Third card
- !
- do while (isum.lt.17)
- If (isum.lt.17) then
- ipos1=ipos1+11
- ipos2=ipos2+11
- i=ichar(parse(ipos1))
- j=ichar(parse(ipos2))
- If(i.eq.49) then
- ival1 = 10
- else
- if(j.lt.65) then
- ival1=j-48
- else
- if(j.eq.74) ival1=10
- if(j.eq.81) ival1=10
- if(j.eq.75) ival1=10
- if(j.eq.65) then
- ival1=11
- ace = ace + 1
- endif
- endif
- endif
- isum = isum+ival1
- print *,' sum of dealer cards = ',isum
- endif
- If(isum.gt.21) then
- dealer_busted = .true.
- return
- endif
- enddo
- return
- end subroutine dealer_play
- subroutine player_play(player_cards,parse, player_busted, isum)
- !CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
- !C C
- !C PROGRAM NAME: PLAYER_PLAY C
- !C C
- !C INPUTS: PLAYER_CARDS C
- !C C
- !C OUTPUTS: <none> C
- !C C
- !CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
- implicit none
- integer i,j,ival1, ival2, ace, isum,ipos1,ipos2
- character(len=11) player_cards(13)
- character(len=1) parse(572)
- logical hit, player_busted
- hit = .true.
- ace = 0
- isum = 0
- ipos1=1
- ipos2=2
- ! turn over first two cards
- write(*,'("PLAYER IS DEALT: ",A11,5x,A11)') player_cards(1), player_cards(2)
- !
- ! Parse the cards
- !
- ! First card
- ! i and j are the ASCII representations of the numbers (1-9) or
- ! letters (J,Q,K,A)
- !
- i=ichar(parse(ipos1))
- j=ichar(parse(ipos2))
- !
- ! If the first character is "1" (ASCII 49), then
- ! the card must be a "ten"
- !
- If(i.eq.49) then
- ival1 = 10
- else
- if(j.lt.65) then ! numbers
- ival1=j-48 ! convert ASCII to base 10
- else
- if(j.eq.74) ival1=10 ! J
- if(j.eq.81) ival1=10 ! Q
- if(j.eq.75) ival1=10 ! K
- if(j.eq.65) then
- ival1=11 ! A
- ace = ace + 1
- endif
- endif
- endif
- !
- ! Second Card
- !
- ipos1=ipos1+11
- ipos2=ipos2+11
- i=ichar(parse(ipos1))
- j=ichar(parse(ipos2))
- If(i.eq.49) then
- ival2 = 10
- else
- if(j.lt.65) then
- ival2=j-48
- else
- if(j.eq.74) ival2=10
- if(j.eq.81) ival2=10
- if(j.eq.75) ival2=10
- if(j.eq.65) then
- ival2=11
- ace = ace + 1
- endif
- endif
- endif
- isum = ival1 + ival2
- print *,' sum of player first two cards = ',isum
- If(isum.eq.21) then
- Write(*,'(" PLAYER HAS BLACKJACK ")')
- Return
- endif
- !
- ! Third card
- !
- print *, "Do you want to hit? T or F"
- read *, hit
- do while (hit)
- If (isum.lt.17) then
- ipos1=ipos1+11
- ipos2=ipos2+11
- i=ichar(parse(ipos1))
- j=ichar(parse(ipos2))
- If(i.eq.49) then
- ival1 = 10
- else
- if(j.lt.65) then
- ival1=j-48
- else
- if(j.eq.74) ival1=10
- if(j.eq.81) ival1=10
- if(j.eq.75) ival1=10
- if(j.eq.65) then
- ival1=11
- ace = ace + 1
- endif
- endif
- endif
- isum = isum+ival1
- print *,' sum of player cards = ',isum
- endif
- If(isum.gt.21) then
- player_busted = .true.
- return
- endif
- enddo
- print *, "Your sum is", isum
- return
- end subroutine player_play
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement