4 Replies - 5427 Views - Last Post: 04 March 2011 - 09:08 AM Rate Topic: -----

#1 nonotchi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-March 11

Making the game Rock,Paper,Scissors using C

Posted 03 March 2011 - 10:42 PM

Hi there.
This is the first post I'm making.
A bit of myself:
I'm a university student who finished the C programming course at his first year and passed it. Now, I'm looking forward to improving myself.

Just to let you know, I got 60% as the final mark which is not satisfying in my opinion. So, I'm going to do extra effort and exercise C programing during my free time =)

I'm still novice and I'm going to start bit by bit getting to know this language. Today, I decided to make a C-language code for the the game "Rock,Paper,Scissors". Just to let you know, I'm using ideas from other programmers and I'm attempting to learn through examples =)

I have made these functions:
char *GetUserChoice();
char *GetComChoice();
void PlayGame(char *ComMove, char *UserMove);
void WhoWins(char *resultCom, char *resultUser);




This is the Game that would print out who chose which:
void PlayGame(char *ComMove, char *UserMove)
{
if (ComMove="Rock")
printf("Com chose Rock!\n");
else if (ComMove="Scissors")
printf("Com chose Scissors!\n");
else printf("Com chose Papers\n");

if (UserMove="Rock")
printf("User chose Rock!\n");
else if (UserMove="Scissors")
printf("User chose Scissors!\n");
else printf("User chose Papers\n");

}




For this code, I'm trying to randomise the COM's choice:
char *GetComChoice()
{
	enum CHOICE {Rock=0,Paper,Scissors} TheChoice;

	int j;
	srand((unsigned)time(0)); //<--Please, explain why this is crusial?
	j = (rand()%3);  //Because wihtout ^^/>^ it, I'd receive a constant =S
	TheChoice = CHOICE(j);
	//printf("%d\n", j);
	//printf("%c",TheChoice);
	return (char *)TheChoice;
}



In here, I'm prompting the USER to choose:
char *GetUserChoice()
{
	char buff[10];
	printf("Enter your choice:  ");
	scanf("%s", buff);
	return buff;
}




And here I'm making the judge of the game:
void WhoWins(char *resultCom, char *resultUser)
{
	if (resultCom==resultUser)
		printf("It's a Tie!\n");
	else if(resultCom=="Rock" && resultUser=="Scissors")
		printf("Rock blunts scissors, User Wins!\n");
	else if(resultUser=="Rock" && resultCom=="Scissors")
		printf("Rock blunts scissors, Com Wins!\n");
	else if(resultCom=="Rock" && resultUser=="Paper")
		printf("Paper covers Rock, User Wins!");
	else if(resultUser=="Rock" && resultCom=="Paper")
		printf("Paper covers Rock, Com Wins!\n");
	else if(resultCom=="Scissors" && resultUser=="Paper")
		printf("Scissors cut Paper, Com Wins!\n");
	else printf("Scissors cut Paper, User Wins!\n");
}




My main function is not that hard to understand, just using the functions I made and plug in the variables =)
int main()
{
	char *ComMove;
	char *UserMove;
UserMove = GetUserChoice();
ComMove = GetComChoice();
PlayGame(ComMove, UserMove);
WhoWins (ComMove, UserMove);

return 0;
}



What I receive always is that both my GetChoice functions return Rock, and my WhoWins function gives me the "else" result, but not the "if" result. (if says when they're both the same, it prints Tie).

My opinion:
I believe I'm having difficulties understanding pointers.

My question:
What's wrong?

Thank you for reading my topic =)

Is This A Good Question/Topic? 0
  • +

Replies To: Making the game Rock,Paper,Scissors using C

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Making the game Rock,Paper,Scissors using C

Posted 03 March 2011 - 11:42 PM

The thing that I see right away is wrong is that you have a bunch of statements in PlayGame like this:

if (ComMove="Rock")

Those are assignments, not tests for equality, so every time you call PlayGame you assign "Rock" as the move for both players. So why don't you change all those = to == and see if you can take it from there...
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Making the game Rock,Paper,Scissors using C

Posted 03 March 2011 - 11:52 PM

Oh, regarding srand:
rand() doesn't really generate random numbers. It generates pseudorandom numbers. It takes a starting value called a seed and performs certain calculations based on in that generates a series of output values that "look" random. But if you always give it the same seed, it will always generate the same series of numbers. If you don't seed it by calling srand(), rand will always have the same default seed (0 I think, but that doesn't matter -- whatever it is it will always be the same, and then rand will always generate the same outputs.

When you call srand and give it time(NULL) (you should use NULL instead of 0), that's giving it the current system time (from the computer's clock) as its seed and since the time is always changing it always gets a different seed.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,593
  • Joined: 16-October 07

Re: Making the game Rock,Paper,Scissors using C

Posted 04 March 2011 - 05:53 AM

This won't work:
char *resultCom;
if(resultCom=="Rock" ...



You're essentially saying "does the address pointed to by resultCom match the address pointed to by the static constant string Rock." It doesn't, never will. You need strcmp.

Throwing strings about is often a bad idea. They're big and they can be anything. You have a finite list of allowable values. Lock the user down to those and you'll have an easier time.

e.g.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CHOICE_COUNT 3
typedef enum { Rock, Paper, Scissors } ChoiceType;
const char *ChoiceTypeNames[CHOICE_COUNT] = {"Rock", "Paper", "Scissors" };

ChoiceType GetComChoice();
ChoiceType GetUserChoice();

void PlayGame(ChoiceType, ChoiceType);
void WhoWins(ChoiceType, ChoiceType);

int main() {
	ChoiceType computer = GetComChoice();
	ChoiceType user = GetUserChoice();
	PlayGame(computer, user); 
	WhoWins(computer, user); 
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Making the game Rock,Paper,Scissors using C

Posted 04 March 2011 - 09:08 AM

View Postbaavgai, on 04 March 2011 - 07:53 AM, said:

This won't work:
char *resultCom;
if(resultCom=="Rock" ...


Ugh...yeah.
c-strings...c++ strings...c-strings...c++ strings...dammit...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1