Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1.  
  2. # include < stdlib.h >
  3. # include < stdio.h >
  4. # include < time.h >
  5.  
  6. # define PEDRA 1
  7. # define PAPEL 2
  8. # define TESOURA 3
  9.  
  10. int pontosJogador;
  11. int pontosComputador;
  12.  
  13. void jogo ();
  14. void imprimeItem ( int item);
  15. int verificação ( int p1, int p2);
  16.  
  17. int main () {
  18.  
  19. srand ( tempo ( NULL )); // usado para gerar números aleatórios diferentes
  20.  
  21. printf ( " ----------------------------------------------- -------------------------------- \ n " );
  22. printf ( " PEDRA - PAPEL - TESOURA \ n " );
  23. printf ( " ----------------------------------------------- -------------------------------- \ n " );
  24.  
  25. int i;
  26. para (i = 0 ; i < 3 ; i ++) {
  27. jogo ();
  28. printf ( " ----------------------------------------------- -------------------------------- \ n " );
  29. }
  30.  
  31. printf ( " Total de pontos \ n " );
  32. printf ( " Voce: % d \ n " , pontosJogador);
  33. printf ( " Computador: % d \ n " , pontosComputador);
  34. printf ( " \ n Obrigado por jogar! " );
  35.  
  36. sistema ( " pausa> nul " );
  37. retornar 0 ;
  38. }
  39.  
  40.  
  41. void jogo () {
  42.  
  43. int itemJogador;
  44. int itemComputador;
  45.  
  46. printf ( " \ n Escolha 1.Pedra 2.Papel 3.Tesoura \ n Item: " );
  47. scanf ( " % d " , & itemJogador); // jogador faz sua escolha
  48.  
  49. itemComputador = rand ()% 3 + 1 ; // computador faz sua escolha
  50.  
  51. // mostra quem escolheu oque
  52. printf ( " \ n Voce -> " );
  53. imprimeItem (itemJogador);
  54. printf ( " x " );
  55. imprimeItem (itemComputador);
  56. printf ( " <- Computador. \ n " );
  57.  
  58. // verifica quem ganhou
  59. int ganhador = verifica (itemJogador, itemComputador);
  60.  
  61. // mostra o ganhador
  62. printf ( " \ n " );
  63. if (ganhador == 1 ) {
  64. printf ( " VOCE GANHOU! \ n " );
  65. pontosJogador ++;
  66. }
  67. senão se (ganhador == 2 ) {
  68. printf ( " COMPUTADOR GANHOU! \ n " );
  69. pontosComputador ++;
  70. }
  71. mais {
  72. printf ( " EMPATOU! \ n " );
  73. }
  74. printf ( " \ n " );
  75. }
  76.  
  77.  
  78. void imprimeItem ( item int ) {
  79. if (item == PEDRA) {
  80. printf ( " PEDRA " );
  81. }
  82. caso contrário, se (item == PAPEL) {
  83. printf ( " PAPEL " );
  84. }
  85. mais {
  86. printf ( " TESOURA " );
  87. }
  88. }
  89.  
  90.  
  91. / *
  92. Função que verifica qual jogador ganhou e retorna 1 ou 2
  93. retorna 0 ao empatar
  94. * /
  95. int verificação ( int p1, int p2) {
  96.  
  97. int ganhador;
  98. if (p1 == p2) { // empate
  99. ganhador = 0 ;
  100. }
  101.  
  102. if (p1 == PEDRA && p2 == TESOURA) {ganhador = 1 ; }
  103. if (p1 == PEDRA && p2 == PAPEL) {ganhador = 2 ; }
  104.  
  105. if (p1 == PAPEL && p2 == PEDRA) {ganhador = 1 ; }
  106. if (p1 == PAPEL && p2 == TESOURA) {ganhador = 2 ; }
  107.  
  108. if (p1 == TESOURA && p2 == PAPEL) {ganhador = 1 ; }
  109. if (p1 == TESOURA && p2 == PEDRA) {ganhador = 2 ; }
  110.  
  111. retorno ganhador;
  112. }
  113. #include <stdio.h>
  114. #include <stdlib.h>
  115. #include <time.h>
  116. int pontos_jogador=0, pontos_cpu=0;
  117. main()
  118. {
  119. jokenpo();
  120. }
  121. int jokenpo()
  122. {
  123. int jogador, cpu;
  124. printf ("---JokenPo---\n");
  125. printf ("%d Jogador X CPU %d\n", pontos_jogador, pontos_cpu);
  126. printf ("-------------\n");
  127. printf ("0. Pedra\n");
  128. printf ("1. Papel\n");
  129. printf ("2. Tesoura\n");
  130. printf ("3. Sair\n");
  131. scanf ("%d", &jogador);
  132. if (jogador < 0 || jogador >=3)
  133. {
  134. exit(0);
  135. }
  136. srand(time(NULL));
  137. cpu = rand() % 3; //gera um numero aleatorio
  138. switch(cpu)
  139. {
  140. case 0: printf ("CPU -> Pedra\n"); break;
  141. case 1: printf ("CPU -> Papel\n"); break;
  142. case 2: printf ("CPU -> Tesoura\n"); break;
  143. }
  144. if ((jogador == 0 && cpu==2) || (jogador == 1 && cpu == 0) || (jogador == 2 && cpu == 1)) //verifica se o jogador venceu
  145. {
  146. printf("Voce venceu!\n");
  147. pontos_jogador++;
  148. jokenpo();
  149. }
  150. if (jogador == cpu) //verifica se houve empate
  151. {
  152. printf ("Empate!\n");
  153. jokenpo();
  154. }
  155. else
  156. {
  157. printf ("CPU venceu!\n");
  158. pontos_cpu++;
  159. jokenpo();
  160.  
  161. }
  162. return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement