Solution - Imagine

Transcription

Solution - Imagine
Introduction à la Programmation
TP #3 - Corrigé
1
G1: monasse(at) imagine.enpc.fr
G2: nicolas.audebert(at) onera.fr
G3: alexandre.boulch(at) onera.fr
G4: antoine.recanati(at) inria.fr
G5: bourkia(at) imagine.enpc.fr
G6: ker.fadhela(at) gmail.com
Tableaux. Corrigé.
1.1
Solution
1.1.1
Version Console
//
I m a g i n e++
project
//
Project :
Mastermind
//
Author :
keriven
//
Date :
2007/10/16
#i n c l u d e <i o s t r e a m >
#i n c l u d e <c s t d l i b >
#i n c l u d e <ctime>
#i n c l u d e <s t r i n g >
u s i n g namespace s t d ;
//
Parametres
du
jeu
const int nbcases = 5;
const i n t nbcoul = 4 ;
const int nbessais = 10;
c o n s t i n t ncmax = 7 ;
//
nombre
de
pions
a
//
nombre
de
types
de
//
nombre
d ' essais
//
nombre
maximal
deviner
pions
pour
de
trouver
couleurs
la
combinaison
disponibles
// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
//
Fonctions
Mastermind
v o i d genereCombinaison ( i n t s o l u t [ n b c a s e s ] )
{
s r a n d ( ( u n s i g n e d i n t ) time ( 0 ) ) ;
f o r ( i n t i =0; i <n b c a s e s ; ++i )
s o l u t [ i ] = rand ()% n b c o u l ;
}
//
Saisie
//
( nbcases
d ' une
combinaison
chiffres
tapes
au
a
la
clavier
suite )
v o i d getCombinaison ( i n t e s s a i , i n t combi [ n b c a s e s ] )
{
b o o l ok ;
do {
c o u t << " E s s a i #" << e s s a i +1 << " : " ;
string s ;
c i n >> s ;
i f ( s . s i z e ()!= nbcases )
ok=f a l s e ;
1
}
else {
ok=t r u e ;
f o r ( i n t i =0; i <n b c a s e s && ok;++ i ) {
combi [ i ]= s [ i ] − ' 0 ' ;
i f ( combi [ i ]<0 | | combi [ i ]>=n b c o u l )
ok=f a l s e ;
}
}
i f ( ! ok )
c o u t << " Combinaison i m p o s s i b l e . Recommencez ! " << e n d l ;
} w h i l e ( ! ok ) ;
//
Calcul
//
mal
dans
une
combinaison
du
nombre
de
bien
places
et
du
nombre
de
places
void traiteCombinaison ( i n t combinaison [ nbcases ] , i n t s o l u t i o n [ nbcases ] ,
i n t &bp , i n t &mp)
{
b o o l combi [ n b c a s e s ] , s o l u t [ n b c a s e s ] ; // A e t e u t i l i s e
f o r ( i n t j =0; j <n b c a s e s ; ++j )
combi [ j ]= s o l u t [ j ]= f a l s e ;
//
Les
bien
places
bp=0;
f o r ( i n t j =0; j <n b c a s e s ; ++j )
i f ( s o l u t i o n [ j ]==c o m b i n a i s o n [ j ] ) {
++bp ;
s o l u t [ j ]= combi [ j ]= t r u e ;
}
//
}
Les
mal
places
mp=0;
f o r ( i n t j =0; j <n b c a s e s ; ++j )
f o r ( i n t k=0; k<n b c a s e s ; ++k ) {
i f ( ! s o l u t [ j ] && ! combi [ k ] && s o l u t i o n [ j ]==c o m b i n a i s o n [ k ] ) {
++mp;
s o l u t [ j ]= combi [ k]= t r u e ;
}
}
// / / / / / / / / / / / / / / / / / / / / / / /
//
Fonctions
d ' affichage
v o i d a f f i c h e ( i n t combi [ n b c a s e s ] )
{
f o r ( i n t i =0; i <n b c a s e s ; ++i )
c o u t << combi [ i ] ;
c o u t << e n d l ;
}
v o i d a f f i c h e ( i n t combi [ n b c a s e s ] , i n t b i e n p l a c e s , i n t m a l p l a c e s )
{
f o r ( i n t i =0; i <n b c a s e s ; ++i )
c o u t << combi [ i ] ;
c o u t << " − " << b i e n p l a c e s << " , " << m a l p l a c e s << e n d l ;
}
i n t main ( )
{
int s o l u t i o n [ nbcases ] ;
i n t combinaison [ nbcases ] ;
i n t bp ,mp;
2
genereCombinaison ( s o l u t i o n ) ;
//
Affichage
de
la
solution
pour
debuggage
// a f f i c h e ( s o l u t i o n ) ;
c o u t <<
<<
c o u t <<
c o u t <<
"La c o m b i n a i s o n que vous devez t r o u v e r f a i t "
n b c a s e s << " c h i f f r e s de l o n g . " << e n d l ;
" Ces c h i f f r e s s o n t c o m p r i s e n t r e 0 e t " << nbcoul −1 << e n d l ;
"Vous avez d r o i t a " << n b e s s a i s << " e s s a i s . " << e n d l ;
b o o l t r o u v e=f a l s e ;
f o r ( i n t i =0; i <n b e s s a i s && ! t r o u v e ; ++i ) {
getCombinaison ( i , c o m b i n a i s o n ) ;
t r a i t e C o m b i n a i s o n ( combinaison , s o l u t i o n , bp ,mp ) ;
a f f i c h e ( combinaison , bp ,mp ) ;
i f ( bp==n b c a s e s )
t r o u v e=t r u e ;
}
i f ( trouve )
c o u t << "VOUS AVEZ GAGNE ! ! ! " << e n d l ;
else {
c o u t << "Vous avez e p u i s e vos " << n b e s s a i s << " e s s a i s " << e n d l ;
c o u t << "VOUS AVEZ PERDU . . . " << e n d l ;
c o u t << "La s o l u t i o n e t a i t : " ;
affiche ( solution );
}
return 0;
}
1.1.2
Version Graphique
//
I m a g i n e++
project
//
Project :
Mastermind
//
Author :
keriven
//
Date :
2007/10/16
Graphique
#i n c l u d e <i o s t r e a m >
#i n c l u d e <c s t d l i b >
#i n c l u d e <ctime>
u s i n g namespace s t d ;
#i n c l u d e <Imagine / G ra ph ic s . h>
u s i n g namespace Imagine ;
//
Parametres
du
jeu
const int nbcases = 5;
const i n t nbcoul = 6 ;
const int nbessais = 10;
c o n s t i n t ncmax = 7 ;
//
nombre
de
pions
a
//
nombre
de
types
de
deviner
//
nombre
d ' essais
//
nombre
maximal
pions
pour
de
trouver
couleurs
la
combinaison
disponibles
// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
//
Fonctions
d ' affichage
C o l o r c o u l e u r s [ ncmax+1]={RED,GREEN, BLUE,YELLOW,MAGENTA,CYAN,BLACK,WHITE} ;
c h a r t o u c h e _ c o u l e u r s [ ncmax+1]={ 'R ' , 'V ' , 'B ' , ' J ' , 'M' , 'C ' , 'N ' , ' ' } ;
const int c_size = 24;
const int ecart = 10;
//
Affichage
d ' un
curseur
indiquant
le
prochain
void a f f i c h e S a i s i e ( i n t n , i n t l i g n e , Color c )
3
pion
a
saisir
{
d r a w C i r c l e ( n ∗ c _ s i z e+c _ s i z e / 2 , l i g n e ∗ c _ s i z e+c _ s i z e / 2 , c _ s i z e / 2 , c ) ;
}
//
Affichage
d ' un
combinaison
v o i d a f f i c h e C o m b i n a i s o n ( i n t combi [ n b c a s e s ] , i n t l i g n e )
{
f o r ( i n t i =0; i <n b c a s e s ; ++i )
f i l l C i r c l e ( i ∗ c _ s i z e+c _ s i z e / 2 , l i g n e ∗ c _ s i z e+c _ s i z e / 2 ,
c _ s i z e / 2 , c o u l e u r s [ combi [ i ] ] ) ;
}
//
Affichage
des
indices
void a f f i c h e I n d i c e s ( i n t bienplaces , i n t malplaces , i n t l i g n e )
{
f o r ( i n t i =0; i <b i e n p l a c e s ; ++i )
f i l l C i r c l e ( n b c a s e s ∗ c _ s i z e+e c a r t+i ∗ c _ s i z e /2+ c _ s i z e / 4 ,
l i g n e ∗ c _ s i z e+c _ s i z e / 2 , c _ s i z e / 4 ,BLACK) ;
f o r ( i n t i =0; i <m a l p l a c e s ; ++i )
d r a w C i r c l e ( n b c a s e s ∗ c _ s i z e+e c a r t +( b i e n p l a c e s+i ) ∗ c _ s i z e /2+ c _ s i z e / 4 ,
l i g n e ∗ c _ s i z e+c _ s i z e / 2 , c _ s i z e / 4 ,BLACK) ;
}
//
Affichage
d ' un
trait
horizontal
de
separation
void a f f i c h e S e p a r a t i o n ( i n t l i g n e )
{
drawLine ( 0 , ( 2 ∗ l i g n e +1) ∗ c _ s i z e / 2 , 5 0 0 , ( 2 ∗ l i g n e +1) ∗ c _ s i z e / 2 ,BLACK) ;
}
//
Affichage
//
correspond
d ' un
a
mini
quelle
mode
d ' emploi
recapitulant
quelle
touche
couleur
void afficheTouches ( i n t l i g n e )
{
char c [ 2 ] = {0 ,0};
f o r ( i n t i =0; i <n b c o u l ; ++i ) {
f i l l C i r c l e ( i ∗ c _ s i z e+c _ s i z e / 2 , l i g n e ∗ c _ s i z e+c _ s i z e / 2 ,
c_size /2 , c o u l e u r s [ i ] ) ;
c [ 0 ] = touche_couleurs [ i ] ;
d r a w S t r i n g ( i ∗ c _ s i z e , ( l i g n e +1) ∗ c _ s i z e , c ,BLACK) ;
}
}
// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
//
Fonction
de
gestion
du
clavier
int Clavier ()
{
//
Renvoie
une
couleur
entre
}
−1 ,
0
et
nbcoul
−1 p o u r s i g n i f i e r un r e t o u r
// −2 p o u r une m a u v a i s e t o u c h e
i n t k = getKey ( ) ;
i f ( k==KEY_BACK) // B a c k s p a c e
r e t u r n − 1;
f o r ( i n t i =0; i <n b c o u l ; i ++)
i f ( k==t o u c h e _ c o u l e u r s [ i ] )
return i ;
r e t u r n − 2; // M a u v a i s e t o u c h e
en
arriere
//
d ' une
case
// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
//
//
Fonctions
la
meme
Mastermind
que
pour
le
Mastermind
txt
4
v o i d genereCombinaison ( i n t combin [ n b c a s e s ] )
{
s r a n d ( ( u n s i g n e d i n t ) time ( 0 ) ) ;
f o r ( i n t i =0; i <n b c a s e s ; ++i )
combin [ i ] = rand ()% n b c o u l ;
}
v o i d getCombinaison ( i n t combi [ n b c a s e s ] , i n t l i g n e )
{
f o r ( i n t j =0; j <n b c a s e s ; ++j )
combi [ j ]=ncmax ;
f o r ( i n t j =0; j <n b c a s e s ; ++j ) {
a f f i c h e S a i s i e ( j , l i g n e , Color ( 1 6 0 , 1 6 0 , 1 6 0 ) ) ;
int c ;
do
c = Clavier ( ) ;
w h i l e ( c==−2);
i f ( c >=0){
//
prise
en
compte
de
la
nouvelle
couleur
combi [ j ]= c ;
} else {
//
effacement
de
la
en
a
derniere
couleur
ajoutee
i f ( j ==0){
//
il
n 'y
pas
car
on
est
au
debut
j =−1;
continue ;
} else {
//
on
efface
la
derniere
combi [ j −1]=ncmax ;
j −=2;
}
}
}
}
a f f i c h e S a i s i e ( j , l i g n e ,WHITE) ;
a f f i c h e C o m b i n a i s o n ( combi , l i g n e ) ;
void traiteCombinaison ( i n t combinaison [ nbcases ] , i n t s o l u t i o n [ nbcases ] ,
i n t &bp , i n t &mp)
{
b o o l combi [ n b c a s e s ] , s o l u t [ n b c a s e s ] ; // A e t e u t i l i s e
f o r ( i n t j =0; j <n b c a s e s ; ++j )
combi [ j ]= s o l u t [ j ]= f a l s e ;
//
Les
bien
places
bp=0;
f o r ( i n t j =0; j <n b c a s e s ; ++j )
i f ( s o l u t i o n [ j ]==c o m b i n a i s o n [ j ] ) {
++bp ;
s o l u t [ j ]= combi [ j ]= t r u e ;
}
//
}
Les
mal
places
mp=0;
f o r ( i n t j =0; j <n b c a s e s ; ++j )
f o r ( i n t k=0; k<n b c a s e s ; ++k ) {
i f ( ! s o l u t [ j ] && ! combi [ k ] && s o l u t i o n [ j ]==c o m b i n a i s o n [ k ] ) {
++mp;
s o l u t [ j ]= combi [ k]= t r u e ;
}
}
5
// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
//
Fonction
principale
i n t main ( )
{
//
On
genere
une
combinaison
int s o l u t i o n [ nbcases ] ;
genereCombinaison ( s o l u t i o n ) ;
solution [0]=4;
solution [1]=2; solution [2]=2; solution [3]=2; solution [4]=2;
//
Initialisation
de
l ' affichage
i n t width = n b c a s e s ∗ c _ s i z e + e c a r t + n b c a s e s ∗ c _ s i z e / 2 ;
i n t h e i g h t = ( n b e s s a i s +6) ∗ c _ s i z e ;
openWindow ( width , h e i g h t , " Master Mind" ) ;
afficheSeparation ( nbessais );
a f f i c h e S e p a r a t i o n ( n b e s s a i s +3);
a f f i c h e T o u c h e s ( n b e s s a i s +4);
//
Affichage
//
a f f i c h e C o m b i n a i s o n ( s o l u t i o n , n b e s s a i s +1);
de
la
solution
pour
debuggage
//
Jeu
b o o l t r o u v e=f a l s e ;
f o r ( i n t i =0; i <n b e s s a i s && ! t r o u v e ; ++i ) {
//
Saisie
d ' un
combinaison
i n t combi [ n b c a s e s ] ;
getCombinaison ( combi , i ) ;
//
Traitement
de
la
combinaison
i n t bp ,mp;
t r a i t e C o m b i n a i s o n ( combi , s o l u t i o n , bp ,mp ) ;
a f f i c h e I n d i c e s ( bp , mp, i ) ;
//
Detection
de
victoire
i f ( bp==n b c a s e s )
t r o u v e=t r u e ;
}
}
a f f i c h e C o m b i n a i s o n ( s o l u t i o n , n b e s s a i s +1);
i f ( trouve )
d r a w S t r i n g ( 0 , ( n b e s s a i s +2) ∗ c _ s i z e , " GAGNE" ,RED) ;
else
d r a w S t r i n g ( 0 , ( n b e s s a i s +2) ∗ c _ s i z e , " PERDU" ,RED) ;
endGraphics ( ) ;
return 0;
6