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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,065 changes: 501 additions & 564 deletions demos/TicTacToe/Classes/MainMenuScene.cpp

Large diffs are not rendered by default.

15 changes: 5 additions & 10 deletions demos/TicTacToe/Classes/MainMenuScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,14 @@
#ifndef TICTACTOE_DEMO_CLASSES_MAINMENU_SCENE_H_
#define TICTACTOE_DEMO_CLASSES_MAINMENU_SCENE_H_

#include "cocos2d.h"
#include <string.h>

#include "firebase/auth.h"
#include "firebase/database.h"
#include "firebase/future.h"
#include "firebase/util.h"
#include "ui/CocosGUI.h"
using std::to_string;
#include "firebase/database.h"
#include "cocos2d.h"

// TODO(grantpostma): Create a common util.h & util.cpp file.
void LogMessage(const char*, ...);
void ProcessEvents(int);
void WaitForCompletion(const firebase::FutureBase&, const char*);
std::string GenerateUid(std::size_t);
using std::to_string;

class MainMenuScene : public cocos2d::Layer, public cocos2d::TextFieldDelegate {
public:
Expand Down
4 changes: 3 additions & 1 deletion demos/TicTacToe/Classes/TicTacToeLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ class SampleChildListener : public firebase::database::ChildListener {
}

public:
// Vector of strings that define the events we saw, in order.
// Vector of strings that contains the events in the order in which they
// occurred.
std::vector<std::string> events_;
};

Expand Down Expand Up @@ -182,6 +183,7 @@ static bool ColumnCrossed(int board[][kTilesY]) {
}
return (false);
}

// A function that returns true if any of the diagonal
// is crossed with the same player's move
static bool DiagonalCrossed(int board[][kTilesY]) {
Expand Down
2 changes: 1 addition & 1 deletion demos/TicTacToe/Classes/TicTacToeLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
#include <string>
#include <unordered_set>

#include "MainMenuScene.h"
#include "TicTacToeScene.h"
#include "cocos2d.h"
#include "util.h"

using cocos2d::Director;
using cocos2d::Event;
Expand Down
1 change: 1 addition & 0 deletions demos/TicTacToe/Classes/TicTacToeScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "MainMenuScene.h"
#include "TicTacToeLayer.h"
#include "cocos2d.h"
#include "util.h"

class TicTacToe : public cocos2d::Layer {
public:
Expand Down
67 changes: 67 additions & 0 deletions demos/TicTacToe/Classes/util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2020 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "util.h"

#include <random>

// Logs the message passed in to the console.
void LogMessage(const char* format, ...) {
va_list list;
va_start(list, format);
vprintf(format, list);
va_end(list);
printf("\n");
fflush(stdout);
}

// Based on operating system, waits for the specified amount of miliseconds.
void ProcessEvents(int msec) {
#ifdef _WIN32
Sleep(msec);
#else
usleep(msec * 1000);
#endif // _WIN32
}

// Wait for a Future to be completed. If the Future returns an error, it will
// be logged.
void WaitForCompletion(const firebase::FutureBase& future, const char* name) {
while (future.status() == firebase::kFutureStatusPending) {
ProcessEvents(100);
}
if (future.status() != firebase::kFutureStatusComplete) {
LogMessage("ERROR: %s returned an invalid result.", name);
} else if (future.error() != 0) {
LogMessage("ERROR: %s returned error %d: %s", name, future.error(),
future.error_message());
}
}

// Generates a random uid of a specified length.
std::string GenerateUid(std::size_t length) {
const std::string kCharacters = "0123456789abcdefghjkmnpqrstuvwxyz";

std::random_device random_device;
std::mt19937 generator(random_device());
std::uniform_int_distribution<> distribution(0, kCharacters.size() - 1);

std::string generate_uid;

for (std::size_t i = 0; i < length; ++i) {
generate_uid += kCharacters[distribution(generator)];
}

return generate_uid;
}
43 changes: 43 additions & 0 deletions demos/TicTacToe/Classes/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2020 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef TICTACTOE_DEMO_CLASSES_UTIL_H_
#define TICTACTOE_DEMO_CLASSES_UTIL_H_

#include "firebase/future.h"

// inputs: const char* as the message that will be displayed.
//
// Logs the message to the user through the console.
void LogMessage(const char*, ...);

// inputs: integer for number of miliseconds.
//
// Acts as a blocking statement to wait for the specified time.
void ProcessEvents(int);

// inputs: FutureBase waiting to be completed and message describing the future
// action.
//
// Spins on ProcessEvents() while the FutureBase's status is pending, which it
// will then break out and LogMessage() notifying the FutureBase's status is
// completed.
void WaitForCompletion(const firebase::FutureBase&, const char*);

// inputs: size_t integer specifying the length of the uid.
//
// Generates a unique string based on the length passed in, grabbing the allowed
// characters from the character string defined inside the function.
std::string GenerateUid(std::size_t);

#endif // TICTACTOE_DEMO_CLASSES_UTIL_H_