Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <windows.h>
- using namespace std;
- #define size_vet 3
- #define null ' '
- /*Pulisci array*/
- void wipe(char array[size_vet][size_vet])
- {
- for(int i=0; i!=size_vet; ++i)
- for(int j=0; j!=size_vet; ++j)
- array[i][j]=null;
- }
- /*Mostra tabella di tris*/
- void stampa(char array[size_vet][size_vet])
- {
- int k=0;
- for(int i=0; i!=size_vet; ++i)
- {
- if(i==0) cout<<"-----------------\n";
- for(int j=0; j!=size_vet; ++j)
- {
- ++k;
- cout<<k<<' '<<array[i][j]<<" | ";
- }
- cout<<"\n-----------------\n";
- }
- }
- /*Inserisci posizione*/
- bool posizione(vector<int> &pos, int p)
- {
- if(p>0 && p<=9)
- {
- for(int i=0; i!=pos.size(); ++i)
- {
- if(pos[i] == p) return true;
- }
- }
- else return true;
- return false;
- }
- /*Inserisci la posizione selezionata nel array*/
- void inserisci(char array[size_vet][size_vet], int i, vector<int> &pos)
- {
- int p;
- int j=0;
- do{
- if(j==0) cout<<"Inserisci la posizione: ";
- else cout<<"Posizione non disponibile: ";
- ++j;
- cin>>p;
- }while(posizione(pos, p));
- pos.push_back(p);
- --p;
- if(p<3)
- {
- if(i%2==0) array[0][p]='X';
- else array[0][p]='O';
- }
- else if(p>=3 || p<6)
- {
- p-=3;
- if(i%2==0) array[1][p]='X';
- else array[1][p]='O';
- }
- else if(p>=6 || p<9)
- {
- p-=6;
- if( array[2][p] == null )
- if(i%2==0) array[2][p]='X';
- else array[2][p]='O';
- }
- }
- /*Controllo se ha fatto tris. invocato in stop*/
- char controllo(char array[size_vet][size_vet])
- {
- for(int i=0; i!=size_vet; ++i)
- {
- for(int j=0; j!=size_vet; ++j)
- {
- if(array[i][j]!=' ')
- {
- /*Controllo orizzontale*/
- if(array[i][0]!=' ')
- if(array[i][0]==array[i][1])
- if(array[i][1]==array[i][2]) return array[i][j];
- /*Controllo diagonale*/
- if(array[0][0]!=' ')
- if(array[0][0]==array[1][1])
- if(array[1][1]==array[2][2]) return array[0][0];
- if(array[0][2]!=' ')
- if(array[0][2]==array[1][1])
- if(array[1][1]==array[2][0]) return array[0][2];
- /*Controllo verticale*/
- if(array[0][i]==array[1][i])
- if(array[1][i]==array[2][i]) return array[j][i];
- }
- }
- }
- return null;
- }
- /*Termina la partita*/
- bool stop(char array[size_vet][size_vet], int i, int *punti)
- {
- char c=controllo(array);
- switch(c)
- {
- case 'X':
- cout<<"Giocata finita. Ha vinto: "<<c<<"\n";
- punti[0]++;
- return true;
- case 'O':
- cout<<"Giocata finita. Ha vinto: "<<c<<"\n";
- punti[1]++;
- return true;
- case null:
- return false;
- }
- if(i<10) return false;
- return true;
- }
- void benvenuto(string firm)
- {
- for(int i=0; i!=firm.size(); ++i)
- cout<<" "<<firm[i]<<"\n";
- }
- char riapri()
- {
- int i=0;
- char c;
- cout<<"Vuoi ripetere il gioco? Y/N\n";
- do{
- if(i!=0) cout<<"Errore di input. ";
- cin>>c;
- switch(c)
- {
- case 'y':
- case 'Y':
- return 'Y';
- case 'N':
- case 'n':
- return 'N';
- }
- ++i;
- }while(true);
- }
- void mostra_punti(int *punti)
- {
- cout<<"\nPunti di X: "<<punti[0];
- cout<<"\nPunti di O: "<<punti[1];
- cout<<"\n";
- }
- bool pulisci(int *punti)
- {
- char c=riapri();
- if(c=='N')
- {
- cout<<"\nFine gioco!\n";
- mostra_punti(punti);
- cout<<"\n\n";
- return false;
- }
- else if(c=='Y')
- {
- system("cls");
- return true;
- }
- }
- int main()
- {
- vector<int> pos;
- int punti[2]={0,0};
- string firm;
- firm="By Peppe";
- int i=0;
- char matrix[size_vet][size_vet];
- benvenuto(firm);
- cout<<"benvenuto nel programma di tris!\n";
- do{
- i=0;
- mostra_punti(punti);
- wipe(matrix);
- stampa(matrix);
- do{
- inserisci(matrix, i, pos);
- stampa(matrix);
- ++i;
- }while(!stop(matrix,i,punti));
- pos.clear();
- }while(pulisci(punti));
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement