237
>X si O program in C++ – metoda 2
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int tabla[9], muta, mutare;
int castig()
{
if(tabla[0]*tabla[1]*tabla[2]==27) return 1; //castiga X
else if(tabla[0]*tabla[1]*tabla[2]==125) return 2; //castiga 0
else if(tabla[3]*tabla[4]*tabla[5]==27) return 1;
else if(tabla[3]*tabla[4]*tabla[5]==125) return 2;
else if(tabla[6]*tabla[7]*tabla[8]==27) return 1;
else if(tabla[6]*tabla[7]*tabla[8]==125) return 2;
else if(tabla[0]*tabla[3]*tabla[6]==27) return 1;
else if(tabla[0]*tabla[3]*tabla[6]==125) return 2;
else if(tabla[1]*tabla[4]*tabla[7]==27) return 1;
else if(tabla[1]*tabla[4]*tabla[7]==125) return 2;
else if(tabla[2]*tabla[5]*tabla[8]==27) return 1;
else if(tabla[2]*tabla[5]*tabla[8]==125) return 2;
else if(tabla[0]*tabla[4]*tabla[8]==27) return 1;
else if(tabla[0]*tabla[4]*tabla[8]==125) return 2;
else if(tabla[2]*tabla[4]*tabla[6]==27) return 1;
else if(tabla[2]*tabla[4]*tabla[6]==125) return 2;
else return 0;
}
void init()
{
for(int i=0;i<9;i++) tabla[i]=2;
muta=0;
}
char tabaf(int as)
{
if(as==2) return ' ';
else if(as==3) return 'X';
else if(as==5) return 'O';
else return ' ';
}
void afis()
{
cout<<"_______n";
cout<<"|"<<tabaf(tabla[0])<<"|"<<tabaf(tabla[1])<<"|"<<tabaf(tabla[2])<<"|";
cout<<"n|"<<tabaf(tabla[3])<<"|"<<tabaf(tabla[4])<<"|"<<tabaf(tabla[5])<<"|";
cout<<"n|"<<tabaf(tabla[6])<<"|"<<tabaf(tabla[7])<<"|"<<tabaf(tabla[8])<<"|";
cout<<"n-------n";
}
int mut_valid(int cemut)
{
if(tabla[cemut]!=2)
{
cout<<"nMutare invalida! mai incearca!n";
return 1;
}
else return 0;
}
int citeste_mutare()
{
cout<<"Muta: ";
cin>>muta;
while(mut_valid(muta-1))
{
cout<<"Muta: ";
cin>>muta;
}
return muta-1;
}
int valid_move(int cemut)
{
if(tabla[cemut]!=2) return 0;
else return 1;
}
int rand_mutare()
{
int mt;
mt=(int) random(8);
while(!valid_move(mt)) mt=(int) random(8);
return mt;
}
void move(int ce)
{
if(mutare%2!=0) //daca e par muta x
tabla[ce]=3;
else tabla[ce]=5;
}
void main()
{
clrscr();
init();
mutare=1;
while( (mutare<10) || (castig()==0) )
{
move(citeste_mutare());
afis();
mutare++;
if(mutare>9)break;
if(castig()!=0) break;
move(rand_mutare());
afis();
if(castig()!=0) break;
mutare++;
if(mutare>9)break;
}//END WHILE
cout<<"nGame Over!";
getch();
clrscr();
afis();
if(castig()==0) cout<<"Egalitate!!!"; else cout<<"A castigat jucatorul "<<castig()<<"!";
getch();
}