/** * Moves a pile the user selects over 2 piles * * @param cardSelected The card to be moved * @param isAI Whether the AI called the function or not * @param isLegal Whether the move is legal or not */ private void movePileTwoPlacesLeft(int cardSelected, boolean isAI, boolean isLegal) { int selectedCard = cardSelected - 1; int movePlace = selectedCard - 3; if (cardStrings.size() > 3 && selectedCard > 2 && selectedCard < cardStrings.size()) { if (!isAI) { // If the AI is calling this function, isLegal has already been decided, so // shouldn't be changed isLegal = false; } if (!isAI) { isLegal = piles.movePileTwoPlacesLeft(isLegal, selectedCard, moveMade); } if (isLegal) { // If the move can be made String cardToMove = cardStrings.get(selectedCard); // Get the pile you want to move int moveTarget = movePlace; while (cardStrings.indexOf(cardToMove) != moveTarget) { // while the pile isn't in the correct new position int i = cardStrings.indexOf(cardToMove); Collections.swap(cardStrings, i, i - 1); // Swap the pile with the one next to it } int index = cardStrings.indexOf(cardToMove) + 1; // Get the index of the pile being removed String cardToDelete = cardStrings.get(index); // Get the pile in that index cardStrings.remove(cardToDelete); // Remove the pile the pile was placed onto saveLog(); } printFrame(); } else { if (!isAI) { System.out.println("There is no pile to move the selected pile onto"); } } }
/** * Deals a new card onto the board. Removes the card it adds from the pack. Prints all the cards * on the board */ private void dealCard() { ; char aChar = 0; char bChar = 0; toggleLock = true; // Makes it so you can no longer change if the log is on or off if (cardsInDeck > 0) { // Only deals a card if there is a card to deal String card = pack.getFirtCard(); // Gets the card on top of the deck cardStrings.add(card); // Adds this card to the arraylist that the frame prints from. for (String c : cardStrings) { // For all the cards on the table aChar = c.charAt(0); bChar = c.charAt(1); StringBuilder sb = new StringBuilder(3); sb.append(aChar).append(bChar).append(" "); if (printBoard) { System.out.print(sb); // Prints the current board to the command line } } frame.cardDisplay(cardStrings); // Prints the cards to the frame System.out.println(""); Pile pile = new Pile(aChar, bChar); // Creates a new pile of the newly laid card piles.addPile(pile); // Adds it to the arrayList of current piles on board cardsInDeck--; saveLog(); // Calls the function to save the new move to the log } else { printFrame(); drawCard = false; // Tells the AI that he cannot draw a card System.out.println("No more cards in deck!"); } }
/** Plays the game automatically for the user */ private void AI() { isAI = true; // This tells other function that the AI is calling them AIUsed = true; // This stops the score being added to the low score list once the AI is used moveMade = false; // This is so the AI knows if it has made a move boolean isLegal = false; for (int i = 4; i < cardStrings.size() + 1; i++) { // Starts at i=4 as that is the first position in which jumping 2 piles is possible isLegal = piles.movePileTwoPlacesLeft( isLegal, i - 1, moveMade); // Checks if it is legal for that pile if (isLegal) { // If one of the piles can legally move moveMade = true; movePileTwoPlacesLeft(i, isAI, isLegal); // Move the pile over 2 piles to the left break; } } if (!moveMade) { // If up until here no move was made, then start checking if any card can move // to cards next to them for (int i = 1; i < cardStrings.size(); i++) { // Starts at i=1 as its the first position this move is possible isLegal = piles.movePileOntoPrevious(isLegal, i, moveMade); // Checks if it is legal if (isLegal) { // If it is legal moveMade = true; movePileOntoPrevious(i, isAI, isLegal); // Makes the move break; } } } if (!moveMade) { // If here, then no moves were possible, so (try to) draw a card if (drawCard) { // If drawing a card is possible dealCard(); // Deal a new card } } isAI = false; moveMade = false; }
/** * Move the last pile onto the previous if it is possible * * @param cardSelected The card to be moved * @param isAI Whether the AI has called this function * @param isLegal Whether the move is legal */ private void movePileOntoPrevious(int cardSelected, boolean isAI, boolean isLegal) { if (cardStrings.size() > 1 && cardSelected > 0 && cardSelected < cardStrings.size()) { if (!isAI) { // If the AI is calling this function, isLegal has already been decided, so // shouldn't be changed isLegal = false; } int pileToMove = cardSelected; int pileToRemove = cardSelected - 1; if (!isAI) { isLegal = piles.movePileOntoPrevious(isLegal, pileToMove, moveMade); } if (isLegal) { cardStrings.remove(pileToRemove); // Remove the pile from the table saveLog(); // Calls the function to log the turn } printFrame(); // Updates the frame } else { if (!isAI) { System.out.println("There are no pile to move the last pile onto"); } } isLegal = false; }