Skip to content

Commit

Permalink
Make GUI reflect game board
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanbir Banipal committed Jul 26, 2022
1 parent d32ca85 commit d8e8ba4
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 58 deletions.
Binary file added 16020_FUTURAM.ttf
Binary file not shown.
18 changes: 9 additions & 9 deletions board.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,14 @@ void Board::promote(Position p, char c){

Board::Board(): grid{}, td{new TextDisplay}, gui{new GUI} {
for(int i = 1; i <= 8; ++i){
grid.push_back({Cell({A,i},nullptr,{td,gui}),
Cell({B,i},nullptr,{td,gui}),
Cell({C,i},nullptr,{td,gui}),
Cell({D,i},nullptr,{td,gui}),
Cell({E,i},nullptr,{td,gui}),
Cell({F,i},nullptr,{td,gui}),
Cell({G,i},nullptr,{td,gui}),
Cell({H,i},nullptr,{td,gui})});
grid.push_back({Cell({A,i},nullptr,td),
Cell({B,i},nullptr,td),
Cell({C,i},nullptr,td),
Cell({D,i},nullptr,td),
Cell({E,i},nullptr,td),
Cell({F,i},nullptr,td),
Cell({G,i},nullptr,td),
Cell({H,i},nullptr,td)});
}
}

Expand All @@ -273,6 +273,6 @@ Board::~Board(){
}

std::ostream& operator<<(std::ostream& out, Board& pb){
pb.gui->update();
pb.gui->update(pb.td);
return out << *pb.td;
}
6 changes: 2 additions & 4 deletions cell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Cell::Cell(Position Pos, std::shared_ptr<ChessPiece> a) : ob{}, obj{a},
void Cell::notifyObservers(){
Cell temp{getCoords(), obj};
ob[0]->notify(temp);
ob[1]->notify(temp);
}

void Cell::attach(Observer* obs){
Expand Down Expand Up @@ -68,9 +67,8 @@ void Cell::undo(){
notifyObservers();
}

Cell::Cell(Position Pos, std::shared_ptr<ChessPiece> p, Observe a): ob{}, obj{p}, Pos{Pos} {
attach(a.first);
attach(a.second);
Cell::Cell(Position Pos, std::shared_ptr<ChessPiece> p, Observer* a): ob{}, obj{p}, Pos{Pos} {
attach(a);
notifyObservers();
}

Expand Down
2 changes: 1 addition & 1 deletion cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Cell{
void movent();
void test();
void undo();
Cell(Position Pos, std::shared_ptr<ChessPiece> ,Observe a); //ctor
Cell(Position Pos, std::shared_ptr<ChessPiece> ,Observer* a); //ctor
~Cell(); //dtor
};

Expand Down
88 changes: 51 additions & 37 deletions gui.cc
Original file line number Diff line number Diff line change
@@ -1,54 +1,60 @@
#include "gui.h"
#include "cell.h"
#include <iostream>
#include <string>

#include "gui.h"
#include "cell.h"
#include "textdisplay.h"

#include <chrono>
#include <thread>

void GUI::notify(Cell&c){
if(updates == 0){
drawBoard();
}
void GUI::drawPiece(char type, int j, int i){
updates++;
int square = dispsize/9;
int start = square/2;
int i = (int) c.getCoords().first;
--i;
int j = c.getCoords().second;
--j;
int locx = start+(square*i);
int locy = start+(square*j);
Colour outcol = {(char)0, (char)0, (char)0};
if((i+j)%2 == 0){outcol = light;}
else{outcol = dark;}
s.draw_rect(locx, locy, square, square, outcol);
if(c.getType().first != PieceType::Empty){
switch(c.getType().first){
case PieceType::Pawn:
s.draw_img("Pawn", locx, locy);
break;
case PieceType::Rook:
s.draw_img("Rook", locx, locy);
break;
case PieceType::Knight:
s.draw_img("Knight", locx, locy);
break;
case PieceType::Bishop:
s.draw_img("Bishop", locx, locy);
break;
case PieceType::Queen:
s.draw_img("Queen", locx, locy);
break;
case PieceType::King:
s.draw_img("King", locx, locy);
break;
}
if(c.getType().second == true){outcol = {(char)255,(char)255,(char)255};}
else{outcol = {(char)0, (char)0, (char)0};}
switch(type){
case 'P':
s.draw_img("Pawn", locx, locy);
break;
case 'p':
s.draw_img("Pawn", locx, locy);
break;
case 'R':
s.draw_img("Rook", locx, locy);
break;
case 'r':
s.draw_img("Rook", locx, locy);
break;
case 'N':
s.draw_img("Knight", locx, locy);
break;
case 'n':
s.draw_img("Knight", locx, locy);
break;
case 'B':
s.draw_img("Bishop", locx, locy);
break;
case 'b':
s.draw_img("Bishop", locx, locy);
break;
case 'Q':
s.draw_img("Queen", locx, locy);
break;
case 'q':
s.draw_img("Queen", locx, locy);
break;
case 'K':
s.draw_img("King", locx, locy);
break;
case 'k':
s.draw_img("King", locx, locy);
break;
}
//std::this_thread::sleep_for(std::chrono::milliseconds(500));
}

void GUI::drawBoard(){
Expand All @@ -66,8 +72,16 @@ void GUI::drawBoard(){
}


void GUI::update(){
void GUI::update(TextDisplay* disp){
updates = 0;
drawBoard();
int square = dispsize/9;
int start = square/2;
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
drawPiece(disp->atPos(i, j), i, j);
}
}
s.update();
}

Expand All @@ -79,7 +93,7 @@ GUI::GUI(): s{Screen(dispsize,dispsize,"Chess")}, updates{0}{
s.add_img("Queen", "images/queen.png");
s.add_img("King", "images/king.png");
drawBoard();
s.update();
//255,228,181
};

GUI::~GUI(){}
11 changes: 6 additions & 5 deletions gui.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#ifndef GUI_H_
#define GUI_H_
#include "sdl_wrap.h"
#include "observer.h"

class GUI : public Observer{
class TextDisplay;

class GUI {
const Colour light = {(char)255,(char)228,(char)181};
const Colour dark = {(char)131, (char)42, (char)13};
const int dispsize = 702;
Screen s;
void drawBoard();
int updates;
public:
void notify(Cell&) override;
void update();
void drawPiece(char, int, int);
void update(TextDisplay*);
GUI();
virtual ~GUI(){};
virtual ~GUI();
};

#endif
2 changes: 1 addition & 1 deletion sdl_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void Screen::draw_img(std::string key, int x, int y) {
}

void Screen::update() {
//SDL_RenderClear(render);
SDL_RenderClear(render);
for (auto &r : rects) {
SDL_SetRenderDrawColor(render, r.c.r, r.c.g, r.c.b, SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(render, &r.rect);
Expand Down
4 changes: 4 additions & 0 deletions textdisplay.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ void TextDisplay::notify(Cell& c){
display[8-temp.second][((int)temp.first)-1] = intoDisp;
}

char TextDisplay::atPos(int i, int j){
return display.at(i).at(j);
}

std::ostream &operator<<(std::ostream& out, const TextDisplay& ptd){
int k = 8;
for(auto i: ptd.display){
Expand Down
1 change: 1 addition & 0 deletions textdisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TextDisplay : public Observer{
public:
void notify(Cell&) override;
friend std::ostream& operator<<(std::ostream&, const TextDisplay&);
char atPos(int, int);
TextDisplay();
virtual ~TextDisplay(){};
};
Expand Down
1 change: 0 additions & 1 deletion types.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ typedef std::pair<PieceType, bool> Piece;
typedef std::pair<locationx, int> Position;
typedef std::pair<Position, Position> ChessMove;
typedef std::pair<ChessMove, Piece> MoveInfo;
typedef std::pair<Observer*, Observer*> Observe;


struct PossibleMoves{
Expand Down

0 comments on commit d8e8ba4

Please sign in to comment.