KeplerBR

[Learning] Jogo da Velha

Dec 27th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <locale.h>
  3. #include <stdlib.h>
  4.  
  5. class classPlace {
  6.     public:
  7.     classPlace();
  8.     char value;
  9.     void setValue(char newValue) {
  10.         value = newValue;
  11.     }
  12. };
  13.  
  14. void turn();
  15. void drawScene();
  16. void passTurn();
  17. bool checkVictory();
  18. void menu();
  19.  
  20. int numberTurn = 0;
  21. int playerTurn = 'X';
  22. classPlace place[9];
  23. classPlace::classPlace() {
  24.     for (int i = 0; i < 9; i++) {
  25.         place[i].setValue(' ');
  26.     }
  27. }
  28.  
  29. void drawScene() {
  30.     for (int i = 0; i < 9; i += 3) {
  31.         printf("%c | %c | %c\n", place[i].value, place[i + 1].value, place[i + 2].value);
  32.     }
  33. }
  34.  
  35. void passTurn() {
  36.     if (playerTurn == 'X') {
  37.         playerTurn = 'O';
  38.     } else {
  39.         playerTurn = 'X';
  40.     }
  41. }
  42.  
  43. bool checkVictory() {
  44.     if (numberTurn < 5) {
  45.         return 0;
  46.     }
  47.  
  48.     // Horizontais
  49.     for (int i = 0; i < 9; i += 3) {
  50.         if (place[i].value == ' ') {
  51.             continue;
  52.         }
  53.  
  54.         if (place[i].value == place[i + 1].value && place[i + 1].value == place[i + 2].value) {
  55.             printf("O jogador %c venceu!\n\n", place[i].value);
  56.             return 1;
  57.         }
  58.     }
  59.  
  60.     // Verticais
  61.     for (int i = 0; i < 3; i++) {
  62.         if (place[i].value == ' ') {
  63.             continue;
  64.         }
  65.  
  66.         if (place[i].value == place[i + 3].value && place[i + 3].value == place[i + 6].value) {
  67.             printf("O jogador %c venceu!\n\n", place[i].value);
  68.             return 1;
  69.         }
  70.     }
  71.  
  72.     // Diagonais
  73.     if (place[0].value != ' ' &&
  74.         place[0].value == place[4].value && place[4].value == place[8].value) {
  75.         printf("O jogador %c venceu!\n\n", place[0].value);
  76.         return 1;
  77.     }
  78.  
  79.     if (place[2].value != ' ' &&
  80.         place[2].value == place[4].value && place[4].value == place[6].value) {
  81.         printf("O jogador %c venceu!\n\n", place[2].value);
  82.         return 1;
  83.     }
  84.  
  85.     return 0;
  86. }
  87.  
  88. void menu() {
  89.     printf("Escolha alguma opção:\n"
  90.            "[1] Nova partida\n"
  91.            "[0] Sair\n\n");
  92.  
  93.     int input;
  94.     scanf("%d", &input);
  95.  
  96.     if (input == 1) {
  97.         for (int i = 0; i < 9; i++) {
  98.             place[i].setValue(' ');
  99.         }
  100.         numberTurn = 0;
  101.         turn();
  102.     } else {
  103.         exit(1);
  104.     }
  105. }
  106.  
  107. void turn() {
  108.     drawScene();
  109.     numberTurn++;
  110.  
  111.     printf("É a vez do jogador %c\nInsira onde deseja escrever\n\n", playerTurn);
  112.  
  113.     int inputPlace;
  114.     scanf("%d", &inputPlace);
  115.  
  116.     if (place[inputPlace].value != ' ') {
  117.         printf("Esse local já esta ocupado! Tente outro!\n\n");
  118.         turn();
  119.         return;
  120.     } else {
  121.         place[inputPlace].setValue(playerTurn);
  122.     }
  123.  
  124.     if (checkVictory() == 1) {
  125.         drawScene();
  126.         menu();
  127.     } else {
  128.         passTurn();
  129.         turn();
  130.     }
  131. }
  132.  
  133. int main(void) {
  134.     setlocale(LC_ALL, "Portuguese");
  135.     turn();
  136. }
Add Comment
Please, Sign In to add comment