@@ -7,33 +7,26 @@ class TextDisplay;
77class Move ;
88
99void Grid::init (int n){ // Randomly initializes the board
10- // td = new TextDisplay(n);
10+ td = new TextDisplay (n);
1111 // sets the new grid
1212 size = n;
1313 int green = 0 ;
1414 int red = 0 ;
15+
1516 for (int i = 0 ; i < n; i ++){
1617 vector<Cell> A;
1718 theGrid.push_back (A);
1819 for (int j = 0 ; j < n; j++) {
19- Cell B{1 ,2 };
20- int randy = rand ();
21- if (randy % 2 == 0 && green < 14 ) {
22- B.setColour (Colour::GREEN);
23- green++;
24- }
25- if (randy % 2 == 1 && red < 14 ){
26- B.setColour (Colour::RED);
27- red++;
28- }
20+ Cell B{i,j};
2921 theGrid[i].push_back (B);
3022 }
3123 }
3224
3325 // sets observers
3426 for (int r = 0 ; r < n; r++){
3527 for (int c = 0 ; c < n; c++) {
36- // theGrid[r][c].attach(td);
28+ theGrid[r][c].attach (td);
29+ theGrid[r][c].attach (&theGrid[r][c]);
3730 if (r-1 >= 0 )
3831 theGrid[r][c].attach (&theGrid[r-1 ][c]);
3932 if (c-1 >= 0 && r-1 >= 0 )
@@ -52,39 +45,85 @@ void Grid::init(int n){ //Randomly initializes the board
5245 theGrid[r][c].attach (&theGrid[r][c-1 ]);
5346 }
5447 }
55-
56- }
57- bool Grid::winner () {
58- return true ;
59- }
60- std::string Grid::Display (){
61- stringstream ss;
62- std::string display =" Test" ;
63- int n = size;
6448 for (int i = 0 ; i < n; i ++) {
6549 for (int j = 0 ; j < n; j++) {
66- Colour colours = theGrid[i][j].getColour ();
67- if (colours == Colour::GREEN) {
68- ss << " G" ;
69- }
70- else if (colours == Colour::RED) {
71- ss << " R" ;
50+ int randy = rand ();
51+ srand (randy);
52+ if (randy % 2 == 1 ) {
53+ theGrid[i][j].setColour (Colour::RED);
54+ red++;
55+ }
56+ else {
57+ theGrid[i][j].setColour (Colour::GREEN);
58+ green++;
7259 }
73- else
74- ss << " - " ;
60+ Move m{ 0 };
61+ theGrid[i][j]. notifyObservers (m) ;
7562 }
7663 }
77- ss >> display;
78- return display;
64+ }
65+ bool Grid::winner () {
66+ return true ;
67+ }
68+ std::string Grid::Display (){
69+ return td->UpdateDisplay ();
7970}
8071
8172void Grid::use (int row, int col, int p) {
8273 Move m{p};
83- theGrid[row][col].setDir (Direction::None );
74+ theGrid[row][col].setDir (Direction::C );
8475 theGrid[row][col].notifyObservers (m);
8576}
8677
8778std::ostream& operator <<(std::ostream &out, const Grid &g) {
8879 // out << *(g.td);
8980 return out;
81+ }
82+ // Constructs a blank text display
83+ TextDisplay::TextDisplay (int n) : gridSize{n} {
84+ for (int i = 0 ; i < n; i ++){
85+ vector<char > A;
86+ theDisplay.push_back (A);
87+ for (int j = 0 ; j < n; j++) {
88+ theDisplay[i].push_back (' -' );
89+ }
90+ }
91+ }
92+
93+ // Gets Notified of a change on the board
94+ void TextDisplay::notify (Move m, Cell &from) {
95+ const int r = from.getRow ();
96+ const int c = from.getCol ();
97+ Colour colours = from.getColour ();
98+ if (colours == Colour::GREEN) {
99+ theDisplay[r][c] = ' G' ;
100+ }
101+ if (colours == Colour::RED) {
102+ theDisplay[r][c] = ' R' ;
103+ }
104+ }
105+ std::string TextDisplay::UpdateDisplay () {
106+ display = " " ;
107+ stringstream sss;
108+ const int size = gridSize;
109+ for (int i = 0 ; i < size; i++) {
110+ for (int j = 0 ; j < size; j++) {
111+ sss << theDisplay[i][j];
112+ }
113+
114+ }
115+ sss >> display;
116+ return display;
117+ }
118+
119+ // Output display to the screen - friend of grids display
120+ std::ostream& operator <<(std::ostream &out, const TextDisplay &td) {
121+ const int size = td.gridSize ;
122+ for (int i = 0 ; i < size; i++) {
123+ for (int j = 0 ; j < size; j++) {
124+ out << td.theDisplay [i][j];
125+ }
126+ out << endl;
127+ }
128+ return out;
90129}
0 commit comments