📄 solitare.java
字号:
package homeTask_06_solitare;/*Simple Solitare Card Game in JavaWritten by Tim Budd, Oregon State University, 1996*/import java.awt.*;import java.applet.*;class Card {// constructorCard(int sv, int rv) { s = sv; r = rv; faceup = false;}// access attributes of cardpublic int rank() { return r;}public int suit() { return s;}public boolean faceUp() { return faceup;}public void flip() { faceup = !faceup; }public int color() { if (suit() == heart || suit() == diamond) return red; return black;}public void draw(Graphics g, int x, int y) { String names[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; // clear rectangle, draw border g.clearRect(x, y, width, height); g.setColor(Color.black); g.drawRect(x, y, width, height); // draw body of card if (faceUp()) { if (color() == red) g.setColor(Color.red); else g.setColor(Color.blue); g.drawString(names[rank()], x + 3, y + 15); if (suit() == heart) { g.drawLine(x + 25, y + 30, x + 35, y + 20); g.drawLine(x + 35, y + 20, x + 45, y + 30); g.drawLine(x + 45, y + 30, x + 25, y + 60); g.drawLine(x + 25, y + 60, x + 5, y + 30); g.drawLine(x + 5, y + 30, x + 15, y + 20); g.drawLine(x + 15, y + 20, x + 25, y + 30); } else if (suit() == spade) { g.drawLine(x + 25, y + 20, x + 40, y + 50); g.drawLine(x + 40, y + 50, x + 10, y + 50); g.drawLine(x + 10, y + 50, x + 25, y + 20); g.drawLine(x + 23, y + 45, x + 20, y + 60); g.drawLine(x + 20, y + 60, x + 30, y + 60); g.drawLine(x + 30, y + 60, x + 27, y + 45); } else if (suit() == diamond) { g.drawLine(x + 25, y + 20, x + 40, y + 40); g.drawLine(x + 40, y + 40, x + 25, y + 60); g.drawLine(x + 25, y + 60, x + 10, y + 40); g.drawLine(x + 10, y + 40, x + 25, y + 20); } else if (suit() == club) { g.drawOval(x + 20, y + 25, 10, 10); g.drawOval(x + 25, y + 35, 10, 10); g.drawOval(x + 15, y + 35, 10, 10); g.drawLine(x + 23, y + 45, x + 20, y + 55); g.drawLine(x + 20, y + 55, x + 30, y + 55); g.drawLine(x + 30, y + 55, x + 27, y + 45); } } else { // face down g.setColor(Color.yellow); g.drawLine(x + 15, y + 5, x + 15, y + 65); g.drawLine(x + 35, y + 5, x + 35, y + 65); g.drawLine(x + 5, y + 20, x + 45, y + 20); g.drawLine(x + 5, y + 35, x + 45, y + 35); g.drawLine(x + 5, y + 50, x + 45, y + 50); }}// data fields for colors and suitsfinal static int width = 50;final static int height = 70;final static int red = 0;final static int black = 1;final static int heart = 0;final static int spade = 1;final static int diamond = 2;final static int club = 3;// private static String names[] = {"A", "2", "3", "4", "5", "6",// "7", "8", "9", "10", "J", "Q", "K"};// data fieldsprivate boolean faceup;private int r;private int s;public Card link;}class CardPile {CardPile(int xl, int yl) { x = xl; y = yl; firstCard = null;}// access to cards are not overriddenprotected int getX(){ return x;}protected int getY(){ return y;} public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public Card top() { return firstCard;}public boolean empty() { return firstCard == null;}public Card pop() { Card result = null; if (firstCard != null) { result = firstCard; firstCard = firstCard.link; result.link = null; } return result;}// the following are sometimes overriddenpublic boolean includes(int tx, int ty) { return x <= tx && tx <= x + Card.width && y <= ty && ty <= y + Card.height;}public void select(int tx, int ty) {// do nothing }public void addCard(Card aCard) { aCard.link = firstCard; firstCard = aCard;}public void display(Graphics g) { g.setColor(Color.black); if (firstCard == null) g.drawRect(x, y, Card.width, Card.height); else firstCard.draw(g, x, y);}public boolean canTake(Card aCard) { return false;}// coordinates of the card pileprotected int x;protected int y;private Card firstCard;}class DeckPile extends CardPile {DeckPile(int x, int y) { // first initialize parent super(x, y); // then create the new deck // first put them into a local pile CardPile pileOne = new CardPile(0, 0); CardPile pileTwo = new CardPile(0, 0); int count = 0; for (int i = 0; i < 4; i++) for (int j = 0; j <= 12; j++) { pileOne.addCard(new Card(i, j)); count++; } // then pull them out randomly for (; count > 0; count--) { int limit = ((int) (Math.random() * 1000)) % count; // move down to a random location for (int i = 0; i < limit; i++) pileTwo.addCard(pileOne.pop()); // then add the card found there addCard(pileOne.pop()); // then put the decks back together while (!pileTwo.empty()) pileOne.addCard(pileTwo.pop()); }}public void select(int tx, int ty) { if (empty()) {// create new DeckPile Solitare.allPiles[0] = Solitare.deckPile = new DeckPile(335, 5); Card card=Solitare.discardPile.pop(); card.flip(); Solitare.deckPile.addCard(card); card.link=null;// take into acount that cards maybe removes from diskardPile while(!Solitare.discardPile.empty()){ card=Solitare.discardPile.pop(); card.flip(); Solitare.deckPile.addCard(card); } } else // if DeckPile not empty Solitare.discardPile.addCard(pop()); }}class DiscardPile extends CardPile {DiscardPile(int x, int y) { super(x, y);}public int getX() { return x;}public int getY() { return y;} public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public void addCard(Card aCard) { if (!aCard.faceUp()) aCard.flip(); super.addCard(aCard);}}class SuitPile extends CardPile {SuitPile(int x, int y) { super(x, y);}public boolean canTake(Card aCard) { if (empty()) return aCard.rank() == 0; Card topCard = top(); return (aCard.suit() == topCard.suit()) && (aCard.rank() == 1 + topCard.rank());}public void select(int tx, int ty) { Card TopCardSubsid = Solitare.CardPileSubsid.top();if ((canTake(TopCardSubsid))&& (Solitare.CountOfRemovedCards==1)) { addCard(Solitare.CardPileSubsid.pop()); }else { Solitare.allPiles[Solitare.numberOfCurrentPile].addCard(Solitare.CardPileSubsid.pop()); } if (Solitare.allPiles[Solitare.numberOfCurrentPile]== null){ Solitare.allPiles[Solitare.numberOfCurrentPile]=new TablePileSubrid(Solitare.old_x, 80, 0); } if (!Solitare.allPiles[Solitare.numberOfCurrentPile].empty()){ if (!Solitare.allPiles[Solitare.numberOfCurrentPile].top().faceUp()){ Solitare.allPiles[Solitare.numberOfCurrentPile].top().flip();} }}}class TablePile extends CardPile {TablePile(int x, int y, int c) { // initialize the parent class super(x, y); // then initialize our pile of cards for (int i = 0; i < c; i++) { addCard(Solitare.deckPile.pop()); } // flip topmost card face up top().flip();}public int getX() { return x; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public int getY() { return y; } public boolean canTake(Card aCard) { if (empty()) return aCard.rank() == 12; Card topCardTwo = top(); return (aCard.color() != topCardTwo.color()) && (aCard.rank() == topCardTwo.rank() - 1);}public boolean includes(int tx, int ty) { int localy = 0; int count=0; //check the bottom if (x <= tx && tx <= x + Card.width) { Card topCard; topCard =top(); while(topCard!=null){ topCard=topCard.link; count++; } localy = y + 35*(count+1); } return x <= tx && tx <= x + Card.width && y <= ty && ty <= localy;}public void select(int tx, int ty) {// check rules of game and remove cards from subsidiary cardPile if its possible Card TopCardSubsid = Solitare.CardPileSubsid.pop(); if (canTake(TopCardSubsid)) { Solitare.CardPileSubsid.addCard(TopCardSubsid); for (int i = 1; i <= Solitare.CountOfRemovedCards; i++) { addCard(Solitare.CardPileSubsid.pop()); }// if not possible return them back }else{Solitare.CardPileSubsid.addCard(TopCardSubsid); for (int i = 1; i <= Solitare.CountOfRemovedCards; i++){ Solitare.allPiles[Solitare.numberOfCurrentPile].addCard(Solitare.CardPileSubsid.pop()); } } if (Solitare.allPiles[Solitare.numberOfCurrentPile]== null){ Solitare.allPiles[Solitare.numberOfCurrentPile]=new TablePileSubrid(Solitare.old_x, 80, 0); } // flip top card in pile where was removed cards if (!Solitare.allPiles[Solitare.numberOfCurrentPile].empty()){ if (!Solitare.allPiles[Solitare.numberOfCurrentPile].top().faceUp()){ Solitare.allPiles[Solitare.numberOfCurrentPile].top().flip();}} } private int stackDisplay(Graphics g, Card aCard) { int localy; if (aCard == null) return y; localy = stackDisplay(g, aCard.link); aCard.draw(g, x, localy); return localy + 35;}public void display(Graphics g) { stackDisplay(g, top());} }// create subsidiary cardPileclass TablePileSubrid extends CardPile{TablePileSubrid(int x, int y, int c) { super(x, y);} public void display(Graphics g) { DisplayPileSubrid(g, top());}// display this pile in preorder traversalprivate void DisplayPileSubrid(Graphics g, Card aCard) { for (int i = 1; i <=Solitare.CountOfRemovedCards ; i++) { if (aCard!=null) aCard.draw(g, x, y); aCard=aCard.link; y=y+35; } }}public class Solitare extends Applet {/** * */ private static final long serialVersionUID = 1L;static DeckPile deckPile;static DiscardPile discardPile;static TablePile tableau[];static SuitPile suitPile[];static CardPile allPiles[];static int old_x, old_y;static CardPile CardPileSubsid;static int numberOfCurrentPile;static int numberOfTakingPile; Card TopCardCurrent;static int CountOfRemovedCards;public void init() {//first allocate the array allPiles = new CardPile[13]; suitPile = new SuitPile[4]; tableau = new TablePile[7]; // then fill them in allPiles[0] = deckPile = new DeckPile(335, 5); allPiles[1] = discardPile = new DiscardPile(268, 5); for (int i = 0; i < 4; i++) allPiles[2 + i] = suitPile[i] = new SuitPile(15 + 60 * i, 5); for (int i = 0; i < 7; i++) allPiles[6 + i] = tableau[i] = new TablePile(5 + 55 * i, 80, i + 1);}public void paint(Graphics g) {// if there is subsidiary cardPile then paint herif(CardPileSubsid!=null){ for (int i = 0; i < 13; i++) allPiles[i].display(g); g.setColor(Color.black); CardPileSubsid.display(g); } else for (int i = 0; i < 13; i++) allPiles[i].display(g); }public boolean mouseDown(Event evt, int x, int y) { int localy;// coordinate y current subsidiary cardPile int coordPileSubsid_y = 0; if ((allPiles[1].includes(x, y)) && (!allPiles[1].empty()) ){ // if already created subsidiary cardPile but was not moves and select another pile then add card back if (CardPileSubsid!=null){ CardPileSubsid=null; allPiles[numberOfCurrentPile].addCard(TopCardCurrent); } old_x=allPiles[1].getX(); old_y=allPiles[1].getY(); TopCardCurrent = allPiles[1].pop(); numberOfCurrentPile=1; CardPileSubsid = new CardPile (0, 0); CardPileSubsid.addCard(TopCardCurrent); CountOfRemovedCards=1; } else{ for (int i = 6; i < 13; i++) if (allPiles[i].includes(x, y)&& (!allPiles[i].empty())) { if (CardPileSubsid!=null){ CardPileSubsid=null; allPiles[numberOfCurrentPile].addCard(TopCardCurrent); } old_x=allPiles[i].getX(); localy=allPiles[i].getY(); Card topCard = allPiles[i].top();// count cards in current select pile int count=0; while(topCard!=null){ topCard=topCard.link; count++; } old_y = localy + 35*(count-1); // define where were coordinates of mouse and find how many cards will remove if ((y<=old_y+70)&& (y>old_y)){ CountOfRemovedCards=1; coordPileSubsid_y = old_y; } for (int j = 2; j <= count; j++) { if ((y<=old_y+70-j*35)&&(y>old_y+70-j*35-35)){ CountOfRemovedCards=j; coordPileSubsid_y = old_y+70-j*35-35 ; } } Card TopCardTwo=allPiles[i].top();// count how many cards in current pile are faceUp int t=0; for (int j = 1; j<=CountOfRemovedCards; j++) if (TopCardTwo.faceUp()){ TopCardTwo=TopCardTwo.link; t++; } numberOfCurrentPile=i; if (t==CountOfRemovedCards) {// create subsidiary cardPile CardPileSubsid = new TablePileSubrid (old_x, coordPileSubsid_y,CountOfRemovedCards);// add cards in subsidiary cardPile for (int j = 1; j <=CountOfRemovedCards; j++) { TopCardCurrent = allPiles[i].pop(); CardPileSubsid.addCard(TopCardCurrent); } } repaint(); } return true; } return true;}@Override public boolean mouseDrag(Event evt, int x, int y) { if(CardPileSubsid!=null) { // move subsidiary cardPile CardPileSubsid.setX(x); CardPileSubsid.setY(y); repaint(); return true;} return true; }public boolean mouseUp(Event evt, int x, int y) {// if select deckPile then dont create subsidiary cardPile if (allPiles[0].includes(x, y)) { allPiles[0].select(x, y); repaint(); return true; }// else create subsidiary cardPile and check rules of game if (CardPileSubsid!=null){ for (int i = 2; i < 13; i++) if (allPiles[i].includes(x, y)) { allPiles[i].select(x, y); CardPileSubsid=null; repaint();} return true; }return true;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -