package solitaire.game;
import java.util.ArrayList;
import java.util.Random;
import solitaire.pile.*;
import solitaire.card.*;
public class Game {
static public ArrayList<Card> allCard;//存储所有纸牌
static public CardPile allPiles [];
static public DeckPile deckPile;
static public DiscardPile discardPile;
static public TablePile tablePile [];
static public SuitPile suitPile [];
static public MoveCardPile moveCard;//干么用的?
static{
//初始化纸牌
allCard = new ArrayList<Card>();
for (int i = 0; i < 4; i++)
for (int j = 0; j <= 12; j++)
allCard.add(new Card(j, i));
//洗牌
Random generator = new Random();
for (int i = 0; i < 52; i++) {
int j = Math.abs(generator.nextInt() % 52);
// swap the two card values
Card temp = allCard.get(i);
allCard.set(i, allCard.get(j));
allCard.set(j, temp);
}
//初始化牌堆
allPiles = new CardPile[13];
suitPile = new SuitPile[4];
tablePile = new TablePile[7];
// then fill them in
allPiles[0] = deckPile = new DeckPile(200, 40);
allPiles[1] = discardPile = new DiscardPile(200+Card.width+50, 40);
for (int i = 0; i < 4; i++)
allPiles[2+i] = suitPile[i] =
new SuitPile(200+Card.width+50 + Card.width + 150 + (40+Card.width) * i, 40);
for (int i = 0; i < 7; i++)
allPiles[6+i] = tablePile[i] =
new TablePile(200 + (50+Card.width) * i, 40 + Card.height + 40, i);
////////////////////////////////////////
for (int i = 0; i < 7; i++){
ArrayList<Card> al = new ArrayList<Card>();
for(int j = 0;j < tablePile[i].getCardNum();j++){
al.add(allCard.remove(allCard.size()-1));
}
tablePile[i].addCard(al);
tablePile[i].setCardNum(tablePile[i].getNotFlipNum()+1);
//System.out.println(tablePile[i].getCardNum());
tablePile[i].top().setFaceup(true);
}
//////////////////////////////////////////////////
//把分配完TablePile的牌放入未发放堆中
int rest = allCard.size();
for(int i = 0;i < rest;i++ ){
deckPile.addCard(allCard.remove(allCard.size()-1));
//System.out.println( i);
}
moveCard = new MoveCardPile();
}
//从discard移出一张牌放入deck中
public static void transferFromDiscardToDeck(){
// System.out.println( "transferFromDiscardToDeck");
while(!(discardPile.isEmpty())){
Card card = discardPile.pop();
card.setFaceup(false);
deckPile.addCard(card);
}
}
public static boolean testDeckPile(int x,int y){//x和y应该是鼠标点击的坐标吧......
int selectNum = deckPile.select(x, y);
if(selectNum >= 0){
discardPile.addCard(deckPile.pop());
// System.out.println("deckPile_selectNum:"+selectNum);
return true;
}
else if(selectNum == -2){
Game.transferFromDiscardToDeck();
// System.out.println("deckPile_selectNum:"+-2);
return true;
}
else{
// System.out.println("deckPile_selectNum:"+-1);
return false;
}
// System.out.println("deckSize:"+deckPile.thePile.size() );
}
//////////////////////////////
public static boolean testDisCardPile(int x,int y){
int selectNum = discardPile.select(x, y);
if(selectNum >= 0){
moveCard.clear();
moveCard.addCard(discardPile.pop());
moveCard.setFromPile(discardPile);
// System.out.println("discardPile_selectNum:"+selectNum);
return true;
}
else if(selectNum == -2){
// System.out.println("discardPile_selectNum:"+-2);
}
else{
// System.out.println("discardPile_selectNum:"+-1);
}
//System.out.println("discardSize:"+discardPile.thePile.size() );
return false;
}
public static boolean testTablePile(int x,int y){
boolean isDrag = false;
for(int i = 0;i<tablePile.length;i++){
int selectNum = tablePile[i].select(x, y);
if(selectNum >= 0){
moveCard.clear();
int num = tablePile[i].getCardNum();
for(int j = selectNum;j < num;j++){
moveCard.addCard(tablePile[i].pop());
}
moveCard.setFromPile(tablePile[i]);
//System.out.println("moveCard_size:"+moveCard.size() );
return true;
}
else{
// System.out.println("tablePile["+i+"]_selectNum:"+-1);
}
//System.out.println("tablePile["+i+"]_size:"+tablePile[i].thePile.size() );
}
return isDrag;
}
public static boolean isCanAddToSuitPile(int x,int y){
if(moveCard.size()==1){
for(int i = 0;i < 4;i++){
if(suitPile[i].includes(x, y)){
if(suitPile[i].isCanAdd(moveCard.getCard())){
suitPile[i].addCard(moveCard.removeCard());
return true;
}
}
}
}
return false;
}
public static boolean isCanAddtoTablePile(int x,int y){
for(int i=0;i<7;i++){
if(tablePile[i].includes(x, y)){
if(tablePile[i].hashCode()!=moveCard.getFromPile().hashCode())
if(tablePile[i].isCanAdd(moveCard.getCard())){
tablePile[i].addCard(moveCard.clear());
return true;
}
}
}
return false;
}
public static void refreshTablePile(){
// System.out.println("refreshTablePile");
for(int i=0;i<7;i++){
if(tablePile[i].top() != null)
if(!(tablePile[i].top().isFaceup())){
tablePile[i].top().setFaceup(true);
tablePile[i].setNotFlipNum(tablePile[i].getNotFlipNum()-1);
}
}
}
public static void returnToFromPile(){
if(moveCard.getFromPile()!=null)
if(moveCard.getFromPile().hashCode()==discardPile.hashCode()){
while(!(moveCard.isEmpty())){
moveCard.getFromPile().addCard(moveCard.removeCard());
}
}
else
moveCard.getFromPile().addCard(moveCard.clear());
}
}