Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- char tabuleiro[10][10];
- void resetar_tabuleiro() {
- int i;
- int j;
- for (i = 0; i < 10; ++i)
- for (j = 0; j < 10; ++j)
- tabuleiro[i][j] = ' ';
- }
- void imprimir_tabuleiro() {
- int i;
- int j;
- for (i = 0; i < 10; ++i) {
- printf("+---+---+---+---+---+---+---+---+---+---+\n");
- for (j = 0; j < 10; ++j) {
- printf("| %c ", tabuleiro[i][j]);
- }
- printf("|\n");
- }
- printf("+---+---+---+---+---+---+---+---+---+---+\n");
- }
- int coluna(int i, int j) {
- if (tabuleiro[i][j] == tabuleiro[i+1][j] && tabuleiro[i][j] == tabuleiro[i+2][j] && tabuleiro[i][j] == tabuleiro[i+3][j]) {
- if (tabuleiro[i][j] == '#')
- return 1;
- else if (tabuleiro[i][j] == '@')
- return 2;
- else
- return 0;
- }
- }
- int linha(int i, int j) {
- if (tabuleiro[i][j] == tabuleiro[i][j+1] && tabuleiro[i][j] == tabuleiro[i][j+2] && tabuleiro[i][j] == tabuleiro[i][j+3]) {
- if (tabuleiro[i][j] == '#')
- return 1;
- else if (tabuleiro[i][j] == '@')
- return 2;
- else
- return 0;
- }
- }
- int diagonal_principal(int i, int j) {
- if (tabuleiro[i][j] == tabuleiro[i+1][j+1] && tabuleiro[i][j] == tabuleiro[i+2][j+2] && tabuleiro[i][j] == tabuleiro[i+3][j+3]) {
- if (tabuleiro[i][j] == '#')
- return 1;
- else if (tabuleiro[i][j] == '@')
- return 2;
- else
- return 0;
- }
- }
- int diagonal_secundaria(int i, int j) {
- if (tabuleiro[i][j] == tabuleiro[i+1][j-1] && tabuleiro[i][j] == tabuleiro[i+2][j-2] && tabuleiro[i][j] == tabuleiro[i+3][j-3]) {
- if (tabuleiro[i][j] == '#')
- return 1;
- else if (tabuleiro[i][j] == '@')
- return 2;
- else
- return 0;
- }
- }
- int verificar_vencedor() {
- int i;
- int j;
- for (i = 0; i < 10; ++i) {
- for (j = 0; j < 10; ++j) {
- if (i < 7) {
- if (coluna(i, j) == 1)
- return 1;
- else if (coluna(i, j) == 2)
- return 2;
- }
- if (j < 7) {
- if (linha(i, j) == 1)
- return 1;
- else if (linha(i, j) == 2)
- return 2;
- }
- if (j < 7 && i < 7) {
- if (diagonal_principal(i, j) == 1)
- return 1;
- else if (diagonal_principal(i, j) == 2)
- return 2;
- }
- if (j > 2 && i < 7) {
- if (diagonal_secundaria(i, j) == 1)
- return 1;
- else if (diagonal_secundaria(i, j) == 2)
- return 2;
- }
- }
- }
- return 0;
- }
- int jogar(char jogador) {
- int tmp;
- int i;
- printf("coluna: ");
- scanf("%d", &tmp);
- for (i = 0; i < 10; ++i) {
- if (tabuleiro[i][tmp] != ' ')
- break;
- }
- if (tabuleiro[0][tmp] != ' ' || tmp < 0 || tmp > 9) {
- printf("nao eh possivel jogar nessa posicao\n");
- return 0;
- }
- else {
- for (i = 0; i < 10; ++i) {
- if (tabuleiro[i][tmp] != ' ')
- break;
- }
- tabuleiro[i - 1][tmp] = jogador;
- return 1;
- }
- }
- int main() {
- resetar_tabuleiro();
- int vencedor = 0;
- char jogador = '#';
- int jogada_ok = 0;
- char * nome1;
- char * nome2;
- int idade1;
- int idade2;
- nome1 = malloc(10 * sizeof(char));
- printf("Digite o nome do Primeiro jogador:\n");
- scanf("%s", nome1);
- printf("Digite sua idade:\n");
- scanf("%i", &idade1);
- nome2 = malloc(10 * sizeof(char));
- printf("Digite o nome do Segundo jogador:\n");
- scanf("%s", nome2);
- printf("Digite sua idade:\n");
- scanf("%i", &idade2);
- printf("\n");
- printf("Hora de começar o jogo!\n");
- printf("\n");
- printf("O jogador %s ficara com a peça # e o jogador %s ficara com a peça @ \n", nome1, nome2);
- printf("\n");
- while (vencedor == 0) {
- imprimir_tabuleiro();
- printf("Eh a vez do jogador que esta com a peça %c\n", jogador);
- jogada_ok = 0;
- while (jogada_ok == 0) {
- jogada_ok = jogar(jogador);
- }
- vencedor = verificar_vencedor();
- if (jogador == '#')
- jogador = '@';
- else
- jogador = '#';
- }
- imprimir_tabuleiro();
- printf("jogador %d venceu!\n", vencedor);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement