//the draw function will pull a single card from the drawPile and return it CardSolitaire Draw() { CardSolitaire cd = drawPile [0]; //pull the 0th CardSolitaire drawPile.RemoveAt(0); //then remove it from List<> drawPile return(cd); //and return it }
public void DrawMoreCards() { for (int i = 0; i < 10 && i < drawPile.Count; i++) { CardSolitaire cd = Draw(); cd.faceUp = true; cd.Join(firstRowOfCards[i].BottomOfStack(), true); } }
public void OnTriggerEnter(Collider other) { //get CardSolitaire if (SpiderSolitaire.S.Card != null && SpiderSolitaire.S.Card.name == name && other.GetComponent <CardSolitaire> ().lesserCard == null && other.GetComponent <CardSolitaire> ().faceUp) { targetCard = other.GetComponent <CardSolitaire>(); print("New target card:" + targetCard.name); } }
/* * void OnMouseEnter () { * if (SpiderSolitaire.S.Card == null) { * SpiderSolitaire.S.Card = this.gameObject; * } * } */ //rejoin all lower cards private void RejoinCards() { CardSolitaire temp = this; while (temp.lesserCard != null) { temp.lesserCard.Join(temp); temp = temp.lesserCard; } }
public void OnTriggerLeave(Collider other) { if (greaterCard == null && targetCard != null) { if (other.gameObject.GetComponent <CardSolitaire>() != null && other.gameObject.GetComponent <CardSolitaire> () == targetCard) { targetCard = null; } } }
//Split this card from the stack it's resting on while still keeping track of the others void Split() { //update last position and card lastPosition = transform.position; print("Last position:" + lastPosition); lastCard = greaterCard; //unlink card above from this card greaterCard.lesserCard = null; //unlink it from the card above it greaterCard = null; //remove the parenting of this card from the greater card transform.parent = SpiderSolitaire.S.layoutAnchor; }
//moves the current target to the discardPile void MoveToDiscard(CardSolitaire cd) { //set the state of the card to discard cd.state = CardState.discard; discardPile.Add(cd); //add it to the discardPile List<> cd.transform.parent = layoutAnchor; //update its transform parent cd.transform.localPosition = new Vector3(layout.multiplier.x * layout.discardPile.x, layout.multiplier.y * layout.discardPile.y, -layout.discardPile.layerID + 0.5f); //^position it on the discard pile cd.faceUp = true; //Place it on top of the pile for depth sorting cd.SetSortingLayerName(layout.discardPile.layerName); cd.SetSortOrder(-100 + discardPile.Count); }
//cardClicked is called any time a card in the game is clicked public void CardClicked(CardSolitaire cd) { //the reaction is determined by the stated of the clicked card switch (cd.state) { case CardState.target: //clicking the target card does nothing break; case CardState.drawpile: //clicking any card in the drawPile will draw the nesxt card MoveToDiscard(target); //moves the target to the discard pile UpdateDrawPile(); //Restacks the drawPile ScoreManager(ScoreEvent.draw); break; case CardState.tableau: //clicking a card in the tableau will check if it's a valid play bool validMatch = true; if (!cd.faceUp) { //if the card is face-down, it's not valid validMatch = false; } if (!AdjacentRank(cd, target)) { //if it's not an adjacent rank, it's not valid validMatch = false; } if (!validMatch) { return; //return if not valid } //yay! it's a valid card. tableau.Remove(cd); //Remove it from the tableau List SetTableauFaces(); //update tableau card face-ups ScoreManager(ScoreEvent.mine); break; } //check to see wheether the game is over or not CheckForGameOver(); }
//Join this card to a stack with a card of higher rank public void Join(CardSolitaire other, bool lerp = false) { //link greater card to us other.lesserCard = this; //link ourselves to the greater card greaterCard = other; //update column index row = other.row + 1; //set this card to be a child of the previous card transform.parent = greaterCard.transform; //align this card to the one above it Vector3 pos = new Vector3(0, -1, 0.125f); if (lerp) { Move(pos + transform.parent.position, 0.5f); } else { transform.localPosition = pos; } if (greaterCard == null) { SetSortingLayerName("Row0"); } else { SetSortingLayerName((greaterCard.GetSortingOrderLayerName().StartsWith("Row") ? "Row" + row : "PickedUp")); if (GetSortingOrderLayerName().Equals("PickedUp") && greaterCard != null) { SetSortOrder(greaterCard.GetTopSortOrder() + 3); //set this card to be the layer above the card it's on top of } } CheckForCompleteStack(); //check if the newly formed stack is complete }
//Return true if the two cards are adjacent in rank (A & K wrap around) public bool AdjacentRank(CardSolitaire c0, CardSolitaire c1) { //if either card is face-down, it's not adjacent if (!c0.faceUp || !c1.faceUp) { return(false); } //if they are 1 apart, they are adjacent if (Mathf.Abs(c0.rank - c1.rank) == 1) { return(true); } //if one is A and the other King, they're adjacent if (c0.rank == 13 && c1.rank == 1) { return(true); } if (c0.rank == 1 && c1.rank == 13) { return(true); } //Otherwise, return false return(false); }
void Awake() { lesserCard = null; greaterCard = null; targetCard = null; }
///check if we have a complete stack of cards void CheckForCompleteStack() { //ints to track the end points of our stack int highestRank, LowestRank; //card object for traversing linked list of cards CardSolitaire currentCard = this; bool[] cardsInStack = new bool[13]; cardsInStack [rank - 1] = true; //find greatest card while (currentCard.greaterCard != null) { if (cardsInStack[currentCard.rank - 1] && currentCard.greaterCard.LesserRank(currentCard)) { currentCard = currentCard.greaterCard; cardsInStack [currentCard.rank - 1] = true; } else { break; } } //assign rank to greatest highestRank = currentCard.rank; //traverse list again currentCard = this; //look for least rank while (currentCard.lesserCard != null) { if (cardsInStack[currentCard.rank - 1] && currentCard.LesserRank(currentCard.lesserCard)) { currentCard = currentCard.lesserCard; cardsInStack [currentCard.rank - 1] = true; } else { break; } } //assign to lowest LowestRank = currentCard.rank; if (cardsInStack[0] && cardsInStack[cardsInStack.Length - 1]) //(highestRank == 13 && LowestRank == 1) { //move cards out of way and remove stack { while (currentCard.greaterCard != null) { //remove parent link to greater card/stack currentCard.transform.parent = null; //move card to discard area currentCard.Move(new Vector3(0, -8, 0), 0.25f); //set card to inactive currentCard.active = false; if (currentCard.rank == 13) { currentCard.greaterCard.faceUp = true; break; } //select next card currentCard = currentCard.greaterCard; } currentCard.active = false; currentCard.Move(new Vector3(0, -8, 0), 0.25f); } }
//Returns true if the other card may be placed on this card, i.e. the other card is a lesser adjacent rank bool LesserRank(CardSolitaire other) { return(other.rank == this.rank - 1); }
//LayoutGame() positions the initial tableau of cards, a.k.a. the "mine" void LayoutGame() { //Create an empty GameObject to serve as an anchor for the tableau if (layoutAnchor == null) { GameObject tGO = new GameObject("_LayoutAnchor"); //^ Create an empty GameObject named _LayoutAnchor in the Hierarchy layoutAnchor = tGO.transform; //grab its Tranform layoutAnchor.transform.position = layoutCenter; //position it } CardSolitaire cp; //initialize first row of cards array slotPositions = new Transform[10]; firstRowOfCards = new CardSolitaire[slotPositions.Length]; int slotCards = 0; //follow the layout foreach (SlotDef tSD in layout.slotDefs) { //^Iterate through all the SlotDefs in the layout.slotDefs as tSD cp = Draw(); //pull a card from the top (beginning) of the drawPile if (tSD.type == "slot") { firstRowOfCards[slotCards] = cp; slotPositions[slotCards] = cp.transform; cp.faceUp = false; slotCards++; } cp.faceUp = tSD.faceUp; //set its faceUp to the value in slotDef cp.transform.parent = layoutAnchor; //make its parent layoutAnchor //this replaces the previous parent: deck.deckAnchor, which appears as _Deck in the Hierarchy when the scene is playing. cp.transform.localPosition = new Vector3(layout.multiplier.x * tSD.x, layout.multiplier.y * tSD.y, -tSD.layerID); //^Set the localPosition of the card based on slotDef cp.layoutID = tSD.id; cp.slotDef = tSD; cp.state = CardState.tableau; //CardSolitaires in the tableau have the state CardState.tableau cp.SetSortingLayerName(tSD.layerName); //set the sorting layers tableau.Add(cp); //add this CardSolitaire to the List<> tableau } /* * //Set which cards are hiding others * foreach (CardSolitaire tCP in tableau) { * foreach(int hid in tCP.slotDef.hiddenBy){ * cp=FindCardByLayoutID(hid); * tCP.hiddenBy.Add(cp); * } * } */ //add other cards to stack for (int i = 0; i < 3 * firstRowOfCards.Length + 4; i++) { //get bottom of current card CardSolitaire bottomCard = firstRowOfCards[i % firstRowOfCards.Length].BottomOfStack(); //flip this card over bottomCard.faceUp = false; //add this card to that stack CardSolitaire tempCard = Draw(); //tempCard.transform.Find("back").GetComponent<SpriteRenderer>().sortingLayerName = bottomCard.GetSortingOrderLayerName(); tempCard.SetSortingLayerName(bottomCard.GetSortingOrderLayerName()); //set its row tempCard.Join(bottomCard); } //set up the Draw pile UpdateDrawPile(); }