hej
unknown
plain_text
2 years ago
22 kB
1
Indexable
Never
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class Main { //Initializing all our piles static Pile C1 = new Pile(); static Pile C2 = new Pile(); static Pile C3 = new Pile(); static Pile C4 = new Pile(); static Pile C5 = new Pile(); static Pile C6 = new Pile(); static Pile C7 = new Pile(); static Pile F1 = new Pile(); static Pile F2 = new Pile(); static Pile F3 = new Pile(); static Pile F4 = new Pile(); static String message = "HELLO"; static String lastCommand = "NONE"; //Node of cards private static class Card { Card next; Card prev; char number; char suit; char isHidden; } //Node of piles private static class Pile { Pile next; Pile prev; Card head; Card tail; int sizeOfPile; } //This returns the tail of a given pile static Card chooseTailCardFromPile(Pile pile) { return pile.tail; } //This removes the tail from a given pile static void removeCardFromPile(Pile pile) { if (pile.sizeOfPile != 0) { if (pile.sizeOfPile > 1) { pile.tail = pile.tail.prev; pile.tail.isHidden = 0; pile.tail.next = null; } else if (pile.sizeOfPile == 1) { pile.tail = null; pile.head = null; } message = "OK"; } else { message = "error, empty pile"; } pile.sizeOfPile--; } //This either removes a bottom card from one pile and adds it to the other //Or it removes a certain amount of cards from one pile to another static void moveCard(char commands[], Pile fromPile, Pile toPile) { char c3 = commands[3]; if ((commands[0] == 'C' || commands[0] == 'F') && commands[2] == '-' && commands[3] == '>' && (commands[4] == 'C' || commands[4] == 'F')) { Card card = chooseTailCardFromPile(fromPile); removeCardFromPile(fromPile); moveCardToPile(card, toPile); } else if (commands[0] == 'C' && commands[2] == ':' && commands[5] == '-' && commands[6] == '>' && commands[7] == 'C') { Card chosenCard = chooseFromSpecificCardInColumn(commands[4], commands[3], selectFromPile(commands)); if (chosenCard != null) { moveCardsToPile(chosenCard, selectSeveralToPile(commands)); message = "OK"; } else { message = "card not found"; } } else { message = "error in command"; } } //This returns the pile that the user wants to select a card from static Pile selectFromPile(char command[]) { Pile chosenPile = null; if (command[0] == 'C') { switch (command[1]) { case '1': chosenPile = C1; break; case '2': chosenPile = C2; break; case '3': chosenPile = C3; break; case '4': chosenPile = C4; break; case '5': chosenPile = C5; break; case '6': chosenPile = C6; break; case '7': chosenPile = C7; break; } } else if (command[0] == 'F') { switch (command[1]) { case '1': chosenPile = F1; break; case '2': chosenPile = F2; break; case '3': chosenPile = F3; break; case '4': chosenPile = F4; break; } } return chosenPile; } //This returns the pile that the user wants to select a card to static Pile selectToPile(char command[]) { Pile chosenPile = null; if (command[4] == 'C') { switch (command[5]) { case '1': chosenPile = C1; break; case '2': chosenPile = C2; break; case '3': chosenPile = C3; break; case '4': chosenPile = C4; break; case '5': chosenPile = C5; break; case '6': chosenPile = C6; break; case '7': chosenPile = C7; break; } } else if (command[4] == 'F') { switch (command[5]) { case '1': chosenPile = F1; break; case '2': chosenPile = F2; break; case '3': chosenPile = F3; break; case '4': chosenPile = F4; break; } } return chosenPile; } //This returns the pile when user has entered a specific card from a specific pile static Pile selectSeveralToPile(char command[]) { Pile chosenPile = null; if (command[7] == 'C') { switch (command[8]) { case '1': chosenPile = C1; break; case '2': chosenPile = C2; break; case '3': chosenPile = C3; break; case '4': chosenPile = C4; break; case '5': chosenPile = C5; break; case '6': chosenPile = C6; break; case '7': chosenPile = C7; break; } } else if (command[7] == 'F') { switch (command[8]) { case '1': chosenPile = F1; break; case '2': chosenPile = F2; break; case '3': chosenPile = F3; break; case '4': chosenPile = F4; break; } } return chosenPile; } //This returns the next card number in the hierachy static char canBeMoved(char numberOfCardMoved) { switch (numberOfCardMoved) { case 'A': return '2'; case '2': return '3'; case '3': return '4'; case '4': return '5'; case '5': return '6'; case '6': return '7'; case '7': return '8'; case '8': return '9'; case '9': return 'T'; case 'T': return 'J'; case 'J': return 'Q'; case 'Q': return 'K'; case 'K': return '0'; } return '0'; } //This moves one card to the bottom of a pile static void moveCardToPile(Card cardMoved, Pile pile) { if (pile.sizeOfPile == 0) { pile.head = cardMoved; pile.tail = cardMoved; pile.sizeOfPile++; return; } if (pile.tail != null && cardMoved.suit != pile.tail.suit && canBeMoved(cardMoved.number) == pile.tail.number) { cardMoved.prev = pile.tail; cardMoved.next = null; pile.tail.next = cardMoved; pile.tail = cardMoved; pile.sizeOfPile++; message = "OK"; } else { message = "error, card cannot be moved"; } } static void startAdding(Card cardMoved, Pile pile) { if (pile.sizeOfPile == 0) { pile.head = cardMoved; pile.tail = cardMoved; pile.sizeOfPile++; return; } if (pile.tail != null) { cardMoved.prev = pile.tail; cardMoved.next = null; pile.tail.next = cardMoved; pile.tail = cardMoved; pile.sizeOfPile++; } } //This moves several cards from one pile to another pile private static void moveCardsToPile(Card cardMoved, Pile pile) { Card lastCard = cardMoved; while (lastCard.next != null) { lastCard = lastCard.next; } if (pile.sizeOfPile == 0) { pile.head = cardMoved; pile.tail = cardMoved; pile.sizeOfPile++; return; } if (pile.tail != null && cardMoved.suit != pile.tail.suit && canBeMoved(cardMoved.number) == pile.tail.number) { pile.tail.next = cardMoved; cardMoved.prev = pile.tail; pile.tail = lastCard; pile.sizeOfPile += i; message = "OK"; } else { message = "error, cards cannot be moved"; } } static int i; //This chooses a specific card from a specific pile static Card chooseFromSpecificCardInColumn(char cardSuit, char number, Pile pile) { Card chosenCard = null; Card current = pile.tail; i = 1; boolean again = true; while (current.suit != cardSuit && current.number != number && i < pile.sizeOfPile) { current = current.prev; i++; } if (current.suit == cardSuit && current.number == number) { chosenCard = current; if (current.prev != null) { Card beforeCurrent = current.prev; beforeCurrent.next = null; pile.tail = beforeCurrent; pile.tail.isHidden = 0; } pile.sizeOfPile -= i; message = "OK"; } else { message = "error, card not found"; } return chosenCard; } static char[] SI(char allCards[]) { char halfOne[] = new char[52]; char halfTwo[] = new char[52]; for (int j = 0; j < halfOne.length; j++) { halfOne[j] = allCards[j]; } for (int j = halfOne.length; j < allCards.length; j++) { halfTwo[j - halfOne.length] = allCards[j]; } char shuffledAllCards[] = new char[104]; int k = 0; for (int j = 0; j < halfOne.length - 1; j += 2) { shuffledAllCards[k] = halfOne[j]; k++; shuffledAllCards[k] = halfOne[j + 1]; k++; shuffledAllCards[k] = halfTwo[j]; k++; shuffledAllCards[k] = halfTwo[j + 1]; k++; } return shuffledAllCards; } static void addInShuffledCardsIntoColumn(char shuffledCards[]) { for (int j = 0; j < shuffledCards.length; j += 2) { Card card = new Card(); card.number = shuffledCards[j]; card.suit = shuffledCards[j + 1]; int k = j / 2; if (k == 0) { startAdding(card, C1); } if (k > 0 && k < 7) { if (k == 1) { card.isHidden = 1; } startAdding(card, C2); } if (k > 6 && k < 14) { if (k == 7 || k == 8) { card.isHidden = 1; } startAdding(card, C3); } if (k > 13 && k < 22) { if (k == 14 || k == 15 || k == 16) { card.isHidden = 1; } startAdding(card, C4); } if (k > 21 && k < 31) { if (k == 22 || k == 23 || k == 24 || k == 25) { card.isHidden = 1; } startAdding(card, C5); } if (k > 30 && k < 41) { if (k == 31 || k == 32 || k == 33 || k == 34 || k == 35) { card.isHidden = 1; } startAdding(card, C6); } if (k > 40 && k < 52) { if (k == 41 || k == 42 || k == 43 || k == 44 || k == 45 || k == 46) { card.isHidden = 1; } startAdding(card, C7); } } } static void printBoard() { System.out.println("C1" + "\t" + "\t" + "C2" + "\t" + "\t" + "C3" + "\t" + "\t" + "C4" + "\t" + "\t" + "C5" + "\t" + "\t" + "C6" + "\t" + "\t" + "C7"); Card trackOfC1 = C1.head; Card trackOfC2 = C2.head; Card trackOfC3 = C3.head; Card trackOfC4 = C4.head; Card trackOfC5 = C5.head; Card trackOfC6 = C6.head; Card trackOfC7 = C7.head; boolean done = false; while (!done) { if (trackOfC1 != null) { System.out.print(trackOfC1.number + "" + trackOfC1.suit + "\t" + "\t"); trackOfC1 = trackOfC1.next; } else { System.out.print("\t" + "\t"); } if (trackOfC2 != null) { if (trackOfC2.isHidden == 1) { System.out.print("[]" + "\t" + "\t"); } else { System.out.print(trackOfC2.number + "" + trackOfC2.suit + "\t" + "\t"); } trackOfC2 = trackOfC2.next; } else { System.out.print("\t" + "\t"); } if (trackOfC3 != null) { if (trackOfC3.isHidden == 1) { System.out.print("[]" + "\t" + "\t"); } else { System.out.print(trackOfC3.number + "" + trackOfC3.suit + "\t" + "\t"); } trackOfC3 = trackOfC3.next; } else { System.out.print("\t" + "\t"); } if (trackOfC4 != null) { if (trackOfC4.isHidden == 1) { System.out.print("[]" + "\t" + "\t"); } else { System.out.print(trackOfC4.number + "" + trackOfC4.suit + "\t" + "\t"); } trackOfC4 = trackOfC4.next; } else { System.out.print("\t" + "\t"); } if (trackOfC5 != null) { if (trackOfC5.isHidden == 1) { System.out.print("[]" + "\t" + "\t"); } else { System.out.print(trackOfC5.number + "" + trackOfC5.suit + "\t" + "\t"); } trackOfC5 = trackOfC5.next; } else { System.out.print("\t" + "\t"); } if (trackOfC6 != null) { if (trackOfC6.isHidden == 1) { System.out.print("[]" + "\t" + "\t"); } else { System.out.print(trackOfC6.number + "" + trackOfC6.suit + "\t" + "\t"); } trackOfC6 = trackOfC6.next; } else { System.out.print("\t" + "\t"); } if (trackOfC7 != null) { if (trackOfC7.isHidden == 1) { System.out.print("[]" + "\t" + "\t"); } else { System.out.print(trackOfC7.number + "" + trackOfC7.suit + "\t" + "\t"); } trackOfC7 = trackOfC7.next; } else { System.out.print("\t" + "\t"); } System.out.println(); if (trackOfC1 == null && trackOfC2 == null && trackOfC3 == null && trackOfC4 == null && trackOfC5 == null && trackOfC6 == null && trackOfC7 == null) { done = true; } } } static char[] SR(char[] allCards) { char shuffledCards[] = new char[104]; int checker[] = new int[52]; int random = 0; int k = 0, i = 0; boolean isTheSame = true, done = false; while (!done) { while (isTheSame) { random = getRandomNumber(-1, 103); for (int j = 0; j < checker.length - 1; j++) { if (checker[j] == random) { isTheSame = true; break; } else { isTheSame = false; } } } checker[i] = random; i++; shuffledCards[k] = allCards[random]; k++; shuffledCards[k] = allCards[random + 1]; k++; isTheSame = true; for (int j = 0; j < shuffledCards.length; j++) { if (shuffledCards[j] == '\0') { done = false; isTheSame = true; break; } else { done = true; isTheSame = false; } } } return shuffledCards; } static int getRandomNumber(int min, int max) { boolean again = true; int random = 0; while (again) { random = (int) (Math.random() * (max - min) + min); if (random % 2 == 0) { again = false; } } return random; } static File LD(){ System.out.print("Which file do you want to load: "); Scanner sc = new Scanner(System.in); String firstCommand = sc.nextLine(); File file = new File(firstCommand); if(file.exists()){ System.out.println("Opening file"); }else{ System.out.println("File does not exist, opening standard deck"); file = new File("Cards.txt"); } return file; } static void chooseShuffle(){ System.out.println("SR for random shuffling, or SI for splitting in half and shuffling each pile onto a new " + "shuffled pile"); Scanner sc = new Scanner(System.in); } public static void main(String[] args) { File file = LD(); char allCards[] = new char[104]; Scanner sc = null; try { sc = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); } int k = 0; while (sc.hasNextLine()) { String currentLine = sc.nextLine(); allCards[k] = currentLine.charAt(0); k++; allCards[k] = currentLine.charAt(1); k++; } sc.close(); sc = new Scanner(System.in); addInShuffledCardsIntoColumn(SR(allCards)); boolean gameFinished = false; while(!gameFinished){ System.out.println(); printBoard(); System.out.println("LAST COMMAND: " + lastCommand); System.out.println("Message: " + message); System.out.print("INPUT > "); String stringCommands = sc.nextLine(); char commands[] = stringCommands.toCharArray(); try { moveCard(commands, selectFromPile(commands), selectToPile(commands)); }catch (Exception e){ message = "Invalid command"; } lastCommand = stringCommands; } } } /*public static void main(String[] args) { File file = new File("Cards.txt"); char allCards[] = new char[104]; Scanner sc = null; try { sc = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); } int k = 0; while(sc.hasNextLine()) { String currentLine = sc.nextLine(); allCards[k] = currentLine.charAt(0); k++; allCards[k] = currentLine.charAt(1); k++; } addInShuffledCardsIntoColumn(SR(allCards)); printBoard(); } } */ /*public static void main(String[] args) { File file = new File("Cards.txt"); char allCards[] = new char[104]; Scanner sc = null; try { sc = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); } int k = 0; while(sc.hasNextLine()) { String currentLine = sc.nextLine(); allCards[k] = currentLine.charAt(0); k++; allCards[k] = currentLine.charAt(1); k++; } addInShuffledCardsIntoColumn(SI(allCards)); printBoard(); } } */ /*//tilføj noget på C1 (logik) V //Fra c2->C1 (logik) V //Fra C2:AC->C1 (logik) V Card card1 = new Card(); card1.number = '2'; card1.suit = 'H'; Card card2 = new Card(); card2.number = 'A'; card2.suit = 'C'; moveCardToPile(card1, C1); moveCardToPile(card2, C2); char commands[] = new char[20]; commands[0] = 'C'; commands[1] = '2'; commands[2] = ':'; commands[3] = 'A'; commands[4] = 'C'; commands[5] = '-'; commands[6] = '>'; commands[7] = 'C'; commands[8] = '1'; System.out.println("before: " + C1.tail.suit); moveCard(commands, selectFromPile(commands), selectToPile(commands)); System.out.println(C1.tail.suit); } /*Card chosenCard = chooseFromSpecificCardInColumn('A','2', C1); if(chosenCard != null) { moveCardsToPile(chosenCard, C2); } */