Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 54.14 KB | None | 0 0
  1. package giftest;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.Toolkit;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.event.KeyListener;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16. import javax.swing.ImageIcon;
  17.  
  18. import javax.swing.JMenuBar;
  19. import javax.swing.JPanel;
  20. import javax.swing.Timer;
  21.  
  22. public class Board extends JPanel implements ActionListener {
  23.  
  24. private Dimension d;
  25. private Image ii;
  26. private CardListener cL;
  27. private CardGame theGame;
  28. private Deck deck;
  29. private ArrayList<Stack> piles;
  30. private ArrayList<SortedStack> sortedPiles;
  31. private Shallow shallow;
  32. private MoveMemory mm;
  33. private JMenuBar menuBar_1;
  34.  
  35. // was going to implement this but not enough time.
  36. public Timer timer;
  37. //public boolean isStarted = false;
  38. //public boolean isPaused = false;
  39.  
  40.  
  41. // called if no seed is presented
  42. public Board() {
  43. // create seed based off time...
  44. long seed = System.currentTimeMillis()%32767;
  45. // load in the games data and set the board up...
  46. loadData(seed);
  47. // load the drawing assests and other nonesense...
  48. init(seed, null);
  49. timer = new Timer(400, this);
  50. timer.start();
  51.  
  52. // last minute changes before drawing....
  53. setBackground(Color.DARK_GRAY);
  54. // refresh twice as often...
  55. setDoubleBuffered(true);
  56. // obvious...
  57. setFocusable(true);
  58. }
  59.  
  60. public Board(long seed, String saveFile){
  61. // load game data and recreate board state from save...
  62. loadData(seed);
  63. // load assests and over nonesense...
  64. init(seed, saveFile);
  65. timer = new Timer(400, this);
  66. timer.start();
  67.  
  68. // last minute changes before drawing....
  69. setBackground(Color.DARK_GRAY);
  70. // refresh twice as often...
  71. setDoubleBuffered(true);
  72. // obvious...
  73. setFocusable(true);
  74. }
  75.  
  76. // add a specific card movement to move history, boolean t says if we carry.
  77. public void addMove(Card c, CardContainer prev_cc, CardContainer cur_cc, boolean t) {
  78. this.mm.addMove(new Move(c.getSerialized(), prev_cc, cur_cc, t));
  79. }
  80.  
  81. // add specific cards to move history, carry assumed false.
  82. public void addMove(ArrayList<Card> c, CardContainer prev_cc, CardContainer cur_cc) {
  83. ArrayList<String> cl = new ArrayList<>();
  84. for (Card cidx : c) {
  85. cl.add(cidx.getSerialized());
  86. }
  87. this.mm.addMove(new Move(cl, prev_cc, cur_cc));
  88. }
  89.  
  90. // add specific cards to move history, boolean t says if we carry.
  91. public void addMove(ArrayList<Card> c, CardContainer prev_cc, CardContainer cur_cc, boolean t) {
  92. ArrayList<String> cl = new ArrayList<>();
  93. for (Card cidx : c) {
  94. cl.add(cidx.getSerialized());
  95. }
  96. this.mm.addMove(new Move(cl, prev_cc, cur_cc, t));
  97. }
  98.  
  99. private void init(long seed, String saveFile) {
  100. d = new Dimension(400, 400);
  101. Card.back = new ImageIcon(this.getClass().getResource("assets/back01.gif")).getImage();
  102.  
  103. //record or set the boards initial state...
  104. theGame = new Solitaire(this);
  105. if(saveFile != null){
  106. mm = new MoveMemory(this, seed, saveFile);
  107. } else {
  108. mm = new MoveMemory(this, seed);
  109. }
  110. theGame.processMove();
  111.  
  112. menuBar_1 = new JMenuBar();
  113. //menuBar_1.setBounds(0, 0, 441, 21);
  114. this.add(menuBar_1);
  115.  
  116. cL = new CardListener(this, theGame);
  117. this.addMouseMotionListener(cL);
  118. this.addMouseListener(cL);
  119.  
  120. // messy keylistener; didnt deserve its own class...
  121. this.addKeyListener(new KeyListener() {
  122.  
  123. @Override
  124. public void keyTyped(KeyEvent ke) {}
  125. @Override
  126. public void keyReleased(KeyEvent ke) {}
  127. @Override
  128. public void keyPressed(KeyEvent ke) {
  129. if(ke.isControlDown()){
  130. if(ke.getKeyCode() == KeyEvent.VK_Z){
  131. mm.Undo();
  132. theGame.processMove();
  133. }
  134. if(ke.getKeyCode() == KeyEvent.VK_S){
  135. try {
  136. mm.saveGame();
  137. } catch (IOException ex) {
  138. Logger.getLogger(Board.class.getName()).log(Level.SEVERE, null, ex);
  139. }
  140. }
  141. }
  142. }
  143. });
  144. }
  145.  
  146. public CardContainer getContainerByID(String id){
  147. for (Stack s : piles) {
  148. if(s.getID() == null ? id == null : s.getID().equals(id)){
  149. return s;
  150. }
  151. }
  152.  
  153. if(deck.getID() == null ? id == null : deck.getID().equals(id)){
  154. return deck;
  155. }
  156.  
  157. if(shallow.getID() == null ? id == null : shallow.getID().equals(id)){
  158. return shallow;
  159. }
  160.  
  161. for (SortedStack ssl : sortedPiles) {
  162. if(ssl.getID() == null ? id == null : ssl.getID().equals(id)){
  163. return ssl;
  164. }
  165. }
  166.  
  167. return null;
  168. }
  169.  
  170. private void loadData(long seed){
  171. //loads new data...
  172. // add deck..
  173. deck = new Deck(seed, "deck");
  174. deck.shuffle();
  175. deck.shuffle();
  176.  
  177. // make some stacks from the deck..
  178. piles = new ArrayList<>();
  179. for (int n = 0; n < 7; n++) {
  180. piles.add(new Stack(n * 32, 60, "stack"+n));
  181. for (int a = 0; a < n + 1; a++) {
  182. deck.dealCard(piles.get(n));
  183. }
  184. piles.get(n).getTopCard().setRevealed(true);
  185. }
  186.  
  187. // specifiy a shallow...
  188. shallow = new Shallow(40, 0, "shallow");
  189.  
  190. // add sorted piles...
  191. sortedPiles = new ArrayList<>();
  192. for (int idx = 0; idx < 4; idx++) {
  193. sortedPiles.add(new SortedStack(32 * (3 + idx), 0, "sortedstack"+idx));
  194. }
  195. }
  196.  
  197.  
  198. // draws all the cards in all of the containers...
  199. private void drawCards(Graphics g) {
  200. for (Stack s : piles) {
  201. s.drawCards(g, this);
  202. }
  203.  
  204. for (Card c : deck.getDeck()) {
  205. c.draw(g, this);
  206. }
  207.  
  208. for (Card c : getShallow().getStack()) {
  209. c.draw(g, this);
  210. }
  211.  
  212. for (SortedStack ssl : sortedPiles) {
  213. for (Card c : ssl.getStack()) {
  214. c.draw(g, this);
  215. }
  216. }
  217.  
  218. if (cL.isDragging()) {
  219. for (Card c : cL.getManipulatedCards()) {
  220. c.draw(g, this);
  221. }
  222.  
  223. }
  224.  
  225. }
  226.  
  227. // finds the manipulated card and removes it from its container.
  228. public boolean reportManipulatedCard(Card c) {
  229. for (Stack s : piles) {
  230. for (Card c_idx : s.getStack()) {
  231. if (c_idx == c) {
  232. return s.removeCard(c);
  233. }
  234. }
  235. }
  236. return deck.removeCard(c);
  237. }
  238.  
  239.  
  240. // gets all cards and returns it as a list.
  241. public ArrayList<Card> getCards() {
  242. ArrayList<Card> cards = new ArrayList<>();
  243. for (Stack s : piles) {
  244. cards.addAll(s.getStack());
  245. }
  246. cards.addAll(deck.getStack());
  247. cards.addAll(shallow.getStack());
  248. for (CardContainer s : sortedPiles) {
  249. if (!s.cards.isEmpty()) {
  250. cards.addAll(s.getStack());
  251. }
  252. }
  253. return cards;
  254. }
  255.  
  256. public ArrayList<Stack> getStacks() {
  257. return piles;
  258. }
  259.  
  260. public ArrayList<SortedStack> getSortedStacks() {
  261. return sortedPiles;
  262. }
  263.  
  264. public Deck getDeck() {
  265. return deck;
  266. }
  267.  
  268. public Shallow getShallow() {
  269. return shallow;
  270. }
  271.  
  272. public MoveMemory getMoveMemory() {
  273. return mm;
  274. }
  275.  
  276. // gets the stack containing a specific card.
  277. public Stack getStackContaining(Card c) {
  278. for (Stack s : piles) {
  279. for (Card c_idx : s.getStack()) {
  280. if (c == c_idx) {
  281. return s;
  282. }
  283. }
  284. }
  285. return null;
  286. }
  287.  
  288. // gets the real container of the card; not a instanced reference of it...
  289. // wish I could use pointers...
  290. public CardContainer getContainer(CardContainer cc_ref){
  291. for(CardContainer cc : this.getStacks()){
  292. if(cc == cc_ref){
  293. return cc;
  294. }
  295. }
  296. for(CardContainer cc : this.getSortedStacks()){
  297. if(cc == cc_ref){
  298. return cc;
  299. }
  300. }
  301. if(cc_ref == this.getShallow()){
  302. return this.getShallow();
  303. }
  304. //should never reach this.
  305. return null;
  306. }
  307.  
  308. @Override
  309. public void paintComponent(Graphics g) {
  310. super.paintComponent(g);
  311. drawCards(g);
  312. g.drawImage(ii, 0, 0, this);
  313. Toolkit.getDefaultToolkit().sync();
  314. g.dispose();
  315. System.out.println("paint");
  316. }
  317.  
  318. @Override
  319. public void actionPerformed(ActionEvent e) {
  320. repaint();
  321. }
  322.  
  323. public void forceRepaint() {
  324. repaint();
  325. }
  326.  
  327. }
  328. /*
  329. * To change this license header, choose License Headers in Project Properties.
  330. * To change this template file, choose Tools | Templates
  331. * and open the template in the editor.
  332. */
  333. package giftest;
  334.  
  335. import java.awt.Dimension;
  336. import java.awt.Graphics;
  337. import java.awt.Image;
  338. import java.awt.Point;
  339. import java.awt.Rectangle;
  340. import java.awt.image.ImageObserver;
  341.  
  342.  
  343. // card is very forward; doesnt require too much commenting because the names
  344. // of the methods and variables are pretty obvious.
  345. public class Card {
  346. private final int SUIT;
  347. private final int VALUE;
  348. // point in space.
  349. private Point p;
  350. // dimensions of the cards
  351. private int width;
  352. private int height;
  353. private boolean revealed;
  354. // hight with 0 being the bottom and n being the top. 0 <= n < highest int
  355. private int stackHeight;
  356. private CardContainer container;
  357. // card face to draw.
  358. public Image i;
  359. // card back; static because call cards share one instance of the image.
  360. public static Image back;
  361.  
  362. // DONT USE THIS!!
  363. Card(int suit, int value){
  364. this.SUIT = suit;
  365. this.VALUE = value;
  366. }
  367.  
  368. Card(Image i, int suit, int value){
  369. this.i = i;
  370. this.SUIT = suit;
  371. this.VALUE = value;
  372. this.revealed = true;
  373. this.stackHeight = 0;
  374. this.container = null;
  375. p = new Point(0,0);
  376. width = 32;
  377. height = 48;
  378. }
  379.  
  380. Card(Image i, int suit, int value, CardContainer container){
  381. this.i = i;
  382. this.SUIT = suit;
  383. this.VALUE = value;
  384. this.revealed = false;
  385. this.stackHeight = 0;
  386. this.container = container;
  387. p = new Point(0,0);
  388. width = 32;
  389. height = 48;
  390. }
  391.  
  392. Image getImage(){
  393. return revealed ? i : back;
  394. }
  395.  
  396. void setPos(int x, int y){
  397. p = new Point(x, y);
  398. }
  399.  
  400. void setPos(Point pos){
  401. p = pos;
  402. }
  403.  
  404. void setContainer(CardContainer cc){
  405. this.container = cc;
  406. }
  407.  
  408. Point getPos(){
  409. return p;
  410. }
  411.  
  412. CardContainer getContainer(){
  413. return container;
  414. }
  415.  
  416. Point getCenterPoint(){
  417. return new Point(p.x+(width/2), p.y+(height/2));
  418. }
  419.  
  420. int getValue(){
  421. return VALUE;
  422. }
  423.  
  424. int getSuit(){
  425. return SUIT;
  426. }
  427.  
  428. // returns the card in a form that can be saved and loaded from file.
  429. String getSerialized(){
  430. String derp = "";
  431. derp += this.VALUE > 9 ? this.VALUE : "0"+this.VALUE;
  432. derp += "0"+this.SUIT;
  433.  
  434. return derp;
  435. }
  436.  
  437. void setRevealed(boolean b){
  438. revealed = b;
  439. }
  440.  
  441. void setStackHeight(int d){
  442. stackHeight = d;
  443. }
  444.  
  445. int getStackHeight(){
  446. return stackHeight;
  447. }
  448.  
  449. int x(){
  450. return p.x;
  451. }
  452.  
  453. int y(){
  454. return p.y;
  455. }
  456.  
  457. boolean isRevealed(){
  458. return revealed;
  459. }
  460.  
  461. // draws the front or back of the card based on revealed
  462. void draw(Graphics g, ImageObserver io){
  463. g.drawImage((revealed ? i : back), p.x, p.y, io);
  464. }
  465.  
  466. // returns true if the point is inside the bounds of the card.
  467. boolean containsPoint(Point p){
  468. Rectangle r = new Rectangle(this.p, new Dimension(width, height));
  469. return r.contains(p);
  470. }
  471.  
  472. // takes a serialized card and turns it into a card instance with no image.
  473. static Card deSerialize(String id){
  474. String derp = id+"";
  475.  
  476. int val = Integer.parseInt(derp.substring(2, 4));
  477. int suit = Integer.parseInt(derp.substring(0, 2));
  478. return new Card(val, suit);
  479. }
  480. }
  481. /*
  482. * To change this license header, choose License Headers in Project Properties.
  483. * To change this template file, choose Tools | Templates
  484. * and open the template in the editor.
  485. */
  486. package giftest;
  487.  
  488. import java.awt.Dimension;
  489. import java.awt.Graphics;
  490. import java.awt.Point;
  491. import java.awt.Rectangle;
  492. import java.awt.image.ImageObserver;
  493. import java.util.ArrayList;
  494.  
  495. /**
  496. *
  497. * @author Ryland
  498. */
  499.  
  500. // does this really need any comments? its all very readable...
  501. public abstract class CardContainer {
  502. protected ArrayList<Card> cards;
  503. protected Point pos;
  504. protected int width;
  505. protected int height;
  506. protected String id;
  507.  
  508. CardContainer(){
  509. cards = new ArrayList<>();
  510. pos = new Point(0,0);
  511. width = 32;
  512. height = 48;
  513. }
  514.  
  515. CardContainer(int x, int y){
  516. cards = new ArrayList<>();
  517. pos = new Point(x,y);
  518. width = 32;
  519. height = 48;
  520. }
  521.  
  522. public void setStack(ArrayList<Card> s){
  523. cards = s;
  524. }
  525.  
  526. public void addCard(Card c){
  527. cards.add(c);
  528. processCard(c);
  529. }
  530.  
  531. public boolean addCards(ArrayList<Card> cL){
  532. for(Card c : cL){
  533. if(cards.contains(c)){
  534. return false;
  535. } else {
  536. cards.add(c);
  537. processCard(c);
  538. }
  539. }
  540. return true;
  541. }
  542.  
  543. public ArrayList<Card> getStack(){
  544. return cards;
  545. }
  546.  
  547. public boolean removeCard(Card c){
  548. for(Card c_idx : cards){
  549. if(c_idx == c){
  550. cards.remove(c);
  551. processCard(null);
  552. return true;
  553. }
  554. }
  555. return false;
  556. }
  557.  
  558. public boolean removeCards(ArrayList<Card> cL){
  559. for(Card c : cL){
  560. if(!cards.remove(c)){return false;}
  561. }
  562. return true;
  563. }
  564.  
  565. public Card getTopCard(){
  566. if(cards.isEmpty()){return null;}
  567. return cards.get(cards.size()-1);
  568. }
  569.  
  570.  
  571. String getID(){
  572. return id;
  573. }
  574.  
  575. // check to see if the coordinate is inside the cards bounds...
  576. boolean containsPoint(Point p){
  577. Rectangle r = new Rectangle(this.pos, new Dimension(width, height));
  578. return r.contains(p);
  579. }
  580.  
  581. // made for any specific behaviour the cardcontainers might want to do...
  582. abstract void processCard(Card c);
  583. // handles any funny ways to draw the card if they want to do anyting...
  584. abstract void drawCards(Graphics g, ImageObserver io);
  585. }
  586. /*
  587. * To change this license header, choose License Headers in Project Properties.
  588. * To change this template file, choose Tools | Templates
  589. * and open the template in the editor.
  590. */
  591. package giftest;
  592.  
  593. import java.util.ArrayList;
  594.  
  595. /**
  596. *
  597. * @author Ryland
  598. */
  599. public interface CardGame {
  600.  
  601. // can a card move to this container?
  602. public boolean canMoveCardTo(CardContainer cc, Card c);
  603. // can a stack of cards be changed?
  604. public boolean canManipulateStack(ArrayList<Card> c);
  605. // do logic after a move is done...
  606. public void processMove();
  607. // do something when a card container is clicked?
  608. public void interact(CardContainer cL);
  609. }
  610. /*
  611. * To change this license header, choose License Headers in Project Properties.
  612. * To change this template file, choose Tools | Templates
  613. * and open the template in the editor.
  614. */
  615. package giftest;
  616.  
  617. import java.awt.Point;
  618. import java.awt.event.MouseAdapter;
  619. import java.awt.event.MouseEvent;
  620. import java.util.ArrayList;
  621.  
  622. class CardListener extends MouseAdapter {
  623.  
  624. private final Board b;
  625. private ArrayList<Card> manipulatedCards;
  626. private boolean dragging;
  627. private Point absHandle;
  628. private CardContainer lastCardContainer;
  629. private final CardGame theGame;
  630.  
  631. CardListener(Board b, CardGame theGame) {
  632. // get reference of the board
  633. this.b = b;
  634. // get reference of the game being played...
  635. this.theGame = theGame;
  636. // initialize other things...
  637. manipulatedCards = new ArrayList<>();
  638. dragging = false;
  639. }
  640.  
  641. ArrayList<Card> getManipulatedCards() {
  642. return manipulatedCards;
  643. }
  644.  
  645. boolean isDragging() {
  646. return dragging;
  647. }
  648.  
  649. @Override
  650. public void mouseDragged(MouseEvent me) {
  651. // if were holding a card....
  652. if (dragging) {
  653. // for each card were moving...
  654. for (int idx = 0; idx < manipulatedCards.size(); idx++) {
  655. // move them to our new mouse location with an offset
  656. // based on where we click the card...
  657. Point p = new Point(
  658. me.getPoint().x - absHandle.x,
  659. me.getPoint().y - absHandle.y + +20 * idx
  660. );
  661. // set its position to our current cursor pos + the offset
  662. // given by where we held the card.
  663. manipulatedCards.get(idx).setPos(p);
  664. }
  665. // repaint because we dont want to look like were running at 2 fps..
  666. b.forceRepaint();
  667. }
  668. }
  669.  
  670. @Override
  671. public void mouseMoved(MouseEvent me) {
  672.  
  673. }
  674.  
  675. @Override
  676. public void mousePressed(MouseEvent me) {
  677. manipulatedCards = new ArrayList<>();
  678. // get the container we clicked on if we clicked on a container...
  679. CardContainer cc = getContainer(me.getPoint());
  680. // we didnt click on the container, do nothing...
  681. if (cc == null) {
  682. return;
  683. }
  684. // we clicked on the deck so dont allow a drag but interact instead.
  685. if (cc.getClass() == Deck.class) {
  686. theGame.interact(cc);
  687. return;
  688. }
  689.  
  690. // if the container we clicked on is not empty...
  691. if (!cc.getStack().isEmpty()) {
  692. // get the card we clicked on.
  693. Card c = getCard(me.getPoint());
  694. // if its the shallow only allow the top card as our selected card.
  695. if (cc.getClass() == Shallow.class) {
  696. if (c != cc.getTopCard()) {
  697. return;
  698. }
  699. }
  700.  
  701. //get all cards under the selected card.
  702. for (int idx = c.getStackHeight(); idx < cc.getStack().size(); idx++) {
  703. manipulatedCards.add(cc.getStack().get(idx));
  704. }
  705. // see if we can move these cards based on the games logic...
  706. if (theGame.canManipulateStack(manipulatedCards)) {
  707. // if so start dragging...
  708. dragging = true;
  709. // record the last container it was in...
  710. lastCardContainer = manipulatedCards.get(0).getContainer();
  711. // and create a relative offset based on click...
  712. absHandle = new Point(
  713. me.getPoint().x - manipulatedCards.get(0).getPos().x,
  714. me.getPoint().y - manipulatedCards.get(0).getPos().y
  715. );
  716. // remove the cards from their' container.
  717. manipulatedCards.get(0).getContainer().removeCards(manipulatedCards);
  718. }
  719. }
  720. }
  721.  
  722. @Override
  723. public void mouseReleased(MouseEvent me) {
  724. // if were dragging some cards...
  725. if (dragging && manipulatedCards != null) {
  726. // see if we dropped them into another container...
  727. CardContainer cc = approximateDrop(me);
  728. // if we did then see if were allowed to move the cards to that
  729. // container based on the games logic...
  730. if (theGame.canMoveCardTo(cc, manipulatedCards.get(0))) {
  731. // if we did then record the cards movement for the movement list.
  732. CardContainer prev_cc = manipulatedCards.get(0).getContainer();
  733. // move the card to the new container...
  734. cc.addCards(manipulatedCards);
  735. b.addMove(manipulatedCards, prev_cc, manipulatedCards.get(0).getContainer());
  736. // let the game do logic to determine if it needs to do anything.
  737. theGame.processMove();
  738. } else {
  739. // move the card to the last container it was in...
  740. lastCardContainer.addCards(manipulatedCards);
  741. }
  742. // clear the card list for later...
  743. manipulatedCards = new ArrayList<>();
  744. // clear the container for later..
  745. lastCardContainer = null;
  746. // tell the game were no longer holding cards...
  747. dragging = false;
  748. }
  749. }
  750.  
  751. private CardContainer approximateDrop(MouseEvent me) {
  752. Point cCenter = manipulatedCards.get(0).getCenterPoint();
  753. //try to see if our mouse is hovering over a top card or an empty container...
  754. CardContainer best_match = getContainer(me.getPoint());
  755. if (best_match != null) {
  756. return best_match;
  757.  
  758. }
  759. // try to see if the center of our card is hovering over the top of a
  760. // card or an empty container...
  761. best_match = getContainer(cCenter);
  762. if (best_match != null) {
  763. return best_match;
  764. }
  765. // return whatever container the cards were already in as a last effort
  766. // to find a container.
  767. return manipulatedCards.get(0).getContainer();
  768. }
  769.  
  770. // looks for the container that contains the point p and returns the
  771. // container; returns null if no container was found.
  772. private CardContainer getContainer(Point p) {
  773. for (Stack s : b.getStacks()) {
  774. if ((s.containsPoint(p) && s.getStack().isEmpty()) || s.containsPoint(p)) {
  775. return (CardContainer) s;
  776. }
  777. }
  778.  
  779. if ((b.getShallow().getTopCard() != null) && b.getShallow().containsPoint(p)) {
  780. return b.getShallow();
  781. }
  782.  
  783. for (Card c_idx : b.getCards()) {
  784. if (c_idx.containsPoint(p)) {
  785. return c_idx.getContainer();
  786. }
  787. }
  788.  
  789. return null;
  790. }
  791.  
  792. // finds the card that intersects with point p that is the closest to the
  793. // top
  794. private Card getCard(Point p) {
  795. int height = -1;
  796. Card c = null;
  797. for (Card c_idx : b.getCards()) {
  798. if (c_idx.containsPoint(p) && (c_idx.getStackHeight() > height)) {
  799. c = c_idx;
  800. }
  801. }
  802. return c;
  803. }
  804. }
  805. /*
  806. * To change this license header, choose License Headers in Project Properties.
  807. * To change this template file, choose Tools | Templates
  808. * and open the template in the editor.
  809. */
  810. package giftest;
  811.  
  812. import java.awt.Graphics;
  813. import java.awt.image.ImageObserver;
  814. import java.net.URL;
  815. import java.util.ArrayList;
  816. import javax.swing.ImageIcon;
  817.  
  818.  
  819. public class Deck extends CardContainer {
  820. // clubs, diamonds, hearts, spades.
  821. private final String suits = "cdhs";
  822. private final long seed;
  823.  
  824. public Deck(long seed, String id){
  825. super();
  826. this.id = "deck";
  827. this.seed = seed;
  828. String str = "";
  829. // load a deck of cards!
  830. // values...
  831. for (int n = 1; n <= 13; n++) {
  832. // suits...
  833. for (int s = 0; s < 4; s++) {
  834. // basic parsing for assets...
  835. if (n <= 9) {
  836. str = "assets/0" + n + suits.charAt(s) + ".gif";
  837. } else {
  838. str = "assets/" + n + suits.charAt(s) + ".gif";
  839. }
  840. // get the image...
  841. URL loc = this.getClass().getResource(str);
  842. // load the image...
  843. ImageIcon iia = new ImageIcon(loc);
  844. // hold a reference of the image in the card.
  845. Card c = new Card(iia.getImage(), s, n, this);
  846. // add the card to the deck.
  847. cards.add(c);
  848. }
  849. }
  850. }
  851.  
  852. public void shuffle(){
  853. // super mario 64 rng code!
  854. // I seriously cannot step through this code but I can assure you it is
  855. // sufficently random; unfortunately java doesnt support unsigned
  856. // variables so Ive had to improvise...
  857. for(short idx = 0; idx < cards.size(); idx++){
  858. short in = (short)(idx+seed);
  859. short s0 = (short) (in << 8);
  860. s0 = (short) (s0 ^ in);
  861. in = (short) (((s0 & 0xFF) << 8) | ((s0 & 0xFF00) >> 8));
  862. s0 = (short) ((s0 << 1) ^ in);
  863. short s1 = (short) ((s0 >> 1) ^ 0xFF80);
  864. if((s0 & 1) == 0){
  865. in = (short) (s1 ^ 0x1FF4);
  866. } else {
  867. in = (short) (s1 ^ 0x8180);
  868. }
  869.  
  870. // java doesnt support unsigned... :(
  871. Card temp = cards.get(Math.abs(in%cards.size()));
  872. cards.set(Math.abs(in%cards.size()), cards.get(idx));
  873. cards.set(idx, temp);
  874. }
  875. }
  876.  
  877. public Card drawCard(){
  878. Card c = this.getTopCard();
  879. cards.remove(c);
  880. processCard(null);
  881. return c;
  882. }
  883.  
  884. public void addToBottom(Card c){
  885. cards.add(0, c);
  886. processCard(c);
  887. }
  888.  
  889. public void dealCard(Stack p){
  890. p.addCard(cards.get(cards.size()-1));
  891. cards.remove(cards.size()-1);
  892. processCard(null);
  893. }
  894.  
  895. public ArrayList<Card> getDeck(){
  896. return cards;
  897. }
  898.  
  899. @Override
  900. public void drawCards(Graphics g, ImageObserver io) {
  901. for(Card c : cards){
  902. c.draw(g, io);
  903. }
  904. }
  905.  
  906. @Override
  907. public void processCard(Card c) {
  908. if(c != null){
  909. c.setPos(this.pos);
  910. c.setRevealed(false);
  911. c.setContainer(this);
  912. }
  913. getTopCard().setRevealed(false);
  914. }
  915. }
  916. /*
  917. * To change this license header, choose License Headers in Project Properties.
  918. * To change this template file, choose Tools | Templates
  919. * and open the template in the editor.
  920. */
  921. package giftest;
  922.  
  923. import java.awt.EventQueue;
  924. import java.io.BufferedReader;
  925. import java.io.FileNotFoundException;
  926. import java.io.FileReader;
  927. import java.io.IOException;
  928. import javax.swing.JFileChooser;
  929. import javax.swing.JFrame;
  930. import javax.swing.JLabel;
  931.  
  932. public class Main extends JFrame {
  933.  
  934. JLabel statusbar;
  935. private MainMenuPanel mmp;
  936.  
  937. public Main() {
  938. // create the main menu panel and add it to the frame
  939. mmp = new MainMenuPanel(this);
  940. this.add(mmp);
  941. // set some basic stuff fo the panel
  942. setTitle("Pacman");
  943. setDefaultCloseOperation(EXIT_ON_CLOSE);
  944. setSize(32*8, 48*6);
  945. setVisible(true);
  946. }
  947.  
  948.  
  949. // removes anything in the panel and creates the game from scratch.
  950. public void makeGame(){
  951. this.remove(mmp);
  952. this.add(new Board());
  953. setVisible(true);
  954. }
  955.  
  956. // removes everything from panel and loads the game from a saved state
  957. public void loadGame() throws FileNotFoundException, IOException{
  958. // open a file chooser to go get the file.
  959. final JFileChooser fc = new JFileChooser();
  960. fc.showOpenDialog(this);
  961. // get the name of the file
  962. String saveFile = fc.getSelectedFile().toString();
  963. // does it end with .txt?
  964. if(!saveFile.endsWith(".txt")){
  965. // nope; cry about it.
  966. System.out.println("bad save!");
  967. } else {
  968. // remove the main menu panel and read the random seed of the game.
  969. this.remove(mmp);
  970. BufferedReader r = new BufferedReader(new FileReader(saveFile));
  971. String seed = r.readLine();
  972. // create a new instance of solitaire with the seed.
  973. this.add(new Board(Long.parseLong(seed), saveFile));
  974. setVisible(true);
  975. }
  976. }
  977.  
  978. public JLabel getStatusBar() {
  979. return statusbar;
  980. }
  981.  
  982. public static void main(String[] args) {
  983.  
  984. EventQueue.invokeLater(new Runnable() {
  985. // run on a new thread....
  986. @Override
  987. public void run() {
  988. Main ex = new Main();
  989. ex.setVisible(true);
  990. }
  991. });
  992. }
  993. }
  994. /*
  995. * To change this license header, choose License Headers in Project Properties.
  996. * To change this template file, choose Tools | Templates
  997. * and open the template in the editor.
  998. */
  999. package giftest;
  1000.  
  1001. import java.io.IOException;
  1002. import java.util.logging.Level;
  1003. import java.util.logging.Logger;
  1004.  
  1005. /**
  1006. *
  1007. * @author Ryland
  1008. */
  1009. public class MainMenuPanel extends java.awt.Panel {
  1010.  
  1011. Main m;
  1012.  
  1013. public MainMenuPanel(Main m) {
  1014. initComponents();
  1015. this.m = m;
  1016. }
  1017.  
  1018. /**
  1019. * This method is called from within the constructor to initialize the form.
  1020. * WARNING: Do NOT modify this code. The content of this method is always
  1021. * regenerated by the Form Editor.
  1022. */
  1023. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  1024. private void initComponents() {
  1025.  
  1026. jButton1 = new javax.swing.JButton();
  1027. jPanel1 = new javax.swing.JPanel();
  1028. jLabel1 = new javax.swing.JLabel();
  1029. filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
  1030. jButton2 = new javax.swing.JButton();
  1031. jButton3 = new javax.swing.JButton();
  1032. jButton4 = new javax.swing.JButton();
  1033.  
  1034. jButton1.setText("jButton1");
  1035.  
  1036. setLayout(new java.awt.BorderLayout());
  1037.  
  1038. jPanel1.setAutoscrolls(true);
  1039. jPanel1.setPreferredSize(new java.awt.Dimension(256, 288));
  1040.  
  1041. jLabel1.setFont(new java.awt.Font("Comic Sans MS", 0, 48)); // NOI18N
  1042. jLabel1.setText("Solitaire");
  1043.  
  1044. jButton2.setAction(jButton1.getAction());
  1045. jButton2.setText("New game");
  1046. jButton2.addActionListener(new java.awt.event.ActionListener() {
  1047. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1048. jButton2ActionPerformed(evt);
  1049. }
  1050. });
  1051.  
  1052. jButton3.setText("Load game");
  1053. jButton3.addActionListener(new java.awt.event.ActionListener() {
  1054. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1055. jButton3ActionPerformed(evt);
  1056. }
  1057. });
  1058.  
  1059. jButton4.setText("Quit");
  1060. jButton4.addActionListener(new java.awt.event.ActionListener() {
  1061. public void actionPerformed(java.awt.event.ActionEvent evt) {
  1062. jButton4ActionPerformed(evt);
  1063. }
  1064. });
  1065.  
  1066. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  1067. jPanel1.setLayout(jPanel1Layout);
  1068. jPanel1Layout.setHorizontalGroup(
  1069. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  1070. .addGroup(jPanel1Layout.createSequentialGroup()
  1071. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  1072. .addGroup(jPanel1Layout.createSequentialGroup()
  1073. .addGap(31, 31, 31)
  1074. .addComponent(jLabel1))
  1075. .addGroup(jPanel1Layout.createSequentialGroup()
  1076. .addGap(62, 62, 62)
  1077. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  1078. .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  1079. .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
  1080. .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
  1081. .addGap(324, 324, 324))
  1082. );
  1083. jPanel1Layout.setVerticalGroup(
  1084. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  1085. .addGroup(jPanel1Layout.createSequentialGroup()
  1086. .addGap(26, 26, 26)
  1087. .addComponent(jLabel1)
  1088. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  1089. .addComponent(jButton2)
  1090. .addGap(18, 18, 18)
  1091. .addComponent(jButton3)
  1092. .addGap(18, 18, 18)
  1093. .addComponent(jButton4)
  1094. .addContainerGap())
  1095. );
  1096.  
  1097. add(jPanel1, java.awt.BorderLayout.CENTER);
  1098. }// </editor-fold>
  1099.  
  1100. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  1101. m.makeGame();
  1102. }
  1103.  
  1104. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  1105. try {
  1106. m.loadGame();
  1107. } catch (IOException ex) {
  1108. Logger.getLogger(MainMenuPanel.class.getName()).log(Level.SEVERE, null, ex);
  1109. }
  1110. }
  1111.  
  1112. private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
  1113. System.exit(0);
  1114. }
  1115.  
  1116.  
  1117. // Variables declaration - do not modify
  1118. private javax.swing.Box.Filler filler1;
  1119. private javax.swing.JButton jButton1;
  1120. private javax.swing.JButton jButton2;
  1121. private javax.swing.JButton jButton3;
  1122. private javax.swing.JButton jButton4;
  1123. private javax.swing.JLabel jLabel1;
  1124. private javax.swing.JPanel jPanel1;
  1125. // End of variables declaration
  1126. }
  1127. package giftest;
  1128.  
  1129. import java.util.ArrayList;
  1130.  
  1131.  
  1132. public class Move {
  1133. private ArrayList<String> Cards;
  1134. private CardContainer prev_cc;
  1135. private CardContainer cur_cc;
  1136. private boolean carry;
  1137.  
  1138. // bad constructor...
  1139. public Move(){
  1140. Cards = new ArrayList<>();
  1141. prev_cc = null;
  1142. cur_cc = null;
  1143. this.carry = false;
  1144. }
  1145.  
  1146. // single card with no carry.
  1147. public Move(String c, CardContainer prev_cc, CardContainer cur_cc){
  1148. Cards = new ArrayList<>();
  1149. Cards.add(c);
  1150. this.prev_cc = prev_cc;
  1151. this.cur_cc = cur_cc;
  1152. this.carry = false;
  1153. }
  1154.  
  1155. // many cards with no carry.
  1156. public Move(ArrayList<String> c, CardContainer prev_cc, CardContainer cur_cc){
  1157. Cards = c;
  1158. this.prev_cc = prev_cc;
  1159. this.cur_cc = cur_cc;
  1160. this.carry = false;
  1161. }
  1162.  
  1163. // many cards with carry.
  1164. public Move(ArrayList<String> c, CardContainer prev_cc, CardContainer cur_cc, boolean carry){
  1165. Cards = c;
  1166. this.prev_cc = prev_cc;
  1167. this.cur_cc = cur_cc;
  1168. this.carry = carry;
  1169. }
  1170.  
  1171. // single card with carry.
  1172. public Move(String c, CardContainer prev_cc, CardContainer cur_cc, boolean carry){
  1173. Cards = new ArrayList<>();
  1174. Cards.add(c);
  1175. this.prev_cc = prev_cc;
  1176. this.cur_cc = cur_cc;
  1177. this.carry = carry;
  1178. }
  1179.  
  1180. public void setCarry(boolean t){
  1181. this.carry = t;
  1182. }
  1183.  
  1184. public boolean getCarry(){
  1185. return this.carry;
  1186. }
  1187.  
  1188. public ArrayList<String> getSeralizedCards(){
  1189. return Cards;
  1190. }
  1191.  
  1192. public CardContainer getPreviousCardContainer(){
  1193. return prev_cc;
  1194. }
  1195.  
  1196. public CardContainer getCurrentCardContainer(){
  1197. return cur_cc;
  1198. }
  1199.  
  1200. //creats a seralized version of the move...
  1201. @Override
  1202. public String toString(){
  1203. String derp = "*";
  1204. for(int idx = 0; idx < Cards.size(); idx++){
  1205. // get the cards serialized value...
  1206. derp += Cards.get(idx)+"";
  1207. if(idx != Cards.size()-1){
  1208. derp+=",";
  1209. }
  1210. }
  1211. // get the seralized moves...
  1212. derp += "*"+prev_cc.getID()+","+cur_cc.getID()+"*";
  1213. return derp;
  1214. }
  1215. }
  1216. package giftest;
  1217.  
  1218. import java.io.BufferedReader;
  1219. import java.io.File;
  1220. import java.io.FileNotFoundException;
  1221. import java.io.FileReader;
  1222. import java.io.FileWriter;
  1223. import java.io.IOException;
  1224. import java.util.ArrayList;
  1225. import java.util.logging.Level;
  1226. import java.util.logging.Logger;
  1227.  
  1228. public class MoveMemory {
  1229.  
  1230. // reference to the board...
  1231. private Board b;
  1232. // an array of moves to keep track of the moves made...
  1233. private ArrayList<Move> moveHistory;
  1234. // the seed of the current game.
  1235. private final long seed;
  1236.  
  1237. // creates fresh move history.
  1238. public MoveMemory(Board b, long seed) {
  1239. moveHistory = new ArrayList<>();
  1240. this.b = b;
  1241. this.seed = seed;
  1242. }
  1243.  
  1244. // loads move history from a file and continues from there...
  1245. public MoveMemory(Board b, long seed, String saveFile) {
  1246. moveHistory = new ArrayList<>();
  1247. this.b = b;
  1248. this.seed = seed;
  1249. try {
  1250. // try to load the file!
  1251. loadSave(saveFile);
  1252. } catch (IOException ex) {
  1253. // woops something went wrong 50ms after initial check...
  1254. Logger.getLogger(MoveMemory.class.getName()).log(Level.SEVERE, null, ex);
  1255. }
  1256. }
  1257.  
  1258. public void addMove(Move m) {
  1259. moveHistory.add(m);
  1260. }
  1261.  
  1262. public void Undo() {
  1263. // continue until the move is not a carry move...
  1264. while (true) {
  1265. // is there a move to undo?
  1266. if (moveHistory.isEmpty()) {
  1267. // no; were done here.
  1268. break;
  1269. }
  1270. // yes; lets get the most recent move...
  1271. Move m = moveHistory.get(moveHistory.size() - 1);
  1272. // is the move a compound move?
  1273. boolean carryNext = m.getCarry();
  1274. // if we somehow got a null move.... end here; honestly cant
  1275. // remember why this is here but Im pretty certain its here for a
  1276. // reason...
  1277. if (m == null) {
  1278. break;
  1279. }
  1280. // get a shallow reference of the card that was last moved...
  1281. Card c_ref = Card.deSerialize(m.getSeralizedCards().get(0));
  1282. // for each card on the board...
  1283. for (Card c : b.getCards()) {
  1284. // if the shallow card matches the real cards values; use the
  1285. // matching card.
  1286. if (c.getValue() == c_ref.getValue() && c.getSuit() == c_ref.getSuit()) {
  1287. ArrayList<Card> cl = new ArrayList<>();
  1288. ArrayList<Card> cs = c.getContainer().getStack();
  1289. // chances are if the card is up the stack, then they were
  1290. // all moved together; so take all of the cards under the
  1291. // the given card.
  1292. for (int idx = cs.indexOf(c); idx < cs.size(); idx++) {
  1293. cl.add(cs.get(idx));
  1294. }
  1295. // if the container we're moving the cards back to is not
  1296. // empty then set the card's face to not be revealed.
  1297. if (!b.getContainer(m.getPreviousCardContainer()).getStack().isEmpty()) {
  1298. b.getContainer(m.getPreviousCardContainer()).getTopCard().setRevealed(false);
  1299. }
  1300. // move cards back to their original container...
  1301. b.getContainer(m.getPreviousCardContainer()).addCards(cl);
  1302. // and remove them from their current container...
  1303. b.getContainer(m.getCurrentCardContainer()).removeCards(cl);
  1304. // undo history! remove the history that the move ever happened.
  1305. moveHistory.remove(m);
  1306.  
  1307. }
  1308. }
  1309. // did it mention to carry to the next move?
  1310. if (!carryNext) {
  1311. // no; return.
  1312. break;
  1313. }
  1314. }
  1315. }
  1316.  
  1317. // sets a previous move to be a compound move just in case we need it...
  1318. public void setLastCarry(boolean t) {
  1319. if (moveHistory.size() > 0) {
  1320. moveHistory.get(0).setCarry(t);
  1321. }
  1322. }
  1323.  
  1324. public int getSize() {
  1325. return moveHistory.size();
  1326. }
  1327.  
  1328. public void saveGame() throws IOException {
  1329. // create a file with the seed of this game...
  1330. File file = new File("solitaire" + seed + ".txt");
  1331. file.createNewFile();
  1332.  
  1333. // write the seed of the game, followed by all the moves serialized...
  1334. FileWriter writer = new FileWriter(file);
  1335. writer.write(seed + "\n");
  1336. for (Move m : moveHistory) {
  1337. writer.write(m.toString() + "\n");
  1338. }
  1339.  
  1340. // clean up...
  1341. writer.flush();
  1342. writer.close();
  1343. }
  1344.  
  1345. private void loadSave(String saveFile) throws FileNotFoundException, IOException {
  1346. // try to open the file
  1347. BufferedReader r = new BufferedReader(new FileReader(saveFile));
  1348. // skip the seed of the game, we already have it.
  1349. r.readLine();
  1350. // loop until we have no more lines from the file.
  1351. while (true) {
  1352. String s = r.readLine();
  1353. // if there are no more lines then stop loading.
  1354. if (s == null) {
  1355. break;
  1356. }
  1357. // cut off some meta characters from the data..
  1358. s = s.substring(1, s.length() - 1);
  1359. // split the cards from the containers...
  1360. String ss[] = s.split("\\*");
  1361. // get a list of the shallow references of cards.
  1362. String[] string_list = ss[0].split(",");
  1363. ArrayList<Card> card_list = new ArrayList<>();
  1364. // only one card?
  1365. if(string_list == null) {
  1366. // hold the shallow references to help find real cards soon.
  1367. card_list.add(Card.deSerialize(ss[0]));
  1368. } else {
  1369. // many cards; iterate through them...
  1370. for(int idx = 0; idx < string_list.length; idx++){
  1371. // hold the shallow references to help find real cards soon.
  1372. card_list.add(Card.deSerialize(string_list[idx]));
  1373. }
  1374. }
  1375. // go through our cards...
  1376. for (Card c_ref : card_list) {
  1377. // go through the real cards...
  1378. for (Card c : b.getCards()) {
  1379. // find the matching real cards to our fake cards...
  1380. if (c.getValue() == c_ref.getValue() && c.getSuit() == c_ref.getSuit()) {
  1381. // and move the real cards to the corrisponding containers
  1382. // that the move specified....
  1383. c.getContainer().removeCard(c);
  1384. // work around for moving cards to the deck instead of
  1385. // the shallow...
  1386. String c_name = (ss[1].split(","))[1].equals("shallow") ? "deck" : (ss[1].split(","))[1];
  1387. // move cards to the container.
  1388. b.getContainerByID(c_name).addCard(c);
  1389. // card has been moved; its probably revealed...
  1390. c.setRevealed(true);
  1391.  
  1392. }
  1393. }
  1394. }
  1395.  
  1396. // re-serialize our cards and recreate the move history for the game.
  1397. ArrayList<String> card_serial = new ArrayList<>();
  1398. for(Card c : card_list){
  1399. card_serial.add(c.getSerialized());
  1400. }
  1401. CardContainer prev = b.getContainerByID((ss[1].split(","))[0]);
  1402. CardContainer cur = b.getContainerByID((ss[1].split(","))[1]);
  1403. moveHistory.add(new Move(card_serial, prev, cur));
  1404. }
  1405. }
  1406. }
  1407. /*
  1408. * To change this license header, choose License Headers in Project Properties.
  1409. * To change this template file, choose Tools | Templates
  1410. * and open the template in the editor.
  1411. */
  1412. package giftest;
  1413.  
  1414. import java.awt.Graphics;
  1415. import java.awt.Point;
  1416. import java.awt.image.ImageObserver;
  1417.  
  1418. public class Shallow extends CardContainer {
  1419. // how much to offset each card from eachother.
  1420. private final static int X_OFFSET = 10;
  1421.  
  1422. Shallow(){
  1423. super();
  1424. }
  1425.  
  1426. Shallow(int x, int y, String id){
  1427. super(x,y);
  1428. this.id = id;
  1429. }
  1430.  
  1431. // moves the cards by the offset...
  1432. @Override
  1433. void processCard(Card c) {
  1434. Point p = pos;
  1435. for(int idx = 0; idx < cards.size(); idx++){
  1436. cards.get(idx).setRevealed(true);
  1437. cards.get(idx).setPos(p.x+(idx*X_OFFSET), p.y);
  1438. cards.get(idx).setStackHeight(idx);
  1439. }
  1440. if(c != null){
  1441. c.setContainer(this);
  1442. }
  1443. }
  1444.  
  1445. @Override
  1446. void drawCards(Graphics g, ImageObserver io) {
  1447. for(Card c : cards){
  1448. c.draw(g, io);
  1449. }
  1450. }
  1451. }
  1452. package giftest;
  1453.  
  1454. import java.util.ArrayList;
  1455.  
  1456. /**
  1457. *
  1458. * @author Ryland
  1459. */
  1460. public class Solitaire implements CardGame {
  1461. private final Board b;
  1462.  
  1463. public Solitaire(Board b){
  1464. // hold a reference of the board...
  1465. this.b = b;
  1466.  
  1467. // set all of the tops of the stacks as revealed...
  1468. for(CardContainer p : this.b.getStacks()){
  1469. if(p.getClass() == Stack.class){
  1470. p.getTopCard().setRevealed(true);
  1471. }
  1472. }
  1473.  
  1474. }
  1475.  
  1476. @Override
  1477. public boolean canMoveCardTo(CardContainer cc, Card c){
  1478. // get the top card of the container the card is moving to...
  1479. Card c_top = cc.getTopCard();
  1480. // if the stack is a sorted stack; dont let the player move it there;
  1481. // the computer should do that.
  1482. if(cc.getClass() == SortedStack.class){return false;}
  1483. // if the container were moving to is empty, return false...
  1484. if(c_top == null){return true;}
  1485. // is our card 1 value lower than the top card of the pile?
  1486. boolean decending_value = c.getValue() == c_top.getValue()-1;
  1487. int cts = c_top.getSuit();
  1488. int cs = c.getSuit();
  1489. // is our suit a different color than the other
  1490. boolean different_suit = (cs == 0 || cs == 3) ? (cts == 1 || cts == 2) : (cts == 0 || cts == 3);
  1491. return decending_value && different_suit;
  1492. }
  1493.  
  1494. @Override
  1495. public boolean canManipulateStack(ArrayList<Card> cL) {
  1496. // get the top cards we want to move...
  1497. Card c = cL.get(0);
  1498. // if the top card is in the deck
  1499. if(c.getContainer().getClass() == Deck.class){
  1500. return false;
  1501. }
  1502. // otherwise if our card is revealed and the cards are in the right order...
  1503. if(c.isRevealed()){
  1504. for(int idx = 0; idx < cL.size()-1; idx++){
  1505. if(cL.get(idx).getValue() <= cL.get(idx+1).getValue()){
  1506. return false;
  1507. }
  1508. }
  1509. // the card was not revealed...
  1510. } else {
  1511. return false;
  1512. }
  1513. // the card is revealed and all the cards are in the right order.
  1514. return true;
  1515. }
  1516.  
  1517. @Override
  1518. public void processMove() {
  1519. ArrayList<SortedStack> ssl = b.getSortedStacks();
  1520.  
  1521. while(true){
  1522. // toggle to see if we should cycle through again.
  1523. boolean toggle = true;
  1524.  
  1525. // look througth all of the stacks on the board.
  1526. for(Stack s : b.getStacks()){
  1527. // get the top card
  1528. Card c = s.getTopCard();
  1529. // if there is a top card...
  1530. if(c != null){
  1531. int suitID = c.getSuit();
  1532. // look at the cards corrisponding sorted pile and see if
  1533. // that pile is not empty
  1534. if(!ssl.get(suitID).getStack().isEmpty()){
  1535. // if so is our card the next to go on the list
  1536. if(ssl.get(suitID).getTopCard().getValue()+1 == c.getValue()){
  1537. // then hold the previous container...
  1538. CardContainer prev_cc = c.getContainer();
  1539. // add the card to the sorted pile..
  1540. ssl.get(suitID).addCard(c);
  1541. // remove the card from its original pile
  1542. s.removeCard(c);
  1543. // then add the move to the move list.
  1544. b.addMove(c, prev_cc , c.getContainer(), true);
  1545. // tell the program to cycle through again to see
  1546. // if anything has changed...
  1547. toggle = false;
  1548. }
  1549. }
  1550. // if there is no top card then is our card an ace?
  1551. else if(c.getValue() == 1) {
  1552. // make the last move we did combine with this move
  1553. // because this move is reflexive.
  1554. b.getMoveMemory().setLastCarry(true);
  1555. // hold the container...
  1556. CardContainer prev_cc = c.getContainer();
  1557. // add the card to the sorted pile
  1558. ssl.get(suitID).addCard(c);
  1559. // remove the card from the current container...
  1560. s.removeCard(c);
  1561. // add the move to the move list
  1562. b.addMove(c, prev_cc , c.getContainer(), true);
  1563. // cycle through the list again for changes...
  1564. toggle = false;
  1565. }
  1566. }
  1567. }
  1568. // true = dont cycle, false = cycle again.
  1569. if(toggle){
  1570. break;
  1571. }
  1572. }
  1573.  
  1574. //check to see if any cards need to be revealed...
  1575. for(Stack s: b.getStacks()){
  1576. Card c = s.getTopCard();
  1577. if(c != null){
  1578. if(!c.isRevealed()){
  1579. c.setRevealed(true);
  1580. }
  1581. }
  1582. }
  1583. }
  1584.  
  1585. @Override
  1586. public void interact(CardContainer cL) {
  1587. // check to see if the container is the deck...
  1588. if(cL.getClass() == Deck.class){
  1589. // if so cycle the shallow...
  1590. Deck deck = (Deck)cL;
  1591. Shallow sh = b.getShallow();
  1592. // move the shallow to the bottom of the deck...
  1593. while(!sh.getStack().isEmpty()){
  1594. deck.addToBottom(sh.getStack().get(0));
  1595. sh.removeCard(sh.getStack().get(0));
  1596. }
  1597. int upper = cL.getStack().size();
  1598. int lower = upper-3;
  1599. // and move the top 3 cards of the deck to the shallow.
  1600. for(; lower < upper; lower++){
  1601.  
  1602. sh.addCard(deck.drawCard());
  1603. }
  1604. }
  1605. }
  1606.  
  1607.  
  1608. }
  1609.  
  1610. package giftest;
  1611.  
  1612. import java.awt.Graphics;
  1613. import java.awt.image.ImageObserver;
  1614.  
  1615.  
  1616. public class SortedStack extends CardContainer {
  1617.  
  1618. public SortedStack(){
  1619. super();
  1620. }
  1621.  
  1622. public SortedStack(int x, int y, String id){
  1623. super(x,y);
  1624. this.id = id;
  1625. }
  1626.  
  1627. @Override
  1628. void processCard(Card c) {
  1629. if(c != null){
  1630. // set all cards to be in the container and revealed.
  1631. for(int idx = 0; idx < cards.size(); idx++){
  1632. cards.get(idx).setPos(this.pos);
  1633. cards.get(idx).setContainer(this);
  1634. cards.get(idx).setStackHeight(idx);
  1635. cards.get(idx).setRevealed(true);
  1636. }
  1637. }
  1638. }
  1639.  
  1640. @Override
  1641. void drawCards(Graphics g, ImageObserver io) {
  1642. getTopCard().draw(g, io);
  1643. }
  1644.  
  1645. }
  1646. /*
  1647. * To change this license header, choose License Headers in Project Properties.
  1648. * To change this template file, choose Tools | Templates
  1649. * and open the template in the editor.
  1650. */
  1651. package giftest;
  1652.  
  1653. import java.awt.Graphics;
  1654. import java.awt.Point;
  1655. import java.awt.image.ImageObserver;
  1656.  
  1657. /**
  1658. *
  1659. * @author Ryland
  1660. */
  1661. public class Stack extends CardContainer {
  1662. // how far should each card be offset from eachother?
  1663. private final static int Y_OFFSET = 20;
  1664.  
  1665. Stack(){
  1666. super();
  1667. }
  1668.  
  1669. Stack(int x, int y, String id){
  1670. super(x,y);
  1671. this.id = id;
  1672. }
  1673.  
  1674. @Override
  1675. void processCard(Card c){
  1676. Point p = pos;
  1677. // sets the cards in the right spots...
  1678. for(int idx = 0; idx < cards.size(); idx++){
  1679. if(c == cards.get(idx)){
  1680. cards.get(idx).setPos(p.x, p.y + (idx*Y_OFFSET));
  1681. cards.get(idx).setContainer(this);
  1682. cards.get(idx).setStackHeight(idx);
  1683. }
  1684. }
  1685. }
  1686.  
  1687. // draws the cards based off of cards' draw method.
  1688. @Override
  1689. void drawCards(Graphics g, ImageObserver io) {
  1690. for(Card c : cards){
  1691. c.draw(g, io);
  1692. }
  1693. }
  1694. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement