java 21点游戏源码

kldzcm123 kldzcm123 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
/*
Simple Solitare Card Game in Java
Written by Tim Budd, Oregon State University, 1996
*/

import java.awt.*;
import java.applet.*;

class Card {
// constructor
Card (int sv, int rv) {
s = sv;
r = rv;
faceup = false;
}

// access attributes of card
public 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 suits
final 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 fields
private 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 overridden

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;
}
return result;
}

// the following are sometimes overridden

public 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 pile
protected 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())
return;
Solitare.discardPile.addCard(pop());
}
}

class DiscardPile extends CardPile {

DiscardPile (int x, int y) { super (x, y); }

public void addCard (Card aCard) {
if (! aCard.faceUp())
aCard.flip();
super.addCard(aCard);
}

public void select (int tx, int ty) {
if (empty())
return;
Card topCard = pop();
for (int i = 0; i < 4; i++)
if (Solitare.suitPile[i].canTake(topCard)) {
Solitare.suitPile[i].addCard(topCard);
return;
}
for (int i = 0; i < 7; i++)
if (Solitare.tableau[i].canTake(topCard)) {
Solitare.tableau[i].addCard(topCard);
return;
}
// nobody can use it, put it back on our list
addCard(topCard);
}
}

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());
}
}

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 boolean canTake (Card aCard) {
if (empty())
return aCard.rank() == 12;
Card topCard = top();
return (aCard.color() != topCard.color()) &&
(aCard.rank() == topCard.rank() - 1);
}

public boolean includes (int tx, int ty) {
// don't test bottom of card
return x <= tx && tx <= x + Card.width &&
y <= ty;
}

public void select (int tx, int ty) {
if (empty())
return;

// if face down, then flip
Card topCard = top();
if (! topCard.faceUp()) {
topCard.flip();
return;
}

// else see if any suit pile can take card
topCard = pop();
for (int i = 0; i < 4; i++)
if (Solitare.suitPile[i].canTake(topCard)) {
Solitare.suitPile[i].addCard(topCard);
return;
}
// else see if any other table pile can take card
for (int i = 0; i < 7; i++)
if (Solitare.tableau[i].canTake(topCard)) {
Solitare.tableau[i].addCard(topCard);
return;
}
// else put it back on our pile
addCard(topCard);
}

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());
}

}

public class Solitare extends Applet {
static DeckPile deckPile;
static DiscardPile discardPile;
static TablePile tableau[];
static SuitPile suitPile[];
static CardPile allPiles[];

public void init() {
// first allocate the arrays
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) {
for (int i = 0; i < 13; i++)
allPiles[i].display(g);
}

public boolean mouseDown(Event evt, int x, int y) {
for (int i = 0; i < 13; i++)
if (allPiles[i].includes(x, y)) {
allPiles[i].select(x, y);
repaint();
return true;
}
return true;
}
}


...全文

相关推荐

java开发的21点游戏源码

用java开发的21点游戏,值得一看哦!

善良比聪明更重要

Java网络版21点游戏源码深度剖析

Java是一种广泛使用的面向对象编程语言,它以其跨平台性、安全性和对象导向的特性而闻名。Java代码首先被编译成字节码,可以在任何安装了Java运行时环境(JRE)的平台上运行,从而实现了”一次编写,到处运行”的特性。Java语言的规范性和健壮性使其成为企业级应用、移动应用和Web服务开发的首选。Java序列化机制允许Java对象转换成字节序列,这个序列可以被存储到磁盘或通过网络进行传输。反序列化则是将这些字节序列恢复为Java对象的过程。要使一个类的对象可序列化,该类必须实现接口。

牛新哲

Java网络对战21点游戏源码解析与实战项目

在本系统中,选用JSON格式作为主要的消息载体,因其具备良好的可读性、跨平台兼容性和丰富的库支持(如Jackson、Gson)。同时,结合预定义的指令码(Command Code),实现语义明确的请求-响应模型。所有消息统一采用如下JSON结构:"data": {各字段含义如下:字段类型说明cmdString操作命令类型(枚举值)seqLong消息序列号,用于去重与追踪timestampLong毫秒级时间戳,用于超时判断data。

语嫣凝冰

21点【java游戏源码】

java小游戏,java源代码,可以自己在编译器里面进行运行,玩游戏!

greatwqs

网络对战21点游戏java源码.rar

网络对战21点游戏java源码

shuizimuzhongling
评论 15
收藏 已收藏
分享
返回顶部
kldzcm123
Java SE 62,621 成员 307,251 内容