Skip to content

Commit c36f238

Browse files
committed
Changes
1 parent 3111ae5 commit c36f238

File tree

10 files changed

+231
-8
lines changed

10 files changed

+231
-8
lines changed

Cell.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "Cell.h"
2+
#include "Observer.h"
3+
4+
5+
Cell::Cell(int r, int c) : row{r}, col{c} {}//Constructor
6+
void Cell::notify(move m, Cell &from){ //Notify surrounding peices
7+
//int R = this->row - from.getRow();
8+
//int C = this->row - from.getCol();
9+
//Direction D;
10+
this->toggle();
11+
//Use this when you want to add more peices
12+
/*
13+
if (R == -1 && C == -1)
14+
D = Direction::NE
15+
else if (R == -1 && C == 0)
16+
D = Direction::E
17+
else if (R == -1 && C == 1)
18+
D = Direction::SE
19+
else if (R == 1 && C == -1)
20+
D = Direction::NW
21+
else if (R == 1 && C == 0)
22+
D = Direction::W
23+
else if (R == 1 && C == 1)
24+
D = Direction::SW
25+
else if (R == 0 && C == 1)
26+
D = Direction::S
27+
else if (R == 0 && C == -1)
28+
D = Direction::N
29+
30+
if (m.valid(D))
31+
this->toggle();
32+
33+
this->notify(m,*this);
34+
*/
35+
}
36+
37+
38+
void Cell::attach(observer &o){ //Add an Observer
39+
ob.emplaceback(o);
40+
}
41+
42+
Cell::int getRow() {return row;}
43+
Cell::int getCol() {return col;}
44+
void Cell::toggle() {color == Colour::RED ? colour = Colour::RED : colour = Colour::GREEN}

Cell.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
#ifndef cell_H
2-
#define Cell_H
1+
#ifndef Cell_H
2+
#define Cell_H
3+
#include <vector>
4+
5+
class Cell: public Observer {
6+
int row,col;
7+
Colour colour;
8+
vector<* Observers> ob
9+
public:
10+
Cell(int r, int c); //Constructor
11+
void notify(move m, Cell &from); //Notify surrounding peices
12+
void attach(observer *o); //Add an Observer
13+
int getRow();
14+
int getCol();
15+
void toggle();
16+
}
17+
18+
#endif

Grid.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "Grid.h"
2+
3+
void Grid::init(int size){ //Randomly initializes the board
4+
td = new TextDisplay(n);
5+
//sets the new grid
6+
for(size_t i = 0; i < n; i ++){
7+
vector<Cell> A;
8+
theGrid.push_back(A);
9+
for(size_t j = 0; j < n; j++) {
10+
theGrid[i].push_back(Cell{i,j});
11+
}
12+
}
13+
14+
//sets observers
15+
for(int r = 0; r < n; r++){
16+
for(int c = 0; c < n; c++) {
17+
theGrid[r][c].attach(td);
18+
if (r-1 > 0)
19+
theGrid[r][c].attach(&theGrid[r-1][c]);
20+
if (c-1 > 0 && r-1 > 0)
21+
theGrid[r][c].attach(&theGrid[r-1][c-1]);
22+
if (r+1 < n && c-1 > 0)
23+
theGrid[r][c].attach(&theGrid[r+1][c-1]);
24+
if (r+1 < n)
25+
theGrid[r][c].attach(&theGrid[r+1][c]);
26+
if (r+1 < n && c+1 < n)
27+
theGrid[r][c].attach(&theGrid[r+1][c+1]);
28+
if (r-1 > 0 && c+1 < n)
29+
theGrid[r][c].attach(&theGrid[r-1][c+1]);
30+
if (c+1 < n)
31+
theGrid[r][c].attach(&theGrid[r][c+1]);
32+
if (c-1 > 0)
33+
theGrid[r][c].attach(&theGrid[r][c-1]);
34+
35+
}
36+
}
37+
38+
}
39+
void Grid::winner() {
40+
return true;
41+
}
42+
43+
void Grid::use(int row, int col) {
44+
theGrid[row][col].notify();
45+
}
46+
47+
std::ostream& operator<<(std::ostream &out, const Grid &g) {
48+
out << *(g.td);
49+
return out;
50+
}

Grid.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef GRID_H
2+
#define GRID_H
3+
#include "Observer.h"
4+
#include <vector>
5+
6+
class Grid {
7+
vector<vector<Cell>> theGrid;
8+
TextDisplay *td = nullptr; // The text display.
9+
int size;
10+
public:
11+
void init(int size); //Randomly initializes the board
12+
void winner();
13+
void use(int row, int col);
14+
friend std::ostream &operator<<(std::ostream &out, const Grid &g);
15+
}
16+
#endif

Misc.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "Misc.h"
2+
3+
bool move::valid(Direction D) {
4+
return true;
5+
}

Misc.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef Misc_H
2+
#define Misc_H
3+
#include <map>
4+
5+
class move {
6+
std::map <Direction, int> move;
7+
public
8+
bool valid(Direction D);
9+
}
10+
11+
#endif

Observer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#ifndef Observer_H
22
#define Observer_H
33

4+
//These are the enums defined for the game
5+
const enum Colour {RED, GREEN, BLUE, NONE}
6+
const enum Activity {ALL, NONE}
7+
const enum Direction {N,NE,E,SE,S,SW,W,NW}
8+
49
class Observer {
510
public:
6-
virtual void notify() = 0;
11+
virtual void notify(move m, Cell &from) = 0;
712
~Observer() = default;
813
};
914

main.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
//This is the main file
2+
#include "grid.h"
3+
#include "cell.h"
4+
#include <string>
5+
#include "textdisplay.h"
26

37

48

59
int main() {
10+
Grid g;
611

7-
8-
9-
10-
11-
12+
while (true) {
13+
cin >> cmd;
14+
if (cmd == "new") {
15+
int size;
16+
cin >> size;
17+
g.init(size);
18+
}
19+
if (cmd == "play"){
20+
int x,y;
21+
cin >> x >> y;
22+
g.use(x,y);
23+
}
24+
}
1225
}

textdisplay.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "textdisplay.h"
2+
#include "state.h"
3+
#include <iostream>
4+
#include <sstream>
5+
6+
using namespace std;
7+
8+
9+
//Constructs a blank text display
10+
TextDisplay::TextDisplay(int n) : gridSize{n} {
11+
for(int i = 0; i < n; i ++){
12+
vector<char> A;
13+
theDisplay.push_back(A);
14+
for(int j = 0; j < n; j++) {
15+
theDisplay[i].push_back('-');
16+
}
17+
}
18+
}
19+
20+
//Gets Notified of a change on the board
21+
void TextDisplay::notify(move m, Cell &from) {
22+
const int r = from.getRow();
23+
const int c = from.getCol();
24+
Colour colours = whoNotified.getInfo().colour;
25+
if (colours == Colour::Black)
26+
theDisplay[r][c] = 'B';
27+
if (colours == Colour::White)
28+
theDisplay[r][c] = 'W';
29+
}
30+
31+
32+
//Output display to the screen - friend of grids display
33+
std::ostream& operator<<(std::ostream &out, const TextDisplay &td) {
34+
const int size = td.gridSize;
35+
for (int i = 0; i < size; i++) {
36+
for(int j = 0; j < size; j++) {
37+
out << td.theDisplay[i][j];
38+
}
39+
out << endl;
40+
}
41+
return out;
42+
}
43+

textdisplay.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef TEXTDISPLAY_H
2+
#define TEXTDISPLAY_H
3+
#include <iostream>
4+
#include <vector>
5+
#include "observer.h"
6+
7+
class Cell;
8+
9+
class TextDisplay: public Observer {
10+
std::vector<std::vector<char>> theDisplay;
11+
const int gridSize;
12+
public:
13+
TextDisplay(int n);
14+
15+
void notify(move m, Cell &from) override;
16+
17+
friend std::ostream &operator<<(std::ostream &out, const TextDisplay &td);
18+
};
19+
#endif
20+

0 commit comments

Comments
 (0)