一场小游戏 [英] A small game

查看:58
本文介绍了一场小游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的朋友给我发了一个用C ++编写的游戏原型并要求

建议:


IDEA:游戏会让用户瘦下来一个号码。用户在想到一个号码后会按下

击键。然后程序会问他

做一些小算法,最后会询问结果。然后打印用户想到的东西。


这是我完成的。任何建议表示赞赏:)


#include< iostream>


int main()

{

const int think_time = 5;


std :: cout<< std :: endl;

std :: cout<< "<<<<<<<让我们玩一个游戏>>>>>> \ n" << std :: endl;

std :: cout<< \tThink一个介于1到9之间的数字

<< 完成时\\按下Ctrl-D;

char ch;

while(std :: cin> ch)

{

继续;

}


std :: cout<< std :: endl<< std :: endl;

std :: cout<< \t *将10添加到该号码\ n;;

sleep(think_time);

std :: cout<< \t *现在从它中减去5 \ n;

sleep(think_time);

std :: cout<< " \t *在它上面添加另一个20 \ nn ;;

sleep(think_time);

std :: cout<< \t *从它中减去7 \ n;

sleep(think_time);

std :: cout<< \t *从它减去3 \ n;

sleep(think_time);

std :: cin.clear();

std :: cout<<" \tHow你现在有多少: - " ;;


int think_num;

std: :cin> think_num;


int result = think_num - 15;


if(result< 0)

{

result = - (result);

}


std :: cout<<"。 ....你认为的数字是: - "

<<结果

<< std :: endl;

返回0;

}


-
www.lispmachine.wordpress.com

我的电子邮件是@上面的博客。

谷歌群组被封锁。原因:过度垃圾邮件

解决方案

9月16日上午10:12 * am,arnuld< sunr ... @ invalid.addresswrote :


我的朋友给我发了一个用C ++编写的游戏原型并要求

建议:


IDEA:游戏会要求用户减少数字。用户在想到一个号码后会按下

击键。然后程序会问他

做一些小算法,最后会询问结果。然后打印用户想到的东西。


这是我完成的。任何建议表示赞赏:)


#include< iostream>


int main()

{

* const int think_time = 5;


* std :: cout<< std :: endl;

* std :: cout<< "<<<<<<<让我们玩一个游戏>>>>>> \ n" << std :: endl;


* std :: cout<< \tThink一个介于1到9之间的数字

* * * * * *<< 完成后\t按Ctrl-D;


* char ch;

* while(std :: cin> ch)

* * {

* * * * *继续;

* *}


* std :: cout<< std :: endl<< std :: endl;

* std :: cout<< \t *将10添加到该号码\ n;;

* sleep(think_time);

* std :: cout<< \t *现在从它中减去5 \ n;;

* sleep(think_time);

* std :: cout<< \t *在它上面添加另一个20 \ n;

* sleep(think_time);

* std :: cout<< \t *从它中减去7 \ n;;

* sleep(think_time);

* std :: cout<< \t *从它中减去3 \ n;;

* sleep(think_time);

* std :: cin.clear();

* std :: cout<<" \tHow你现在有多少: - " ;;


* int think_num;

* std :: cin> think_num;


* int result = think_num - 15;


* if(result< 0 )

* * {

* * * result = * - (结果);

* *}


* std :: cout<<" .....您认为的数字是: - "

* * * * * *<<结果

* * * * * *<< std :: endl;


*返回0;


}


--www。 lispmachine.wordpress.com

我的电子邮件是@上面的博客。

谷歌群组被阻止。原因:过度垃圾邮件



您好Arnuld

这是一个简单的程序,可以练习I / O流,

输入循环,简单算术。以下是一些简单的事情:

1.假设程序在Unix环境下运行。

在消息中使用Ctrl + D / Ctrl + Z:

2.包括睡眠的接口头文件。功能。

3.使用类似Abs的函数代码如下:

if(结果< 0)

{

result = - (result);

}


int abs(int a){return a 0? a:-a; }


祝你好运

Saeed Amrollahi


9月16日下午4:12,arnuld < sunr ... @ invalid.addresswrote:


这是我完成的。任何建议表示赞赏:)

std :: cout<< std :: endl<< std :: endl;

std :: cout<< \t *将10添加到该号码\ n;;

sleep(think_time);



std :: endl写一个换行符(''\ n'')并刷新输出流。当你想确定在下一个命令运行之前屏幕上会显示消息

时,你想要这样做.b $ b想要这样做。所以,你应该在调用sleep之前使用endl

而不是\ n(或者你可能看不到所有平台上的消息),你不应该使用endl这对夫妇中的每一个都有前缀换行符(或者你会养成一个坏习惯,这将使你的b / b
更大的程序慢慢写出他们的输出)。同样的原则可以在你的程序中的许多其他地方应用




此外,期望能够在控制之后恢复从cin读取 -

D可能无法移植。我建议你只要求用户按

输入,然后读取/忽略字符,直到收到''\ n''。那个

有其他小的好处,比如你可以测试你的程序ala(echo;

echo 22)| ./program"。

干杯,

Tony


9月17日,4:01 am,tony_in_da ... @ yahoo.co.uk写道:


9月16日下午4:12,arnuld< sunr ... @ invalid.addresswrote :


这是我完成的。任何建议表示赞赏:)

std :: cout<< std :: endl<< std :: endl;

std :: cout<< \t *将10添加到该号码\ n;;

sleep(think_time);


std :: endl写一个换行符(''\ n'')并刷新输出

流。如果您想确保在下一个

命令运行之前屏幕上会显示

消息,您希望这样做。所以,在

调用sleep之前你应该使用endl而不是\ n(或者你可能没有在所有
平台上看到这条消息),你不应该使用endl这对夫妇中的每一个都有前缀换行符(或者你会养成一个坏习惯,这将使你的大型程序慢慢地写出他们的输出)。相同的

原则可以应用于

计划中的许多其他地方。



你应该使用endl直到你理解缓冲问题。

考虑到发布的水平,他还远远没有。另外,

默认情况下,std :: cin和std :: cout是绑定的,所以在std :: cin上读取
的任何尝试都会刷新std上的缓冲区: :COUT。 (一个简单的

输出语句的末尾使用endl,但是当紧接着有更多文本时使用''\\ n'''。

所以他可能写上面的内容:

std :: cout<<" \ n\\\
\t *将10添加到该数字< ;< std :: endl;

..这就是你推荐的内容,在这种情况下,但

规则并不需要任何理解缓冲。)


此外,期待能够在

控制-D之后能够从cin恢复读取-D可能无法移植。



如果有效,我会认为这是一个错误。没有

std :: cin.clear(),标准保证它不起作用;

带有std :: cin.clear(),它'QoI问题,但确实

也不应该工作。


我建议你只要求用户按 ;输入&,和

读取/忽略字符,直到收到''\ n''。



更好的设计。


这有其他小的好处,比如你可以测试你的程序

ala"(echo; echo 22)| ./program" ;.



他真的应该编写一些脚本来自动化测试

的程序。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34


My friend sent me the prototype of a game written in C++ and asked for
advice:

IDEA: Game will ask the user to thin of a number. User will press a
keystroke after he has thought of a number. Then program will ask him to
do some little arithmetic and will finally ask for the result. It will
then print what user has thought of.

Here is what I finished with. Any advice is appreciated :)

#include<iostream>

int main()
{
const int think_time = 5;

std::cout << std::endl;
std::cout << "<<<<<<< Let''s play a Game >>>>>>\n" << std::endl;
std::cout << "\tThink of a number between 1 to 9\n"
<< "\tPress Ctrl-D when you are done";
char ch;
while( std::cin >ch )
{
continue;
}

std::cout << std::endl << std::endl;
std::cout << "\t*Add 10 to that number\n";
sleep(think_time);
std::cout << "\t*Now subtract 5 from it\n";
sleep(think_time);
std::cout << "\t*Add another 20 to it\n";
sleep(think_time);
std::cout << "\t*Subtract 7 from it\n";
sleep(think_time);
std::cout << "\t*Subtract 3 from it\n";
sleep(think_time);
std::cin.clear();
std::cout <<"\tHow Much U Have Now :- ";

int think_num;
std::cin >think_num;

int result = think_num - 15;

if(result < 0)
{
result = -(result);
}

std::cout <<".....Number you thought is :- "
<< result
<< std::endl;
return 0;
}


--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

解决方案

On Sep 16, 10:12*am, arnuld <sunr...@invalid.addresswrote:

My friend sent me the prototype of a game written in C++ and asked for
advice:

IDEA: Game will ask the user to thin of a number. User will press a
keystroke after he has thought of a number. Then program will ask him to
do some little arithmetic and will finally ask for the result. It will
then print what user has thought of.

Here is what I finished with. Any advice is appreciated :)

#include<iostream>

int main()
{
* const int think_time = 5;

* std::cout << std::endl;
* std::cout << "<<<<<<< Let''s play a Game >>>>>>\n" << std::endl;

* std::cout << "\tThink of a number between 1 to 9\n"
* * * * * * << "\tPress Ctrl-D when you are done";

* char ch;
* while( std::cin >ch )
* * {
* * * * * continue;
* * }

* std::cout << std::endl << std::endl;
* std::cout << "\t*Add 10 to that number\n";
* sleep(think_time);
* std::cout << "\t*Now subtract 5 from it\n";
* sleep(think_time);
* std::cout << "\t*Add another 20 to it\n";
* sleep(think_time);
* std::cout << "\t*Subtract 7 from it\n";
* sleep(think_time);
* std::cout << "\t*Subtract 3 from it\n";
* sleep(think_time);
* std::cin.clear();
* std::cout <<"\tHow Much U Have Now :- ";

* int think_num;
* std::cin >think_num;

* int result = think_num - 15;

* if(result < 0)
* * {
* * * result = *-(result);
* * }

* std::cout <<".....Number you thought is :- "
* * * * * * << result
* * * * * * << std::endl;

* return 0;

}

--www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

Hi Arnuld
It is a simple program that exercises about I/O streams,
input loop, and simple arithmetic. Here are few simple things:
1. It is assumed program is running under Unix environment.
Use Ctrl+D/Ctrl+Z in your message:
2. Include the interface header file for "sleep" function.
3. Use a function like Abs for the following code:
if(result < 0)
{
result = -(result);
}

int abs(int a) { return a 0 ? a : -a; }

Good luck
Saeed Amrollahi


On Sep 16, 4:12 pm, arnuld <sunr...@invalid.addresswrote:

Here is what I finished with. Any advice is appreciated :)
std::cout << std::endl << std::endl;
std::cout << "\t*Add 10 to that number\n";
sleep(think_time);

std::endl writes a newline (''\n'') and flushes the output stream. You
want to do this when you want to be sure the message will be displayed
on the screen before the next command runs. So, you should use endl
rather than \n before calling sleep (or you may not see the message on
all platforms), and you should not use endl for each of the couple
prefixed newlines (or you''ll get into a bad habit that will make your
larger programs write their output slowly). Same principles can be
applied in many other places in your program.

Also, expecting to be able to resume reading from cin after a control-
D may not be portable. I suggest you just ask the user to press
"Enter", and read/ignore characters until you receive a ''\n''. That
has other small benefits, like you can test your program ala "( echo;
echo 22 ) | ./program".

Cheers,
Tony


On Sep 17, 4:01 am, tony_in_da...@yahoo.co.uk wrote:

On Sep 16, 4:12 pm, arnuld <sunr...@invalid.addresswrote:

Here is what I finished with. Any advice is appreciated :)
std::cout << std::endl << std::endl;
std::cout << "\t*Add 10 to that number\n";
sleep(think_time);

std::endl writes a newline (''\n'') and flushes the output
stream. You want to do this when you want to be sure the
message will be displayed on the screen before the next
command runs. So, you should use endl rather than \n before
calling sleep (or you may not see the message on all
platforms), and you should not use endl for each of the couple
prefixed newlines (or you''ll get into a bad habit that will
make your larger programs write their output slowly). Same
principles can be applied in many other places in your
program.

You should use endl until you understand buffering issues.
Given the level of the posting, he''s far from that yet. Also,
by default, std::cin and std::cout are tied, so any attempt to
read on std::cin will flush the buffer on std::cout. (A simple
but useful general rule might be to use endl at the end of an
output statement, but ''\n'' when more text immediately follows.
So he might write the above:
std::cout << "\n\n\t*Add 10 to that number" << std::endl ;
.. Which comes out to what you recommend, in this case, but
the rule doesn''t require any understanding of buffering.)

Also, expecting to be able to resume reading from cin after a
control-D may not be portable.

I''d consider it an error if it worked. Without a
std::cin.clear(), the standard guarantees that it won''t work;
with an std::cin.clear(), it''s a QoI issue, but it really
shouldn''t work either.

I suggest you just ask the user to press "Enter", and
read/ignore characters until you receive a ''\n''.

Much better design.

That has other small benefits, like you can test your program
ala "( echo; echo 22 ) | ./program".

He really should write a number of scripts to automate testing
of the program.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于一场小游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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