import java.lang.*; import java.util.*; import java.awt.*; /* computer player is an extension of the player class that will automatically play its turns. The too long function, ChoosePlay, attempts to play from the store stack, then from the hand, then from a discard stack, and then finally if plays from the hand to a discard stack to end its turn. Any successful play to the build stacks (either from the hand, store, or discard stack) will return a value of true indicating that the computer player will continue. Any play from the hand to a discard stack will return false indicating that the turn is over. */ public class CompPlayer extends Player { private Random rand = new Random(); private CompWindow window = new CompWindow(); public CompPlayer () { super("Computer"); } public boolean choosePlay (BuildStack b[], int gamestate[], Deck deck) { boolean decided = false; int i=0, j=0, value1=0, value2=0, value3=0, temp; Card card; this.showPlayer (gamestate, b); //Play Store(Play card on store to first possible build stack) if (!store.empty()) { for (i = 0; i < 4; i++) { if (playStore(b[i])) { storecount--; window.setText("Computer is playing from store. " + storecount + " left on store stack."); if (storecount == 0) window.setText("The Computer has won!"); return true; } } } //Play Hand Wildcard (Play widcard from the hand) i = 4; if (!decided) { value1 = gamestate[i]; while (value1 != 14 && i < 9) { value1 = gamestate[i]; i++; } if (value1 == 14) { for (j = 0; j < 4; j++) { if (b[j].empty()) { if (playHand(b[j], i-4)) { window.setText("Computer is playing from hand."); return true; } } } for (j = 0; j < 4; j++) { if (playHand(b[j], i-4)) { window.setText("Computer is playing from hand."); return true; } } } } //Play Hand if (!decided) { for (i = 0; i < 5; i++) { for (j = 0; j < 4; j++) { if (playHand (b[j], i, deck, size_of_hand)) { window.setText("Computer is playing from hand."); return true; } } } } //Play Discard (No Wildcard Plays...Only Dc) if (!decided) { for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { if (playDiscard (b[i], j)) { window.setText("Computer is playing from a discard stack."); return true; } } } } //Finish Turn (Place card on Discard in order) if (!decided) { for (j = 4; j < 9; j++) { for (i = 10; i < 14; i++) { if (gamestate[j] == gamestate[i] + 1) { if (playFinish (j-4, i-10)) { window.setText("Computer is finishing its turn by discarding."); return false; } } } } } //Finish Turn (Place random card on Empty Discard) if (!decided) { for (i = 0; i < 4; i++) { while (discard[i].empty()) { value1 = rand.nextInt (); if (value1 < 0) value1 = value1 * -1; value1 = value1 % size_of_hand; if (playFinish(value1, i)) { window.setText("Computer is finishing its turn by discarding."); return false; } } } } //Finish Turn (Place random card on random Discard) if (!decided) { while (!decided) { value1 = rand.nextInt (); if (value1 < 0) value1 = value1 * -1; value1 = value1 % size_of_hand; i = rand.nextInt (); if (i < 0) i = i * -1; i = i % 4; if (playFinish(value1, i)) { window.setText("Computer is finishing its turn by discarding."); return false; } } } return false; } public void dispose () /* kills the display window when the applet is exited */ { window.hide(); window.dispose(); } }