built a paper rock scissor game

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
Two things before I comment on the code:
1. Saying it doesn't work doesn't help others understand what it is doing, which is often a big indicator as to where to look for a problem. Explain what it does and any errors you encounter.
2. Please put your code between code tags ([code_][/code_] (without the underscore) - it makes it easier to read without making the page unnecessarily long.

As for the code, you have

Which will generate numbers 0-3. You are expecting 1-3.
hm sorry,, i couldn't understand point no.2 (please put your code tags). hm is it too long?? i need to demonstrate it in the lab :confused::(
 

tshuck

Joined Oct 18, 2012
3,534
hm sorry,, i couldn't understand point no.2 (please put your code tags). hm is it too long?? i need to demonstrate it in the lab :confused::(
Points 1 and 2 are about forum etiquette, not regarding the problem.

So, once you've made the modification to the problem I pointed out in my previous post, what does it do?
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
Points 1 and 2 are about forum etiquette, not regarding the problem.

So, once you've made the modification to the problem I pointed out in my previous post, what does it do?
so you mean that it is too long in this forum page? hehe sorry.. it will make it shorter, i mean less space used.. i'm so sorry coz this is my second post. i'm still new here :(
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
No, just add
Code:
[code]
to the beginning of a section of code and at the end. This tells the forum software to format it specially.
like this??? hm can i ask you something? how to present the record in html format in table??

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main()
{
  char bl;
  int ch1, ch2, count1=0, count2=0; 
  do
  {
  srand(time(NULL));
  printf("\n\tThis is the paper rock scissor game");
  printf("\nYou may choose \n'1' for paper\n'2' for rock\n'3' for scissor");
  printf("\n\nPlayer 1 make a choose :");
  scanf("%d", &ch1);
  ch2=rand()%4;
   
  switch (ch1)
  {
  case 1:  printf("\n\nThe player 1 has choose paper");
  break;
  case 2:  printf("\n\nThe player 1 has choose rock");
  break;
  case 3:  printf("\n\nThe player 1 has choose scissor");
  break;
  default:  printf("\n\nInvalid grade");
  break;
   
  }
   
  switch (ch2)
  {
  case 1:  printf("\nThe player 2 has choose paper");
  break;
  case 2:  printf("\nThe player 2 has choose rock");
  break;
  case 3:  printf("\nThe player 2 has choose scissor");
  break;
  default:  printf("\nInvalid grade");
  break;
   
  }
   
  if(ch1==1 && ch2==2)
  {
  printf("\nThe player 2 win the game");
  count2++;
  }
  else if(ch1==1 && ch2==3)
  {
  printf("\nThe player 2 win the game");
  count2++;
  }
  else if(ch1==1 && ch2==1)
  {
  printf("\nThe game tie -,-");
  }
  else if(ch1==2 && ch2==1)
  {
  printf("\nThe player 1 win the game");
  count1++;
  }
  else if(ch1==2 && ch2==2)
  {
  printf("\nThe game tie");
  }
  else if(ch1==2 && ch2==3)
  {
  count2++;
  printf("\nThe player 2 win the game");
  }
  else if(ch1==3 && ch2==1)
  {
  count1++;
  printf("\nThe player 1 win the game");
  }
  else if(ch1==3 && ch2==2)
  {
  count2++;
  printf("\nThe player 2 win the game");
  }
  else if(ch1==3 && ch2==3)
  {
  printf("\nThe game tie");
  }
   
  printf("\n\nDo you want to continue ? (y/n)");
  scanf(" %c", &bl);
  }while (bl!='n');   
   
  printf("\nThe score is player1 \tplayer2\n");
  printf("  %d  \t  %d\n\n", count1, count2);
}
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
Yes, that's it.


You could generate the HTML by building the string as the program progresses.
You can add the HTML to a string and write that string to a file. Do you know HTML?
thank you. yes i know it but just know about the basic. heading, paragraph, list, link.. just write it on notepad?
 

tshuck

Joined Oct 18, 2012
3,534
thank you. yes i know it but just know about the basic. heading, paragraph, list, link.. just write it on notepad?
In the program:
E.g.
Code:
string htmlStr = new string();
htmlStr.append("<html><body><table>");
//more code
htmlStr.append("</table></body></html>");
This way, the program creates the HTML file. You'll need to add the table functionality.

Do you know how to write to a file from the program?
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
In the program:
E.g.
Code:
string htmlStr = new string();
htmlStr.append("<html><body><table>");
//more code
htmlStr.append("</table></body></html>");
This way, the program creates the HTML file. You'll need to add the table functionality.

Do you know how to write to a file from the program?
hm not yet, i don't reach the topic yet. maybe i need to study on my own first. in linux, it is a bit different from windows
 

tshuck

Joined Oct 18, 2012
3,534
hm not yet, i don't reach the topic yet. maybe i need to study on my own first. in linux, it is a bit different from windows
Don't be so quick to assume that just because you don't know something the other person doesn't understand.

Since you don't know that method, do the same using char[] htmlStr and the functions defined in string.h.
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
rand()%4 will produce values {0,1,2,3}. If you want random values {1,2,3} then you want to do

k = rand()%3 + 1;
yes i found it. i forgot to write srand(time(NULL)).. hehe thank you, my friend. i had to learn structure programming this semester
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
Don't be so quick to assume that just because you don't know something the other person doesn't understand.

Since you don't know that method, do the same using char[] htmlStr and the functions defined in string.h.
first, i need to #define <string.h> and then where should i put the char[] htmlStr?? i should declare it first, right?
hm i'm so sorry if i can't understand what u said in a quick time. i am a bit blur
 

tshuck

Joined Oct 18, 2012
3,534
first, i need to #define <string.h> and then where should i put the char[] htmlStr?? i should declare it first, right?
hm i'm so sorry if i can't understand what u said in a quick time. i am a bit blur
It is a learning process and takes time. You should declare char[] htmlStr (or whatever you want to call it) before you overwrite any of the variables (ch1 and ch2) once they've been assigned.

Since the program runs sequentially, you will need to add the HTML string data after the data is initialized and before the data is overwritten by a subsequent game.
So, something like:
  1. User selects choice
  2. Generate opponent's choice
  3. Add data to htmlStr
  4. Display result and add that result to htmlStr
  5. If the player wants to play again, go to 1, otherwise, go to 6.
  6. Write htmlStr to a file (don't forget to close it!)
  7. Done!
By the way, this is what I was trying to get you to do beforehand - define your goals, then your functionality. The steps above are your functionality. You should try to do this for every problem you approach (programming and in general), this way, you know the approach and have a definite direction to move in.
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
It is a learning process and takes time. You should declare char[] htmlStr (or whatever you want to call it) before you overwrite any of the variables (ch1 and ch2) once they've been assigned.

Since the program runs sequentially, you will need to add the HTML string data after the data is initialized and before the data is overwritten by a subsequent game.
So, something like:
  1. User selects choice
  2. Generate opponent's choice
  3. Add data to htmlStr
  4. Display result and add that result to htmlStr
  5. If the player wants to play again, go to 1, otherwise, go to 6.
  6. Write htmlStr to a file (don't forget to close it!)
  7. Done!
By the way, this is what I was trying to get you to do beforehand - define your goals, then your functionality. The steps above are your functionality. You should try to do this for every problem you approach (programming and in general), this way, you know the approach and have a definite direction to move in.
Wow need to learn a lot... need time. Generally the question stated that to record 10 matches. I will try it out later. hehe
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
It is a learning process and takes time. You should declare char[] htmlStr (or whatever you want to call it) before you overwrite any of the variables (ch1 and ch2) once they've been assigned.

Since the program runs sequentially, you will need to add the HTML string data after the data is initialized and before the data is overwritten by a subsequent game.
So, something like:
  1. User selects choice
  2. Generate opponent's choice
  3. Add data to htmlStr
  4. Display result and add that result to htmlStr
  5. If the player wants to play again, go to 1, otherwise, go to 6.
  6. Write htmlStr to a file (don't forget to close it!)
  7. Done!
By the way, this is what I was trying to get you to do beforehand - define your goals, then your functionality. The steps above are your functionality. You should try to do this for every problem you approach (programming and in general), this way, you know the approach and have a definite direction to move in.

The html still under learning. But still i dont know how to put html in the code. The code itself run on text editor .c . Hm i am so sorry if i made you have a hard time teaching me. And actually you help my work. Thank you my friend :)
 

tshuck

Joined Oct 18, 2012
3,534
The html still under learning. But still i dont know how to put html in the code. The code itself run on text editor .c . Hm i am so sorry if i made you have a hard time teaching me. And actually you help my work. Thank you my friend :)
Essentially, you are creating the HTML file within your code.

For instance try this in your program:
Code:
FILE* f=fopen("<path_to_file>.htm","w");
if (f !=NULL)
{
   char* text = "<html><body><p>Hi</p></body></html>"
   fprintf(f,"%s",text);
   fclose(f);
}
Once the program runs, open the file pointed to by "<path_to_file>.htm" using a browser.
 
Last edited:

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
Essentially, you are creating the HTML file within your code.

For instance try this in your program:
Code:
FILE* f=fopen("<path_to_file>.htm","w");
if (f !=NULL)
{
   char* text = "<html><body><p>Hi</p></body></html>"
   fprintf(f,"%s",text);
   fclose(f);
}
Once the program runs, open the file pointed to by "<path_to_file>.htm" using a browser.

wow its working!!!! but how to do this actually?? hm as i know my lecturer ask me to display a result of the paper rock scissor game in html. how about the number? i gonna try it later. i have a programming class after this :) hehe
 

MCU88

Joined Mar 12, 2015
358
I see but only 3 building blocks with this game in computer code.

a. Player input to select their move (paper, rock or scissors)
b. Computer to random generate number 0 to 2 (0 equates to paper, 1 for rock, 2 for scissors)
c. Logic to decide winner & print on screen. 3-Bit boolean table.

Nice to see you are using the switch statement. Keep using it...
 
Top