here is the cardTable class
Java: |
//////===========================================================//////
////// DAJM (Dime) //////
//////-----------------------------------------------------------//////
////// CardTable.java //////
////// Version 0.41 Pre-Alpha //////
////// January 11, 2009 Build //////
//////-----------------------------------------------------------//////
////// Copyright 2008-2009 Drew Burden, Yanko Petrovic //////
//////-----------------------------------------------------------//////
////// This file is part of Dajm. //////
////// //////
////// Dajm is free software: you can redistribute it and/or //////
////// modify it under the terms of the GNU General Public //////
////// License as published by the Free Software Foundation, //////
////// either version 3 of the License, or (at your option) //////
////// any later version. //////
////// //////
////// Dajm is distributed in the hope that it will be //////
////// useful, but WITHOUT ANY WARRANTY; without even the //////
////// implied warranty of MERCHANTABILITY or FITNESS FOR A //////
////// PARTICULAR PURPOSE. See the GNU General Public //////
////// License for more details. //////
////// //////
////// You should have received a copy of the GNU General //////
////// Public License along with Dajm. If not, see //////
////// <http://www.gnu.org/licenses/>. //////
//////===========================================================//////
// Best viewed in Notepad++
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.util.Random;
////////////////////////////////////////////////////////////////////// CardTable
public class CardTable extends JComponent implements MouseListener, MouseMotionListener {
//================================================================ constants
private final Color BACK = Color. BLACK; // Used for redraw.
private final int TABLE_SIZE = 550; // Window width and height. *** CAUTION *** Changing this will screw up some stuff!
//=================================================================== variables
//------- Initial image coords.
private int _initX = 0; // x coord - set from drag.
private int _initY = 0; // y coord - set from drag.
//------- Position in image of mouse press to make dragging look better
private int _dragFromX = 0; // Displacement inside image of mouse press.
private int _dragFromY = 0; // Displacement inside image of mouse press.
//------- Array location variables
private int _cardPlace = 0; // Place in the array of the current card.
private int _discardPlace = 0; // Place in the array of the current discard.
//------- X, Y Positions
private int n = 0; // Which card in the array (array position).
private int xPos = 0, yPos = 0; // Where it should be placed initially.
private int _oldX = 0, _oldY = 0; // Used for snapping the card back into place.
private int _X1, _X2, _Y1, _Y2; // For placing the rearranged cards.
//------- Deck Initialization Variables
private String suits = "shdc"; // Holds all the suits in a string (each one char long).
private String faces = "a23456789tjqk"; // Holds all the face values in a string (each one char long).
private int _suitval; // Used to plug into the Card creator for returning a value later (checkDajm method).
private int _faceval; // Used to plug into the Card creator for returning a value later (checkDajm method).
private String _prompt = "new"; // Which text do we display at the bottom of the screen? It's a string to limit confusion.
private boolean _chip = false; // Is this the first time for the current round that we are painting the dealer chip?
private int _round = 3; // Not the round we are in, but rather the amount of cards that each player has...
// (it starts at 3 since the dealing function adds one to the variable right at the start.
//------- Setup Various Decks
private Card [] _deck = new Card [54]; // Main Deck pefectly organized (not shuffled and never to be shuffled itself!).
private Card [] _handp1 = new Card [14]; // Player's hand (14 available cards: 13 for the last round plus 1 extra for drawing from the deck).
private Card [] _handc1 = new Card [14]; // Computer's hand (14 available cards: 13 for the last round plus 1 extra for drawing from the deck).
private Card [] _misc = new Card [3]; // The miscellaneous cards such as the card backs, the draw pile, and the discard pile outline.
private Card [] _rearrange = new Card [2]; // Used to store temporary cards for rearranging.
private int[] _rearrangePlace = new int[2]; // Used to store the place in the array that the card was taken from (for rearranging).
private Card [] _discard = new Card [54]; // Contains all cards that are discarded.
private Card _temp; // Holds various temporary cards for movement between decks or whatever.
private Card _dragging; // Only here so the card that is being dragged is drawn on top of everything else.
private Card _currentCard = null; // Used for reference between functions so we know what card is currently being acted upon.
private Card _currentDiscard = null; // Used for reference between functions with relation to the discard pile.
private boolean _yourTurn = false; // Can you (the player) do anything? If true, then yes.
private int[] _numSuits = new int[4]; // Used when checking for Dajm... Aligned with the suits string. (shdc)
private int[] _numFaces = new int[13]; // Used when checking for Dajm... Aligned with the faces string. (a23456789tjqk)
private int _numWilds = 0; // Used when checking for Dajm... How many wild cards?
//------- Misc.
ClassLoader cldr = this. getClass(). getClassLoader(); // ClassLoader is used to get images from a jar file.
private Random rand = new Random(); // Random number generator. Used for dealing cards randomly from the organized deck.
private int randomdeal = 0; // Position in the deck to get the card from.
private String imagePath; // Relative path to images.
private URL imageURL; // Variable to plug into the class loader.
private ImageIcon img; // The actual image that we use to draw to the screen (using a method).
//============================================================== constructor
public CardTable () {
//... Initialize graphics
setPreferredSize (new Dimension(TABLE_SIZE, TABLE_SIZE ));
//... Add mouse listeners.
addMouseListener (this);
addMouseMotionListener (this);
}
//=========================================================== resetCheckDajmVars();
public void resetCheckDajmVars () {
// THIS FUNCTION IS INCOMPLETE!
_numSuits [0] = 0;
_numSuits [1] = 0;
_numSuits [2] = 0;
_numSuits [3] = 0;
_numFaces [0] = 0;
_numFaces [1] = 0;
_numFaces [2] = 0;
_numFaces [3] = 0;
_numFaces [4] = 0;
_numFaces [5] = 0;
_numFaces [6] = 0;
_numFaces [7] = 0;
_numFaces [8] = 0;
_numFaces [9] = 0;
_numFaces [10] = 0;
_numFaces [11] = 0;
_numFaces [12] = 0;
_numWilds = 0;
}
//=========================================================== checkDajm
public void checkDajm () {
// THIS FUNCTION IS INCOMPLETE!
resetCheckDajmVars ();
for (int x = 0; x < _round; x++ ) {
if (_handp1 [x ]. getSuit() != 5)
_numSuits [_handp1 [x ]. getSuit()]++;
if (_handp1 [x ]. getFace() != 14 && _handp1 [x ]. getFace() != _round )
_numFaces [_handp1 [x ]. getFace()- 1]++;
else
_numWilds++;
if (_handp1 [x ]. getFace() != 14 && _handp1 [x ]. getSuit() != 5 && _handp1 [x ]. getFace() == _round )
_numSuits [_handp1 [x ]. getSuit()]--;
}
for (int x = 0; x < 4; x++ ) {
if (_numSuits [x ] == _round || _numSuits [x ] == 13) {
callDajm ();
break;
}
else if (_numSuits [x ] > 2 && _numWilds > 0) {
callDajm ();
break;
}
}
for (int x = 0; x < 13; x++ ) {
if (_numFaces [x ] == 4) {
callDajm ();
break;
}
else if (_numFaces [x ] > 2 && _numWilds > 0) {
callDajm ();
break;
}
}
}
//=========================================================== callDajm
public void callDajm () {
// THIS FUNCTION IS INCOMPLETE!
System. out. println("DAJM!");
nextRound ();
}
//=========================================================== newGame
public void newGame () {
_yourTurn = true;
_round = 3;
_chip = false;
nextRound ();
}
//=========================================================== nextRound
public void nextRound () {
_round++;
resetVars ();
setupMainDeck ();
setupSecDecks ();
dealPlayer (_round );
dealComputer (_round );
_chip = true;
}
//=========================================================== resetVars
public void resetVars () {
n = 0;
_prompt = "draw";
for (int x = 0; x < 54; x++ ) {
_deck [x ] = null;
_discard [x ] = null;
}
for (int x = 0; x < 14; x++ ) {
_handp1 [x ] = null;
_handc1 [x ] = null;
}
}
//=========================================================== setupMainDeck
public void setupMainDeck () {
for (int suit= 0; suit < suits. length(); suit++ ) {
for (int face= 0; face < faces. length(); face++ ) {
//... Get the image from the images subdirectory.
imagePath = "img/" + suits. charAt(suit ) +
faces. charAt(face ) + ".png";
imageURL = cldr. getResource(imagePath );
img = new ImageIcon(imageURL );
_suitval = suit;
_faceval = face;
//... Create a card and add it to the deck.
Card card = new Card (img, _suitval, _faceval+ 1, true, true);
_deck [n ] = card;
n++;
}
}
// Blue Joker.
imagePath = "img/jb.png";
imageURL = cldr. getResource(imagePath );
img = new ImageIcon(imageURL );
Card joker1 = new Card (img, 5, 14, true, true);
_deck [n ] = joker1;
n++;
// Red Joker.
imagePath = "img/jr.png";
imageURL = cldr. getResource(imagePath );
img = new ImageIcon(imageURL );
Card joker2 = new Card (img, 5, 14, true, true);
_deck [n ] = joker2;
}
//=========================================================== setupSecDecks
public void setupSecDecks () {
//Discard pile outline.
imagePath = "img/outline.png";
imageURL = cldr. getResource(imagePath );
img = new ImageIcon(imageURL );
Card outline = new Card (img, - 1, - 1, false, true);
outline. moveTo(150, 210);
_misc [0] = outline;
// Deck you draw from.
imagePath = "img/pile.png";
imageURL = cldr. getResource(imagePath );
img = new ImageIcon(imageURL );
Card pile = new Card (img, - 1, - 1, false, true);
pile. moveTo(325, 205);
_misc [1] = pile;
// Red deck back (for displaying the computer's cards on screen).
imagePath = "img/b.png";
imageURL = cldr. getResource(imagePath );
img = new ImageIcon(imageURL );
Card discard = new Card (img, - 1, - 1, false, false);
_misc [2] = discard;
}
//=========================================================== dealPlayer
public void dealPlayer (int cards ) {
xPos = 120;
yPos = 388;
for (int x = 0; x < cards; x++ ) {
while (true) {
randomdeal = rand. nextInt(54);
if (_deck [randomdeal ] != null) {
_handp1 [x ] = _deck [randomdeal ];
_handp1 [x ]. moveTo(xPos, yPos );
_deck [randomdeal ] = null;
break;
}
}
xPos += 20;
}
// Flip a card over onto the discard pile
while (true) {
randomdeal = rand. nextInt(54);
if (_deck [randomdeal ] != null) {
_discard [0] = _deck [randomdeal ];
_discard [0]. moveTo(150, 210);
_deck [randomdeal ] = null;
break;
}
}
}
//=========================================================== dealComputer
public void dealComputer (int cards ) {
xPos = 120;
yPos = 30;
for (int x = 0; x < cards; x++ ) {
while (true) {
randomdeal = rand. nextInt(54);
if (_deck [randomdeal ] != null) {
_handc1 [x ] = _deck [randomdeal ];
_handc1 [x ]. moveTo(xPos, yPos );
imagePath = "img/b.png";
imageURL = cldr. getResource(imagePath );
img = new ImageIcon(imageURL );
_handc1 [x ]. setImg(img );
_deck [randomdeal ] = null;
break;
}
}
xPos += 20;
}
}
//=========================================================== drawCard
public void drawCard () {
while (true) {
randomdeal = rand. nextInt(54);
if (_deck [randomdeal ] != null) {
_handp1 [_round ] = _deck [randomdeal ];
_handp1 [_round ]. moveTo(_handp1 [_round- 1]. getX() + 20, _handp1 [_round- 1]. getY());
_deck [randomdeal ] = null;
break;
}
}
_prompt = "discard";
this. repaint();
}
//=========================================================== drawDiscard
public void drawDiscard () {
for (int crd=_discard. length- 1; crd>= 0; crd-- ) {
if (_discard [crd ] != null) {
Card testCard = _discard [crd ];
_discard [crd ] = null;
//... Found, remember this card for dragging.
_handp1 [_round ] = testCard; // Remember what we're dragging.
_handp1 [_round ]. moveTo(_handp1 [_round- 1]. getX() + 20, _handp1 [_round- 1]. getY());
_discardPlace = crd;
_prompt = "discard";
this. repaint();
break; // Stop when we find the first match.
}
}
}
//=========================================================== paintPrompt
public void paintPrompt (Graphics g ) {
// Draw a little helper text at the bottom telling the
// player what stage of their turn they are in.
if (_prompt == "computer") {
imagePath = "img/prmpt-computer.png";
checkDajm ();
}
else if (_prompt == "discard") {
imagePath = "img/prmpt-discard.png";
}
else if (_prompt == "draw") {
imagePath = "img/prmpt-draw.png";
}
else if (_prompt == "new") {
imagePath = "img/prmpt-new.png";
}
imageURL = cldr. getResource(imagePath );
img = new ImageIcon(imageURL );
img. paintIcon(this, g, (getWidth () / 2 - img. getIconWidth() / 2), (getHeight () - 40));
}
//=========================================================== paintChip
public void paintChip (Graphics g ) {
// Dealer chip
imagePath = "img/d.png";
imageURL = cldr. getResource(imagePath );
img = new ImageIcon(imageURL );
// Check to see who the dealer is...
// The computer always starts out as the dealer, so this
// means if _round is even, the computer is the dealer.
if ((_round % 2) == 0) {
// _round is even
if (_chip ) {
_yourTurn = true;
_prompt = "draw";
_chip = false;
}
// Paint the dealer chip by the computer
img. paintIcon(this, g, 50, 65);
}
else {
// _round is odd
if (_chip ) {
_yourTurn = false;
_prompt = "computer";
_chip = false;
}
// Paint the dealer chip by the player
img. paintIcon(this, g, 50, 420);
}
}
//=========================================================== paintComponent
@Override
public void paintComponent (Graphics g ) {
// Paint background.
int width = getWidth ();
int height = getHeight ();
g. setColor(BACK );
g. fillRect(0, 0, width, height );
// Draw the table background image...
// Because we do this, we don't really need the code directly above
// this, but I like to have it there just in case the window is sized
// differently on different operating systems, that way it makes it look
// a little less ugly.
imagePath = "img/back.png";
imageURL = cldr. getResource(imagePath );
img = new ImageIcon(imageURL );
img. paintIcon(this, g, (getWidth () / 2 - img. getIconWidth() / 2), (getHeight () / 2 - img. getIconHeight() / 2));
// Have we started the game yet?
if (_round > 3) {
// Draw all all misc cards (discard outline, draw deck, etc).
for (Card m : _misc ) {
if (m != null && m. shouldDisplay())
m. draw(g, this);
}
for (Card d : _discard ) {
if (d != null && d. shouldDisplay())
d. draw(g, this);
}
// Display the cards, starting with the first array element.
// *** The array order defines the z-axis depth. The lower the array number, the closer to the table. ***
for (Card c : _handc1 ) {
if (c != null && c. shouldDisplay())
c. draw(g, this);
}
for (Card p : _handp1 ) {
if (p != null && p. shouldDisplay())
p. draw(g, this);
}
// Display the current card that is being dragged, if any.
if (_dragging != null && _dragging. shouldDisplay())
_dragging. draw(g, this);
// Paint the dealer chip
paintChip (g );
}
// Paint the helper text at the bottom.
paintPrompt (g );
}
//====================================================== method mousePressed
public void mousePressed (MouseEvent e ) {
if (_yourTurn ) {
int x = e. getX(); // Save the x coord of the click.
int y = e. getY(); // Save the y coord of the click.
// Find card image this is in. Check from top down.
_currentCard = null; // Assume not in any image.
_cardPlace = - 1;
for (int crd=_round; crd>= 0; crd-- ) {
if (_handp1 [crd ] != null) {
Card testCard = _handp1 [crd ];
if (testCard. contains(x, y )) {
_dragging = testCard;
_handp1 [crd ] = null;
// Found, remember this card for dragging.
_dragFromX = x - _dragging. getX(); // how far from left.
_dragFromY = y - _dragging. getY(); // how far from top.
_currentCard = _dragging; // Remember what we're dragging.
_oldX = _currentCard. getX();
_oldY = _currentCard. getY();
_cardPlace = crd;
break; // Stop when we find the first match.
}
}
}
if (_misc [1]. contains(x, y ) && _handp1 [_round ] == null) {
drawCard ();
}
else if (_misc [0]. contains(x, y )) {
drawDiscard ();
}
}
}
//============================================================= mouseDragged
public void mouseDragged (MouseEvent e ) {
// First, we need to make sure it's the player's turn.
if (_yourTurn ) {
if (_currentCard != null && _currentCard. isMoveable()) { // Non-null if pressed inside card image.
int newX = e. getX() - _dragFromX;
int newY = e. getY() - _dragFromY;
// Don't move the image off the screen sides
newX = Math. max(newX, 0);
newX = Math. min(newX, getWidth () - _currentCard. getWidth());
// Don't move the image off top or bottom
newY = Math. max(newY, 0);
newY = Math. min(newY, getHeight () - _currentCard. getHeight());
_currentCard. moveTo(newX, newY );
this. repaint(); // Repaint because position changed. (Called rapidly!)
}
}
}
//======================================================= method mouseReleased
public void mouseReleased (MouseEvent e ) {
// Make sure it's the player's turn.
if (_yourTurn ) {
if (_currentCard != null) { // Non-null if dragging.
int x = e. getX(); // Save the x coord of the release.
int y = e. getY(); // Save the y coord of the release.
// Did we try to discard, and if we did, are we at the discard stage?
if (_misc [0]. contains(x, y ) && _prompt. equals("discard")) {
_currentCard. moveTo(150, 210);
for (int crd = 0; crd <= _round; crd++ ) {
if (_discard [crd ] == null) {
_discard [crd ] = _dragging;
break;
}
}
// If we discarded, then it must be the computer's turn.
_prompt = "computer";
_yourTurn = false;
_dragging = null;
// Bump all the cards down in the array so they are next to each other on the table.
if (_cardPlace != - 1) {
_handp1 [_cardPlace ] = null;
for (int crd = _cardPlace; crd <= _handp1. length- 2; crd++ ) {
if (_handp1 [crd ] == null && _handp1 [(crd+ 1)] != null) {
_handp1 [crd ] = _handp1 [crd+ 1];
_handp1 [crd+ 1] = null;
_handp1 [crd ]. moveTo(_handp1 [crd ]. getX()- 20, _handp1 [crd ]. getY());
}
}
}
}
else {
// Snap the card back to the previous position.
_handp1 [_cardPlace ] = _dragging;
_handp1 [_cardPlace ]. moveTo(_oldX, _oldY );
_dragging = null;
}
this. repaint(); // Repaint because position changed.
}
}
}
//======================================================= method mouseClicked
public void mouseClicked (MouseEvent e ) {
if (_yourTurn ) {
int x = e. getX(); // Save the x coord of the click
int y = e. getY(); // Save the y coord of the click
// Is the user trying to get a card from the deck?
if (_misc [1]. contains(x, y ) && _handp1 [_round ] == null) {
drawCard ();
}
// Is the user trying to get a card from the discard pile?
else if (_misc [0]. contains(x, y )) {
drawDiscard ();
}
// Rearranging cards.
else if (_currentCard != null && _currentCard. getY() > 385) {
_oldX = _currentCard. getX();
_oldY = _currentCard. getY();
_currentCard. moveTo(_currentCard. getX(), _currentCard. getY() - 20);
_currentCard. setMoveable(false);
if (_rearrange [0] == null) {
_rearrange [0] = _handp1 [_cardPlace ];
_rearrangePlace [0] = _cardPlace;
}
else if ( _rearrange [1] == null) {
_rearrange [1] = _handp1 [_cardPlace ];
_rearrangePlace [1] = _cardPlace;
}
if (_rearrange [0] != null && _rearrange [1] != null) {
_X1 = _rearrange [0]. getX();
_Y1 = _rearrange [0]. getY();
_X2 = _rearrange [1]. getX();
_Y2 = _rearrange [1]. getY();
_handp1 [_rearrangePlace [0]] = _rearrange [1];
_handp1 [_rearrangePlace [0]]. moveTo(_X1, _Y1 + 20);
_handp1 [_rearrangePlace [1]] = _rearrange [0];
_handp1 [_rearrangePlace [1]]. moveTo(_X2, _Y2 + 20);
_currentCard. setMoveable(true);
_handp1 [_rearrangePlace [0]]. setMoveable(true);
_handp1 [_rearrangePlace [1]]. setMoveable(true);
_rearrangePlace [0] = - 1;
_rearrangePlace [1] = - 1;
_rearrange [0] = null;
_rearrange [1] = null;
_currentCard = null;
}
this. repaint(); // Repaint because position changed.
}
// Rearranging cards.
else if (_currentCard != null && _currentCard. getY() < 390) {
_oldX = _currentCard. getX();
_oldY = _currentCard. getY();
if (_rearrange [0] != null) {
_rearrange [0] = null;
_rearrangePlace [0] = - 1;
}
else if ( _rearrange [1] != null) {
_rearrange [1] = null;
_rearrangePlace [1] = - 1;
}
_currentCard. moveTo(_currentCard. getX(), _currentCard. getY() + 20);
_currentCard. setMoveable(true);
this. repaint(); // Repaint because position changed.
}
}
}
//=============================================== Ignore other mouse events.
public void mouseEntered (MouseEvent e ) {} // ignore these events
public void mouseExited (MouseEvent e ) {} // ignore these events
public void mouseMoved (MouseEvent e ) {} // ignore these events
}
////////////////////////////
////////// EOF //////////
////////////////////////////
|
|