如何用C ++编写程序? [英] How to write programming in C++?

查看:83
本文介绍了如何用C ++编写程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第1部分
使用数组创建标准牌组。 (1维或2维,由您决定)。牌组应该有52张牌。对于4件套装(黑桃,心形,俱乐部和钻石)中的每一套,甲板应该有13个等级(1到King)。
之后,添加能够从套牌中随机抽取卡片的代码。

第2部分
接下来实施一个3卡猜谜游戏,它将执行以下操作:
1.每张左手,中等和右手一张卡。
2.向用户显示(或显示)所绘制的卡片及其原始位置。
3.明确指出MID中的卡片。
4.翻转卡片并随意洗牌。 (不要向玩家显示哪张牌位于哪个位置)
5.让玩家猜猜原始MID牌的位置。
6.如果玩家猜测正确,请将玩家声明为胜利者。如果玩家猜错了,请显示正确的牌位置并将玩家声明为输家。
7.如果他/她想再玩一次,请询问玩家。





我尝试了什么:



  #include   <   iostream  >  
#include < cassert >
# include < fstream >
#include < 字符串 >
#include < iomanip >

使用 namespace std;


const unsigned int DECK_SIZE = 52 ;
const unsigned int MAX_RANK = 13 ;
const unsigned int MAX_SUIT = 4 ;

const 字符串排名[MAX_RANK] =
{
Ace 两个 < span class =code-string>四, Seven 八个 Nine Jack Queen King

};

const 字符串适合[MAX_SUIT] =
{
黑桃 Hearts 分会 < span class =code-string>钻石
};

struct
{
字符串排名;
字符串套装;
};
卡片组[DECK_SIZE];
void initializeDeck();

void initializeDeck()
{
int I,J;
int k = 0 ;


for (i = 0 ; i< MAX_RANK; i ++)
{
for (j = 0 ; j< MAX_SUIT; j ++ )
{
deck [k] .ranks = ranking [i];
deck [k] .suits = suit [j];
k ++;
}
}

}


int main()
{
initializeDeck();

for int i = 0 ; i< 52; i ++)
{
int j =(rand()%52);
卡片temp = deck [i];
deck [i] = deck [j];
deck [j] = temp;
}


for int i = 0 ; i< 52; i ++)
{
cout<< setw( 10 )LT;<甲板[I] .ranks;
cout<< setw( 8 )<< deck [i] .suits;

if (i%4 == 3
{
cout<< ENDL;
}
}




return 0 ;
}

解决方案

我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



你已经开始了,所以测试一下,当你开心的时候它会起作用(而且这根本不是一个很好的洗牌,但是你的作业并没有提到改组)继续练习的下一部分。亲自尝试一下,你可能会发现它并不像你想象的那么困难!



如果你遇到一个具体的问题,那么请询问一下,我们会尽力而为帮助。但是我们不会为你做这一切!


你有什么问题?

你能完成第1部分(显示随机卡) ?

您是否需要实际绘制卡片(即您是否必须使用图形库)?

因为它是stanbds,很难回答您的问题。

Part 1
Create a standard deck of cards using an array. (1-dimensional or 2-dimensional, up to you). The deck should have 52 cards. The deck should have 13 ranks (1 to King) for each of the 4 suits (Spades, Hearts, Clubs and Diamonds). 
After that, add in code that will be able to draw a card randomly from the deck.

Part 2
Next implement a 3-card guessing game that will do the following:
1.Draw ONE card each for LEFT, MID and RIGHT.
2.Indicate (or show) the user the cards drawn and their original position. 
3.Clearly point out the card in MID.
4.Flip the cards around and shuffle them. (don’t show the player which card is in which position)
5.Let the player guess where the original MID card is.
6.If the player guess correct, declare the player as a winner. If the player guess wrongly, show where the correct card is and declare the player as loser.
7.Ask the player if he/she wish to play again.



What I have tried:

#include<iostream>
#include<cassert>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;


const unsigned int DECK_SIZE=52;
const unsigned int MAX_RANK=13;
const unsigned int MAX_SUIT=4;

const string ranks[MAX_RANK]=
{
	"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"
	
};

const string suits[MAX_SUIT]=
{
	"Spades","Hearts","Clubs","Diamonds"
};

struct Card
{
	string ranks;
	string suits;
};
Card deck[DECK_SIZE];
void initializeDeck();

void initializeDeck()
{
	int i,j;
	int k=0;
	
	
	for(i=0; i<MAX_RANK; i++)
	{
		for(j=0; j<MAX_SUIT;j++ )
		{
			deck[k].ranks=ranks[i];
			deck[k].suits=suits[j];
			k++;
		}
	}
	
}


int main()
{
	initializeDeck();
	
	for(int i=0; i<52; i++)
	{
		int j=(rand()%52);
	 	Card temp=deck[i];
		deck[i]=deck[j];
		deck[j]=temp;
	}
	

	for(int i=0; i<52; i++)
	{
		cout<<setw(10)<<deck[i].ranks;
		cout<<setw(8)<<deck[i].suits;
	
		if	(i%4==3)
		{
		    cout<<  endl;
		}
	}
	

	
	
	return 0;
}

解决方案

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

You've made a start, so test that, and when you are happy it works (and that's not a good shuffle at all but your homework doesn't mention shuffling) move on to the next part of the exercise. Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


What is your problem?
Are you able to complete part 1 (show a random card)?
Do you need to actually draw the cards (i.e. do you have to use graphic library)?
As it stanbds, it is difficult to answer your question.


这篇关于如何用C ++编写程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆