Java 卡片把戏
我已经为这个项目工作了30多个小时,现在我几乎和刚开始时一样迷茫。我终于能够打印出7行3张卡片,但我的程序仍然有一些问题Java 卡片把戏,java,Java,我已经为这个项目工作了30多个小时,现在我几乎和刚开始时一样迷茫。我终于能够打印出7行3张卡片,但我的程序仍然有一些问题 我需要把卡片排在“的”字上 当我调用PrintDeck()函数时,它应该打印出卡片组中的所有卡片,但它没有 在我的一生中,我都不知道如何拿起卡片的列来做这个把戏,如果用户选择了第1列,我应该第二个拿起它,然后在7行3列中再次处理它们。然后,让他们再次选择卡片2,并做同样的事情 如果您能提供任何帮助,我们将不胜感激。我知道有些代码不是很漂亮,但为了让我获得学分,作业必须是这样:
- 为每张卡生成一个随机整数
- 显示每张卡的字符串值(“钻石王牌”)
- 使用rand()函数生成整型卡值
- 一旦程序经过测试并正常运行,使用srand(time(0))命令(仅在程序开始时一次)生成真正的随机数据组
您的程序必须按行处理卡片并按列拾取它们(3次以使其正常工作)。
您的程序必须询问玩家是否希望在玩游戏前打印出整副牌。每张卡必须按上述格式打印。
您的程序必须询问玩家是否想再次玩游戏,并根据玩家的意愿继续玩游戏。
在整个游戏过程中,程序必须询问玩家的姓名,并按姓名提及玩家。
程序代码必须使用函数进行逻辑组织。您还必须使用提供的启动程序文件。如果不使用启动程序文件,您将得到零(0) 提供的启动程序文件:CardtrickStarterFile.java如果粘贴奇怪,这里有一个pastebin链接:
import java.util.Scanner;
导入java.lang.String;
导入java.util.Random;
公共类CardTrickStarterFile{
私有静态随机兰德=新随机();
再次播放私有静态字符串;
公共静态void main(字符串[]args){
/*声明和初始化变量*/
int列=0,i=0;
/*声明一个52个元素的整数数组,用作卡片组*/
int[]甲板=新int[52];
/*声明一个7乘3的阵列以接收为玩该把戏而发放的牌*/
int[][]play=新int[7][3];
/*声明用于输入的扫描程序对象*/
扫描仪输入=新扫描仪(System.in);
/*为随机数生成器生成随机种子*/
/*打开消息。询问玩家的姓名*/
System.out.println(“\nHello,我是一个非常聪明的计算机程序”);
System.out.println(“我甚至可以玩纸牌把戏。下面是方法。\n”);
System.out.println(“要开始卡片技巧,请键入您的姓名:”);
字符串名称=input.nextLine();
char firstL=name.charAt(0);
firstL=字符.toUpperCase(firstL);
name=replaceCharAt(name,0,firstL);
/*将此人姓名的第一个字母大写*/
System.out.println(“\n谢谢”+姓名);
做
{
/*建造甲板*/
建筑甲板(甲板);
/*询问玩家是否想看到整副牌。如果是,请打印出来*/
System.out.println(“Ok”+name+”,第一件事,你想看看是什么吗);
System.out.println(“卡片组看起来像(y/n)?”;
字符串SeeDeck=input.nextLine();
开关(SeeDeck)
{
案例“y”:
印刷甲板(甲板);
打破
案例“n”:
System.out.println(“好的,那么让我们开始吧。”);
交易(甲板、游戏);
打破
违约:
打破
}
System.out.printf(“\n%s,选择一张卡并记住它…\n”,名称);
/*开始卡片技巧循环*/
对于(i=0;i<3;i++)
{
/*通过调用函数来分发前21张牌,开始这个技巧*/
/*包括输入哪一列的错误检查*/
做
{
/*请玩家选择一张牌,并确定牌所在的列*/
System.out.print(“\n您的卡在(0、1或2)中的哪一列?”:”;
column=input.nextInt();
}而(列<0 | |列>2);
/*按列拾取卡片,然后选择第二列*/
}
/*展示前十张卡片,然后展示秘密卡片*/
/*如果球员想再次比赛*/
System.out.printf(“%s,是否要再次播放(y/n)?”,名称);
字符串playtaline=input.nextLine();
}
while(playreach==“y”);
/*退出消息*/
System.out.println(“\n感谢您玩牌!\n”);
返回;
}
公共静态字符串替换字符(字符串s、int-pos、字符c){
返回s.substring(0,pos)+c+s.substring(pos+1);
}
公共静态空心建筑甲板(内部甲板[])
{
int[]已使用=新int[52];
int i=0;
int卡=0;
/*生成卡片,直到卡片组充满整数*/
而(i<甲板长度)
{
/*生成一个介于0和51之间的随机数*/
卡=兰特·耐克斯汀(52);
/*在卡的位置检查使用的阵列。
如果为0,则添加卡并将使用的位置设置为1。如果为1,则生成另一个号码*/
如果(已使用[卡]==0)
{
已用[卡片]=1;
牌组[i]=卡片;
i++;
}
}
}
公共静态void打印组(内部组[])
{
对于(int i=0;i<52;i++){
打印卡(一);
}
/*打印出卡片组中的每张卡片*/
}
公共静态无效交易(整数组[],整数播放[])
{
int卡=0;
/*通过将cardvalues的地址从
甲板排列为
import java.util.Scanner;
import java.lang.String;
import java.util.Random;
public class CardTrickStarterFile {
private static Random rand = new Random();
private static String PlayAgain;
public static void main(String[] args) {
/* declare and initialize variables */
int column = 0, i = 0;
/* Declare a 52 element array of integers to be used as the deck of cards */
int[] deck = new int[52];
/* Declare a 7 by 3 array to receive the cards dealt to play the trick */
int[][] play = new int[7][3];
/* Declare a Scanner object for input */
Scanner input = new Scanner(System.in);
/* Generate a random seed for the random number generator. */
/* Openning message. Ask the player for his/her name */
System.out.println("\nHello, I am a computer program that is so smart");
System.out.println("I can even perform a card trick. Here's how.\n");
System.out.println("To begin the card trick type in your name: ");
String name = input.nextLine();
char firstL = name.charAt(0);
firstL = Character.toUpperCase(firstL);
name = replaceCharAt(name, 0, firstL);
/* Capitalize the first letter of the person's name. */
System.out.println("\nThank you " + name);
do
{
/* Build the deck */
BuildDeck(deck);
/* Ask if the player wants to see the entire deck. If so, print it out. */
System.out.println("Ok " + name + ", first things first. Do you want to see what ");
System.out.println("the deck of cards looks like (y/n)? ");
String SeeDeck = input.nextLine();
switch (SeeDeck)
{
case "y":
PrintDeck(deck);
break;
case "n":
System.out.println("Ok then let us begin.");
Deal(deck,play);
break;
default:
break;
}
System.out.printf("\n%s, pick a card and remember it...\n", name);
/* Begin the card trick loop */
for(i = 0; i < 3; i++)
{
/* Begin the trick by calling the function to deal out the first 21 cards */
/* Include error checking for entering which column */
do
{
/* Ask the player to pick a card and identify the column where the card is */
System.out.print("\nWhich column is your card in (0, 1, or 2)?: ");
column = input.nextInt();
} while(column < 0 || column > 2);
/* Pick up the cards, by column, with the selected column second */
}
/* Display the top ten cards, then reveal the secret card */
/* if the player wants to play again */
System.out.printf("%s, would you like to play again (y/n)? ", name);
String PlayAgain = input.nextLine();
}
while(PlayAgain == "y");
/* Exiting message */
System.out.println("\nThank you for playing the card trick!\n");
return;
}
public static String replaceCharAt(String s, int pos, char c) {
return s.substring(0,pos) + c + s.substring(pos+1);
}
public static void BuildDeck( int deck[])
{
int[] used = new int[52];
int i = 0;
int card = 0;
/* Generate cards until the deck is full of integers */
while(i < deck.length)
{
/* generate a random number between 0 and 51 */
card = rand.nextInt(52);
/* Check the used array at the position of the card.
If 0, add the card and set the used location to 1. If 1, generate another number */
if(used[card] == 0)
{
used[card] = 1;
deck[i] = card;
i++;
}
}
}
public static void PrintDeck( int deck[] )
{
for (int i=0; i < 52; i++){
PrintCard(i);
}
/* Print out each card in the deck */
}
public static void Deal( int deck[], int play[][] )
{
int card = 0;
/* deal cards by passing addresses of cardvalues from
the deck array to the play array */
System.out.println("\n Column 0 Column 1 Column 2");
System.out.println("=======================================================");
for(int row = 0; row < play.length; row++){
for(int col = 0; col < play[row].length; col++){
play[row][col]=deck[card];
card++;
System.out.printf("%s", PrintCard(play[row][col]) + " ");
}
System.out.println();
}
}
public static String PrintCard( int card )
{
int rank = (card % 13);
int suit = (card / 13);
String[] suits = { "Clubs", "Hearts", "Diamonds", "Spades" };
String[] ranks = { "King", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen"};
return (ranks[rank] + " of " + suits[suit]);
}
public static void PickUp( int deck[], int play[][], int column )
{
int card = 0, row = 0;
return;
}
public static void SecretCard( int deck[] )
{
int card = 0;
System.out.println("\nFinding secret card...");
for(card = 0; card < 10; card++)
PrintCard(deck[card]);
System.out.println("\nYour secret card is: ");
PrintCard(deck[card]);
return;
}
}