Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "function.h"
- void jouerOrdinateur (char *possibilite, int choixUtilisateur, int nombreDeFois, int *fin);
- int modifierTTT (int choixUtilisateur, char *possibilite, int *intPossibilite);
- void generateTTT (char *possibilite);
- int main()
- {
- int intPossibilite[9] = {1,2,3,4,5,6,7,8,9};
- char possibilite[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
- int nombreDeFois = 0;
- int fin = 0;
- int choixUtilisateur = 0;
- int gagnant = 0, i = 0, j=0,k=0, l=0;
- int partieGagnant[8][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
- int signeAregarder[2] = {'.', '+'};
- int totalPoint = 0;
- int recommencer = 0;
- //Affichage de la version du logiciel
- printf("\n\t\t----------------------------------\n");
- printf("\t\t\t Tic-Tac-To 1.0\n");
- printf("\t\t----------------------------------\n\n");
- //CrÈation du tableau qui va afficher les X et O
- generateTTT(possibilite);
- do{
- printf("\n\n");
- do{
- printf("Veuillez entrer votre choix : ");
- scanf("%d", &choixUtilisateur);
- }while(modifierTTT(choixUtilisateur, possibilite, intPossibilite) != 1);
- nombreDeFois++;
- jouerOrdinateur(possibilite, choixUtilisateur, nombreDeFois, &fin);
- for(i=0;i<2;i++){
- for(j=0;j<8;j++){
- for(k=0;k<3;k++){
- if(possibilite[partieGagnant[j][k]] == signeAregarder[i]){
- totalPoint++;
- }
- }
- if(totalPoint == 3 && signeAregarder[i] == '.'){
- gagnant = 1;
- break;
- }else if(totalPoint == 3 && signeAregarder[i] == '+'){
- gagnant = 2;
- break;
- }else{
- totalPoint=0;
- }
- }
- if(gagnant > 0)
- break;
- }
- generateTTT(possibilite);
- }while(gagnant < 1 && fin != 1);
- if(gagnant == 1){
- printf("\n\t\t----------------------------------\n");
- printf("\n\t\t\t Vous avez perdu ! \n\n");
- printf("\t\t----------------------------------\n\n");
- }else if(gagnant == 2){
- printf("\n\t\t----------------------------------\n");
- printf("\n\t\t Bravo, vous avez gagne ! \n\n");
- printf("\t\t----------------------------------\n\n");
- }else{
- printf("\n\t\t----------------------------------\n");
- printf("\n\t\t\t Partie neutre ! \n\n");
- printf("\t\t----------------------------------\n\n");
- }
- printf("Appuyez sur une touche pour quitter le programme...");
- scanf("%d", &recommencer);
- return 0;
- }
- int modifierTTT (int choixUtilisateur, char *possibilite, int *intPossibilite){
- int i = 0;
- if(choixUtilisateur <= 9 && choixUtilisateur >= 1){
- for(i = 0; i < 9; i++){
- if(choixUtilisateur == intPossibilite[i]){
- if(possibilite[i] != '+' && possibilite[i] != '.'){
- possibilite[i] = '+';
- return 1;
- break;
- }else{
- printf("-- Deja prit !\n");
- }
- }
- }
- }else{
- printf("-- Ce choix n'existe pas !\n");
- return 0;
- }
- }
- void jouerOrdinateur (char *possibilite, int choixUtilisateur, int nombreDeFois, int *fin){
- int positionDepart[3][4] = {{1,3,7,9},{2,4,6,8},{5,0,0,0}};
- int i = 0, j = 0, k = 0;
- if(nombreDeFois == 1){
- for(i=0;i<3;i++){
- for(j=0;j<4;j++){
- if(choixUtilisateur == positionDepart[i][j]){
- if(positionDepart[i][j] <= 7){
- possibilite[positionDepart[i][j]] = '.';
- break;
- }else{
- possibilite[positionDepart[i][j]-2] = '.';
- break;
- }
- }
- }
- }
- }else{
- //Nous testons les trois lignes horizontal de gauche ‡ droite
- int check1=0;
- int check2=0;
- int coupJouer=0;
- for(i=0;i<3;i++){
- check1 = 0+i*3;
- check2 = 1+i*3;
- if(possibilite[check1] == possibilite[check2] && possibilite[check2+1] != '+' && possibilite[check2+1] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
- possibilite[check2+1] = '.';
- coupJouer++;
- break;
- }else if(possibilite[check1] == possibilite[check2+1] && possibilite[check1+1] != '+' && possibilite[check1+1] != '.' && possibilite[check2+1] != '.' && possibilite[check1] != '.' && coupJouer==0){
- possibilite[check1+1] = '.';
- coupJouer++;
- break;
- }
- }
- //Nous testons les trois lignes horizontal de droite ‡ gauche
- check1=0;
- check2=0;
- for(i=0;i<3;i++){
- check1 = 1+i*3;
- check2 = 2+i*3;
- if(possibilite[check1] == possibilite[check2] && possibilite[check1-1] != '+' && possibilite[check1-1] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
- possibilite[check1-1] = '.';
- coupJouer++;
- break;
- }
- }
- //Nous testons les trois lignes vertical de haut en bas
- check1=0;
- check2=0;
- for(i=0;i<3;i++){
- check1 = i;
- check2 = i+3;
- if(possibilite[check1] == possibilite[check2] && possibilite[check2+3] != '+' && possibilite[check2+3] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
- possibilite[check2+3] = '.';
- coupJouer++;
- break;
- }else if(possibilite[check1] == possibilite[check2+3] && possibilite[check2] != '+' && possibilite[check2] != '.' && possibilite[check2+3] != '.' && possibilite[check1] != '.' && coupJouer==0){
- possibilite[check2] = '.';
- coupJouer++;
- break;
- }
- }
- //Nous testons les trois lignes vertical de bas en haut
- check1=0;
- check2=0;
- for(i=0;i<3;i++){
- check1 = i+3;
- check2 = check1+3;
- if(possibilite[check1] == possibilite[check2] && possibilite[check1-3] != '+' && possibilite[check1-3] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
- possibilite[check1-3] = '.';
- coupJouer++;
- break;
- }
- }
- //Nous testons la diagonal de haut gauche ‡ bas droit de gauche ‡ droite
- check1=0;
- check2=0;
- for(i=0;i<2;i++){
- check1 = i*2;
- check2 = 4;
- if(possibilite[check1] == possibilite[check2] && possibilite[8-check1] != '+' && possibilite[8-check1] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
- possibilite[8-check1] = '.';
- coupJouer++;
- break;
- }else if(possibilite[check1] == possibilite[check2+check2-check1] && possibilite[8-check1] != '+' && possibilite[8-check1] != '.' && possibilite[check2+check2-check1] != '.' && possibilite[check1] != '.' && coupJouer==0){
- possibilite[4] = '.';
- coupJouer++;
- break;
- }
- }
- //Nous testons la diagonal de haut gauche ‡ bas droit de droite ‡ gauche
- check1=0;
- check2=0;
- for(i=0;i<2;i++){
- check1 = 6+i*2;
- check2 = 4;
- if(possibilite[check1] == possibilite[check2] && possibilite[8-check1] != '+' && possibilite[8-check1] != '.' && possibilite[check2] != '.' && possibilite[check1] != '.' && coupJouer==0){
- possibilite[8-check1] = '.';
- coupJouer++;
- break;
- }
- }
- if(coupJouer==0){
- if(possibilite[4] != '.' && possibilite[4] != '+'){
- possibilite[4] = '.';
- coupJouer++;
- }else{
- for(i=0;i<9;i++){
- if(possibilite[i] != '.' && possibilite[i] != '+' && coupJouer == 0){
- possibilite[i] = '.';
- coupJouer++;
- }
- }
- }
- }
- if(coupJouer==0){
- *fin = 1;
- }
- }
- }
- void generateTTT(char *possibilite){
- printf("\t\t\t ___ ___ ___\n");
- printf("\t\t\t| | | | | |\n");
- printf("\t\t\t| %c | | %c | | %c |\n", possibilite[0], possibilite[1], possibilite[2]);
- printf("\t\t\t|___| |___| |___|\n");
- printf("\t\t\t| | | | | |\n");
- printf("\t\t\t| %c | | %c | | %c |\n", possibilite[3], possibilite[4], possibilite[5]);
- printf("\t\t\t|___| |___| |___|\n");
- printf("\t\t\t| | | | | |\n");
- printf("\t\t\t| %c | | %c | | %c |\n", possibilite[6], possibilite[7], possibilite[8]);
- printf("\t\t\t|___| |___| |___|\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement