/****************************************************************************** *MachineGoFishPlayer.java * * - artificially intelligent player for go fish game * *Author - Graig M. Fisher * *Written February 14, 2001 * *Last Modified March 8, 2001 * *****************************************************************************/ public class MachineGoFishPlayer extends GoFishPlayer { private int possibleCards[]; private int timesRequested[]; private CardList humanList; MachineGoFishPlayer(int numberPlayers){ super(); possibleCards = new int[13]; timesRequested = new int[13]; for(int i = 0; i < 13; i++) possibleCards[i] = 1; }//constructor MachineGoFishPlayer public Card beRequested(Card check){ Card temp = checkPair(check); if(temp != null){ hand.remove(temp); possibleCards[temp.value - 1] = possibleCards[temp.value - 1] + 4; return temp; }//if statement else return null; }//method be_requested public void knowledgeUpdate(CardList in){ humanList = in; }//mutator knowledgeUpdate //this is the function that is the entire point of the project //it is where the AI is actually implemented //thanks to Anthony Emma who provided the original algorithm public Card makeRequest(){ Card currentCard, requestCard = hand.getHead(); int unknown, remainingCards, j = -1; float prob = -10, newProb; unknown = 52 - (count(pairs) + count(hand) + count(humanList)); for(int i = 1; i < 14; i++){ currentCard = new Card(i, 0); if(checkPair(currentCard) != null){ remainingCards = 4 - (count(hand, i) + count(pairs, i) + count(humanList, i)); newProb = ((possibleCards[i-1] * remainingCards) - timesRequested[i-1]) / (float) unknown; if(newProb > prob){ prob = newProb; requestCard = currentCard; j = i-1; }//if statement }//if statement }//for loop timesRequested[j] = timesRequested[j] + 1; return checkPair(requestCard); }//method request private int count(CardList lookAt){ Card currentCard = lookAt.getHead(); int number = 0; if(currentCard != null){ number++; while(currentCard.getNext() != null){ currentCard = currentCard.getNext(); number++; }//while loop }//if statment return number; }//method count private int count(CardList lookAt, int val){ Card currentCard = lookAt.getHead(); int number = 0; if(currentCard != null){ if(currentCard.value == val) number++; while(currentCard.getNext() != null){ currentCard = currentCard.getNext(); if(currentCard.value == val) number++; }//while loop }//if statement return number; }//method count }//class MachineGoFishPlayer /****************************************************************************** *END OF FILE - MachineGoFishPlayer.java * *****************************************************************************/