// computer should play all safeties in it's hand
function CplayAllSafes()
{
var madePlay = 0

for (cardIdx = 0; cardIdx <= 6; cardIdx++) {
   switch (compHand[cardIdx]) {
      case "Right of Way": 
         madePlay = playRW(0, cardIdx)       
      break
      case "Extra Tank":
         madePlay = playET(0, cardIdx)       
      break
      case "Driving Ace":
         madePlay = playDA(0, cardIdx)       
      break
      case "Puncture Proof":
         madePlay = playPP(0, cardIdx)       
      break 
   }
}
return madePlay
}

// the computer takes a turn 
function compTurn()
{  
   var madePlay = 0
   var maxIdx  
   var CMilestogo
   var tmpMiles 
   var tmpCard 
   var cardIdx
   var delCandidates = new Array() 
   var HzCnt
   var handCnt 
   var goAgain = 0
   
   maxMiles = 0
   minMiles = 0
   skip_rest = 0
      
   // Coup Fourre!
   // when the opponent plays a hazard a 
   // flag is set to indicate that this is the
   // window where a coupe fourre could be played
   // if a card is drawn first the window is closed
   if (CCFwindow && !cdrewCard) {
      for (var cardIdx = 0; cardIdx <= 5, CF = 0; cardIdx++) {
         switch (compHand[cardIdx]) {
            case "Right of Way":
               if (CplayCard == "Stop" || SL == spdLim) {
                  CF = 1
                  CCFcnt += 1
                  compHand[cardIdx] = "RWCF"
                  madePlay = playRW(0, cardIdx)
               }
            break
            case "Driving Ace":
               if (CplayCard == "Accident") {
                  CF = 1
                  CCFcnt += 1
                  compHand[cardIdx] = "DACF"
                  madePlay = playDA(0, cardIdx)
               } 
            break
            case "Extra Tank":
               if (CplayCard == "Out of Gas") {
                  CF = 1 
                  CCFcnt += 1 
                  compHand[cardIdx] = "ETCF" 
                  madePlay = playET(0, cardIdx)
               } 
            break
            case "Puncture Proof":
               if (CplayCard == "Flat Tire") {
                  CF = 1 
                  CCFcnt += 1
                  compHand[cardIdx] = "PPCF"
                  madePlay = playPP(0, cardIdx)
               } 
            break
         }       
      }
      CCFwindow = 0
      if (CF) {
         skip_rest = 1
      }
   }

   if (!skip_rest) {
   
   do {
      goAgain = 0
      madePlay = 0 
      maxMiles = 0
      minMiles = 0

      // if not a possible coupe fourre, draw a card
      if (moreCards && !madePlay) {
         getCard(0)
         if (showCompPlay) 
            window.alert("computer drew " + compHand[6])
      }

      // if possible for opponent to finish with next play, play any safeties in hand
      if (Rolling && (miles >= 600  ||  miles == 500) && !madePlay) {
         madePlay = CplayAllSafes()
         if (madePlay)
            goAgain = 1      
      } 

      // if the opponent is rolling, attempt to stop
      if (Rolling && !madePlay) {
         for (var i = 0; i <= 6 && !madePlay; i++) { 
            switch (compHand[i]) {
               case "Stop":
                  if (!RW) { 
                     playCard = compHand[i]
                     placeCard(0,i)
                     Rolling = 0
                     hazardCard = 1
                     madePlay = 1
                     cdrewCard = 0
                     CFwindow = 1
                     if (showCompPlay)
                        window.alert("Computer played: Stop")
                  }
               break
               case "Accident":
                  if (!DA) { 
                     playCard = compHand[i]
                     placeCard(0,i) 
                     Rolling = 0
                     hazardCard = 1
                     madePlay = 1
                     CFwindow = 1
                     if (showCompPlay)
                     window.alert("Computer played: Accident")
                  }
               break
               case "Flat Tire":
                  if (!PP) { 
                     playCard = compHand[i]
                     placeCard(0,i) 
                     Rolling = 0
                     hazardCard = 1
                     madePlay = 1
                     CFwindow = 1
                     if (showCompPlay)
                        window.alert("Computer played: Flat Tire")
                  }
               break
               case "Out of Gas":
                  if (!ET) { 
                     playCard = compHand[i]
                     placeCard(0,i) 
                     Rolling = 0
                     hazardCard = 1
                     madePlay = 1
                     CFwindow = 1
                     if (showCompPlay)
                        window.alert("Computer played: Out of Gas")
                  }
               break
               case "Speed Limit":
                  if (!RW && !spdLim) { 
                     SL = compHand[i]
                     placeCard(0,i)  
                     spdLim = 1
                     madePlay = 1
                     CFwindow = 1
                     if (showCompPlay)
                        window.alert("Computer played: Speed Limit")
                  } 
               break
                 
            }
         } 
      }

      // if computer is not rolling attempt to play Roll card
      if (!CRolling && !madePlay && !ChazardCard){
         for (var i = 0; i <= 6 && !madePlay; i++) {
            if (compHand[i] == "Roll") {
               CplayCard = "Roll"
               placeCard(0,i)
               CRolling = 1
               madePlay = 1 
               cdrewCard = 0
               if (showCompPlay)
                  window.alert("Comp played: Roll card")
            }
         }
      }

      // if we have the right of way card, play it to start rolling
      // could use some strategy here to determine if this is best 
      // move at this time.  Maybe if human isn't rolling, don't play 
      // RoW?
      if (!CRolling && !madePlay && !ChazardCard){
         for (var i = 0; i <= 6 && !madePlay; i++) {
            if (compHand[i] == "Right of Way") {
               madePlay = playRW(0, i)
               goAgain = 1
            }
         }
      }

      // if the computer has a hazard card, try to clear it
      if (!madePlay && ChazardCard) {
         switch (CplayCard) {
            case "Stop":
               for (var i = 0; i <= 6 && !madePlay; i++) {
                  if (compHand[i] == "Roll") {
                     CplayCard = "Roll"
                     placeCard(0,i)
                     CRolling = 1
                     madePlay = 1 
                     cdrewCard = 0
                     ChazardCard = 0
                     if (showCompPlay)
                        window.alert("Comp played: Roll card")
                  }
               }
               if (!madePlay) {
                  for (var i = 0; i <= 6 && !madePlay; i++) {
                     if (compHand[i] == "Right of Way") {
                        madePlay = playRW(0, i)
                        goAgain = 1
                     }
                  }
               }      
            break
            case "Flat Tire":
               for (var i = 0; i <= 6 && !madePlay; i++) {
                  if (compHand[i] == "Spare Tire") {
                     if (CRW) {
                        CplayCard = "Right of Way"
                        CRolling = 1
                     } else
                         CplayCard = "Spare Tire"
                     placeCard(0,i)
                     madePlay = 1 
                     cdrewCard = 0
                     ChazardCard = 0
                     if (showCompPlay)
                        window.alert("Comp played: Spare Tire")
                  }
               } 
               if (!madePlay) {
                  for (var i = 0; i <= 6 && !madePlay; i++) {
                     if (compHand[i] == "Puncture Proof") {
                        madePlay = playPP(0, i)
                        goAgain = 1
                     }
                  }
               }      
            break
            case "Accident":
               for (var i = 0; i <= 6 && !madePlay; i++) {
                  if (compHand[i] == "Repairs") {
                     if (CRW) {
                        CplayCard = "Right of Way"
                        CRolling = 1
                     } else
                        CplayCard = "Repairs"
                     placeCard(0,i)
                     madePlay = 1 
                     cdrewCard = 0
                     ChazardCard = 0
                     if (showCompPlay)
                        window.alert("Comp played: Repairs")
                  }
               } 
               if (!madePlay) {
                  for (var i = 0; i <= 6 && !madePlay; i++) {
                     if (compHand[i] == "Driving Ace") {
                        madePlay = playDA(0, i)
                        goAgain = 1
                     }
                  }
               }      
            break
            case "Out of Gas":
               for (var i = 0; i <= 6 && !madePlay; i++) {
                  if (compHand[i] == "Gasoline") {
                     if (CRW) {
                        CplayCard = "Right of Way"
                        CRolling = 1
                     } else
                        CplayCard = "Gasoline"
                     placeCard(0,i)
                     madePlay = 1 
                     cdrewCard = 0
                     ChazardCard = 0
                     if (showCompPlay)
                        window.alert("Comp played: Out of Gas")
                  }
               }      
               if (!madePlay) {
                  for (var i = 0; i <= 6 && !madePlay; i++) {
                     if (compHand[i] == "Extra Tank") {
                        madePlay = playET(0, i)
                        goAgain = 1
                     }
                  }
               }      
            break
         }
      }

      // if computer has a speed limit, try to clear it
      if (!madePlay && CspdLim) {
         for (var i = 0; i <= 6 && !madePlay; i++) {
            switch(compHand[i]) {
               case "End of Limit":
                  CSL = compHand[i]
                  madePlay = 1
                  placeCard(0, i)
                  CspdLim = 0 
                  cdrewCard = 0
                  if (showCompPlay)
                     window.alert("Comp played: " + compHand[i])
               break
               case "Right of Way":
                  madePlay = playRW(0, i)
                  goAgain = 1
               break
            }
         }
      } 

      // if the computer is rolling, try to play mileage 
      if (CRolling && !madePlay) {
         CMilestogo = currGoal - cmiles
         maxMiles = 0 
         for (var i = 0; i <= 6; i++) {
            switch (compHand[i]) {
               case "25":
               case "50":
                  // if we have a speed limit, only play 25 & 50
                  tmpMiles = Number(compHand[i])
                  if (tmpMiles > maxMiles && tmpMiles <= CMilestogo) {
                     maxMiles = tmpMiles    
                     maxIdx = i
                  }            
               break
               case "75":
               case "100":
                  // if no speed limit, we can play any mile card
                  if (!CspdLim) {
                     tmpMiles = Number(compHand[i])
                     if (tmpMiles > maxMiles && tmpMiles <= CMilestogo) {
                        maxMiles = tmpMiles    
                        maxIdx = i
                     }
                  }            
               break
               case "200":
                  if (!CspdLim) {
                     // only two 200 mile cards per player
                     if (C2HndCnt < 2) {
                        tmpMiles = Number(compHand[i])
                        if (tmpMiles > maxMiles && tmpMiles <= CMilestogo) {
                           maxMiles = tmpMiles    
                           maxIdx = i
                        }
                     }
                  }            
               break
            }     
         }  
         if (maxMiles > 0) {
            tmpMiles = cmiles + maxMiles
            if (maxMiles == 200)
               C2HndCnt += 1
            if (showCompPlay)
               window.alert("Computer played: " + maxMiles)
            if (tmpMiles == currGoal) {
               // play all safeties in hand before playing winning card
               do {
                  madePlay = CplayAllSafes()
               }while (madePlay) 
            } 
            cmiles = tmpMiles
            if (cmiles == currGoal){
               tripCompleted = 1
               GameOver = 1 
            }
            placeCard(0, maxIdx)
            cdrewCard = 0
            madePlay = 1
         }
      }     
      
      // if no play made above, discard
      if (!madePlay) {

         // if opponent has played a safety, discard equivalent hazard
         for (var i = 0; i <= 6  && !madePlay; i++) {
            switch (compHand[i]) {
               case "Stop":
               case "Speed Limit":
                  if (RW) {
                     discarded = compHand[i]
                     if (showCompPlay)
                        window.alert("Computer discarded: " + compHand[i])
                     placeCard(0,i)
                     madePlay = 1
                     cdrewCard = 0
                  }
               break
               case "Accident":
                  if (DA) {
                     discarded = compHand[i]
                     if (showCompPlay)
                        window.alert("Computer discarded: " + compHand[i])
                     placeCard(0,i)
                     madePlay = 1
                     cdrewCard = 0
                  }
               break
               case "Out of Gas":
                  if (ET) {
                     discarded = compHand[i]
                     if (showCompPlay)
                        window.alert("Computer discarded: " + compHand[i])
                     placeCard(0,i)
                     madePlay = 1
                     cdrewCard = 0
                  }
               break
               case "Flat Tire":
                  if (PP) {
                     discarded = compHand[i]
                     if (showCompPlay)
                        window.alert("Computer discarded: " + compHand[i])
                     placeCard(0,i)
                     madePlay = 1
                     cdrewCard = 0
                  }
               break
            }
         } 
         
         // look for duplicate remedy cards
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               tmpCard = compHand[i] 
               if (tmpCard == "Roll" || tmpCard == "Gasoline" || tmpCard == "End of Limit" || tmpCard == "Repairs" || tmpCard == "Spare Tire") {
                  for (var x = 0; x <= 6 && !madePlay; x++) {
                     if (compHand[x] == tmpCard && i != x) {
                        discarded = compHand[i]
                        madePlay = 1
                        if (showCompPlay)
                           window.alert("Computer discarded: " + compHand[i])
                        placeCard(0,i)
                        cdrewCard = 0     
                     } 
                  }
               }
            }
         } 
    
         // look for duplicate hazard cards
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               tmpCard = compHand[i] 
               if (tmpCard == "Stop" || tmpCard == "Out of Gas" || tmpCard == "Speed Limit" || tmpCard == "Accident" || tmpCard == "Flat Tire") {
                  for (var x = i + 1; x <= 6 && !madePlay; x++) {
                     if (compHand[x] == tmpCard) {
                        discarded = compHand[i]
                        if (showCompPlay)
                           window.alert("Computer discarded: " + compHand[i])                   
                        madePlay = 1
                        placeCard(0,i)
                        cdrewCard = 0     
                     } 
                  }
               }
            }
         }

         // look for lowest mileage card
         if (!madePlay) {
            minMiles = 999
            for (var i = 0; i <= 6; i++) {
               if (compHand[i] == "25" || compHand[i] == "50" || compHand[i] == "75" || compHand[i] == "100" || compHand[i] == "200") {
                  if (Number(compHand[i]) < minMiles) {
                     minMiles = Number(compHand[i])  
                     minIdx = i
                  }
               }   
            }
            if (minMiles != 999) {
               discarded = compHand[minIdx]
               if (showCompPlay)
                  window.alert("Computer discarded: " + compHand[minIdx])
               placeCard(0, minIdx)
               madePlay = 1
               cdrewCard = 0
            }
         }

         // if multiple hazards, discard 1 of them
         if (!madePlay) {
            HzCnt = 0
            for (var i = 0; i <= 6; i++) {
               if (compHand[i] == "Stop" || compHand[i] == "Flat Tire" || compHand[i] == "Out of Gas" || compHand[i] == "Accident") 
                  HzCnt += 1  
            }
            if (HzCnt > 1) {
               idx = 99
               for (var i = 0; i <= 6; i++) {
                  if (compHand[i] == "Stop" || compHand[i] == "Flat Tire" || compHand[i] == "Out of Gas" || compHand[i] == "Accident") {
                     for (var x = 0; x <= 3; x++) {
                        if (compHand[i] == SafetyOrder[x]) 
                           if (x < idx) 
                              idx = x
                     }
                  }
               }
               if (idx != 99) {
                  for (var i = 0; i <= 6  && !madePlay; i++) {
                     if (compHand[i] == SafetyOrder[idx]) {
                        discarded = compHand[i]
                        if (showCompPlay)
                           window.alert("Computer discarded: " + compHand[i])
                        placeCard(0,i)
                        madePlay = 1
                        cdrewCard = 0
                     }
                  }
               } 
            }
         }

         // if we have an end of limit, discard it
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               if (compHand[i] == "End of Limit") {
                  discarded = compHand[i]
                  if (showCompPlay)
                     window.alert("Computer discarded: " + compHand[i])
                  placeCard(0,i)
                  madePlay = 1
                  cdrewCard = 0
               } 
            }
         }
  
         // if we have a speed limit, discard it
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               if (compHand[i] == "Speed Limit") {
                  discarded = compHand[i]
                  if (showCompPlay)
                     window.alert("Computer discarded: " + compHand[i])
                  placeCard(0,i)
                  madePlay = 1
                  cdrewCard = 0
               } 
            }
         }
      
         // if we have a stop, discard it
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               if (compHand[i] == "Stop") {
                  discarded = compHand[i]
                  if (showCompPlay)
                     window.alert("Computer discarded: " + compHand[i])
                  placeCard(0,i)
                  madePlay = 1
                  cdrewCard = 0
               } 
            }
         }

         // if we have a flat tire, discard it
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               if (compHand[i] == "Flat Tire") {
                  discarded = compHand[i]
                  if (showCompPlay)
                     window.alert("Computer discarded: " + compHand[i])
                  placeCard(0,i)
                  madePlay = 1
                  cdrewCard = 0
               } 
            }
         }

         // if we have an out of gas, discard it
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               if (compHand[i] == "Out of Gas") {
                  discarded = compHand[i]
                  if (showCompPlay)
                     window.alert("Computer discarded: " + compHand[i])
                  placeCard(0,i)
                  madePlay = 1
                  cdrewCard = 0
               } 
            }
         }

         // if we have an accident, discard it
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               if (compHand[i] == "Accident") {
                  discarded = compHand[i]
                  if (showCompPlay)
                     window.alert("Computer discarded: " + compHand[i])
                  placeCard(0,i)
                  madePlay = 1
                  cdrewCard = 0
               } 
            }
         }

         // if we have a spare tire, discard it
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               if (compHand[i] == "Spare Tire") {
                  discarded = compHand[i]
                  if (showCompPlay)
                     window.alert("Computer discarded: " + compHand[i])
                  placeCard(0,i)
                  madePlay = 1
                  cdrewCard = 0
               } 
            }
         }

         // if we have a gasoline, discard it
         if (!madePlay) {
            for (var i = 0; i <= 6  && !madePlay; i++) {
               if (compHand[i] == "Gasoline") {
                  discarded = compHand[i]
                  if (showCompPlay)
                     window.alert("Computer discarded: " + compHand[i])
                  placeCard(0,i)
                  madePlay = 1
                  cdrewCard = 0
               } 
            }
         }

         // if we have a repairs, discard it
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               if (compHand[i] == "Repairs") {
                  discarded = compHand[i]
                  if (showCompPlay)
                     window.alert("Computer discarded: " + compHand[i])
                  placeCard(0,i)
                  madePlay = 1
                  cdrewCard = 0
               } 
            }
         }

         // if we have an roll, discard it
         if (!madePlay) {
            for (var i = 0; i <= 6 && !madePlay; i++) {
               if (compHand[i] == "Roll") {
                  discarded = compHand[i]
                  if (showCompPlay)
                     window.alert("Computer discarded: " + compHand[i])
                  placeCard(0,i)
                  madePlay = 1
                  cdrewCard = 0
               } 
            }
         }
      }
       
      // if nothing discarded above, see if we have a safety that can be played
      if (!madePlay) {
          for (var i = 0; i <= 6 && !madePlay; i++) {
             switch(compHand[i]) {
                case "Right of Way":
                   madePlay = playRW(0, i)
                   goAgain = 1
                break
                case "Puncture Proof":
                   madePlay = playPP(0, i)
                   goAgain = 1
                break
                case "Driving Ace":
                   madePlay = playDA(0, i)
                   goAgain = 1
                break
                case "Extra Tank":
                   madePlay = playET(0, i)
                   goAgain = 1
                break
             }
          }
      } 

      if (madePlay)
         cdrewCard = 0 

   } while(goAgain)
   
   if (!madePlay)
      window.alert("The computer did not play a card")

   // see if the computer has any cards left in it's hand
   // if not, the game is over whether the trip was completed or not
   if (!GameOver) {
      handCnt = 0
      for (var i = 0; i <= 6  && !handCnt; i++) {

         if (compHand[i] != " ")
            handCnt = 1       
      }
      if (!handCnt)
         GameOver = 1
   }
   }
   
   if (GameOver)
      showScore()
}
