matmatmat
May 18th, 2009, 04:19 PM
I have a simple c program to play rock paper scissors:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char *argv[])
{
/* Simple "srand()" seed: just use "time()" */
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
int ai;
int p = 1000;
while (p != 0){
printf("0) Quit\n1) Rock\n2) Paper\n3)Scissors\n");
scanf("%d", p);
ai = rand() % 3;
if (ai == 1){
if (p == 1){
printf("AI: Rock\n");
printf("Draw!\n");
}
else if (p == 2){
printf("AI: Rock\n");
printf("You win!\n");
}
else if (p == 3){
printf("AI Rock\n");
printf("You lose!\n");
}
}
else if (ai == 2){
if (p == 1){
printf("AI: Paper\n");
printf("You lose!\n");
}
else if (p == 2){
printf("AI: Paper\n");
printf("Draw!\n");
}
else if (p == 3){
printf("AI: Paper\n");
printf("You win\n");
}
}
else{
if (p == 1){
printf("AI: Scissors\n");
printf("You win\n");
}
else if (p == 2){
printf("AI: Scissors\n");
printf("You lose\n");
}
else if (p == 3){
printf("AI: Scissors\n");
printf("Draw!\n");
}
}
}
return 0;
}
it gives a Segmentation fault, I think it's the loop or the random number?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char *argv[])
{
/* Simple "srand()" seed: just use "time()" */
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
int ai;
int p = 1000;
while (p != 0){
printf("0) Quit\n1) Rock\n2) Paper\n3)Scissors\n");
scanf("%d", p);
ai = rand() % 3;
if (ai == 1){
if (p == 1){
printf("AI: Rock\n");
printf("Draw!\n");
}
else if (p == 2){
printf("AI: Rock\n");
printf("You win!\n");
}
else if (p == 3){
printf("AI Rock\n");
printf("You lose!\n");
}
}
else if (ai == 2){
if (p == 1){
printf("AI: Paper\n");
printf("You lose!\n");
}
else if (p == 2){
printf("AI: Paper\n");
printf("Draw!\n");
}
else if (p == 3){
printf("AI: Paper\n");
printf("You win\n");
}
}
else{
if (p == 1){
printf("AI: Scissors\n");
printf("You win\n");
}
else if (p == 2){
printf("AI: Scissors\n");
printf("You lose\n");
}
else if (p == 3){
printf("AI: Scissors\n");
printf("Draw!\n");
}
}
}
return 0;
}
it gives a Segmentation fault, I think it's the loop or the random number?