mardi 11 janvier 2022

Minesweeper like C++ code that needs to be refactored using patterns and according to programing principles

this is one of the final assignments I have for this semester, the code was written by someone (not sure if it was my teacher). I need to refactor it using classes, object-oriented principles and design patterns. All I can really ask is which patterns would you apply and how, because outside of the facade pattern, I don't have many ideas for this assignment as it looks like a mess, all help is welcome. It's a game like minesweeper, made with many loops and if's, also the teacher said we should use at least one pattern, I don't see many behavioral patterns be of use as well.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int maxArraySize = 10;
const int a = 0;
const int b = 9;

int main () {
    char array[maxArraySize][maxArraySize];
    for (int i=0; i < maxArraySize; i++) {
        for (int j=0; j< maxArraySize; j++) {
            array[i][j] = ' ';
        }
    }
    srand(time(0));
    //-----------------------------input
    for (int i=0; i < maxArraySize; i++) {
        bool ok=false;
        do {
            int x = rand()%(b-a+1)+a;
            int y = rand()%(b-a+1)+a;
            if (array[x][y]==' ') {
                array[x][y] = '*';
                ok = true;
            }
        } while (!ok);
    }
    //-----------------------------------
    int foundTerrorists = 0;
    int moves = 0;
    while (moves < 2*maxArraySize && foundTerrorists < maxArraySize ) {
        //------------------------------show only found boms & tryes
        system ("cls");
        cout << "Catch the terrorists!"<<endl;
        cout << "Check neighbourhood. Listen for rummors about terrorists."<<endl;
        cout << endl;
        cout << " |0123456789"<<endl;
        cout << "-+----------"<<endl;
        for (int i=0; i < maxArraySize; i++) {
            cout << i << "|";
            for (int j=0; j < maxArraySize; j++) {
                if (array[i][j] == '*') cout << " ";
                else {
                    if (array[i][j] == '.') {
                        int neighbours = 0;
                        if (i-1>=0 && j-1 >=0 && array[i-1][j-1]=='*') neighbours++;
                        if (i+1<maxArraySize && j+1 <maxArraySize && array[i+1][j+1]=='*') neighbours++;
                        if (i-1>=0 && j+1 <maxArraySize && array[i-1][j+1]=='*') neighbours++;
                        if (i+1<maxArraySize && j-1 >=0 && array[i+1][j-1]=='*') neighbours++;
                        if (i-1>=0 && array[i-1][j]=='*') neighbours++;
                        if (i+1<maxArraySize && array[i+1][j]=='*') neighbours++;
                        if (j-1>=0 && array[i][j-1]=='*') neighbours++;
                        if (j+1<maxArraySize && array[i][j+1]=='*') neighbours++;
                        cout << neighbours;
                    }
                    else cout << array[i][j];
                } 
                    
            }
            cout << endl;
        }
        cout << endl;   
        //------------------------------------
        int x, y;
        cout << "You have made " << moves << " moves. Remaining " << 2*maxArraySize-moves << endl;
        do {
            cout << "Your move (x,y)"<<endl;
            cin >> x >> y;
        } while ((x<0 || x>=maxArraySize)||(array[x][y]=='.')||(array[x][y]=='X'));
        moves++;
        if (array[x][y]=='*') {
            array[x][y]='X';
            foundTerrorists++;
        } else {
            array[x][y]='.';
        }
    }
    //------------------------------output
    system ("cls");
    cout << " |0123456789"<<endl;
    cout << "-+----------"<<endl;
    for (int i=0; i< maxArraySize; i++) {
        cout << i << "|";
        for (int j=0; j< maxArraySize; j++) {
            cout << array[i][j];
        }
        cout << endl;
    }
    cout << endl;   
    //------------------------------------
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire