//Author: ^-^ Veerle ^-^ import java.awt.*; import java.util.Vector; public class solitareDefaultLayout extends Canvas { protected final int gapFaceDown = 10, gapFaceUp = 20; //Gaps in pixels for face down and face up cards protected solitareCard[] cardObject = new solitareCard[52]; protected Image[] imgCardBack = null; protected Vector vecColumn0 = new Vector(); protected Vector vecColumn1 = new Vector(); protected Vector vecColumn2 = new Vector(); protected Vector vecColumn3 = new Vector(); protected Vector vecColumn4 = new Vector(); protected Vector vecColumn5 = new Vector(); protected Vector vecColumn6 = new Vector(); protected Vector vecDeck = new Vector(); protected Vector vecDrag = new Vector(); protected Vector vecAce0 = new Vector(); protected Vector vecAce1 = new Vector(); protected Vector vecAce2 = new Vector(); protected Vector vecAce3 = new Vector(); protected int[] columnFaceUpStartIndex = new int[7]; //Used to store the first index number of each vector that has a face up card protected int[] columnFaceUpStartLocation = new int[7]; //Used to store the amount of pixels from the top that the first face up card is found at protected Vector[] vecColumns = {vecColumn0, vecColumn1, vecColumn2, vecColumn3, vecColumn4, vecColumn5, vecColumn6}; protected Vector[] vecAces = {vecAce0, vecAce1, vecAce2, vecAce3}; public void defaultSoliareLayout () { } public void paint (Graphics g) { g.setColor(new Color(75,141,221)); g.fillRect(0,0,640,600); //Paint the background colour over everything paintSlot(g, 10, 10); //The deck slot holder for (int column = 0; column < 7; column++) //Draw the stack holders { paintSlot(g, ((column * 90) + 10), 120); } for (int column = 0; column < 4; column++) //Draw the ace holders { paintSlot(g, ((column * 90) + 280), 10); } drawExtra(g); } public void update (Graphics g) { paint(g); } private void paintSlot (Graphics g, int startX, int startY) { g.setColor(new Color(149,146,140)); //Grey g.fillRect(startX, startY, 71, 96); //Makes a slot holder g.setColor(new Color(0,0,0)); //black g.drawRect(startX, startY, 71, 96); //Gives the holder a border } protected void drawExtra (Graphics g) { } public void setupCards (Image[] imgCards, Image imgCardBack[]) { int counter = 0; for (int colour = 0; colour < 4; colour++) { for (int rank = 0; rank < 13; rank++) { cardObject[counter] = new solitareCard(colour, (rank + 1), imgCards[counter]); counter++; } } this.imgCardBack = imgCardBack; } protected void shuffleCards () { solitareCard[] newArray = new solitareCard[52]; //Creates a new array to put the shuffled cards in int randomNum = 0, counter = 0; boolean[] isUsed = new boolean[52]; //States if a number has already been used do { randomNum = 1 + (int) (Math.random() * 52); //Get a random num between 1 and 52 randomNum--; //Decrease so it is between 0 and 51 if (!isUsed[randomNum]) //If the number hasn't been used before { newArray[counter] = cardObject[randomNum]; //Put the random card into the new array isUsed[randomNum] = true; //Set it to used so it can't be inserted again counter++; } } while (counter <= 51); cardObject = newArray; //Put the shuffled deck back into cardObject } protected void layoutCards () { resetVectors(); int counter = 0; for (int column = 0; column < 7; column++) //Adds 28 cards to the 7 columns { for (int row = 0; row < (column + 1); row++) //Adds them to each column, increasing the amount in each column by 1 { if (row == column) //If the last card to be placed { columnFaceUpStartIndex[column] = row; //Sets the index number of each column that the first face up card is at columnFaceUpStartLocation[column] = (120 + (columnFaceUpStartIndex[column] * gapFaceDown)); //Sets the location in pixels of the first face up card cardObject[counter].setFaceDown(false); //Let it be seen } else { cardObject[counter].setFaceDown(true); //Hide it } vecColumns[column].addElement(cardObject[counter]); //Add the current card to the column's list counter++; } } for (int card = 28; card < 52; card++) //Add the remaining cards to the deck { cardObject[card].setFaceDown(false); //All of them are visible (when seen) vecDeck.addElement(cardObject[card]); //Add to the deck vector } } private void resetVectors () { for (int i = 0; i < 7; i++) { vecColumns[i].clear(); } for (int i = 0; i < 4; i++) { vecAces[i].clear(); } vecDeck.clear(); vecDrag.clear(); } }