Skip to content

Commit

Permalink
transferred repo from Assembla.com to GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobiletainment committed Feb 22, 2013
1 parent 62b95bf commit e985500
Show file tree
Hide file tree
Showing 36 changed files with 1,375 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/Board.cpp
@@ -0,0 +1,84 @@
#include "precomp.h"
#include "Board.h"
#include "Game.h"

using namespace std;

Board *Board::instance = 0;

//Map Initialization
Board::Board(CL_GraphicContext &gc) : width(7), height(6), Turn(0)
{
Data = vector<vector<char>>(height,vector<char>(width));
tileImages.resize(height, vector<CL_Sprite>(width));

tileSpritedesc.add_frame(CL_ImageProviderFactory::load("resources/Empty2.png"));
tileSpritedesc.add_frame(CL_ImageProviderFactory::load("resources/White.png")); //Player1
tileSpritedesc.add_frame(CL_ImageProviderFactory::load("resources/Black.png")); //Player2

directionArrow = CL_Sprite(gc, "resources/ThrowDirection.png");
directionArrowTop = CL_Sprite(gc, "resources/ThrowDirectionWithoutShadow.png");

for(int row = 0; row < height; ++row)
{
for(int column = 0; column < width; ++column)
{
Data[row][column] = 0;
//Initialize Tile Images
tileImages[row][column] = CL_Sprite(gc, tileSpritedesc);
}
}

}

Board &Board::GetInstance() //Singleton
{
if (!instance)
instance = new Board(*Game::gc);
return *instance;
}

Board::~Board()
{
}

void Board::Draw(CL_GraphicContext &gc)
{
static int tileOffsetX = 128;
static int tileOffsetY = 128;
int tilePosX = tileOffsetX;
int tilePosY = tileOffsetY;
;

int column = 0;
for(int x = 0; x < height; ++x)
{
for(int y = 0; y < width; ++y)
{
//0 -> Leer, 1 -> Gelb, 2 -> rot
if(Data[x][y] == 0)
tileImages[x][y].set_frame(0);
else if(Data[x][y] == 'x')
tileImages[x][y].set_frame(1);
else if(Data[x][y] == 'o')
tileImages[x][y].set_frame(2);


tileImages[x][y].draw(gc, tilePosX, tilePosY);
column++;

if(column == width)
{
tilePosY += Board::GetTileHeight();
tilePosX = tileOffsetX;
column = 0;
}
else
{
tilePosX += Board::GetTileWidth();
}
}
}
directionArrowTop.draw(gc, 0, tileOffsetY);
directionArrow.draw(gc, 0, tilePosY - 2* Board::GetTileHeight());
}
31 changes: 31 additions & 0 deletions src/Board.h
@@ -0,0 +1,31 @@

#ifndef header_map
#define header_map

using namespace std;

class Board
{
public:
static Board &GetInstance(); //Singleton
int GetWidth() { return width; }
int GetHeight() { return height; }
int GetTileWidth() { return 64; }
int GetTileHeight() { return 64; }
int Turn;

vector<vector<char>> Data; //the actual field
void Draw(CL_GraphicContext &gc);
~Board();

private:
Board( CL_GraphicContext &gc); //private constructor for singleton
static Board *instance;
vector<vector<CL_Sprite>> tileImages;
CL_Sprite directionArrow;
CL_Sprite directionArrowTop;
CL_SpriteDescription tileSpritedesc;
int width, height;
};

#endif
140 changes: 140 additions & 0 deletions src/Game.cpp
@@ -0,0 +1,140 @@
#include "precomp.h"
#include "Game.h"
using namespace std;

CL_GraphicContext *Game::gc;
string Game::winner;

Game::Game(CL_DisplayWindow &window)
{
gc = &window.get_gc();
board = &Board::GetInstance();
startMessage = new string("Press [1] to start first, or\n\n\nPress [2] to start with computer");
whichPlayer[0] = "Player White (You)";
whichPlayer[1] = "Player Black (Computer)";
}

Game::~Game(){
delete player;
delete computer;
}

void Game::Run(CL_DisplayWindow &window){
game_display_window = window;

*gc = window.get_gc();

quit = false;
CL_Slot slot_key_down = window.get_ic().get_keyboard().sig_key_down().connect(this, &Game::OnKeyDown);

function<void(string)> callbackFunction;

callbackFunction = &(Game::PlayerHasWonNotification);

player = new Player(*gc, callbackFunction);
player->AttachKeyboard(game_display_window);

computer = new PlayerNPC(*gc,callbackFunction);
sprintf_s(horizonMessage, "Horizon: %d", PlayerNPC::Horizon);
//Set up font
CL_FontDescription system_font_desc;
system_font_desc.set_typeface_name("courier new");
CL_Font_System system_font(*gc, system_font_desc);
system_font.set_font_metrics(CL_FontMetrics(7.0f));
string spacing = " ";
string columnIdentifiers = "Use keys: [1] [2] [3] [4] [5] [6] [7]";

system_font.draw_text(*gc, 10, 20, *startMessage);
window.flip(0);
while (startMessage != nullptr)
{
CL_KeepAlive::process();
}


char numberOfMinMaxCalls[32];
numberOfMinMaxCalls[0] = '\0';
while (!quit)
{
gc->clear(CL_Colorf(20, 56, 108));
int currentPlayer = board->Turn % 2;

if (currentPlayer == 0)
system_font.draw_text(*gc, 10, 20, "Current Turn: " + whichPlayer[0]);
else
system_font.draw_text(*gc, 10, 20, "Current Turn: " + whichPlayer[1]);


//Draw column numbers
system_font.draw_text(*gc, 10, 120, columnIdentifiers);
board->Draw(*gc);

system_font.draw_text(*gc, 350, 50, numberOfMinMaxCalls);
system_font.draw_text(*gc, 410, 80, horizonMessage);

if(!winner.empty())
{
system_font.draw_text(*gc, 10, 60, winner);
}
window.flip(0);
CL_KeepAlive::process();

if (currentPlayer == 0)
player->WaitForTurn();
else
{
int minMaxCalls = computer->WaitForTurn();
sprintf_s(numberOfMinMaxCalls, "#MinMaxCalls: %d", minMaxCalls);
}
}
}

string Game::GetActivePlayerName(Player *playerKI)
{
if(playerKI->PlayerIdentifier() == 'o')
{
return "Black Player";
}
else
return "White Player";
}

void Game::PlayerHasWonNotification(string whosTheWinner)
{
if (winner.empty()) //only assign winner the first time a winner is determined
winner = whosTheWinner;
}

void Game::OnKeyDown(const CL_InputEvent &key, const CL_InputState &state)
{
if (key.id == CL_KEY_ESCAPE) quit = true;
else if (startMessage != nullptr && key.id == CL_KEY_1)
{
board->Turn = 0; //player 1 makes all even turns, computer all odd

delete startMessage;
startMessage = nullptr;
}
else if (startMessage != nullptr && key.id == CL_KEY_2)
{
board->Turn = 1; //cheap trick to start with computer instead of player
delete startMessage;
startMessage = nullptr;
}
else if (key.id == CL_KEY_UP)
{
if (PlayerNPC::Horizon < 42)
{
PlayerNPC::Horizon += 1;
sprintf_s(horizonMessage, "Horizon: %d", PlayerNPC::Horizon);
}
}
else if (key.id == CL_KEY_DOWN)
{
if (PlayerNPC::Horizon > 0)
{
PlayerNPC::Horizon -= 1;
sprintf_s(horizonMessage, "Horizon: %d", PlayerNPC::Horizon);
}
}
}
30 changes: 30 additions & 0 deletions src/Game.h
@@ -0,0 +1,30 @@
#include "precomp.h"
#include "Board.h"
#include <functional>
#include "PlayerNPC.h"

class Game
{
private:
void OnKeyDown(const CL_InputEvent &key, const CL_InputState &state);
Board *board;
int view_x, view_y;
bool quit;
CL_DisplayWindow game_display_window;
void static PlayerHasWonNotification(string winner);
static string winner;
char horizonMessage[12];
string *startMessage;
public:
static CL_GraphicContext *gc;
Game( CL_DisplayWindow &window);
~Game();
Player *player;
PlayerNPC *computer;
string whichPlayer[2];
void Run(CL_DisplayWindow &window);

string GetActivePlayerName(Player *player);

void OnQuit() { quit = true; }
};

0 comments on commit e985500

Please sign in to comment.