wiki:examples_from_lect2
#include <stdio.h>

class Model{
private:
        int board[10][10];        
        //?
public:    
        Model() {
                for(int i = 0; i < 10; i++) {
                        for(int j = 0; j < 10; j++) {
                                board[i][j] = -1;
                        }
                }
        }

        bool move(int x, int y, bool isZero) {
                //?
                board[x][y] = isZero ? 0 : 1;
        }    
        
        // Zero, Christ, Draw, Inprogress, 
        int getState() const { 
                //?
                return 0;
        }
        
        int getCell(int x, int y) const {
                //?
                return board[x][y];
        }
        
        
};

class View {
private:
        Model* myModel;
public:
        View(Model* m) {
                myModel = m;
        }
        
        void show() const {
                for(int i = 0; i < 10; i++) {
                        for(int j = 0; j < 10; j++) {
                                printf("%d", myModel->getCell(i, j));
                        }
                        printf("\n");
                }
        }      
        
        void doGame() const {
                int isEnd = 1;
                while ( isEnd != 0 ) {
                        //input from first player
                        //myModel->move(i, j, player);                        
                        //player = !player;
                        show();
                        isEnd = myModel->getState();
                }
        }

};

class TestModel {
public:
        void checkError(bool b, const char* f) {
                if(!b) {
                        printf("Test failed in %s\n", f);
                
                }
        }
        
        void getStateTest() {
                Model m;
                m.move(1, 1, true);
                m.move(2, 2, false);
                
                checkError( m.getState()!= 0, __func__ );
        
        }
};

/*
// test.cpp
int main() {
        TestModel tm;
        tm.getStateTest();
        return 0;
}
*/

int main() {
        Model m;
        View v(&m);
        v.doGame();
        return 0;
}
Last modified 8 years ago Last modified on 01/21/16 23:14:27

Attachments (1)

Download all attachments as: .zip