diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9b7966391863082d46631183d92dfc693031f3a0..562435ad68c18220cad863ee36527ac183541284 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -18,3 +18,4 @@ include: - local: "/java-h2g2/.gitlab-ci.yml" - local: "/factory-patterns/.gitlab-ci.yml" - local: "/exceptions/.gitlab-ci.yml" + - local: "/puissance-4/.gitlab-ci.yml" diff --git a/README.md b/README.md index 56e2ac80fe8fac253a36fd874b02c6f1ecc5c633..beb77b5ec7bd0442aa93cfc9c09f13237b92bed2 100644 --- a/README.md +++ b/README.md @@ -41,3 +41,4 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré | [Back from GDCR avec Maxime, Séraphin, Anthony et Colin](https://www.youtube.com/watch?v=CHfUGdnSX6I) | Discussion | | | [Factory patterns](/factory-patterns) | Code&coffee | Facile | | [Exceptions](/exceptions) | Code&coffee | Facile | +| [Puissance 4](/puissance-4) | Kata | ? | diff --git a/puissance-4/.gitlab-ci.yml b/puissance-4/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..4c59b1e7eb1022e729257bdcb28416252f9728a3 --- /dev/null +++ b/puissance-4/.gitlab-ci.yml @@ -0,0 +1,11 @@ +package-puissance-4: + variables: + PROJECT_FOLDER: "puissance-4" + extends: .java + only: + refs: + - master + - merge_requests + changes: + - ".gitlab-common-ci.yml" + - "puissance-4/**/*" diff --git a/puissance-4/README.md b/puissance-4/README.md new file mode 100644 index 0000000000000000000000000000000000000000..507aff8a3a7eae8e400e8b6d4457b6d3cfcd5616 --- /dev/null +++ b/puissance-4/README.md @@ -0,0 +1,9 @@ +# Puissance IV + +Résolution, en live, de [Puissance 4](/puissance-4). + +- **Auteurs** : Colin DAMON & Cyril DUPONT +- **Date** : 22/12/2020 +- **Langage** : Java +- **Niveau** : ? +- **Replay** : [Puissance 4 - Cyril & Colin](?) diff --git a/puissance-4/pom.xml b/puissance-4/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..a53e07693296b5b704a322bf84620745e7669fc3 --- /dev/null +++ b/puissance-4/pom.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <version>1.0.0</version> + <groupId>fr.ippon.kata</groupId> + <artifactId>java-parent</artifactId> + <relativePath>../java-parent</relativePath> + </parent> + + <version>1.0.0-SNAPSHOT</version> + <artifactId>puissance-4</artifactId> + + <name>Puissance4</name> + + <developers> + <developer> + <email>cdamon@ippon.fr</email> + <name>Colin DAMON</name> + </developer> + <developer> + <email>cdupont@ippon.fr</email> + <name>Cyril DUPONT</name> + </developer> + </developers> +</project> diff --git a/puissance-4/src/main/java/fr/ippon/kata/puissance4/Puissance4.java b/puissance-4/src/main/java/fr/ippon/kata/puissance4/Puissance4.java new file mode 100644 index 0000000000000000000000000000000000000000..36a6c90c9300af58170ba6168cb4e6d2e1c5fe60 --- /dev/null +++ b/puissance-4/src/main/java/fr/ippon/kata/puissance4/Puissance4.java @@ -0,0 +1,340 @@ +package fr.ippon.kata.puissance4; + +import java.util.Scanner; + +public class Puissance4 { + + private static final Integer N = 20; + + public static int menu(){ + int choix; + Scanner sc = new Scanner(System.in); + System.out.println("\n\t\t\t***************************\n\t\t\t***\tPuissance 4\t***\n\t\t\t***************************"); + System.out.println("\n\n[1] Un joueur\n"); + System.out.println("[2] Deux joueurs\n"); + System.out.println("[3] Regle du jeu"); + + do{ + choix = sc.nextInt(); + }while(choix<1 ||choix>3); + return choix; + } + + public static int afficherGrille(Character grille[][]){ + int j,i,k; + + System.out.println("\n "); + for(k=1;k<=8;k++){ + System.out.print("["+k+"]"); + + } + System.out.println("\n"); + + for(i=3;i<=9;i++){/*9*/ + for(j=3;j<=10;j++){/*10*/ + System.out.print("| "); + System.out.print(grille[i][j]); + } + System.out.println("|"); + } + System.out.println(" ------------------------"); + return 0; + } + + public static int jouerJ1(Character grille[][]){ + int choix,res,j,i=9; + Scanner sc = new Scanner(System.in); + do{ + choix = sc.nextInt(); + if(choix>8 || choix<1){ + System.out.println("Choix incorecte, entrez un nombre entre 1 et8"); + } + }while(choix>8 || choix<1); + j=choix+2; + + while(grille[i][j]=='O' || grille[i][j]=='X'){ + i=i-1; + } + grille[i][j]='X'; + + afficherGrille(grille); + res=estgagne(i,(j),grille); + return res; + } + + + public static int jouerJ2(Character grille[][]){ + int choix,res,j,i=9; + Scanner sc = new Scanner(System.in); + do{ + choix = sc.nextInt(); + if(choix>8 || choix<1){ + System.out.println("Choix incorecte, entrez un nombre entre 1 et 7."); + } + }while(choix>8 || choix<1); + j=choix+2; + + while(grille[i][j]=='O' || grille[i][j]=='X'){ + i=i-1; + } + grille[i][j]='O'; + + + afficherGrille(grille); + res=estgagne(i,(choix+2),grille); + return res; + } + + + public static int jouerIA(Character grille[][]){ + int res,i=0,j=0,x=0,y=0,choix=0; + + + if(choix==0){ + for(i=3;i<=9;i++){ + for(j=3;j<=10;j++){ + if(choix==0){ + if(grille[i][j]=='*' && (grille[i+1][j]=='X' || grille[i+1][j]=='O' || grille[i+1][j]=='$')){ + if((grille[i+1][j]=='O' && grille[i+1][j]==grille[i+2][j] && grille[i+2][j]==grille[i+3][j]) + || (grille[i+1][j-1]=='O' && grille[i+1][j-1]==grille[i+2][j-2] && grille[i+2][j-2]==grille[i+3][j-3]) + || (grille[i][j-1]=='O' && grille[i][j-1]==grille[i][j-2] && grille[i][j-2]==grille[i][j-3]) + || (grille[i-1][j-1]=='O' && grille[i-1][j-1]==grille[i-2][j-2] && grille[i-2][j-2]==grille[i-3][j-3]) + || (grille[i-1][j+1]=='O'&& grille[i-1][j+1]==grille[i-2][j+2] && grille[i-2][j+2]==grille[i-3][j+3]) + || (grille[i][j+1]=='O' && grille[i][j+1]==grille[i][j+2] && grille[i][j+2]==grille[i][j+3]) + || (grille[i+1][j+1]=='O' && grille[i+1][j+1]==grille[i+2][j+2] && grille[i+2][j+2]==grille[i+3][j+3]) + /*new*/ + || (grille[i+1][j+1]=='O' && grille[i-1][j-1]==grille[i+1][j+1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i-1][j-1]=='O' && grille[i+1][j+1]==grille[i-1][j-1] && grille[i+1][j+1]==grille[i+2][j+2]) + ||(grille[i-1][j-1]=='O' && grille[i+1][j+1]==grille[i-1][j-1] && grille[i+1][j+1]==grille[i+2][j+2]) + || (grille[i-1][j+1]=='O' && grille[i-1][j-1]==grille[i-1][j+1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i][j-1]=='O' && grille[i][j+1]==grille[i][j-1] && grille[i][j]==grille[i][j+2]) + ||(grille[i][j+1]=='O' && grille[i][j-1]==grille[i][j+1] && grille[i][j-1]==grille[i][j-2]) + ){ + grille[i][j]='O'; + choix=1; + x=i; + y=j; + } + } + } + } + } + } + + if(choix==0){ + for(i=3;i<=9;i++){ + for(j=3;j<=10;j++){ + if(choix==0){ + if(grille[i][j]=='*' && (grille[i+1][j]=='X' || grille[i+1][j]=='O' || grille[i+1][j]=='$')){ + if((grille[i+1][j]=='X' && grille[i+1][j]==grille[i+2][j] && grille[i+2][j]==grille[i+3][j]) + || (grille[i+1][j-1]=='X' && grille[i+1][j-1]==grille[i+2][j-2] && grille[i+2][j-2]==grille[i+3][j-3]) + || (grille[i][j-1]=='X' && grille[i][j-1]==grille[i][j-2] && grille[i][j-2]==grille[i][j-3]) + || (grille[i-1][j-1]=='X' && grille[i-1][j-1]==grille[i-2][j-2] && grille[i-2][j-2]==grille[i-3][j-3]) + || (grille[i-1][j+1]=='X'&& grille[i-1][j+1]==grille[i-2][j+2] && grille[i-2][j+2]==grille[i-3][j+3]) + || (grille[i][j+1]=='X' && grille[i][j+1]==grille[i][j+2] && grille[i][j+2]==grille[i][j+3]) + || (grille[i+1][j+1]=='X' && grille[i+1][j+1]==grille[i+2][j+2] && grille[i+2][j+2]==grille[i+3][j+3]) + || (grille[i+1][j+1]=='X' && grille[i-1][j-1]==grille[i+1][j+1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i-1][j-1]=='X' && grille[i+1][j+1]==grille[i-1][j-1] && grille[i+1][j+1]==grille[i+2][j+2]) + ||(grille[i-1][j-1]=='X' && grille[i+1][j+1]==grille[i-1][j-1] && grille[i+1][j+1]==grille[i+2][j+2]) + || (grille[i-1][j+1]=='X' && grille[i-1][j-1]==grille[i-1][j+1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i][j-1]=='X' && grille[i][j+1]==grille[i][j-1] && grille[i][j]==grille[i][j+2]) + ||(grille[i][j+1]=='X' && grille[i][j-1]==grille[i][j+1] && grille[i][j-1]==grille[i][j-2]) + ){ + grille[i][j]='O'; + choix=1; + x=i; + y=j; + } + } + } + } + } + } + + if(choix==0){ + for(i=3;i<=9;i++){ + for(j=3;j<=10;j++){ + if(choix==0){ + if(grille[i][j]=='*' && (grille[i+1][j]=='X' || grille[i+1][j]=='O' || grille[i+1][j]=='$')){ + if((grille[i+1][j]=='O' && grille[i+1][j]==grille[i+2][j]) + || (grille[i+1][j-1]=='O' && grille[i+1][j-1]==grille[i+2][j-2]) + || (grille[i][j-1]=='O' && grille[i][j-1]==grille[i][j-2]) + || (grille[i-1][j-1]=='O' && grille[i-1][j-1]==grille[i-2][j-2]) + || (grille[i-1][j+1]=='O'&& grille[i-1][j+1]==grille[i-2][j+2]) + || (grille[i][j+1]=='O' && grille[i][j+1]==grille[i][j+2]) + || (grille[i+1][j+1]=='O' && grille[i+1][j+1]==grille[i+2][j+2]) + || (grille[i+1][j+1]=='O' && grille[i-1][j-1]==grille[i+1][j+1] ) ||(grille[i-1][j-1]=='O' && grille[i+1][j+1]==grille[i-1][j-1]) + ||(grille[i-1][j-1]=='O' && grille[i+1][j+1]==grille[i-1][j-1] ) + || (grille[i-1][j+1]=='O' && grille[i-1][j-1]==grille[i-1][j+1] ) + ||(grille[i][j-1]=='O' && grille[i][j+1]==grille[i][j-1]) + ||(grille[i][j+1]=='O' && grille[i][j-1]==grille[i][j+1]) + ){ + grille[i][j]='O'; + choix=1; + x=i; + y=j; + } + } + } + } + } } + + + if(choix==0){ + for(i=3;i<=9;i++){ + for(j=3;j<=10;j++){ + if(choix==0){ + if(grille[i][j]=='*' && (grille[i+1][j]=='X' || grille[i+1][j]=='O' || grille[i+1][j]=='$')){ + if(grille[i+1][j]=='O' + || grille[i+1][j-1]=='O' + || grille[i][j-1]=='O' + || grille[i-1][j-1]=='O' + || grille[i-1][j+1]=='O' + || grille[i][j+1]=='O' + || grille[i+1][j+1]=='O'){ + + grille[i][j]='O'; + choix=1; + x=i; + y=j; + } + } + } + } + } + } + + if(choix==0){ + x=9; + if(grille[x][7]=='X'){ + y=8; + grille[x][y]='O'; + } + else{ + y=7; + grille[x][y]='O'; + } + } + res=estgagne(x,y,grille); + afficherGrille(grille); + return res; + } + + public static int estgagne(int i, int j ,Character grille[][]){ + int res=0; + if( (grille[i][j]==grille[i+1][j] && grille[i+1][j]==grille[i+2][j] && grille[i+2][j]==grille[i+3][j]) + || (grille[i][j]==grille[i+1][j-1] && grille[i+1][j-1]==grille[i+2][j-2] && grille[i+2][j-2]==grille[i+3][j-3]) + || (grille[i][j]==grille[i][j-1] && grille[i][j-1]==grille[i][j-2] && grille[i][j-2]==grille[i][j-3]) + ||(grille[i][j]==grille[i-1][j-1] && grille[i-1][j-1]==grille[i-2][j-2] && grille[i-2][j-2]==grille[i-3][j-3]) + ||(grille[i][j]==grille[i-1][j+1] && grille[i-1][j+1]==grille[i-2][j+2] && grille[i-2][j+2]==grille[i-3][j+3]) + ||(grille[i][j]==grille[i][j+1] && grille[i][j+1]==grille[i][j+2] && grille[i][j+2]==grille[i][j+3]) + ||(grille[i][j]==grille[i+1][j+1] && grille[i+1][j+1]==grille[i+2][j+2] && grille[i+2][j+2]==grille[i+3][j+3]) + || (grille[i][j]==grille[i+1][j+1] && grille[i][j]==grille[i-1][j-1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i][j]==grille[i-1][j-1] && grille[i][j]==grille[i+1][j+1] && grille[i+1][j+1]==grille[i+2][j+2]) + ||(grille[i][j]==grille[i-1][j-1] && grille[i][j]==grille[i+1][j+1] && grille[i+1][j+1]==grille[i+2][j+2]) + || (grille[i][j]==grille[i-1][j+1] && grille[i][j]==grille[i-1][j-1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i][j]==grille[i][j-1] && grille[i][j]==grille[i][j+1] && grille[i][j]==grille[i][j+2]) + ||(grille[i][j]==grille[i][j+1] && grille[i][j]==grille[i][j-1] && grille[i][j-1]==grille[i][j-2]) + ) + res=1; + + if(res==1) + return res; + return 0; + } + + public static Integer afficherRegle(){ + int retour; + Scanner sc = new Scanner(System.in); + System.out.println("\tLe but du jeu est d'aligner 4 pions sur une grille comptant 7 rangées et 8 colonnes. Chaque joueur dispose de 28 pions d'une certain forme (par convention X ou O). Tour à tour les deux joueurs placent un pion dans la colonne de leur choix. Le vainqueur est le joueur qui réalise le premier un alignement (horizontal, vertical ou diagonal) d'au moins quatre pions de sa meme forme. Si alors que toutes les cases de la grille de jeu sont remplies aucun des deux joueurs n'a réalisé un tel alignement, la partie est déclarée nulle.\n"); + do{ + System.out.println("[1] Retour"); + retour = sc.nextInt(); + }while(retour>1 || retour<1); + return retour; + } + + public static void main(String[] args) { + + int j, res, i, choixMenu, exit; + String prenom1; + String prenom2; + Character[][] grille = new Character[N][N]; + Scanner sc = new Scanner(System.in); + + choixMenu = menu(); + if (choixMenu == 3) { + exit = afficherRegle(); + if (exit == 1) + choixMenu = menu(); + } + + if (choixMenu == 2) { + for (i = 0; i <= 12; i++) { + for (j = 0; j <= 13; j++) { + grille[i][j] = '$'; + } + } + for (i = 3; i <= 9; i++) { + for (j = 3; j <= 10; j++) { + grille[i][j] = '*'; + } + } + + System.out.println("\tEntrez le prenom d'un des deux joueurs\n"); + prenom1 = sc.nextLine(); + System.out.println("\tEntrez le prenom d'un des deux joueurs\n"); + prenom2 = sc.nextLine(); + System.out.println("\t " + prenom1 + " vs " + prenom2 + "\n"); + + do { + afficherGrille(grille); + res = jouerJ1(grille); + + if (res == 1) + System.out.println(prenom1 + " a gagnee !"); + + if (res != 1) { + res = jouerJ2(grille); + if (res == 1) + System.out.println(prenom2 +" a gagnee !"); + } + + } while (res != 1); + + } + + if (choixMenu == 1) { + for (i = 0; i <= 12; i++) { + for (j = 0; j <= 13; j++) { + grille[i][j] = '$'; + } + } + for (i = 3; i <= 9; i++) { + for (j = 3; j <= 10; j++) { + grille[i][j] = '*'; + } + } + + System.out.println("\tEntrez le prenom d'un des deux joueurs\n"); + prenom1 = sc.nextLine(); + System.out.println("\t "+prenom1+" vs ordinateur"); + + + do { + afficherGrille(grille); + res = jouerJ1(grille); + + if (res == 1) + System.out.println(prenom1 +" a gagnee !"); + + if (res != 1) { + res = jouerIA(grille); + if (res == 1) + System.out.println(" L'ordinareur a gagnee !"); + } + + } while (res != 1); + } + } +} diff --git a/puissance-4/src/main/resources/puissance_4.c b/puissance-4/src/main/resources/puissance_4.c new file mode 100644 index 0000000000000000000000000000000000000000..c1cbf926c093b88202b1e96115e87aff00b565bc --- /dev/null +++ b/puissance-4/src/main/resources/puissance_4.c @@ -0,0 +1,337 @@ +#include <stdio.h> +#include <function.h> +#define N 20 +int menu(){ + int choix; + printf("\n\t\t\t***************************\n\t\t\t***\tPuissance 4\t***\n\t\t\t***************************\n"); + printf("\n\n[1] Un joueur\n\n"); + printf("[2] Deux joueurs\n\n"); + printf("[3] Regle du jeu\n"); + + do{ + scanf("%d",&choix); + }while(choix<1 ||choix>3); + return choix; +} + +int afficherGrille(char grille[N][N]){ + int j,i,k; + + printf("\n "); + for(k=1;k<=8;k++){ + printf("[%d]",k); + + } + printf("\n\n"); + + for(i=3;i<=9;i++){/*9*/ + for(j=3;j<=10;j++){/*10*/ + printf("| "); + printf("%c",grille[i][j]); + } + printf("|\n"); + } + printf(" ------------------------\n"); +} + + +int jouerJ1(char grille[N][N]){ + int choix,res,j,i=9; + do{ + scanf("%d",&choix); + if(choix>8 || choix<1){ + printf("Choix incorecte, entrez un nombre entre 1 et8\n"); + } + }while(choix>8 || choix<1); + j=choix+2; + + while(grille[i][j]=='O' || grille[i][j]=='X'){ + i=i-1; + } + grille[i][j]='X'; + + afficherGrille(grille); + res=estgagne(i,(j),grille); + return res; +} + + +int jouerJ2(char grille[N][N]){ + int choix,res,j,i=9; + do{ + scanf("%d",&choix); + if(choix>8 || choix<1){ + printf("Choix incorecte, entrez un nombre entre 1 et 7.\n"); + } + }while(choix>8 || choix<1); + j=choix+2; + + while(grille[i][j]=='O' || grille[i][j]=='X'){ + i=i-1; + } + grille[i][j]='O'; + + + afficherGrille(grille); + res=estgagne(i,(choix+2),grille); + return res; +} + +int jouerIA(char grille[N][N]){ + int res,i=0,j=0,x,y,choix=0; + + + if(choix==0){ + for(i=3;i<=9;i++){ + for(j=3;j<=10;j++){ + if(choix==0){ + if(grille[i][j]=='*' && (grille[i+1][j]=='X' || grille[i+1][j]=='O' || grille[i+1][j]=='$')){ + if((grille[i+1][j]=='O' && grille[i+1][j]==grille[i+2][j] && grille[i+2][j]==grille[i+3][j]) + || (grille[i+1][j-1]=='O' && grille[i+1][j-1]==grille[i+2][j-2] && grille[i+2][j-2]==grille[i+3][j-3]) + || (grille[i][j-1]=='O' && grille[i][j-1]==grille[i][j-2] && grille[i][j-2]==grille[i][j-3]) + || (grille[i-1][j-1]=='O' && grille[i-1][j-1]==grille[i-2][j-2] && grille[i-2][j-2]==grille[i-3][j-3]) + || (grille[i-1][j+1]=='O'&& grille[i-1][j+1]==grille[i-2][j+2] && grille[i-2][j+2]==grille[i-3][j+3]) + || (grille[i][j+1]=='O' && grille[i][j+1]==grille[i][j+2] && grille[i][j+2]==grille[i][j+3]) + || (grille[i+1][j+1]=='O' && grille[i+1][j+1]==grille[i+2][j+2] && grille[i+2][j+2]==grille[i+3][j+3]) + /*new*/ + || (grille[i+1][j+1]=='O' && grille[i-1][j-1]==grille[i+1][j+1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i-1][j-1]=='O' && grille[i+1][j+1]==grille[i-1][j-1] && grille[i+1][j+1]==grille[i+2][j+2]) + ||(grille[i-1][j-1]=='O' && grille[i+1][j+1]==grille[i-1][j-1] && grille[i+1][j+1]==grille[i+2][j+2]) + || (grille[i-1][j+1]=='O' && grille[i-1][j-1]==grille[i-1][j+1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i][j-1]=='O' && grille[i][j+1]==grille[i][j-1] && grille[i][j]==grille[i][j+2]) + ||(grille[i][j+1]=='O' && grille[i][j-1]==grille[i][j+1] && grille[i][j-1]==grille[i][j-2]) + ){ + grille[i][j]='O'; + choix=1; + x=i; + y=j; + } + } + } + } + } + } + + if(choix==0){ + for(i=3;i<=9;i++){ + for(j=3;j<=10;j++){ + if(choix==0){ + if(grille[i][j]=='*' && (grille[i+1][j]=='X' || grille[i+1][j]=='O' || grille[i+1][j]=='$')){ + if((grille[i+1][j]=='X' && grille[i+1][j]==grille[i+2][j] && grille[i+2][j]==grille[i+3][j]) + || (grille[i+1][j-1]=='X' && grille[i+1][j-1]==grille[i+2][j-2] && grille[i+2][j-2]==grille[i+3][j-3]) + || (grille[i][j-1]=='X' && grille[i][j-1]==grille[i][j-2] && grille[i][j-2]==grille[i][j-3]) + || (grille[i-1][j-1]=='X' && grille[i-1][j-1]==grille[i-2][j-2] && grille[i-2][j-2]==grille[i-3][j-3]) + || (grille[i-1][j+1]=='X'&& grille[i-1][j+1]==grille[i-2][j+2] && grille[i-2][j+2]==grille[i-3][j+3]) + || (grille[i][j+1]=='X' && grille[i][j+1]==grille[i][j+2] && grille[i][j+2]==grille[i][j+3]) + || (grille[i+1][j+1]=='X' && grille[i+1][j+1]==grille[i+2][j+2] && grille[i+2][j+2]==grille[i+3][j+3]) + || (grille[i+1][j+1]=='X' && grille[i-1][j-1]==grille[i+1][j+1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i-1][j-1]=='X' && grille[i+1][j+1]==grille[i-1][j-1] && grille[i+1][j+1]==grille[i+2][j+2]) + ||(grille[i-1][j-1]=='X' && grille[i+1][j+1]==grille[i-1][j-1] && grille[i+1][j+1]==grille[i+2][j+2]) + || (grille[i-1][j+1]=='X' && grille[i-1][j-1]==grille[i-1][j+1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i][j-1]=='X' && grille[i][j+1]==grille[i][j-1] && grille[i][j]==grille[i][j+2]) + ||(grille[i][j+1]=='X' && grille[i][j-1]==grille[i][j+1] && grille[i][j-1]==grille[i][j-2]) + ){ + grille[i][j]='O'; + choix=1; + x=i; + y=j; + } + } + } + } + } + } + + if(choix==0){ + for(i=3;i<=9;i++){ + for(j=3;j<=10;j++){ + if(choix==0){ + if(grille[i][j]=='*' && (grille[i+1][j]=='X' || grille[i+1][j]=='O' || grille[i+1][j]=='$')){ + if((grille[i+1][j]=='O' && grille[i+1][j]==grille[i+2][j]) + || (grille[i+1][j-1]=='O' && grille[i+1][j-1]==grille[i+2][j-2]) + || (grille[i][j-1]=='O' && grille[i][j-1]==grille[i][j-2]) + || (grille[i-1][j-1]=='O' && grille[i-1][j-1]==grille[i-2][j-2]) + || (grille[i-1][j+1]=='O'&& grille[i-1][j+1]==grille[i-2][j+2]) + || (grille[i][j+1]=='O' && grille[i][j+1]==grille[i][j+2]) + || (grille[i+1][j+1]=='O' && grille[i+1][j+1]==grille[i+2][j+2]) + || (grille[i+1][j+1]=='O' && grille[i-1][j-1]==grille[i+1][j+1] ) ||(grille[i-1][j-1]=='O' && grille[i+1][j+1]==grille[i-1][j-1]) + ||(grille[i-1][j-1]=='O' && grille[i+1][j+1]==grille[i-1][j-1] ) + || (grille[i-1][j+1]=='O' && grille[i-1][j-1]==grille[i-1][j+1] ) + ||(grille[i][j-1]=='O' && grille[i][j+1]==grille[i][j-1]) + ||(grille[i][j+1]=='O' && grille[i][j-1]==grille[i][j+1]) + ){ + grille[i][j]='O'; + choix=1; + x=i; + y=j; + } + } + } + } + } + } + + if(choix==0){ + for(i=3;i<=9;i++){ + for(j=3;j<=10;j++){ + if(choix==0){ + if(grille[i][j]=='*' && (grille[i+1][j]=='X' || grille[i+1][j]=='O' || grille[i+1][j]=='$')){ + if(grille[i+1][j]=='O' + || grille[i+1][j-1]=='O' + || grille[i][j-1]=='O' + || grille[i-1][j-1]=='O' + || grille[i-1][j+1]=='O' + || grille[i][j+1]=='O' + || grille[i+1][j+1]=='O'){ + + grille[i][j]='O'; + choix=1; + x=i; + y=j; + } + } + } + } + } + } + + if(choix==0){ + x=9; + if(grille[x][7]=='X'){ + y=8; + grille[x][y]='O'; + } + else{ + y=7; + grille[x][y]='O'; + } + } + res=estgagne(x,y,grille); + afficherGrille(grille); + return res; +} +int estgagne(int i, int j ,char grille[N][N]){ + int res=0; + if( (grille[i][j]==grille[i+1][j] && grille[i+1][j]==grille[i+2][j] && grille[i+2][j]==grille[i+3][j]) + || (grille[i][j]==grille[i+1][j-1] && grille[i+1][j-1]==grille[i+2][j-2] && grille[i+2][j-2]==grille[i+3][j-3]) + || (grille[i][j]==grille[i][j-1] && grille[i][j-1]==grille[i][j-2] && grille[i][j-2]==grille[i][j-3]) + ||(grille[i][j]==grille[i-1][j-1] && grille[i-1][j-1]==grille[i-2][j-2] && grille[i-2][j-2]==grille[i-3][j-3]) + ||(grille[i][j]==grille[i-1][j+1] && grille[i-1][j+1]==grille[i-2][j+2] && grille[i-2][j+2]==grille[i-3][j+3]) + ||(grille[i][j]==grille[i][j+1] && grille[i][j+1]==grille[i][j+2] && grille[i][j+2]==grille[i][j+3]) + ||(grille[i][j]==grille[i+1][j+1] && grille[i+1][j+1]==grille[i+2][j+2] && grille[i+2][j+2]==grille[i+3][j+3]) + || (grille[i][j]==grille[i+1][j+1] && grille[i][j]==grille[i-1][j-1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i][j]==grille[i-1][j-1] && grille[i][j]==grille[i+1][j+1] && grille[i+1][j+1]==grille[i+2][j+2]) + ||(grille[i][j]==grille[i-1][j-1] && grille[i][j]==grille[i+1][j+1] && grille[i+1][j+1]==grille[i+2][j+2]) + || (grille[i][j]==grille[i-1][j+1] && grille[i][j]==grille[i-1][j-1] && grille[i-1][j-1]==grille[i-2][j-2]) + ||(grille[i][j]==grille[i][j-1] && grille[i][j]==grille[i][j+1] && grille[i][j]==grille[i][j+2]) + ||(grille[i][j]==grille[i][j+1] && grille[i][j]==grille[i][j-1] && grille[i][j-1]==grille[i][j-2]) + ) + res=1; + + if(res==1) + return res; + + +} + + + +int afficherRegle(){ + int retour; + printf("\tLe but du jeu est d'aligner 4 pions sur une grille comptant 7 rangées et 8 colonnes. Chaque joueur dispose de 28 pions d'une certain forme (par convention X ou O). Tour à tour les deux joueurs placent un pion dans la colonne de leur choix. Le vainqueur est le joueur qui réalise le premier un alignement (horizontal, vertical ou diagonal) d'au moins quatre pions de sa meme forme. Si alors que toutes les cases de la grille de jeu sont remplies aucun des deux joueurs n'a réalisé un tel alignement, la partie est déclarée nulle.\n\n"); + do{ + printf("[1] Retour\n"); + scanf("%d",&retour); + }while(retour>1 || retour<1); + return retour; +} + +int main (void){ + + + int j,res,i,choixMenu,exit; + char prenom1[N]; + char prenom2[N]; + char grille[N][N]; + + choixMenu=menu(); + + if(choixMenu==3){ + exit=afficherRegle(); + if(exit==1) + choixMenu=menu(); + + } + + if(choixMenu==2){ + for (i=0; i<=12; i++){ + for (j=0; j<=13; j++){ + grille[i][j] ='$'; + } + } + for (i=3; i<=9; i++){ + for (j=3; j<=10; j++){ + grille[i][j] ='*'; + } + } + + printf("\tEntrez le prenom d'un des deux joueurs\n"); + scanf("%s",prenom1); + printf("\tEntrez le prenom d'un des deux joueurs\n"); + scanf("%s",prenom2); + printf("\t %s vs %s\n",prenom1,prenom2); + + + do{ + afficherGrille(grille); + res=jouerJ1(grille); + + if(res==1) + printf(" %s a gagnŽe ! \n",prenom1); + + if(res!=1){ + res=jouerJ2(grille); + if(res==1) + printf(" %s a gagnŽe !\n",prenom2); + } + + }while(res!=1); + } + + + if(choixMenu==1){ + for (i=0; i<=12; i++){ + for (j=0; j<=13; j++){ + grille[i][j] ='$'; + } + } + for (i=3; i<=9; i++){ + for (j=3; j<=10; j++){ + grille[i][j] ='*'; + } + } + + printf("\tEntrez le prenom d'un des deux joueurs\n"); + scanf("%s",prenom1); + printf("\t %s vs ordinateur\n",prenom1); + + + do{ + afficherGrille(grille); + res=jouerJ1(grille); + + if(res==1) + printf(" %s a gagnŽe ! \n",prenom1); + + if(res!=1){ + res=jouerIA(grille); + if(res==1) + printf(" L'ordinareur a gagnŽe !\n"); + } + + }while(res!=1); + } + +} + + diff --git a/puissance-4/src/test/java/fr/ippon/kata/puissance4/Puissance4Test.java b/puissance-4/src/test/java/fr/ippon/kata/puissance4/Puissance4Test.java new file mode 100644 index 0000000000000000000000000000000000000000..e329791e7df2522f43e5a40fbb196244b31b3028 --- /dev/null +++ b/puissance-4/src/test/java/fr/ippon/kata/puissance4/Puissance4Test.java @@ -0,0 +1,7 @@ +package fr.ippon.kata.puissance4; + +import static org.junit.jupiter.api.Assertions.*; + +class Puissance4Test { + // todo writing tests +}