biquadris / textdisplay.cc
textdisplay.cc
Raw
#include "textdisplay.h"
#include <vector>
#include <map>
#include <iostream>
#include <iomanip>
#include <string>
#include "shapes.h"
using namespace std;

const int SPACES_BETWEEN_BOARDS = 20;

const int NUM_BOARDS = 2;

TextDisplay::TextDisplay(int width, int height) :
	BiquadrisDisplay{NUM_BOARDS, width, height}, hint{} {

	// Creates empty board
	vector<vector<char>> empty;
	for (int i = 0; i < height; ++i) {
		vector<char> currRow;
		for (int j = 0; j < width; ++j) {
			currRow.emplace_back(' ');
		}
		empty.push_back(currRow);
	}

	for (int i = 0; i < amount; ++i) {
		boards.push_back(empty);
		scores.emplace_back(0);
		highScores.emplace_back(0);
		levels.emplace_back(0);
		nextBlocks.emplace_back('-');
	}
}

void TextDisplay::update(int _highScore) { highScore = _highScore; }

void TextDisplay::update(int boardId, int score) {
	scores[boardId] = score;
}

void TextDisplay::update(int boardId, int level, char nextBlock) {
	levels[boardId] = level;
	nextBlocks[boardId] = nextBlock;
}

void TextDisplay::update(int boardId, int row, int col, char type) {
	boards[boardId][row][col] = type;
}

void TextDisplay::setHint(string _hint) { hint = _hint; }

void TextDisplay::printChar(ostream &out, char c, int amount) {
	for (int i = 0; i < amount; ++i) { out << c; }
}

void TextDisplay::printShape(ostream &out, Shapes &s, int row, int width) {
	vector<vector<bool>> &binaries = s.getBinary();
	vector<bool> thePrinted = binaries[row];
	char fill = s.getType();
	for (auto c : thePrinted) {
		if (c) out << fill;
		else out << ' ';
	}
	printChar(out, ' ', width - thePrinted.size());
}

int TextDisplay::padding(string &str, int width) {
	return width - str.length();
}

void TextDisplay::print(ostream &out) {
	string text = "";
	
	// Outputs header
	printChar(out, '-', 2 * width + SPACES_BETWEEN_BOARDS);
	out << endl;
	vector<string> texts = {"Level:", "Score:"};
	printChar(out, '-', 2 * width + SPACES_BETWEEN_BOARDS);
	out << endl;
	for (auto t : texts) {
		for (int i = 0; i < amount; ++i) {
			out << t << setw(padding(t, width));

			if (t == "Level:") {
				out << levels[i];
				if (i != amount - 1) printChar(out, ' ', SPACES_BETWEEN_BOARDS);
				else out << endl;
			} else {
				out << scores[i];
				if (i != amount - 1)
					out << "  High Score:" << setw(5) << highScore << "  ";
				else out << endl;
			}
		}
	}
	printChar(out, '-', 2 * width + SPACES_BETWEEN_BOARDS);
	out << endl;

	// Outputs board
	for (int h = 0; h < height; ++h) {
		for (int i = 0; i < amount; ++i) {
			for (int w = 0; w < width; ++w) {
					out << boards[i][h][w];
			}
			if (i != amount - 1) {
				out << '|';
				printChar(out, ' ', SPACES_BETWEEN_BOARDS - 2);
				out << '|';
			}
			else out << endl;
		}
	}
	printChar(out, '-', 2 * width + SPACES_BETWEEN_BOARDS);
	out << endl;
	
	// Outputs footer
	for (int i = 0; i < amount; ++i) {
		text = "Next:";
		out << text;
		printChar(out, ' ', padding(text, width));
		if (i != amount - 1) printChar(out, ' ', SPACES_BETWEEN_BOARDS);
		else out << endl;
	}
	
	// Outputs next block shape
	int maxHeight = 0;
	for (int i = 0; i < amount; ++i) {
		char currChar = nextBlocks[i];
		Shapes currShape = Shapes(currChar);
		int currHeight = currShape.getHeight(0);
		if (currHeight > maxHeight) maxHeight = currHeight;
	}

	for (int i = maxHeight; i > 0; --i) {
		for (int j = 0; j < amount; ++j) {
			char currChar = nextBlocks[j];
			Shapes currShape = Shapes(currChar);
			int currHeight = currShape.getHeight(0);
			if (currHeight >= i)
				printShape(out, currShape, currHeight - i, width);
			else printChar(out, ' ', width);

			if (j != amount - j) printChar(out, ' ', SPACES_BETWEEN_BOARDS);
			else out << endl;
		}
	}

	// Outputs hint
	out << hint << endl;
	hint = "";
}

bool TextDisplay::printable() { return true; }

void TextDisplay::playerLoses() {
	cout << "Player has lost" << endl;
}