Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Game {
- private MainDeck mainDeck;
- private FinalDeck[] finalsArr;
- private Column[] colsArr;
- private int score;
- public Game() {
- Deck cardsDeck = new Deck();
- mainDeck = new MainDeck(cardsDeck.getCards(24));
- finalsArr = new FinalDeck[4];
- for (int f = 0; f < 4; f++)
- finalsArr[f] = new FinalDeck();
- colsArr = new Column[7];
- for (int c = 0; c < 7; c++) {
- int count = c + 1;
- colsArr[c] = new Column(cardsDeck.getCards(count));
- }
- score = 0;
- }
- public void start() {
- Scanner in = new Scanner(System.in);
- System.out.println(getInstructions());
- do {
- System.out.println(this);
- } while (makeMove(in) && !gameIsOver());
- in.close();
- if (gameIsOver()) {
- System.out.println("\n\nGame is Over!");
- System.out.println("Your score is " + score + " points");
- } else {
- System.out.println("\n\nSee you later!");
- System.out.println("Your score is " + score + " points");
- }
- }
- private String getInstructions() {
- String res = "Please enter a three digits integer number: \n";
- res += "<source><target><amount>\n";
- res += "to move <amount> cards from <source> to <target>";
- return res;
- }
- private boolean gameIsOver() {
- for (int f = 0; f < 4; f++) {
- if (!finalsArr[f].isFull())
- return false;
- }
- return true;
- }
- private boolean makeMove(Scanner in) {
- System.out.print("Select your move (source, target, count): ");
- int m = in.nextInt();
- int src, trgt, count;
- if (m < 10) {
- src = m;
- trgt = -1;
- count = -1;
- } else if (m < 100) {
- src = m / 10;
- trgt = m % 10;
- count = 1;
- } else {
- src = m / 100;
- trgt = m / 10 % 10;
- count = m % 10;
- }
- switch (src) {
- case 0:
- break;
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- if (trgt >= 1 && trgt <= 7) {
- if (src != trgt)
- colsArr[src - 1].moveToColumn(colsArr[trgt - 1], count);
- } else if (trgt == 9) {
- for (int f = 0; f < 4; f++)
- if (colsArr[src - 1].moveToFinal(finalsArr[f])) {
- score += 10;
- break;
- }
- }
- break;
- case 8:
- if (trgt == -1) {
- mainDeck.showCard();
- break;
- } else if (trgt >= 1 && trgt <= 7) {
- if (mainDeck.moveToColumn(colsArr[trgt - 1]))
- score += 5;
- } else if (trgt == 9) {
- for (int f = 0; f < 4; f++)
- if (mainDeck.moveToFinalDeck(finalsArr[f])) {
- score += 10;
- break;
- }
- }
- break;
- case 9:
- if (trgt >= 1 && trgt <= 7) {
- for (int f = 0; f < 4; f++) {
- if (finalsArr[f].moveToTemp(colsArr[trgt - 1]))
- break;
- }
- }
- break;
- }
- return m != 0;
- }
- public String toString() {
- String res = String.format("\n%s %7s %s %s %s %s\n\n", mainDeck, "", finalsArr[0], finalsArr[1], finalsArr[2],
- finalsArr[3]);
- String[][] colsStr = new String[7][];
- int max = 0;
- for (int c = 0; c < 7; c++) {
- colsStr[c] = colsArr[c].toStringArray();
- max = Math.max(max, colsStr[c].length);
- res = res + String.format(" %s ", c + 1);
- }
- res = res + String.format("\n");
- for (int i = 0; i < max; i++) {
- for (int c = 0; c < 7; c++) {
- if (i < colsStr[c].length)
- res = res + String.format(" %5s ", colsStr[c][i]);
- else
- res = res + String.format(" %5s ", "");
- }
- res = res + String.format("\n");
- }
- res = res + String.format("Your score is " + score + " points");
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement