package oop;
import java.util.ArrayList;
import java.util.Random;
public class Game {
public ArrayList<Card> allCard;
public CardPile allPiles [];
public DeckPile deckPile;
public DiscardPile discardPile;
public TablePile tablePile [];
public SuitPile suitPile [];
public MoveCardPile moveCard;
public Game(){
//初始化纸牌
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(900, 40);
allPiles[1] = discardPile = new DiscardPile(900+Card.width+50, 40);
for (int i = 0; i < 4; i++)
allPiles[2+i] = suitPile[i] =
new SuitPile(Card.width+50 + Card.width + (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);
}
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();
}
public void transferFromDiscardToDeck(){
// System.out.println( "transferFromDiscardToDeck");
while(!(discardPile.isEmpty())){
Card card = discardPile.pop();
card.setFaceup(false);
deckPile.addCard(card);
}
}
public boolean testDeckPile(int x,int 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){
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 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 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 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 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 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 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());
}
}
评论0
最新资源