Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion database/testapp/src/common_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <algorithm>
#include <ctime>

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're not really touching this file, can you just revert this change completely?

#include "firebase/app.h"
#include "firebase/auth.h"
#include "firebase/database.h"
Expand Down Expand Up @@ -1050,4 +1051,4 @@ extern "C" int common_main(int argc, const char* argv[]) {
}

return 0;
}
}
31 changes: 31 additions & 0 deletions demos/TicTacToe/Classes/AppDelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "AppDelegate.h"

#include "TicTacToeScene.h"

USING_NS_CC;

extern const float kFrameWidth = 600;
extern const float kFrameHeight = 600;

AppDelegate::AppDelegate() {}

AppDelegate::~AppDelegate() {}

bool AppDelegate::applicationDidFinishLaunching() {
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if (!glview) {
glview = GLViewImpl::create("Tic-Tac-Toe");
glview->setFrameSize(kFrameWidth, kFrameHeight);
director->setOpenGLView(glview);
}

auto scene = TicTacToe::createScene();
director->runWithScene(scene);

return true;
}

void AppDelegate::applicationDidEnterBackground() {}

void AppDelegate::applicationWillEnterForeground() {}
13 changes: 13 additions & 0 deletions demos/TicTacToe/Classes/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#include "cocos2d.h"

class AppDelegate : private cocos2d::Application {
public:
AppDelegate();
virtual ~AppDelegate();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of virtual, use override.

https://en.cppreference.com/w/cpp/language/override


virtual bool applicationDidFinishLaunching();
virtual void applicationDidEnterBackground();
virtual void applicationWillEnterForeground();
};
81 changes: 81 additions & 0 deletions demos/TicTacToe/Classes/TicTacToeScene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "TicTacToeScene.h"

#include <array>
#include <cstdlib>
#include <iostream>

#include "cocos2d.h"

USING_NS_CC;

const int kNumberOfTiles = 9;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make all of these static.

const int kMaxMovesPerPlayer = 1 + kNumberOfTiles / 2;
const int kTilesX = std::sqrt(kNumberOfTiles);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do this the other way:

kTilesX = 3
kTilesY = 3
kNumberOfTiles = kTilesX * kTilesY;

const int kTilesY = std::sqrt(kNumberOfTiles);
const double kScreenWidth = 600;
const double kScreenHeight = 600;
const double kTileWidth = (kScreenWidth / kTilesX);
const double kTileHeight = (kScreenHeight / kTilesY);
const int kNumberOfPlayers = 2;
std::array<std::string, kNumberOfPlayers> kMoveImageFileNames = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't have std::strings as global variables according to the C++ style guide.

https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables

strings do dynamic allocation and then delete on destruction, which is against the style guide rules.

Instead either use const char* here, or make it a string* and initialize the value at startup.

"tic_tac_toe_x.png", "tic_tac_toe_o.png"};

Scene* TicTacToe::createScene() {
auto scene = Scene::create();
auto layer = TicTacToe::create();

scene->addChild(layer);

return scene;
}

bool TicTacToe::init() {
if (!Layer::init()) {
return false;
}

auto file_names_it = std::begin(kMoveImageFileNames);

// TODO(grantpostma): This should reflect the size that is set in AppDelegate.
// Should modify kTileWidth and kTileHeight based on that size auto
// kScreenWidth = Director::getInstance()->getWinSize().width; auto
// kScreenHeight = Director::getInstance()->getWinSize().height;

auto board_sprite = Sprite::create("tic_tac_toe_board.png");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put "tic_tac_toe_board.png" in a constant

board_sprite->setPosition(0, 0);
board_sprite->setAnchorPoint(Vec2(0.0, 0.0));

auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = [=](Touch* touch,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://google.github.io/styleguide/cppguide.html#Lambda_expressions

Prefer explicit captures when the lambda will escape the current scope.

Only capture the variables you need in the lambda.

Event* event) mutable -> bool {
auto bounds = event->getCurrentTarget()->getBoundingBox();

if (bounds.containsPoint(touch->getLocation())) {
// This takes the touch location and calculates which tile number [0-8]
// corresponds to that location
int move_tile = floor(touch->getLocation().x / kTileWidth) +
kTilesX * floor(touch->getLocation().y / kTileHeight);

auto sprite = Sprite::create(*file_names_it);
// This calculates and sets the position of the sprite based on the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Calculate and set..."

// move_tile and the constant screen variables.
sprite->setPosition((.5 + move_tile % kTilesX) * kTileWidth,
(.5 + move_tile / kTilesY) * kTileHeight);
board_sprite->addChild(sprite);

file_names_it++;
if (file_names_it == std::end(kMoveImageFileNames)) {
file_names_it = std::begin(kMoveImageFileNames);
}
}
return true;
};

Director::getInstance()
->getEventDispatcher()
->addEventListenerWithSceneGraphPriority(touchListener, board_sprite);

this->addChild(board_sprite);

return true;
}
14 changes: 14 additions & 0 deletions demos/TicTacToe/Classes/TicTacToeScene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#include <array>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need any of these in the header. None of them are needed for what is exposed in this header.

#include <cstdlib>
#include <iostream>

#include "cocos2d.h"

class TicTacToe : public cocos2d::Layer {
public:
static cocos2d::Scene* createScene();
virtual bool init();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

override

CREATE_FUNC(TicTacToe);
};
Binary file added demos/TicTacToe/Resources/fonts/Marker Felt.ttf
Binary file not shown.
Binary file added demos/TicTacToe/Resources/fonts/arial.ttf
Binary file not shown.
Empty file.
Binary file added demos/TicTacToe/Resources/tic_tac_toe_board.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/TicTacToe/Resources/tic_tac_toe_o.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demos/TicTacToe/Resources/tic_tac_toe_x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.