You are on page 1of 7

import java.awt.

Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.io.File;
import java.net.URL;
import java.util.List;
import javax.imageio.ImageIO;

public class KlondikeBoard {


Image back;
boolean firstClick = true;
private static final String[] RANKS = new String[]{"ace", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "jack", "queen", "king"};
private static final String[] SUITS = new String[]{"spades", "hearts", "
diamonds", "clubs"};
private static final int[] POINT_VALUES = new int[]{1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13};
public PlayingPile PlayingPile1 = new PlayingPile(50,250);
public PlayingPile PlayingPile2 = new PlayingPile(175,250);
public PlayingPile PlayingPile3 = new PlayingPile(300,250);
public PlayingPile PlayingPile4 = new PlayingPile(425,250);
public PlayingPile PlayingPile5 = new PlayingPile(550,250);
public PlayingPile PlayingPile6 = new PlayingPile(675,250);
public PlayingPile PlayingPile7 = new PlayingPile(800, 250);
PlayingPile[] playingPiles = {null, PlayingPile1, PlayingPile2, Playi
ngPile3, PlayingPile4, PlayingPile5 , PlayingPile6 , PlayingPile7 };
public HomePile HomePile1 = new HomePile();
public HomePile HomePile2 = new HomePile();
public HomePile HomePile3 = new HomePile();
public HomePile HomePile4 = new HomePile();
public Deck deck = new Deck(RANKS, SUITS, POINT_VALUES);
public CardPile cardPile = new CardPile();
public int lastClick;
List<Card> tempList;
public KlondikeBoard(){
deck.shuffle();
PlayingPile1.add(deck.deal());
for (int i=0; i<2; i++){
PlayingPile2.add(deck.deal());
}
for (int i=0; i<3; i++){
PlayingPile3.add(deck.deal());
}
for (int i=0; i<4; i++){
PlayingPile4.add(deck.deal());
}
for (int i=0; i<5; i++){
PlayingPile5.add(deck.deal());
}
for (int i=0; i<6; i++){
PlayingPile6.add(deck.deal());
}
for (int i=0; i<7; i++){
PlayingPile7.add(deck.deal());
}
Card a = PlayingPile1.getCard(0);
a.turnFacingUp();
PlayingPile1.setCard(0, a);
a = PlayingPile2.getCard(1);
a.turnFacingUp();
PlayingPile2.setCard(1, a);
a = PlayingPile3.getCard(2);
a.turnFacingUp();
PlayingPile3.setCard(2, a);
a = PlayingPile4.getCard(3);
a.turnFacingUp();
PlayingPile4.setCard(3, a);
a = PlayingPile5.getCard(4);
a.turnFacingUp();
PlayingPile5.setCard(4, a);
a = PlayingPile6.getCard(5);
a.turnFacingUp();
PlayingPile6.setCard(5, a);
a = PlayingPile7.getCard(6);
a.turnFacingUp();
PlayingPile7.setCard(6, a);
openImage();
}
public void draw(Graphics g) {
PlayingPile1.draw(g);
PlayingPile2.draw(g);
PlayingPile3.draw(g);
PlayingPile4.draw(g);
PlayingPile5.draw(g);
PlayingPile6.draw(g);
PlayingPile7.draw(g);
HomePile1.draw(g,600,50);
HomePile2.draw(g,700,50);
HomePile3.draw(g,800,50);
HomePile4.draw(g,900,50);
cardPile.draw(g, 150, 50);
if (deck.size() == 0)
g.drawRect(50, 50, 73, 97);
else
g.drawImage(back, 50, 50, 73, 97, null);
}
public void openImage(){
try {
URL url = getClass().getResource("cards/back1.GIF");
back = ImageIO.read(url);
} catch (Exception e) {
System.out.println("Problem opening");
e.printStackTrace();
}
}
public void clickedAt(MouseEvent click) {
int x = click.getX();
int y = click.getY();

if (x>50 && x<123 && y>50 && y<147){


System.out.println("deck click");
if (deck.size() >=3) {
for (int i=0; i<3; i++){
cardPile.add(deck.deal());
System.out.println("DEAL DEAL DEAL");
}
}
else if (deck.size()==0){
while(cardPile.size() != 0) {
Card test = cardPile.remove(cardPile.siz
e() - 1);
System.out.println(test);
deck.add(test);
}
System.out.println(deck.size()+"*");
System.out.println(cardPile.size()+"!");
}
else {
for (int i=0; i<deck.size(); i++){
cardPile.add(deck.deal());
System.out.println("LESS THAN 3 CARDS");
}
}
}
if(firstClick){
if (y>249 && y<587){
int playingPile = 1;
int xtemp = x;
while(xtemp>50){
xtemp-= 125;
if(xtemp > 50){
playingPile++;
}
}
int cardIndex = getCardIndex(playingPiles[playin
gPile], y);

//System.out.println(playingPile +" "+ cardIndex


);
System.out.println("The Card index is " + cardIn
dex);
System.out.println("The Card index is " + cardIn
dex);
System.out.println("The Card index is " + cardIn
dex);
System.out.println("The Card index is " + cardIn
dex);
System.out.println("The Card index is " + cardIn
dex);
if (playingPiles[playingPile].getCard(cardIndex)
.isFacingUp()){
playingPileClick(playingPile, cardIndex)
;
firstClick = false;
lastClick = playingPile;
}
}
}
//--------------------------------------
else if (firstClick==false){
if (y>249 && y<587){
int playingPile = 1;
int xtemp = x;
while(xtemp>50){
xtemp-= 125;
if(xtemp > 50){
playingPile++;
}
}

if(!(playingPiles[playingPile]==playingPiles[las
tClick])){// not the same pile
System.out.println("adding cards to empt
y pile");
PlayingPile tempPlayingPile = getPlaying
Pile(playingPile);// moving to
if(tempPlayingPile.size()==0){// trying
to move to an empty pile
if(tempList.get(0).pointValue()=
=13){// king
for(int i = 0; i<tempLis
t.size(); i++){
tempPlayingPile.
add(tempList.get(i));
}
tempList = playingPiles[
lastClick].removeSelected();
for(Card a: tempList){
playingPiles[pla
yingPile].add(a);
}
return;
}
else{
// you are trying to mov
e non-king to open spot, reset
//deselect()
}
}
else{// at least one card, is it face up
?
System.out.println("trying to mo
ve "+tempList.get(0));
Card tempCard = tempPlayingPile.
getCard(tempPlayingPile.size()-1);
if(!tempCard.isFacingUp()){
//deselect
return;
}
System.out.println("temp card "+
tempCard+" is face up");
// face up, so check
if (tempCard.pointValue() - 1 ==
tempList.get(0).pointValue()){// ranks line up
System.out.println("tryi
ng to move "+tempList.get(0)+" onto "+tempCard);
if(tempCard.isRed() != t
empList.get(0).isRed()){
System.out.print
ln("Opposite color");
for(int i = 0; i
<tempList.size(); i++){
tempPlay
ingPile.add(tempList.get(i));
}
//playingPiles[p
layingPile] = tempPlayingPile;
}
// else if (!tempCard.isRed
() && tempList.get(0).isRed()){
// for(int i = 0; i
<tempList.size(); i++){
// tempPlay
ingPile.add(tempList.get(i));
// }
// playingPiles[pla
yingPile] = tempPlayingPile;
// }
}
else{
// point values not with
in one
//deselect
return;
}
// tempList = playingPiles[lastClic
k].removeSelected();
// for(Card a: tempList){
// playingPiles[playingPile
].add(a);
// }
}
}
}
playingPiles[lastClick].deselect();
firstClick = true;
}
}

private int getCardIndex(PlayingPile playingPile, int y) {


int size = playingPile.size();
System.out.println(size + " GETCARDINDEX");
int cardIndex = 0;
int firstCard = (size-1)*20 + 250;
if(y>firstCard && firstCard + 97>y){
return size - 1;
}
else if(y>250 && y <firstCard) {
while(y>250){
y-=20;
cardIndex++;
}
}
return cardIndex - 1;
}
private void playingPileClick(int playingPile, int cardIndex) {
System.out.println("playingPileClick in board");
tempList = playingPiles[playingPile].clickedAt(cardIndex);
}
private PlayingPile getPlayingPile(int playingPile){
return playingPiles[playingPile];
}
public void deckClick(){

System.out.println("deck click");
if (deck.size() >=3) {
for (int i=0; i<3; i++){
cardPile.add(deck.deal());
System.out.println("DEAL DEAL DEAL");
}
}
else if (deck.size()==0){
while(cardPile.size() != 0) {
Card test = cardPile.remove(cardPile.size() - 1)
;
System.out.println(test);
deck.add(test);
}
System.out.println(deck.size()+"*");
System.out.println(cardPile.size()+"!");
}
else {
for (int i=0; i<deck.size(); i++){
cardPile.add(deck.deal());
System.out.println("LESS THAN 3 CARDS");
}
}

}
}

You might also like