PDA

View Full Version : [c++] Gioco del tris (correzione e\o suggerimenti)


jokers85
05-02-2013, 22:58
ho scritto questo programmino per giocare a tris contro il pc, funziona sembra andare bene, ma volevo qualche consiglio su come cercare di migliorarlo

codice:

#include <stdlib.h>
#include <iostream>
#include<math.h>
#include <time.h>

using namespace std;


void campo(char mat[][3]);

void scelta_G(char mat[][3]);

void scelta_C(char mat[][3]);

void scelta_Crand(char mat[][3]);

void randScelta(int& i, int& j);

bool Tris(char mat[][3]);

bool pieno(char mat[][3]);

void tris(char mat[][3], bool& fatto);


int main()

{
system("color 02");
system("CLS");
char scelta;
do{
system("CLS");


char mat[3][3];
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
mat[i][j]=' ';
bool fatto=false;

campo(mat);

do{
scelta_G(mat);
if(Tris(mat)){
system("CLS");

campo(mat);
cout<<"Hai vinto!!!"<<endl;
break;
}
tris(mat,fatto);
if(!fatto)
scelta_C(mat);

if(Tris(mat))
{
system("CLS");

campo(mat);
cout<<"Hai perso..."<<endl;
break;
}
system("CLS");

campo(mat);

}while(!pieno(mat));

cout<<"Vuoi giocare ancora? (s/n)"<<endl;
do{
cin>>scelta;
}while(scelta!='s' && scelta!='n');

}while(scelta=='s');

//system("PAUSE");
return 0;

}



void campo(char mat[][3])

{

cout<<"1\t |2 \t |3 \n "<<mat[0][0]<<" \t | "
<<mat[0][1]<<" \t | "<<mat[0][2]<<" \n\t | \t | \n";


cout<<" ---------------------------\n";

cout<<"4\t |5 \t |6 \n "<<mat[1][0]<<" \t | "
<<mat[1][1]<<" \t | "<<mat[1][2]<<" \n\t | \t | \n";


cout<<" ---------------------------\n";

cout<<"7\t |8 \t |9 \n "<<mat[2][0]<<" \t | "
<<mat[2][1]<<" \t | "<<mat[2][2]<<" \n\t | \t | \n";



}



void scelta_G(char mat[][3])

{

int scelta;

cin>>scelta;

switch(scelta){

case 1: if(mat[0][0]==' ') mat[0][0]='X';
else return scelta_G(mat); break;


case 2: if(mat[0][1]==' ') mat[0][1]='X';
else return scelta_G(mat); break;


case 3: if(mat[0][2]==' ') mat[0][2]='X';
else return scelta_G(mat); break;


case 4: if(mat[1][0]==' ') mat[1][0]='X';
else return scelta_G(mat); break;


case 5: if(mat[1][1]==' ') mat[1][1]='X';
else return scelta_G(mat); break;


case 6: if(mat[1][2]==' ') mat[1][2]='X';
else return scelta_G(mat); break;


case 7: if(mat[2][0]==' ') mat[2][0]='X';
else return scelta_G(mat); break;


case 8: if(mat[2][1]==' ') mat[2][1]='X';
else return scelta_G(mat); break;


case 9: if(mat[2][2]==' ') mat[2][2]='X';
else return scelta_G(mat); break;

}

}



void scelta_Crand(char mat[][3])

{

int i=0;

int j=0;

randScelta(i,j);
while(mat[i][j]!= ' ' && !pieno(mat))

randScelta(i,j);



if(mat[i][j]==' ')

mat[i][j]='O';
}
void randScelta(int& i, int& j)
{
srand(time(NULL));
i=rand()%3;
j=rand()%3;
}
void scelta_C(char mat[][3])

{
bool fatto=false;
int countC=0;
int countL=0;
int countD1=0;
int countD2=0;
int count=0;
bool freeC=false;
bool freeL=false;
bool freeD1=false;
bool freeD2=false;
int cC,rC,cL,rL,cD1,rD1,cD2,rD2;

for(int i=0; i<3; i++){
countC=0;
countL=0;
count=0;
for(int j=0; j<3; j++){
//Controllo la diagonale principale
if(i==j){
if(mat[i][j]=='X')
countD1++;
else if(mat[i][j]==' '){
freeD1=true;
cD1=i;
rD1=j;
}
}

//Controllo la diagonale secondaria
if(mat[i][3-i-1]=='X' && count==0)
{
countD2++;
count++;
}
else if(mat[i][3-i-1]==' '){
freeD2=true;
cD2=i;
rD2=3-i-1;
}

//Controllo le colonne
if(mat[j][i]=='X')
countC++;
else if(mat[j][i]==' '){
freeC=true;
cC=j;
rC=i;
}

//Controllo le righe
if(mat[i][j]=='X')
countL++;
else if(mat[i][j]==' '){
freeL=true;
cL=i;
rL=j;
}
}

if(countD1==2 && freeD1){
mat[cD1][rD1]='O';
fatto=true;
break;
}
else if(countD2==2 && freeD2){
mat[cD2][rD2]='O';
fatto=true;
break;
}
else if(countC==2 && freeC){
mat[cC][rC]='O';
fatto=true;
break;
}
else if(countL==2 && freeL){
mat[cL][rL]='O';
fatto=true;
break;
}
}
if(!fatto)
scelta_Crand(mat);
}
bool Tris(char mat[][3])

{

int contX=0, contO=0;


for(int i=0; i<3; i++){
contX=0;
contO=0;
for(int j=0; j<3; j++){
if(mat[i][j]=='X')
contX++;

if(mat[i][j]=='O')
contO++;
}

if(contX==3)
return true;

if(contO==3)
return true;
}
contX=0; contO=0;

for(int i=0; i<3; i++){
contX=0;
contO=0;
for(int j=0; j<3; j++){
if(mat[j][i]=='X')
contX++;
if(mat[j][i]=='O')
contO++;

}
if(contX==3)
return true;

if(contO==3)
return true;
}

contX=0; contO=0;

for(int i=0; i<3; i++)
for(int j=0; j<3; j++){
if(i==j && mat[i][j]=='X')
contX++;
if(i==j && mat[i][j]=='O')
contO++;
}

if(contX==3)
return true;

if(contO==3)
return true;

contX=0; contO=0;

for(int i=0; i<3; i++){
if(mat[i][3-i-1]=='X')
contX++;

if(mat[i][3-i-1]=='O')
contO++;
}


if(contX==3)
return true;

if(contO==3)
return true;

return false;

}



bool pieno(char mat[][3])

{



for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

if(mat[i][j]==' ')

return false;

return true;



}
void tris(char mat[][3], bool& fatto)
{

fatto=false;
int countC=0;
int countL=0;
int countD1=0;
int countD2=0;
int count=0;
bool freeC=false;
bool freeL=false;
bool freeD1=false;
bool freeD2=false;
int cC,rC,cL,rL,cD1,rD1,cD2,rD2;

for(int i=0; i<3; i++){
countC=0;
countL=0;
count=0;
for(int j=0; j<3; j++){

if(i==j){
if(mat[i][j]=='O')
countD1++;
else if(mat[i][j]==' '){
freeD1=true;
cD1=i;
rD1=j;
}
}

if(mat[i][3-i-1]=='O' && count==0){
countD2++;
count++;
}
else if(mat[i][3-i-1]==' '){
freeD2=true;
cD2=i;
rD2=3-i-1;
}

if(mat[j][i]=='O')
countC++;
else if(mat[j][i]==' '){
freeC=true;
cC=j;
rC=i;
}

if(mat[i][j]=='O')
countL++;
else if(mat[i][j]==' '){
freeL=true;
cL=i;
rL=j;
}
}

if(countD1==2 && freeD1){
mat[cD1][rD1]='O';
fatto=true;
break;
}
else if(countD2==2 && freeD2){
mat[cD2][rD2]='O';
fatto=true;
break;
}
else if(countC==2 && freeC){
mat[cC][rC]='O';
fatto=true;
break;
}
else if(countL==2 && freeL){
mat[cL][rL]='O';
fatto=true;
break;
}
}


}