173 lines
3.7 KiB
C
173 lines
3.7 KiB
C
#include<stdio.h>
|
|
#include<stdlib.h>
|
|
|
|
#define N 5
|
|
#define BATEAUX_COUNT 2
|
|
#define BATEAUX_LENGTHES_ARR {2,2}
|
|
#define J_atoi(x) printf("-----------------------------------\n\e[1;1H\e[2JJoueur %d, c'est à vous: \n",x)
|
|
#define printclear() printf("\e[1;1H\e[2J")
|
|
|
|
/*
|
|
* g==0 -> eau
|
|
* g==1 -> eau torpillée
|
|
* g==2 -> bateau
|
|
* g==3 -> bateau torpillé
|
|
*/
|
|
typedef int Grille[N][N];
|
|
|
|
void initialiserGrille(Grille grille){
|
|
for(int i=0; i<N; i++)
|
|
for(int j=0; j<N; j++)
|
|
grille[i][j] = 0;
|
|
}
|
|
|
|
void afficherGrille(Grille grille, int secret){
|
|
printf("╔");
|
|
for(int i=0; i<N; i++)printf("═");
|
|
printf("╗\n");
|
|
for(int j=0; j<N; j++){
|
|
printf("║");
|
|
for(int i=0; i<N; i++){
|
|
switch(grille[i][j]){
|
|
case 0:
|
|
printf(" ");break;
|
|
case 1:
|
|
printf("░");break;
|
|
case 2:
|
|
printf(secret?" ":"█");break;
|
|
case 3:
|
|
printf("▓");break;
|
|
default:
|
|
printf("?");
|
|
}
|
|
}
|
|
printf("║\n");
|
|
}
|
|
printf("╚");
|
|
for(int i=0; i<N; i++)printf("═");
|
|
printf("╝\n");
|
|
}
|
|
|
|
int viserCase(Grille grille, int x, int y){
|
|
if(grille[x][y]%2==1)return 0;// On ne retouche pas
|
|
grille[x][y] += 1;
|
|
return grille[x][y]>=2;
|
|
}
|
|
|
|
/**
|
|
* dir==0 -> Horizontal
|
|
* dir==1 -> Vertical
|
|
*/
|
|
int setBateau(Grille grille, int longueur, int px, int py, int dir){
|
|
int coefx = dir==0;
|
|
int coefy = dir==1;
|
|
if((dir!=0) && (dir!=1))return 0;
|
|
if((px<0) || (py<0))return 0;
|
|
if((px+coefx*longueur >N) || (py+coefy*longueur>N)) return 0;
|
|
|
|
for(int i=0; i<longueur; i++)
|
|
if(grille[px+coefx*i][py+coefy*i]==2)return 0;
|
|
for(int i=0; i<longueur; i++)
|
|
grille[px+coefx*i][py+coefy*i] = 2;
|
|
return 1;
|
|
}
|
|
|
|
void demandeRemplirGrille(Grille g, int bateauxLength[BATEAUX_COUNT]){
|
|
int px,py,dir;
|
|
for(int k=0; k<BATEAUX_COUNT; k++){
|
|
printclear();
|
|
afficherGrille(g, 0);
|
|
do{
|
|
printf("Placez un bateau de taille %d (x y dir) avec (dir==0 pour horizontal):", bateauxLength[k]);
|
|
scanf("%d %d %d", &px, &py, &dir);
|
|
}while(!setBateau(g, bateauxLength[k], px, py, dir));
|
|
}
|
|
}
|
|
|
|
void remplirAupif(Grille g, int bateauxLength[BATEAUX_COUNT]){
|
|
int k=0;
|
|
while(k<BATEAUX_COUNT){
|
|
int tx = rand() % N;
|
|
int ty = rand() % N;
|
|
int dir = rand() % 2;
|
|
if(setBateau(g, bateauxLength[k], tx, ty, dir))k++;
|
|
}// Presque sûr que ça finit
|
|
}
|
|
|
|
int main(){
|
|
|
|
|
|
int jeu;
|
|
do{
|
|
printf("1 pour jouer contre un humain, 2 pour jouer contre l'ordinateur, 0 pour quitter : ");
|
|
scanf("%d", &jeu);
|
|
}while(jeu<0 || jeu>2);
|
|
|
|
if(jeu==0)return 0;
|
|
|
|
Grille g1, g2;
|
|
|
|
initialiserGrille(g1);
|
|
initialiserGrille(g2);
|
|
|
|
int bateauxLength[BATEAUX_COUNT] = BATEAUX_LENGTHES_ARR;
|
|
|
|
int tx, ty;
|
|
|
|
demandeRemplirGrille(g1,bateauxLength);
|
|
//remplirAupif(g1, bateauxLength);
|
|
|
|
if(jeu==1){
|
|
J_atoi(2);
|
|
demandeRemplirGrille(g2, bateauxLength);
|
|
}else{
|
|
remplirAupif(g2, bateauxLength);
|
|
}
|
|
|
|
int batCount=0;
|
|
for(int k=0; k<BATEAUX_COUNT; k++)batCount+=bateauxLength[k];
|
|
|
|
int bateaux[2] = {batCount, batCount};
|
|
|
|
int joueur = 0; // Inutile si contre un ordinateur.
|
|
|
|
while(bateaux[0]!=0 && bateaux[1]!=0){
|
|
|
|
if(jeu==1)J_atoi(joueur);
|
|
printf("Base: \n");
|
|
afficherGrille(joueur==0?g1:g2,0);
|
|
printf("Objectif: \n");
|
|
afficherGrille(joueur==0?g2:g1,1);
|
|
printf("Où voulez-vous tirer ? (x y) ");
|
|
scanf("%d %d",&tx, &ty);
|
|
|
|
if(viserCase(joueur==0?g2:g1 ,tx, ty)){
|
|
printf("Touché !\n");
|
|
bateaux[(joueur+1)%2]--;
|
|
}else
|
|
printf("Plouf !\n");
|
|
|
|
if(jeu==2){
|
|
//Faire le coup de l'ordinateur
|
|
do{
|
|
tx = rand() % N;
|
|
ty = rand() % N;
|
|
}while(g1[tx][ty]%2!=0);
|
|
if(viserCase(g1, tx, ty)){
|
|
printf("L'ordinateur a touché un bateau !\n");
|
|
bateaux[joueur]--;
|
|
}else
|
|
printf("L'ordinateur a tiré dans l'eau !\n");
|
|
|
|
}else{
|
|
joueur = (joueur+1)%2;
|
|
}
|
|
|
|
}
|
|
|
|
printf("Victoire du joueur %d !\n", (joueur+1)%2)
|
|
|
|
|
|
|
|
}
|