Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int jogada = 1;
- int inGame = 1;
- char A1, A2, A3, B1, B2, B3, C1, C2, C3;
- void limparTela(){
- int i;
- for(i = 0; i < 100; i++){
- printf("\n");
- }
- }
- int menu(){
- int opcao;
- printf("\n\n\n\n\n\n1 - Jogar\n");
- printf("0 - Sair\n");
- printf("\nEscolha uma opcao: ");
- scanf("%d", &opcao);
- switch(opcao){
- case 1:
- inGame = 1;
- break;
- case 0:
- inGame = 0;
- break;
- default:
- limparTela();
- printf("Opcao inválida. \n");
- }
- return opcao;
- }
- void zerarValores(){
- A1 = ' ';
- A2 = ' ';
- A3 = ' ';
- B1 = ' ';
- B2 = ' ';
- B3 = ' ';
- C1 = ' ';
- C2 = ' ';
- C3 = ' ';
- }
- void zerarPartida(){
- zerarValores();
- jogada = 1;
- }
- int inserirValor(int n, int c){
- char playerSimbolo;
- if((n < 1 || n > 3) || (c < 1 || c > 3) ){
- printf("Valor inserido invalido. Número fora do limite\n");
- return 0;
- }else {
- int linha = n;
- int coluna = c;
- if (jogada % 2 == 0) {
- playerSimbolo = 'X';
- } else {
- playerSimbolo = 'O';
- }
- printf("\n\nLinha: %d, Coluna: %c\n\n", linha, coluna);
- if (linha == 1 && coluna == 1 && A1 == ' ') {
- A1 = playerSimbolo;
- return 1;
- } else if (linha == 1 && coluna == 2 && B1 == ' ') {
- B1 = playerSimbolo;
- return 1;
- } else if (linha == 1 && coluna == 3 && C1 == ' ') {
- C1 = playerSimbolo;
- return 1;
- } else if (linha == 2 && coluna == 1 && A2 == ' ') {
- A2 = playerSimbolo;
- return 1;
- } else if (linha == 2 && coluna == 2 && B2 == ' ') {
- B2 = playerSimbolo;
- return 1;
- } else if (linha == 2 && coluna == 3 && C2 == ' ') {
- C2 = playerSimbolo;
- return 1;
- } else if (linha == 3 && coluna == 1 && A3 == ' ') {
- A3 = playerSimbolo;
- return 1;
- } else if (linha == 3 && coluna == 2 && B3 == ' ') {
- B3 = playerSimbolo;
- return 1;
- } else if (linha == 3 && coluna == 3 && C3 == ' ') {
- C3 = playerSimbolo;
- return 1;
- } else {
- printf("Valor inserido invalido. \n");
- return 0;
- }
- }
- }
- void verificarVitoria(){
- //Verificar se jogador 1 venceu
- if( (A1 == 'O' && B1 == 'O' && C1 == 'O') ||
- (A2 == 'O' && B2 == 'O' && C2 == 'O') ||
- (A3 == 'O' && B3 == 'O' && C3 == 'O') ||
- (A1 == 'O' && A2 == 'O' && A3 == 'O') ||
- (B1 == 'O' && B2 == 'O' && B3 == 'O') ||
- (C1 == 'O' && C2 == 'O' && C3 == 'O') ||
- (A1 == 'O' && B2 == 'O' && C3 == 'O') ||
- (A3 == 'O' && B2 == 'O' && C1 == 'O')) {
- printf("\n\nJogador 1 venceu!\n");
- zerarPartida();
- }
- //Verificar se jogador 2 venceu
- if( (A1 == 'X' && B1 == 'X' && C1 == 'X') ||
- (A2 == 'X' && B2 == 'X' && C2 == 'X') ||
- (A3 == 'X' && B3 == 'X' && C3 == 'X') ||
- (A1 == 'X' && A2 == 'X' && A3 == 'X') ||
- (B1 == 'X' && B2 == 'X' && B3 == 'X') ||
- (C1 == 'X' && C2 == 'X' && C3 == 'X') ||
- (A1 == 'X' && B2 == 'X' && C3 == 'X') ||
- (A3 == 'X' && B2 == 'X' && C1 == 'X')) {
- printf("\n \n Jogador 2 venceu! Jogador 1 é horrível\n");
- zerarPartida();
- }
- if(A1 != ' ' && A2 != ' ' && A3 != ' ' && B1 != ' ' && B2 != ' ' && B3 != ' ' &&
- C1 != ' ' && C2 != ' ' && C3 != ' '){
- printf("\n \n Empatou, seus lixos!\n");
- zerarPartida();
- }
- }
- void jogar(){
- int linha, coluna, jogador;
- int resultado = 0;
- if(jogada % 2 == 0){
- jogador = 2;
- }else{
- jogador = 1;
- }
- printf("Vez do jogador: %d\n\n\n\n\n", jogador);
- while(resultado != 1){
- printf("Insira a linha e a coluna: \n");
- scanf("%d %d", &linha, &coluna);
- resultado = inserirValor(linha, coluna);
- }
- jogada = jogada + 1;
- }
- void desenhar(){
- printf(" | | \n");
- printf(" %c | %c | %c \n", A1, B1, C1);
- printf("---|---|---\n");
- printf(" %c | %c | %c \n", A2, B2, C2);
- printf("---|---|---\n");
- printf(" %c | %c | %c \n", A3, B3, C3);
- printf(" | | \n");
- }
- int main(){
- zerarValores();
- limparTela();
- while (inGame != 0){
- if(jogada == 1){
- if(menu() == 1){
- limparTela();
- desenhar();
- jogar();
- limparTela();
- desenhar();
- }else{
- break;
- }
- }else{
- jogar();
- limparTela();
- desenhar();
- verificarVitoria();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement