看了 加我qq通知 立马给分

qq_java_1 2007-12-16 08:56:46
大家好:
您们看一下这个就是关于21点的游戏;
谁给我解决了100分立即给:
看Decks的例子;
public Decks(int numDecks)
{

generator = new Random();
tracing = true;
cards = new String[numDecks][NUM_CARDS * NUM_SUITS];

for (int deck=0; deck<numDecks; deck++)
{
for (int suit=0; suit<NUM_SUITS; suit++)
{
for (int card=0; card<NUM_CARDS; card++)
{
cards[deck][(suit*NUM_CARDS)+card]=CARD_FACES[card]+ " of " + SUITS[suit];
}
}
}
}
playersCards=new int[HAND_SIZE];
computersCards=new int[HAND_SIZE];
}
这段话int numDecks 我的理解是传入几就创建几付牌

public void drawCard(boolean playersTurn)
{
int deck;
int card;
int result;
boolean duplicate;

trace("drawCard: begins");

do
{
//确定要下一张牌
deck=generator.nextInt(cards.length);
card=generator.nextInt(NUM_CARDS*NUM_SUITS);
result=deck*NUM_CARDS+card;

//检查是否已经发牌
duplicate=false;
for (int i=0; i<HAND_SIZE; i++)
{
if ((playersCards[i]==result) || (computersCards[i]==result))
{
duplicate=true;
}
}
}
while (duplicate);

//更新玩家/电脑手中的纸牌
if (playersTurn)
{
playersCards[numPlayersCards]=result;
numPlayersCards++;
totalPlayersCards+=CARD_VALUES[card%NUM_CARDS];
}
else
{
computersCards[numComputersCards]=result;
numComputersCards++;
totalComputersCards+=CARD_VALUES[card%NUM_CARDS];
}

currentCard=result;

trace("drawCard: ends");
}

这里的 deck=generator.nextInt(cards.length);
card=generator.nextInt(NUM_CARDS*NUM_SUITS);
result=deck*NUM_CARDS+card; 这个这么讲为什么这么做呢?

谁能帮我看看 加我qq在线;然后给分;
PlayBackjack07.java:这是驱动程序(有一个main函数),代码已完成,不能更改--他声明,实例化对象 Blackjack class
Decks.java:程序可能会用到,不能更改
Blackjack.java:要完成的程序,类中没有main函数,包含了组织游戏的方法.
注 :Decks class中有的代码不允许再重新在Blackjack.java自己写


玩游戏-玩家被提示 hit/stand 直到游戏结束(won赢/lost输/drawn平局)
电脑爆,(玩家赢)
玩家爆,(玩家输)
分数一样,(平局)
玩家有5张牌,但点数是21或21以下(玩家赢)
玩家点数21或以下,且比电脑高(玩家赢)
电脑点数21或以下,且比玩家高(玩家输)

游戏开始前,玩家会被问到是否想玩游戏(whether or not they want to play),如果不是(no),游戏终止
并且每轮结束,玩家都会被问是否想继续
当玩家不想玩时,以下信息显示:
玩了多少局游戏 How many games were played
玩家赢了几局 How many games were won by the user
玩家输了几局 How many games were lost by the user
平了几局 How many games were drawn.


设计,建立一个GUI用来输出程序(每一轮后显示信息,最后的信息,可能用图解法表示牌),输入(包括提示玩家的信息)
做一个用户交互界面,所有的用户和程序之间的互动都在界面上进行
不用写打开,定位,排列或关闭GUI的程序


例子(没有界面):
Blackjack – A Game of 21
The object of the game is to get as close to 21 as possible or to have 5
cards with a total under 21
The computer continues to draw cards if under 16
Would you like to play Blackjack? y
Player has a Ace of Spades and a 5 of Hearts for a total of 16
Dealer has a 10 of Spades and a ???
Player’s turn...
Hit of Stand?
S
Player sits on 16
Computer’s turn...
Computer has a 7 of Clubs and a 10 of Spades for a total of 17
Computer sits on 17
You LOST!
Would you like to play Blackjack again? y
Player has 3 of Spades and a 7 of Hearts for a total of 10
Dealer has a 4 of Hearts and a ???
Player’s turn...
Hit or Stand?
h
Player draws a Jack of Hearts for a total of 20
Hit or Stand?








...全文
151 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xql80329 2007-12-17
  • 打赏
  • 举报
回复
先把你的问题列出来
ltc_mouse 2007-12-17
  • 打赏
  • 举报
回复
不知道是楼主发上来的时候打错了,还是代码本来就有错,获取同一张牌的点数,3个地方不一致:

public void deal()
{
...
//显示电脑的第二张牌
//怪怪的,这样也能得出点数?%NUM_SUITS,只能得到0~3了...
totalComputersCards=CARD_VALUES[computersCards[1]%cards.length%NUM_SUITS];
...
}

public void drawCard(boolean playersTurn)
{
...
//deck是第几副牌,这里应该使用deck*(NUM_CARDS*NUM_SUITS)+card才对
result=deck*NUM_CARDS+card;
}
public void refreshComputersTotal()
{
//个人认为这里的计算是对的,但遗憾的是跟前面两个无法对应
//而且,这个方法refreshComputersTotal不知道干什么的,发牌时不是计算了吗?
totalComputersCards+=CARD_VALUES[computersCards[i]%(NUM_CARDS*NUM_SUITS)%NUM_CARDS];
}
sunwei_07 2007-12-17
  • 打赏
  • 举报
回复
呵呵,是啊
洪泉 2007-12-17
  • 打赏
  • 举报
回复
我也没看明白
newflypig 2007-12-17
  • 打赏
  • 举报
回复
lz,虽然分数很诱人,本人很想解决,可看了一会儿还是不明白楼主要问什么,能多用点文字来描述问题吗
qq_java_1 2007-12-16
  • 打赏
  • 举报
回复







/**
Decks
@author: Julian Dermoudy for KXT101
@modified: Christian McGee for KXC151
@version: 2007
purpose: 提供一个"Blackjack"游戏版本的资源
*/

import java.util.Random;

public class Decks
{
//常量
public final int NO_ONE=0; //游戏继续
public final int PLAYER=1; //游戏结束,玩家赢
public final int COMPUTER=2; //游戏结束,玩家输
public final int COMPUTER_SITS=16; //游戏默认:当电脑大于或等于16点时,不加牌
public final boolean PLAYERS_TURN=true; //轮到玩家(相反轮到电脑)

private final int NUM_SUITS = 4; //牌的四个花色
private final String[] SUITS = {"Hearts", "Spades", "Diamonds", "Clubs"}; //花色名字:红心,黑桃,方块,梅花
private final int NUM_CARDS = 13; //每个花色的张数,13张
private final String[] CARD_FACES = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; //牌的名字
private final int[] CARD_VALUES = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10}; //牌的数值
private final int FIVE_AND_UNDER=5; //有5张牌,比21点大
private final int HAND_SIZE=5; //最多张数5

//变量
private String[][] cards; //牌的显示方法:花色和名字
private Random generator; //用来随机发牌
private int[] playersCards; //用来记录玩家的牌
private int[] computersCards; //记录电脑的牌
private int currentCard; //最近处理的牌
private int numPlayersCards; //记录玩家牌的张数
private int numComputersCards; //记录电脑牌的张数
private int totalPlayersCards; //记录玩家的总点数
private int totalComputersCards; //记录电脑的总点数
private boolean tracing; //用来描述 开/关


/** constructor
@param int -- 要用多少牌
@return none
*/
public Decks(int numDecks)
{

generator = new Random();
tracing = true;
cards = new String[numDecks][NUM_CARDS * NUM_SUITS];

for (int deck=0; deck<numDecks; deck++)
{
for (int suit=0; suit<NUM_SUITS; suit++)
{
for (int card=0; card<NUM_CARDS; card++)
{
cards[deck][(suit*NUM_CARDS)+card]=CARD_FACES[card]+ " of " + SUITS[suit];
}
}
}

playersCards=new int[HAND_SIZE];
computersCards=new int[HAND_SIZE];
}

/** deal - (re-)开始游戏
@param none
@return none
给玩家两张牌, 隐藏电脑的第一张牌
*/
public void deal()
{
trace("deal: begins");

//初始化,一开始拥有的为零
for (int i=0; i<HAND_SIZE; i++)
{
playersCards[i]=-1;
computersCards[i]=-1;
}

currentCard=-1;
numPlayersCards=0;
totalPlayersCards=0;
numComputersCards=0;
totalComputersCards=0;

//处理第一次的四张牌
drawCard(true);
drawCard(false);
drawCard(true);
drawCard(false);

//显示电脑的第二张牌
totalComputersCards=CARD_VALUES[computersCards[1]%cards.length%NUM_SUITS];

trace("draw: ends");
}

/** drawCard - 发下一张牌
@param booean -- 轮到玩家了?
@return none
发一张新牌给被提名的玩家
*/
public void drawCard(boolean playersTurn)
{
int deck;
int card;
int result;
boolean duplicate;

trace("drawCard: begins");

do
{
//确定要下一张牌
deck=generator.nextInt(cards.length);
card=generator.nextInt(NUM_CARDS*NUM_SUITS);
result=deck*NUM_CARDS+card;

//检查是否已经发牌
duplicate=false;
for (int i=0; i<HAND_SIZE; i++)
{
if ((playersCards[i]==result) || (computersCards[i]==result))
{
duplicate=true;
}
}
}
while (duplicate);

//更新玩家/电脑手中的纸牌
if (playersTurn)
{
playersCards[numPlayersCards]=result;
numPlayersCards++;
totalPlayersCards+=CARD_VALUES[card%NUM_CARDS];
}
else
{
computersCards[numComputersCards]=result;
numComputersCards++;
totalComputersCards+=CARD_VALUES[card%NUM_CARDS];
}

currentCard=result;

trace("drawCard: ends");
}

/** computersTurn - 完成电脑的一轮
@param none
@return none
确定电脑是否要跟这一轮, 如果是,发牌
*/
public void computersTurn()
{
trace("computersTurn: begins");

if (!((numComputersCards == FIVE_AND_UNDER) || (totalComputersCards >= COMPUTER_SITS)))
{
drawCard(! PLAYERS_TURN);
}

trace("computersTurn: ends");
}

/** isBust - 确定是否玩家爆了
@param boolean -- 检查玩家
@return boolean -- 玩家是否爆了
玩家的点数是否超过了21点
*/
public boolean isBust(boolean playersTurn)
{
trace("isBust: begins and ends");

if (playersTurn == PLAYERS_TURN)
{
return totalPlayersCards > 21;
}
else
{
return totalComputersCards > 21;
}
}

/** refreshComputersTotal - 重新计算电脑点数
@param none
@return none
显示电脑所有的牌,计算点数
*/
public void refreshComputersTotal()
{
trace("refreshComputersTotal: begins");

totalComputersCards=0;

for (int i=0; i<numComputersCards; i++)
{
totalComputersCards+=CARD_VALUES[computersCards[i]%(NUM_CARDS*NUM_SUITS)%NUM_CARDS];
}

trace("refreshComputersTotal: ends");
}

/** gameOver -确定这轮是否结束
@param none
@return boolean -- 游戏是否结束
确定游戏游戏是否有效的结束
*/
public boolean gameOver()
{
boolean ans;

trace("gameOver: begins");

ans=false;

// 检查是不是有人超过21点
if (isBust(PLAYERS_TURN) || isBust(!PLAYERS_TURN))
{
trace("gameOver: a player is bust");
ans=true;
}

// 检查玩家是不是有5张牌并且总数少于21点
if ((getNumberOfCards(PLAYERS_TURN) == FIVE_AND_UNDER) && (!isBust(PLAYERS_TURN)))
{
trace("gameOver: player has 5 and under");
ans=true;
}

// 检查电脑是否sits
if (getTotalOfCards(!PLAYERS_TURN) >= COMPUTER_SITS)
{
trace("gameOver: computer sits");
ans=true;
}

trace("gameOver: ends with return value of " + ans);
return ans;
}

/** whoWon - 确定谁赢(是否有人赢)
@param none
@return int -- 定义noone, player, or computer
确定谁赢(是否有人赢)
*/
public int whoWon()
{
int winner;

trace("whoWon: begins");

winner=NO_ONE;

if (isBust(!PLAYERS_TURN))
{
trace("whoWon: computer is bust");
if (!isBust(PLAYERS_TURN))
{
trace("whoWon: player is not bust");
winner=PLAYER;
}
}
else
{
trace("whoWon: computer is not bust");
if (isBust(PLAYERS_TURN))
{
trace("whoWon: player is bust");
winner=COMPUTER;
}
else
{
trace("whoWon: player is not bust");
if ((numPlayersCards == FIVE_AND_UNDER) || (totalPlayersCards > totalComputersCards))
{
trace("whoWon: player has 5 and under or beats the computer's total");
winner=PLAYER;
}
else
{
if ((numComputersCards == FIVE_AND_UNDER) || (totalComputersCards > totalPlayersCards))
{
trace("whoWon: computer has 5 and under or beats the player's total");
winner=COMPUTER;
}
}
}
}

trace("whoWon: ends with return value " + winner);
return winner;
}

/** getNumberOfCards - 定义玩家手上的纸牌张数
@param boolean -- 玩家的张数?
@return int -- 多少张牌
确定玩家/电脑张数
*/

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧