Connecting and changing variables in Bash script

#!/bin/bash

X=$(</home/cogiz/computerhand.txt)                  # (3S 8C 2H 6D QC 8S 4H 5H)
Y=$(</home/cogiz/topcardinplay.txt)                   # KS
A=( "${Y[@]::1}" )
B=( "${Y[@]:1}" )

for e in ${X[*]}; do
  if [[ $e =~ $A|$B|8 ]]; then                                 # searching for valid cards K,S or 8
        echo "$e " >> /home/cogiz/validcards.txt 
  else
        echo "o" >> /home/cogiz/validcards.txt     # adds a 0 to line if no valid card is found to save position
  fi           
done

grep -n -v -b "o" /home/cogiz/validcards.txt | cut -c1 | xargs > /home/cogiz/position.txt    # removes all 0's, leaving position no.

rm /home/cogiz/validcards.txt

for e in ${X[*]}; do
   if [[ $e =~ $A|$B|8 ]]; then                                      # recalculates valid cards to play
     printf "$e " >> /home/cogiz/validcards.txt
   fi
done

VC=()
validcards=$(</home/cogiz/validcards.txt)                         # valid cards in hand available to play
for i in validcards; do                                                          #   (3S 8C 8S)
   VC+=(${validcards[*]})                                       
done

P=()
position=$(</home/cogiz/position.txt)                            # positions available cards are in computer hand
for i in position; do                                                         #   (1 2 6)
   P+=(${position[*]})
done

echo ${#VC[@]} > /home/cogiz/validcardchoices.txt  
N=()
N2=$(</home/cogiz/validcardchoices.txt)
for i in N2; do                                                                           # total of available choices (3)
   N+=(${N2[*]})
done

echo $RANDOM % $N + 1 | bc > /home/cogiz/randomvalidcardchoice.txt             # random choice between 1 and 3
                                                                                                                                 
O=$(</home/cogiz/randomvalidcardchoice.txt)

echo "The computer has chosen to play the card in position $O"     # in this case (3)

#######################################################################

How would I go about connecting the random choice of which card to play (3rd card of 3 valid cards which sits in position 6 of computer's hand) to the proper card in the array
${X[5]}, and therefore making this card (8S) the new topcardinplay ($Y)?

Thank you in advance for any help you can give me.
Cogiz