Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <locale.h>
- #include <stdlib.h>
- class classPlace {
- public:
- classPlace();
- char value;
- void setValue(char newValue) {
- value = newValue;
- }
- };
- void turn();
- void drawScene();
- void passTurn();
- bool checkVictory();
- void menu();
- int numberTurn = 0;
- int playerTurn = 'X';
- classPlace place[9];
- classPlace::classPlace() {
- for (int i = 0; i < 9; i++) {
- place[i].setValue(' ');
- }
- }
- void drawScene() {
- for (int i = 0; i < 9; i += 3) {
- printf("%c | %c | %c\n", place[i].value, place[i + 1].value, place[i + 2].value);
- }
- }
- void passTurn() {
- if (playerTurn == 'X') {
- playerTurn = 'O';
- } else {
- playerTurn = 'X';
- }
- }
- bool checkVictory() {
- if (numberTurn < 5) {
- return 0;
- }
- // Horizontais
- for (int i = 0; i < 9; i += 3) {
- if (place[i].value == ' ') {
- continue;
- }
- if (place[i].value == place[i + 1].value && place[i + 1].value == place[i + 2].value) {
- printf("O jogador %c venceu!\n\n", place[i].value);
- return 1;
- }
- }
- // Verticais
- for (int i = 0; i < 3; i++) {
- if (place[i].value == ' ') {
- continue;
- }
- if (place[i].value == place[i + 3].value && place[i + 3].value == place[i + 6].value) {
- printf("O jogador %c venceu!\n\n", place[i].value);
- return 1;
- }
- }
- // Diagonais
- if (place[0].value != ' ' &&
- place[0].value == place[4].value && place[4].value == place[8].value) {
- printf("O jogador %c venceu!\n\n", place[0].value);
- return 1;
- }
- if (place[2].value != ' ' &&
- place[2].value == place[4].value && place[4].value == place[6].value) {
- printf("O jogador %c venceu!\n\n", place[2].value);
- return 1;
- }
- return 0;
- }
- void menu() {
- printf("Escolha alguma opção:\n"
- "[1] Nova partida\n"
- "[0] Sair\n\n");
- int input;
- scanf("%d", &input);
- if (input == 1) {
- for (int i = 0; i < 9; i++) {
- place[i].setValue(' ');
- }
- numberTurn = 0;
- turn();
- } else {
- exit(1);
- }
- }
- void turn() {
- drawScene();
- numberTurn++;
- printf("É a vez do jogador %c\nInsira onde deseja escrever\n\n", playerTurn);
- int inputPlace;
- scanf("%d", &inputPlace);
- if (place[inputPlace].value != ' ') {
- printf("Esse local já esta ocupado! Tente outro!\n\n");
- turn();
- return;
- } else {
- place[inputPlace].setValue(playerTurn);
- }
- if (checkVictory() == 1) {
- drawScene();
- menu();
- } else {
- passTurn();
- turn();
- }
- }
- int main(void) {
- setlocale(LC_ALL, "Portuguese");
- turn();
- }
Add Comment
Please, Sign In to add comment