Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on August 05, 2011, 09:53:04 am
-
Inspired on the Rock, Papers, Scissors topic I decided to try and make a C version:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main();
int main() {
char options[][8] = {"rocks", "papers", "scissors"}; //Possible options
srand( time(NULL) ); //For random integer
/* Define variables needed */
int user_choice_index;
char user_choice[8];
int cpu_choice_index;
char cpu_choice[8];
int i;
while (1) {
/* Get user choice and index */
fgets(user_choice, sizeof user_choice, stdin);
for (i=0; i<3; i++)
{
if ( user_choice == options[i] )
{
user_choice_index = i; //Index of user's choice
break;
}
}
/* Get computer choice */
cpu_choice_index = ( rand() %3 ); //Random number [0,3]
cpu_choice = options[cpu_choice_index];
/* Check who wins */
if ( strcmp(cpu_choice, user_choice) == 0 )
{
printf("Tie\n");
}
else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
{
printf("You won");
}
else
{
printf("You lose");
}
}
return 0;
}
However, when compiling I get:
rock_papers_scissors.c:33:16: error: incompatible types when assigning to type ‘char[8]’ from type ‘char *’
On the line:
cpu_choice = options[cpu_choice_index];
Each member of the array options is of type char[8], as you can see in the declaration:
char options[][8] = {"rocks", "papers", "scissors"}; //Possible options
So why does it think it is a char * instead of a char[8]? Thanks!
EDIT
I recorded I was copying addresses and used strcpy(); but the program isn't work that well:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main();
int main() {
char options[][8] = {"rocks", "paper", "scissors"}; //Possible options
srand( time(NULL) ); //For random integer
/* Define variables needed */
int user_choice_index;
char user_choice[8];
int cpu_choice_index;
char cpu_choice[8];
int i;
while (1) {
/* Get user choice and index */
fgets(user_choice, sizeof user_choice, stdin);
for (i=0; i<3; i++)
{
if ( user_choice == options[i] )
{
user_choice_index = i; //Index of user's choice
break;
}
}
/* Get computer choice */
cpu_choice_index = ( rand() %3 ); //Random number [0,3]
strcpy(cpu_choice, options[cpu_choice_index]);
printf("%s", cpu_choice);
/* Check who wins */
if ( strcmp(cpu_choice, user_choice) == 0 )
{
printf("Tie\n");
}
else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
{
printf("You won\n");
}
else
{
printf("You lose\n");
}
}
return 0;
}
When I inputed scissors, here's what happened:
scissors
scissors�@You won
rocksYou lose
-
Change
cpu_choice = options[cpu_choice_index];
to
*cpu_choice = options[cpu_choice_index];
-
Change
cpu_choice = options[cpu_choice_index];
to
*cpu_choice = options[cpu_choice_index];
Could you see my edit please? :) And that doesn't work by the way HOMER-16, since you're making an integer from a pointer without a cast.
-
Ah yeah, I didn't see that.
You can fix the
scissors�@You won
By making the char array able to hold 9 chars to make the string 0 terminated.
char options[][9] = {"rocks", "paper", "scissors"}; //Possible options
srand( time(NULL) ); //For random integer
/* Define variables needed */
int user_choice_index;
char user_choice[9];
int cpu_choice_index;
char cpu_choice[9];
Idk enough C to make it say if you won, lost or tied The Game properly though.
(It keeps saying I lost, so I guess I lost [and you did too]) :P
-
By making the char array able to hold 9 chars to make the string 0 terminated.
Nicely spotted. The strcmp( is not working, when we tie, it says "You lose" :/ Current code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main();
int main() {
char options[][9] = {"rocks", "paper", "scissors"}; //Possible options
srand( time(NULL) ); //For random integer
/* Define variables needed */
int user_choice_index;
char user_choice[9];
int cpu_choice_index;
char cpu_choice[9];
int i;
while (1) {
/* Get user choice and index */
fgets(user_choice, sizeof user_choice, stdin);
for (i=0; i<3; i++)
{
if ( user_choice == options[i] )
{
user_choice_index = i; //Index of user's choice
break;
}
}
/* Get computer choice */
cpu_choice_index = ( rand() %3 ); //Random number [0,3]
strcpy(cpu_choice, options[cpu_choice_index]);
printf("%s\n", cpu_choice);
/* Check who wins */
if ( strcmp(cpu_choice, user_choice) == 0 )
{
printf("Tie\n");
continue;
}
else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
{
printf("You won\n");
continue;
}
else
{
printf("You lose\n");
continue;
}
}
return 0;
}
-
I see a few problems:
In your for loop, you should be using strcmp, not ==
You should just be using printf("%s\n", options[cpu_choice_index]);. Don't do whatever mess you were trying with strcpy.
To check who wins, you should not be comparing strings, just use cpu_choice_index and user_choice_index.
Finally, those continue's are unnecessary.
-
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main();
int main() {
char options[][9] = {"rocks", "paper", "scissors"}; //Possible options
srand( time(NULL) ); //For random integer
/* Define variables needed */
int user_choice_index;
char user_choice[9];
int cpu_choice_index;
int i;
while (1) {
/* Get user choice and index */
fgets(user_choice, sizeof user_choice, stdin);
for (i=0; i<3; i++)
{
if ( strcmp(options[i], user_choice) == 0 )
{
user_choice_index = i; //Index of user's choice
break;
}
}
/* Get computer choice */
cpu_choice_index = ( rand() %3 ); //Random number [0,3]
printf("%s\n", options[cpu_choice_index]);
/* Check who wins */
if ( user_choice_index == cpu_choice_index )
{
printf("Tie\n");
}
else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
{
printf("You won\n");
}
else
{
printf("You lose\n");
}
}
return 0;
}
Made some changes calcdude, I think it's fixed :)
-
Excellent! :D
One of the more difficult things about C is remembering that there are no strings with pleasant operations like '==' like in Python, only char pointers and functions on them. C's '==' is very close to (if not the same as!) Python's 'is'
-
If I input scissors though:
scissors
scissors
Tie
rocks
You lose
-
"A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str."
That might be your problem. If it is, you'll need to make user_choice a 10-byte array: 8 for printing characters, one for the newline, and another for the null. That'll throw of your comparisons with strcmp, though, so you could also add a newline to each element of options. Then the printf("%s\n"... line won't need that \n.
-
"A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str."
That might be your problem. If it is, you'll need to make user_choice a 10-byte array: 8 for printing characters, one for the newline, and another for the null. That'll throw of your comparisons with strcmp, though, so you could also add a newline to each element of options. Then the printf("%s\n"... line won't need that \n.
That could add up to multiple bytes uneedlessly wasted by including the newline :P but, since this isn't being optimized for a less-than-16-bit-address-spaced RAM space, I think it'll be quite fine.
And, to point out, == works the same as python's "is" in working, but it's not exactly the same since with C; it just compares the values of the two pointers, and Python's does a tiny bit more just to make sure the two instances are unique. But yeah, to humans, its the same :P
-
Ashbad: it's the same instance if and only if it's at the same address, as far as I can tell. Note that small integers actually do act like C ints, so 5 is 5 returns True, just like 5 == 5 in C returns whatever's C's value for true is.
-
Ashbad: it's the same instance if and only if it's at the same address, as far as I can tell. Note that small integers actually do act like C ints, so 5 is 5 returns True, just like 5 == 5 in C returns whatever's C's value for true is.
Hmm, perhaps I'm thinking of another language, though I was pretty sure python did more to check than just comparing two hashes, since there is some really minor undertakings to achieve attaining a hash, since objects in python aren't referred to as primitively as they are in lower level language (hash and offset :P). Again, I might be thinking of another language.
-
Since when did an address become a hash? ???
Anyway, I think at the lowest level it's by address, since the following code produces an address:
class A:
pass
print repr(A())
That, and pointers are also a special address type. Edit: not in Python, though, which, as we know, doesn't have pointers.