!********o*********o*********o*********o*********o*********o*********o**
! This program computes some statistics on a list of playing cards.  The
! cards should be type in one per line, with no preceding spaces.  Each
! card is a single digit followed by a suit.  A zero marks the end of
! the list.
!
!---------------------
! Inputs
!   number,suit: The current card;  suit is one of 'H','D','S', or 'C'
!
! Outputs
!   numHearts, numDiamonds, numSpades, numClubs: suit counts
!   highestNumber, suitForHighest:               highest card seen
!---------------------
! Author:   Bob Harris
! Class:    CMPSC 201F
! Section:  1234
! Date Due: 20 Oct 2003
!********o*********o*********o*********o*********o*********o*********o**

program cards
implicit none

integer   :: number          ! number on the current card
character :: suit            ! suit on the current card (H,D,S,C)
integer   :: numHearts       ! count of how many hearts we've seen
integer   :: numDiamonds     ! (ditto for diamonds)
integer   :: numSpades       ! (ditto for spades)
integer   :: numClubs        ! (ditto for clubs)
integer   :: highestNumber   ! number on highest card we've seen
character :: suitForHighest  ! suit on the highest card

! initialize our counting and tracking

numHearts   = 0
numDiamonds = 0
numSpades   = 0
numClubs    = 0

highestNumber = 0 ! (zero means we haven't seen any cards yet)

! read cards, one at a time, and track which is highest

do
    ! fetch the next card from the user

    print *,"What's the next card (ns)? (use 0 to quit)"
    read  '(I1,A1)',number,suit

    ! if this is the end marker, we're done fetching cards

    if (number == 0) EXIT

    ! validate and count suits

    select case (suit)
        case ('H')
            numHearts   = numHearts + 1
        case ('D')
            numDiamonds = numDiamonds + 1
        case ('S')
            numSpades   = numSpades + 1
        case ('C')
            numClubs    = numClubs + 1
        case default
            print *, "bad suit!"
            suit = ' ' ! (make it easier to test for bad card, below)
    end select

    ! if the card is valid, see if it's the highest card

    if (suit /= ' ') then
        ! show the user the card she gave us

        print '(A,I1,A,A1)',"That card is the ",number," of ",suit

        ! keep track of the highest card

        if (number > highestNumber) then
            highestNumber  = number
            suitForHighest = suit
        end if
    end if

end do

! tell the user what we think

print *,"there were", numHearts,   "hearts"
print *,"there were", numDiamonds, "diamonds"
print *,"there were", numSpades,   "spades"
print *,"there were", numClubs,    "clubs"

if (highestNumber == 0) then
    print *,"I'm not sure what the highest card might be"
else
    print '(A,I1,A,A1)',"The highest card is the ",highestNumber," of ",suitForHighest
end if

end program cards
