Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * JOGO DA VELHA - V.1.0
- * 2018 Novembro.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #if defined(WIN3) || defined(_WIN32)
- void limparTela()
- {
- system("@cls || clear");
- }
- void dimensiona( int x, int y )
- {
- char command[50];
- sprintf( command, "mode %d, %d", x, y );
- system(command);
- }
- #endif
- // Constantes e Macros
- #define PLAYER_1 'x'
- #define PLAYER_2 'o'
- #define FREE_FIELD '.'
- #define ENTER 0x0D
- #define ESCAPE 0x1B
- #define LEFT 0x61
- #define RIGHT 0x64
- #define UP 0x77
- #define BOTTOM 0x73
- // Prototipos das funcoes
- void imprimeTabuleiro();
- int verificaJogo();
- void jogar( int, int );
- // Variaveis globais
- char cursor = 0x00;
- short x, y;
- unsigned short jogadas = 0;
- unsigned short tabuleiro[3][3] = {
- { FREE_FIELD, FREE_FIELD, FREE_FIELD },
- { FREE_FIELD, FREE_FIELD, FREE_FIELD },
- { FREE_FIELD, FREE_FIELD, FREE_FIELD }
- };
- int main()
- {
- // Variaveis Locais
- unsigned short sair = 0;
- char entrada = 0x00;
- dimensiona( 50, 30 );
- x = 0, y = 0;
- while( !sair )
- {
- limparTela();
- cursor = ((jogadas%2) == 0)?'x':'o';
- printf(" Jogadas: %2d -- (%2d,%2d)\n\n", jogadas, x, y );
- imprimeTabuleiro();
- // Entrada de dados
- entrada = getch();
- fflush(stdin);
- // Tratamento dos dados
- switch( entrada )
- {
- case UP:
- x = ( (x-1) < 0 )?2:(x-1);
- break;
- case BOTTOM:
- x = ( (x+1) > 2 )?0:(x+1);
- break;
- case LEFT:
- y = ( (y-1) < 0 )?2:(y-1);
- break;
- case RIGHT:
- y = ( (y+1) > 2 )?0:(y+1);
- break;
- case ENTER:
- jogar( x, y );
- break;
- case ESCAPE:
- sair = 1;
- break;
- default:
- break;
- }
- // Verifica o estado do jogo
- if( verificaJogo() == 1 )
- {
- printf("\n\t[!] Vitoria do jogador 1 >-<\n");
- sair = 1;
- continue;
- }
- else if( verificaJogo() == 2 )
- {
- printf("\n\t[!] Vitoria do jogador 2 >-<\n");
- sair = 1;
- continue;
- }
- else if( verificaJogo() < 0 )
- {
- printf("\n\t[!] Empate ;-;\n");
- sair = 1;
- continue;
- }
- }
- system("pause>nul");
- return 0;
- }
- void imprimeTabuleiro()
- {
- int i, j;
- for( i=0; i<3; i++ )
- {
- printf("\t\t\t");
- for( j=0; j<3; j++ )
- printf("%c ", ((i == x) && (j == y))?cursor:tabuleiro[i][j] );
- printf("\n");
- }
- };
- int verificaJogo()
- {
- if(
- ( (tabuleiro[0][0] == PLAYER_1) && (tabuleiro[0][1] == PLAYER_1) && (tabuleiro[0][2] == PLAYER_1) ) || \
- ( (tabuleiro[1][0] == PLAYER_1) && (tabuleiro[1][1] == PLAYER_1) && (tabuleiro[1][2] == PLAYER_1) ) || \
- ( (tabuleiro[2][0] == PLAYER_1) && (tabuleiro[2][1] == PLAYER_1) && (tabuleiro[2][2] == PLAYER_1) ) || \
- ( (tabuleiro[0][0] == PLAYER_1) && (tabuleiro[1][0] == PLAYER_1) && (tabuleiro[2][0] == PLAYER_1) ) || \
- ( (tabuleiro[0][1] == PLAYER_1) && (tabuleiro[1][1] == PLAYER_1) && (tabuleiro[2][1] == PLAYER_1) ) || \
- ( (tabuleiro[0][2] == PLAYER_1) && (tabuleiro[1][2] == PLAYER_1) && (tabuleiro[2][2] == PLAYER_1) ) || \
- ( (tabuleiro[0][0] == PLAYER_1) && (tabuleiro[1][1] == PLAYER_1) && (tabuleiro[2][2] == PLAYER_1) ) || \
- ( (tabuleiro[0][2] == PLAYER_1) && (tabuleiro[1][1] == PLAYER_1) && (tabuleiro[2][0] == PLAYER_1) )
- )
- return 1;
- else if(
- ( (tabuleiro[0][0] == PLAYER_2) && (tabuleiro[0][1] == PLAYER_2) && (tabuleiro[0][2] == PLAYER_2) ) || \
- ( (tabuleiro[1][0] == PLAYER_2) && (tabuleiro[1][1] == PLAYER_2) && (tabuleiro[1][2] == PLAYER_2) ) || \
- ( (tabuleiro[2][0] == PLAYER_2) && (tabuleiro[2][1] == PLAYER_2) && (tabuleiro[2][2] == PLAYER_2) ) || \
- ( (tabuleiro[0][0] == PLAYER_2) && (tabuleiro[1][0] == PLAYER_2) && (tabuleiro[2][0] == PLAYER_2) ) || \
- ( (tabuleiro[0][1] == PLAYER_2) && (tabuleiro[1][1] == PLAYER_2) && (tabuleiro[2][1] == PLAYER_2) ) || \
- ( (tabuleiro[0][2] == PLAYER_2) && (tabuleiro[1][2] == PLAYER_2) && (tabuleiro[2][2] == PLAYER_2) ) || \
- ( (tabuleiro[0][0] == PLAYER_2) && (tabuleiro[1][1] == PLAYER_2) && (tabuleiro[2][2] == PLAYER_2) ) || \
- ( (tabuleiro[0][2] == PLAYER_2) && (tabuleiro[1][1] == PLAYER_2) && (tabuleiro[2][0] == PLAYER_2) )
- )
- return 2;
- else if( jogadas == 9 )
- return -1;
- else
- return 0;
- };
- void jogar( int x, int y )
- {
- if( tabuleiro[x][y] == FREE_FIELD )
- {
- tabuleiro[x][y] = ((jogadas%2) == 0)?PLAYER_1:PLAYER_2;
- jogadas++;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement