石头纸剪刀游戏的代码 [英] Code for a stone paper scissor game

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

问题描述

以下代码打印draw!无论用户提供什么输入,都作为输出

请帮助。



我尝试过:



the following code prints "draw!" as output no matter what input the user provides
pls help.

What I have tried:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>

void main()
{
	clrscr();

	int choice1;
	char choice2[50];
	int compare(const char * ,const char * )   ;        //prototype
	char s[100];
	cout<<"\n\n WELCOME TO STONE PAPER SCISSORS "<<endl<<endl;
	cout<<" enter your choice"<<endl;

	cout<<"\n 1:SCISSORS \n";
	cout<<"\n 2:ROCK \n";
	cout<<"\n 3:PAPER \n";
	cout<<"enter choice number";
	cin>>choice1;

	if(choice1==2)
	{
		cout<<"you have entered Stone!";
		strcpy(s,"ROCK");
	}
	if (choice1==1)
	{
		cout<<"you have entered scissors";
		strcpy(s,"SCISSORS");
	}
	if(choice1==3)
	{
		strcpy(s,"PAPER")  ;
		cout<<"you have entered paper";
	}
	randomize();
	float point =2;
	float compchoice;
	compchoice=random(point);

	if (compchoice<0.37)
	{
		strcpy(choice2,"ROCK")    ;

	}
	else  if(compchoice<0.64)
	{
		strcpy( choice2,"PAPER");
	}
	else
	{
		strcpy(choice2,"SCISSORS");
	}

	cout<<endl;
	cout<<"User Choice="<<s<<endl;
	cout<<"Computer Choice="<<choice2<<endl;
	cout<<s<<"\t"<<"VS"<<"\t"<<choice2<<"="<<" ";

	int p=compare(s,choice2);
	if(p==1)
	{
		cout<<"computer wins";
		if(p==0)
			cout<<"user wins";
		if(p==-1)
			cout<<"draw!";

		getch();
	}
	int compare(const char* s, const char * choice2)
	{
		if(s=="SCISSORS")
		{
			if(choice2=="ROCK")
				return 1;
			else
				return 0;
		}
		else if(s=="ROCK")
		{
			if(choice2=="SCISSORS")

				return 0;
			else
				return 1;

		}
		else if(s=="PAPER")
		{
			if(choice2=="SCISSORS")
				return 1;
			else
				return 0;
		}
		else
		return -1;
	}

推荐答案

从调试器开始,尝试找出原因!

在断行上放置一个断点:

So start with the debugger, and try to find out why!
Put a breakpoint on the line:
int p=compare(s,choice2);

并在调试器中运行您的应用程序。当它到达那一行时,它会停止并等待你告诉它该怎么做。

使用调试器来查看各种变量中的确切内容并确切地确定应该发生什么在执行之前执行下一行。它完全符合您的预期吗?如果是这样,继续下一步。如果没有,为什么不呢?你做了什么,你没想到,或者没有做到这一点?



这是一项技能 - 可以预见的称为调试 - 就像你开发的所有技能一样通过使用它。在这样一个简单的应用程序上学习它比在别人写的100,000行庞然大物更容易! :笑:

所以试一试,看看你能找到什么。

And run your app in the debugger. When it gets to that line, it'll stop and wait for you to tell it what to do.
Use the debugger to look at exactly what is in the various variables and work out exactly what should happen when you execute the next line before you execute it. Did it do exactly what you expected? If so, move on to the next. If not, why not? What did it do that you didn't expect, or not do that you did?

This is a skill - predictably called debugging - and like all skills you only develop it by using it. And it's a lot easier to learn it on a simple app like this than a 100,000 line behemoth written by someone else! :laugh:
So give it a try, and see what you can find out.


建议:

使用程序员的文字编辑器并使用缩进

Notepad ++主页 [ ^ ]

通过缩进湖我有在您的问题中完成,可以看到在 main 结尾处缺少}



另一个问题是你无法将char数组与运算符 == 进行比较。

Advice:
Use a programmer's text editor and use indentation
Notepad++ Home[^]
By indenting lake I have done in your question, one can see that a } is missing at the end of main.

Another problem is that you can't compare char arrays with operator ==.
作者:Albert Einstein
Author: Albert Einstein

一切都应尽可能简单,但不能简单。

"Everything should be made as simple as possible, but no simpler."



建议:

简化代码:使用数字存储选项并仅在输出结果时使用字符串。


Advice:
Simplify your code : use numbers to store choices and use strings only when you output a result.


这篇关于石头纸剪刀游戏的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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