Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define N 5
- using namespace std;
- void inizio();
- void draw (char matrice[][N]);
- void logic (char matrice[][N], int casella, char variabile, int contatore);
- int controllo (char matrice[][N]);
- int main() {
- char matrice[N][N];
- char variabile;
- int contatore=0;
- int casella;
- inizio();
- draw(matrice);
- logic(matrice,casella,variabile,contatore);
- }
- void inizio(){
- cout<<"Ecco la tabella di gioco:\n";
- cout<<"1|2|3\n";
- cout<<"-+-+-\n";
- cout<<"4|5|6\n";
- cout<<"-+-+-\n";
- cout<<"7|8|9\n";
- cout<<"premi invio";
- getchar();
- }
- void draw(char matrice[][N]){
- system("cls");
- matrice[0][1]='|';
- matrice[0][3]='|';
- matrice[1][0]='-';
- matrice[1][1]='+';
- matrice[1][2]='-';
- matrice[1][3]='+';
- matrice[1][4]='-';
- matrice[2][1]='|';
- matrice[2][3]='|';
- matrice[3][0]='-';
- matrice[3][1]='+';
- matrice[3][2]='-';
- matrice[3][3]='+';
- matrice[3][4]='-';
- matrice[4][1]='|';
- matrice[4][3]='|';
- for(int i=0;i<N;i++){
- for(int j=0;j<N;j++){
- cout<<matrice[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- void logic(char matrice[][N], int casella, char variabile, int contatore) {
- int gameover=0;
- while(gameover==0) {
- cout<<"Inserisci la casella\n";
- cin>>casella;
- if(contatore%2==0){
- variabile='x';
- }
- else
- variabile='o';
- switch(casella){
- case 1:
- matrice[0][0]=variabile;
- break;
- case 2:
- matrice[0][2]=variabile;
- break;
- case 3:
- matrice[0][4]=variabile;
- break;
- case 4:
- matrice[2][0]=variabile;
- break;
- case 5:
- matrice[2][2]=variabile;
- break;
- case 6:
- matrice[2][4]=variabile;
- break;
- case 7:
- matrice[4][0]=variabile;
- break;
- case 8:
- matrice[4][2]=variabile;
- break;
- case 9:
- matrice[4][4]=variabile;
- break;
- }
- contatore++;
- draw(matrice);
- if(contatore>4) {
- gameover=controllo(matrice);
- logic(matrice,casella,variabile,contatore);
- }
- }
- }
- int controllo (char matrice[][N]) {
- int j=0;
- int fine=0;
- for(int i=0;i<N;i+2) {
- if((matrice[i][j]==matrice[i][j+2])&&(matrice[i][j]==matrice[i][j+4])) {
- fine=1;
- cout<<"\nIL VINCITORE E' IL GIOCATORE "<<matrice[i][j]<<"!\n";
- }
- else if((matrice[j][i]==matrice[j+2][i])&&(matrice[j][i]==matrice[j+4][i])) {
- fine=1;
- cout<<"\nIL VINCITORE E' IL GIOCATORE "<<matrice[j][i]<<"!\n";
- }
- }
- return fine;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement