Skip to content
This repository has been archived by the owner on Jun 18, 2023. It is now read-only.

Commit

Permalink
Add cpplint phase for the CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Ballasi committed Mar 25, 2020
1 parent 8ad3fd8 commit a9465ef
Show file tree
Hide file tree
Showing 39 changed files with 1,116 additions and 1,314 deletions.
6 changes: 3 additions & 3 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ cppcheck:
stage: lint
script: cppcheck --inline-suppr --error-exitcode=1 src

#cpplint:
# stage: lint
# script: cpplint $(find src -type f)
cpplint:
stage: lint
script: cpplint $(find src -type f)

make:
stage: build
Expand Down
2 changes: 1 addition & 1 deletion CPPLINT.cfg
Original file line number Diff line number Diff line change
@@ -1 +1 @@
filter=-legal/copyright,-whitespace/indent,-build/header_guard,-build/include_order,-whitespace/comments
filter=-legal/copyright,-build/header_guard,-whitespace/indent,-whitespace/comments,-build/include_order
69 changes: 22 additions & 47 deletions src/gameoflife/CellMap.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
#include "CellMap.h"
#include "Game.h"
#include <gameoflife/CellMap.h>
#include <gameoflife/Game.h>


CellMap::CellMap(Game* g, size_t w, size_t h)
: game(g), width(w), height(h), length_in_bytes(w * h) {
CellMap::CellMap(Game *g, size_t w, size_t h)
: game(g), width(w), height(h), length_in_bytes(w * h) {
// Adding () calls constructor for every Cell in the array
cells = new Cell[length_in_bytes]();
}


CellMap::CellMap(size_t w, size_t h)
:width(w), height(h), length_in_bytes(w * h) {
: width(w), height(h), length_in_bytes(w * h) {
// Adding () calls constructor for every Cell in the array
cells = new Cell[length_in_bytes]();
}

CellMap::~CellMap() {
delete[] cells;
}
CellMap::~CellMap() { delete[] cells; }

void CellMap::clear(){
for(size_t i = 0; i < length_in_bytes; ++i)
void CellMap::clear() {
for (size_t i = 0; i < length_in_bytes; ++i)
cells[i] = 0;
}

void CellMap::changeCellState(size_t c, size_t l, int toAlive) {
Cell* cell_ptr = cells + (l * width) + c;
Cell *cell_ptr = cells + (l * width) + c;

if (toAlive)
*(cell_ptr) |= LIVING_BIT;
Expand All @@ -41,31 +37,19 @@ void CellMap::nextGeneration() {
updateNeighbourCount();

size_t w = width, h = height;
Cell* cell_ptr;
Cell *cell_ptr;

cell_ptr = cells;
for (size_t l = 0; l < h; ++l)
{
for (size_t c = 0; c < w; ++c)
{
if (*cell_ptr != 0)
{
for (size_t l = 0; l < h; ++l) {
for (size_t c = 0; c < w; ++c) {
if (*cell_ptr != 0) {
size_t neighbours = *cell_ptr >> 1;
if (*cell_ptr & LIVING_BIT)
{
// TODO: adapt the game's rules
if (*cell_ptr & LIVING_BIT) {
if ((neighbours != 2) && (neighbours != 3))
{
changeCellState(c, l, 0);
}
}
else
{
// TODO: adapt the game's rules
} else {
if (neighbours == 3)
{
changeCellState(c, l, 1);
}
}
}
++cell_ptr;
Expand All @@ -74,25 +58,21 @@ void CellMap::nextGeneration() {
}

void CellMap::updateNeighbourCount() {
Cell* cell_ptr;
Cell *cell_ptr;
size_t w = width, h = height;

// we clear previous neighbour calculations
cell_ptr = cells;
for (size_t i = 0; i < length_in_bytes; ++i)
{
for (size_t i = 0; i < length_in_bytes; ++i) {
*(cell_ptr) &= LIVING_BIT;
++cell_ptr;
}

// and we calculate the neighbour count
cell_ptr = cells;
for (size_t l = 0; l < h; ++l)
{
for (size_t c = 0; c < w; ++c)
{
if (*cell_ptr & LIVING_BIT)
{
for (size_t l = 0; l < h; ++l) {
for (size_t c = 0; c < w; ++c) {
if (*cell_ptr & LIVING_BIT) {
if (c > 0 && l > 0)
*(cell_ptr - w - 1) += NEIGHBOR_COUNT_BITS; // top left
if (l > 0)
Expand All @@ -115,11 +95,6 @@ void CellMap::updateNeighbourCount() {
}
}

size_t CellMap::getWidth() { return width; }

size_t CellMap::getWidth() {
return width;
}

size_t CellMap::getHeight() {
return height;
}
size_t CellMap::getHeight() { return height; }
11 changes: 5 additions & 6 deletions src/gameoflife/CellMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <cstdint>
#include <cstring>

#define LIVING_BIT 0b01
#define LIVING_BIT 0b01
#define NEIGHBOR_COUNT_BITS 0b10
typedef uint8_t Cell;

Expand All @@ -23,10 +23,9 @@ class Game;
* This allows us to not use the temp_cells array.
*/

class CellMap
{
class CellMap {
public:
CellMap(Game* g, size_t w, size_t h);
CellMap(Game *g, size_t w, size_t h);
CellMap(size_t w, size_t h);
~CellMap();
void changeCellState(size_t c, size_t l, int toAlive);
Expand All @@ -37,8 +36,8 @@ class CellMap
void clear();

private:
Game* game;
Cell* cells;
Game *game;
Cell *cells;
size_t width;
size_t height;
size_t length_in_bytes;
Expand Down
114 changes: 52 additions & 62 deletions src/gameoflife/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,86 +1,76 @@
#include "Game.h"
#include "CellMap.h"
#include <gameoflife/CellMap.h>
#include <gameoflife/Game.h>

#include <QFile>

Game::Game(std:: size_t width, std::size_t height) : generation(0) {
Game::Game(std::size_t width, std::size_t height) : generation(0) {
map = new CellMap(this, width, height);
}

Game::~Game() { }
Game::~Game() {}

std::size_t Game::getGeneration() {
return generation;
}
std::size_t Game::getGeneration() { return generation; }

CellMap* Game::getMap() {
return map;
}
CellMap *Game::getMap() { return map; }

void Game::nextGeneration() {
++generation;
map->nextGeneration();
}

//TODO: Expand world size if necessary
void Game::loadRLE(QString filename){

void Game::loadRLE(QString filename) {
map->clear();
generation = 0;

QFile file(filename);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
int width = 0;
int height = 0;
QByteArray data("");
while(!file.atEnd())
{
QByteArray line = file.readLine();
if(line[0] == '#'){
continue;
}
if(line[0] == 'x'){
QList<QByteArray> list = line.split(',');
width = ((list[0].split('='))[1].simplified()).toInt();
height = ((list[1].split('='))[1].simplified()).toInt();
continue;
}
data.append(line);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
int width = 0;
int height = 0;
QByteArray data("");
while (!file.atEnd()) {
QByteArray line = file.readLine();
if (line[0] == '#') {
continue;
}
if (line[0] == 'x') {
QList<QByteArray> list = line.split(',');
width = ((list[0].split('='))[1].simplified()).toInt();
height = ((list[1].split('='))[1].simplified()).toInt();
continue;
}
data = data.simplified();
int init_x = map->getWidth()/2 - (int) (width/2);
int init_y = map->getHeight()/2 - (int) (height/2);
data.append(line);
}
data = data.simplified();
int init_x = map->getWidth() / 2 - static_cast<int>(width / 2);
int init_y = map->getHeight() / 2 - static_cast<int>(height / 2);

int curr_x = init_x;
int curr_y = init_y;
int curr_x = init_x;
int curr_y = init_y;

QByteArray qs("");
for(int i = 0; i < data.length(); ++i)
{
if(data[i] == '\0')
continue;
int q;
if(data[i] == '$'){
q = qs.isEmpty() ? 1 : qs.toInt();
curr_y += q;
curr_x = init_x;
qs.clear();
}
if(data[i] >= '0' && data[i] <= '9'){
qs.append(data[i]);
}
if(data[i] == 'o' || data[i] == 'b'){
q = qs.isEmpty() ? 1 : qs.toInt();
for (int n = 0; n < q; n++) {
map->changeCellState(curr_x,curr_y,(data[i] == 'o'));
curr_x++;
}
qs.clear();
QByteArray qs("");
for (int i = 0; i < data.length(); ++i) {
if (data[i] == '\0')
continue;
int q;
if (data[i] == '$') {
q = qs.isEmpty() ? 1 : qs.toInt();
curr_y += q;
curr_x = init_x;
qs.clear();
}
if (data[i] >= '0' && data[i] <= '9') {
qs.append(data[i]);
}
if (data[i] == 'o' || data[i] == 'b') {
q = qs.isEmpty() ? 1 : qs.toInt();
for (int n = 0; n < q; n++) {
map->changeCellState(curr_x, curr_y, (data[i] == 'o'));
curr_x++;
}
if(data[i] == '!')
break;
qs.clear();
}

if (data[i] == '!')
break;
}
}

13 changes: 6 additions & 7 deletions src/gameoflife/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@

#include "CellMap.h"

#include <cstddef>
#include <QString>
#include <cstddef>

class Game
{
class Game {
public:
Game(std:: size_t width, std::size_t height);
Game(std::size_t width, std::size_t height);
~Game();
void nextGeneration();
CellMap* getMap();
std::size_t getGeneration() ;
CellMap *getMap();
std::size_t getGeneration();
void loadRLE(QString filename);

private:
CellMap* map;
CellMap *map;
std::size_t generation;
};

Expand Down
25 changes: 25 additions & 0 deletions src/logic/BigInt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef BIG_INT_HPP
#define BIG_INT_HPP

#include <gmpxx.h>
#include <string>

typedef mpz_class BigInt;

static std::string bigint_to_str(BigInt i) {
std::string str_i = i.get_str();
std::string result;
if (str_i.size() <= 20) {
return str_i;
} else {
result += str_i[0];
result += '.';
result += str_i[1];
result += str_i[2];
result += 'e';
result += std::to_string(str_i.size() - 1);
}
return result;
}

#endif // BIG_INT_HPP
26 changes: 0 additions & 26 deletions src/logic/BigInt.hpp

This file was deleted.

File renamed without changes.
Loading

0 comments on commit a9465ef

Please sign in to comment.