Puissance 4 en mode texte [dev-c++]

Description

Voici un petit jeu de puissance4 pour deux joueur, il marche plutot bien, il est juste pas très beau. Si vous trouvez une maniere de l'optimiser (comme ces affreux tests pour voir si quatre pions sont alignes, il y a surement moyen autrement mais j'ai pas trouve) n'hésitez pas à le dire.

Source / Exemple :


#include <stdio.h>
#include <conio.c> //OU CONIO.H

#define    BLEU      11
#define    ROUGE     12
#define    JAUNE     14
#define    ENTER     13
#define    GAUCHE    75
#define    DROITE    77
#define    ESCAPE    27

void recta(int X,int Y);

void main()
{
  int   sortie=0;//POUR QUITTER
  int   increment;//POUR LES BOUCLES
  int   increment2;//POUR LES BOUCLES
  int   curseur=2;//POUR LE CURSEUR
  int   tour=12;//POUR LA COULEUR ET LE TOUR DU JOUEUR
  int   cara='\x01';//POUR LES PIONS
  int   cara2='\x01';//POUR LE TEST DE PIONS ALIGNES
  int   touche;//POUR LES ENTREES AU CLAVIER DE L'UTILISATEUR
  char  pions[10][6];
  /*INITIALISE "pion"*/
  for (increment=0 ; increment<=5 ; increment++)
  {
    for (increment2=0 ; increment2<=9 ; increment2++) pions[increment2][increment]=' ';
  }
  /*MENU DU DEBUT*/
  gotoxy(1,1);
  textcolor(BLEU);
  printf("<-----PUISSANCE 4----->");
  gotoxy(1,2);
  printf("Faites bouger le curseur avec les fleches de direction et");
  gotoxy(1,3);
  printf("faites ENTER pour mettre le pion. Le premier qui reussit a");
  gotoxy(1,4);
  printf("aligner 4 pions a gagner.");
  gotoxy(1,5);
  printf("Appuyez sur n'importe quelle touche pour continuer.");
  getch();
  clrscr(); //EFFACE L'ECRAN
  recta(1,2);//DESSINE LE TABLEAU
  while (sortie==0) //BOUCLE PRINCIPALE
  {
    textcolor(tour);//COULEUR DU JOUEUR DONT C'EST LE TOUR
    /*DESSINE LES PIONS*/
    for (increment=0 ; increment<=5 ; increment++)
    {
      for (increment2=0 ; increment2<=9 ; increment2++)
      {
        gotoxy(increment2+2,increment+2);
        if (pions[increment2][increment]=='\x01') textcolor(ROUGE);
        else textcolor(JAUNE);
        printf("%c",pions[increment2][increment]);
      }
    }
    /*POUR VERIFIER SI 4 PIONS DE LA MEME COULEUR SONT ALIGNES*/
    for (increment=0 ; increment<=5 ; increment++)
    {
      for (increment2=0 ; increment2<=9 ; increment2++)
      {
        /*PIONS HORIZONTALS*/
        if ((pions[increment2][increment]==cara2)&&(pions[increment2+1][increment]==cara2)&&
           (pions[increment2+2][increment]==cara2)&&(pions[increment2+3][increment]==cara2))
        {
          textcolor(BLEU);
          gotoxy(increment2+2,increment+2);
          printf("B");
          gotoxy(increment2+3,increment+2);
          printf("I");
          gotoxy(increment2+4,increment+2);
          printf("E");
          gotoxy(increment2+5,increment+2);
          printf("N");
          sortie=1;
        }
        /*PIONS VERTICALS*/
        if ((pions[increment2][increment]==cara2)&&(pions[increment2][increment+1]==cara2)&&
           (pions[increment2][increment+2]==cara2)&&(pions[increment2][increment+3]==cara2))
        {
          textcolor(BLEU);
          gotoxy(increment2+2,increment+2);
          printf("B");
          gotoxy(increment2+2,increment+3);
          printf("I");
          gotoxy(increment2+2,increment+4);
          printf("E");
          gotoxy(increment2+2,increment+5);
          printf("N");
          sortie=1;
        }
        /*PIONS DIAGONALE DROITE*/
        if ((pions[increment2][increment]==cara2)&&(pions[increment2+1][increment+1]==cara2)&&
           (pions[increment2+2][increment+2]==cara2)&&(pions[increment2+3][increment+3]==cara2))
        {
          textcolor(BLEU);
          gotoxy(increment2+2,increment+2);
          printf("B");
          gotoxy(increment2+3,increment+3);
          printf("I");
          gotoxy(increment2+4,increment+4);
          printf("E");
          gotoxy(increment2+5,increment+5);
          printf("N");
          sortie=1;
        }
        /*PIONS DIAGONALE GAUCHE*/
        if ((pions[increment2][increment]==cara2)&&(pions[increment2-1][increment+1]==cara2)&&
           (pions[increment2-2][increment+2]==cara2)&&(pions[increment2-3][increment+3]==cara2))
        {
          textcolor(BLEU);
          gotoxy(increment2+2,increment+2);
          printf("N");
          gotoxy(increment2+1,increment+3);
          printf("E");
          gotoxy(increment2,increment+4);
          printf("I");
          gotoxy(increment2-1,increment+5);
          printf("B");
          sortie=1;
        }
        if (sortie==1)//SI 4 PIONS ALIGNES
        {
          gotoxy(15,4);
          if (cara2=='\x01') //SI ROUGE A GAGNE
          {
            textcolor(ROUGE);
            printf("BRAVO ! VOUS AVEZ REUSSI A ALIGNER 4 PIONS !");
            gotoxy(15,5);
            printf("LE JOUEUR %cOUGE GAGNE !",82);
            /*%cOUGE POUR NE PAS QUE LE PREPROCESSEUR REMPLACE ROUGE PAR 12*/
          }
          else //SI JAUNE A GAGNE
          {
            textcolor(JAUNE);
            printf("BRAVO ! VOUS AVEZ REUSSI A ALIGNER 4 PIONS !");
            gotoxy(15,5);
            printf("LE JOUEUR %cAUNE GAGNE !",74);
            /*%cAUNE POUR NE PAS QUE LE PREPROCESSEUR REMPLACE JAUNE PAR 14*/
          }
        }
        /*POUR REFAIRE TOUS LES TESTS AVEC LES PIONS JAUNES*/
        else if ((cara2=='\x01')&&(increment==5)&&(increment2==9))
        {
          increment2=-1;
          increment=0;
          cara2='\x02';
        }
        /*REINITIALISE "cara2" QUAND IL A FAIT TOUS LES TESTS*/
        else if ((cara2=='\x02')&&(increment==5)&&(increment2==9)) cara2='\x01';
      }
    }
    /*AFFICHE C'EST LE TOUR DE QUEL JOUEUR*/
    gotoxy(1,9);
    textcolor(BLEU);
    printf("Tour joueur ");
    textcolor(tour);
    if (tour==14) printf("jaune");
    else printf("rouge");
    /*AFFICHE LE CURSEUR*/
    gotoxy(curseur,1);
    printf(" \x1F ");
    /*ATTEND QUE L'UTILISATEUR APPUIE SUR UNE TOUCHE*/
    if (sortie==0) touche=getch();
    /*SEULEMENT SI SORTIE=0 POUR NE PAS QUE L'UTILISATEUR DOIVENT
      APPUYER SUR UNE TOUCHE POUR CONTINUER QUAND IL A GAGNE*/
    switch(touche)
    {
      case GAUCHE:  /*BOUGE LE CURSEUR A GAUCHE*/
                    curseur--;
                    if (curseur==0) curseur=1;
                    break;
      case DROITE:  /*BOUGE LE CURSEUR A DROITE*/
                    curseur++;
                    if (curseur==11) curseur=10;
                    break;
      case ENTER:   /*POSITIONNE LE PION*/
                    for(increment=5 ; increment>=0 ; increment--)
                    {
                      if (pions[curseur-1][increment]==' ')
                      {
                        pions[curseur-1][increment]=cara;
                        if (tour==12)
                        {
                          tour=14;
                          cara='\x02';//PION JAUNE
                        }
                        else
                        {
                          tour=12;
                          cara='\x01';//PION ROUGE
                        }
                        break;
                      }
                    }
                    break;
      case ESCAPE:  /*SI L'UTILISATEUR VEUT QUITTER*/
                    textcolor(BLEU);
                    gotoxy(15,4);
                    printf("Etes vous sur de vouloir quitter ? (y/n)");
                    touche=getch();
                    if ((touche=='y')||(touche='Y')) sortie=2;//SI OUI
                    else /*SI AUTRE TOUCHE CONTINUE*/
                    {
                      gotoxy(15,4);
                      printf("                                        ");
                    }
                    break;
    }
    if (sortie==1)//SI L'UTILISATEUR A GAGNE
    {
      gotoxy(15,7);
      textcolor(BLEU);
      printf("Voulez vous rejouer ? (y/n)");
      touche=getch();
      if ((touche=='y')||(touche=='Y'))//SI OUI
      {
        sortie=0;
        clrscr();
        recta(1,2);
        for (increment=0 ; increment<=5 ; increment++)
        {
          for (increment2=0 ; increment2<=9 ; increment2++) pions[increment2][increment]=' ';
        }
        tour=12;
      }
      /*SI AUTRE TOUCHE QUITTE*/
    }
  }//REVIENT AU DEBUT SI "sortie" = 0
}

/*PERMET DE TRACER LE TABLEAU*/
void recta(int X,int Y)
{
  int increment;
  int increment2;
  gotoxy(X,Y);
  textcolor(BLEU);
  printf("\xB3");
  for (increment=1 ; increment <= 10 ; increment++) printf(" ");
  printf("\xB3\n");
  for (increment=1 ; increment <= 5 ; increment++)
  {
    printf("\xB3");
    for (increment2=1 ; increment2 <= 10 ; increment2++) printf("\x20");
    printf("\xB3\n");
  }
  printf("\xC0");
  for (increment=1 ; increment <= 10 ; increment++) printf("\xC4");
  printf("\xD9");
}

Conclusion :


Si quand vous compilez tout marche bien mais il n'y a aucune couleur, remplacez tous les printf() du programme par des cprintf().

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.