⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardtableau.java

📁 一个J2ME编写的牌类游戏
💻 JAVA
字号:
package de.tsr.jsol.logic;

import java.util.*;

import javax.microedition.lcdui.*;

import de.tsr.jsol.gui.GraphicsWrapperWrapper;
import de.tsr.jsol.gui.IGraphicsWrapper;
import de.tsr.jsol.util.CVector;
import de.tsr.jsol.util.Position;


// TODO muss nicht sein, dass hier iCardSelection implementiert wird, oder?
public class CardTableau extends CardContainer implements ICardSelection {
	private CVector _tableau = new CVector();
	int _position = 0;
	int _cardDistance = 8;
	CVector _selection;
	
	public CardTableau(int x, int y, int verticalCardDistance ) {
		super(x, y);
		_cardDistance = verticalCardDistance;
	}
	
	protected void Draw(IGraphicsWrapper gw) {
		int distance = 0;
		IGraphicsWrapper g = new GraphicsWrapperWrapper(gw, 0, distance);

		if( _tableau.size() == 0 && _selection == null ) {
			DrawEmptyContainer(gw);
		}
		else if( _tableau.size() != 0 ) {
			Enumeration e = _tableau.elements();
			while( e.hasMoreElements() ) {
				distance = distance + _cardDistance;
				Card c = (Card)e.nextElement();
				c.Paint(g);
				g = new GraphicsWrapperWrapper( gw, 0, distance);			
			}			
		}
		if( _selection != null ) {
			Enumeration e = _selection.elements();
			while( e.hasMoreElements() ) {
				g = new GraphicsWrapperWrapper( gw, 0, distance);
				g.setStrokeStyle( Graphics.DOTTED );
				Card c = (Card)e.nextElement();
				c.Paint(g);
				distance = distance + _cardDistance;
			}
			g.setStrokeStyle( Graphics.SOLID );
		}
	}	
	
	public void Push( Card card ){
		_tableau.addElement(card);
	}
	
	public IConnectable Down() {
		if( _position < _tableau.size() - 1 ) {
			_position++; 
			return this;
		}
		return super.Down();
	}
	
	public IConnectable Up() {
		if( _position == BottomCard() ) 
			return super.Up();
		else { 
			_position--;
			return this;
		}
	}
	
	public Position GetCursorPosition() {
		if( _position < BottomCard() ) {
			_position = BottomCard();
		}
//		else if( _position >= _tableau.size() ) {
//		    _position = _tableau.size()-1;
//		}
		return new Position(XPosition(), YPosition() + (_position * _cardDistance) );
	}

	private ICardSelection VisitCardGroup(ICardSelection selection) {
		if( _selection != null 
				&& _selection.elementAt(0) == selection.SelectedCardAt(0) ) {
			for( int i = 0; selection.SelectionSize() > i; i++ ) {
				_tableau.addElement( selection.SelectedCardAt(i));
			}
			selection.RemoveSelection();
			return null;
		}
		else if( _tableau.size() == 0 ) {
			if( selection.SelectedCardAt(0).Rank() == Card.KING ) {
				for( int i = 0; selection.SelectionSize() > i; i++ ) {
					_tableau.addElement( selection.SelectedCardAt(i));
				}
				selection.RemoveSelection();
				return null;
			}
			else {
				return selection;
			}			
		}
		else if( CheckMatch(_tableau.lastElement(),  selection.SelectedCardAt(0))) {
			for( int i = 0; selection.SelectionSize() > i; i++ ) {
				_tableau.addElement( selection.SelectedCardAt(i));
			}
			selection.RemoveSelection();
			return null;
		}
		return selection;
	}
	
	private ICardSelection VisitClicked() {
		if( _tableau.size() <= 0 ) return null;
		if( _position >= _tableau.size() ) _position = _tableau.size()-1;
		if( _tableau.elementAt(_position).State() == Card.BACK ) {
			_tableau.lastElement().TurnToFront();
			return null;
		}
		_selection = new CVector();
		while( _position < _tableau.size() ) {
			Card card = _tableau.elementAt(_position);
			_selection.addElement(card);
			_tableau.removeElement(card);
		}
		return this;
	}
	
	private boolean CheckMatch( Card tableauCard, Card otherCard ) {
		if( ( ( otherCard.Suit()==ICard.HEARTS || otherCard.Suit() == ICard.DIAMONDS ) 
				&& ( tableauCard.Suit()== ICard.SPADES || tableauCard.Suit()==ICard.CLUBS ) )
				||  ( ( tableauCard.Suit()==ICard.HEARTS || tableauCard.Suit() == ICard.DIAMONDS ) 
				&& ( otherCard.Suit()== ICard.SPADES || otherCard.Suit()==ICard.CLUBS ) ) ) {
			if( tableauCard.Rank() -1 == otherCard.Rank() ) {
				return true;
			}
		}
		return false;
	}
	
	
	public void RemoveSelection() {
		_selection =  null;
		if( _tableau.size() > 0 ) {
			_tableau.lastElement().TurnToFront();
		}
	}
	public ICardSelection Clicked(ICardSelection selection) {
		if( selection == null )
			return VisitClicked();
		else 
			return VisitCardGroup(selection);
	}
	
	public void SingleClicked() {
		if( _tableau.size() == 0 ) return;
		_selection = new CVector();
		_selection.addElement( _tableau.lastElement() );
		_tableau.removeElement(_selection.lastElement() );
		Enumeration e = _singleClickDestinations.elements();
		while( e.hasMoreElements()) {
			IConnectable container = (CardContainer)e.nextElement();
			if ( null == container.Clicked(this) ) {
				RemoveSelection();
				return;
			}
		}
		_tableau.addElement( _selection.lastElement());
		RemoveSelection();
	}
	
	public Card SelectedCardAt(int position) {
		return _selection.elementAt(position);
	}
	public int SelectionSize() {
		return _selection.size();
	}
	
	public Enumeration SelectedElements() {
		return _selection.elements();
	}
	
	private int BottomCard() {
		int pos = 0;
		Enumeration e = _tableau.elements();
		while( e.hasMoreElements()) {
			Card c = (Card)e.nextElement();
			if( c.State() == Card.BACK)
				pos ++;
			else 
				break;
		}
		// sowas wie if pos > 0 then return pos-1; ???
		return pos;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -