/****************************************************************************** *MachineGoFishPlayer.java * * - artificially intelligent player for go fish game * * - instantiates knowledge base object to use as basis for decisions * *Author - Graig M. Fisher * *Written March 30, 2001 * *****************************************************************************/ public class MachineGoFishPlayer extends GoFishPlayer { private KnowledgeBase KB; private GameDisplay display; MachineGoFishPlayer(GameDisplay game){ super(); display = game; KB = new KnowledgeBase(); }//constructor MachineGoFishPlayer //deals with opponents's request for card (and keeps track of it!) public Card beRequested(Card check){ Card temp = checkPair(check); if(temp != null){ hand.remove(temp); return temp; }//if statement else{ KB.updateOppRequests(check.value); return null; }//else statement }//method be_requested //the AI to deal with figuring which card to pick, uses knowledge base public Card makeRequest(){ //get latest size of human's hand from the display to update knowledge KB.updateOppHand(display.handList.getItemCount()); //get latest set of human's pairs from the display to update knowledge String[] temp; if(display.pairList.getItemCount() > 0) temp = display.pairList.getItems(); else temp = new String[0]; int[] oppositionPairs = new int[temp.length]; for(int i = 0; i < temp.length; i++) oppositionPairs[i] = display.cardConvert(temp[i]).value - 1; KB.updateOppPairs(oppositionPairs); //calculate best card to request and return it int[] humanRequests = KB.getOppRequests(); oppositionPairs = KB.getOppPairs(); int j = 0; float prob = -10, newProb = -10; for(int i = 0; i < 13; i++){ if(hand.count(i + 1) > 0){ if(humanRequests[i] > 0) newProb = 1; else newProb = ((3 - pairs.count(i + 1) - oppositionPairs[i]) / (float) KB.getUnknown()) - (KB.getMyRequests()[i] / (float) 10); if(newProb > prob){ prob = newProb; j = i; }//if statement }//if statement }//for loop Card requestCard = checkPair(new Card(j + 1, 0)); KB.updateMe(requestCard.value); return requestCard; }//method makeRequest }//class MachineGoFishPlayer /****************************************************************************** *END OF FILE - MachineGoFishPlayer.java * *****************************************************************************/