Skip to content

Instantly share code, notes, and snippets.

@kikuchy
Last active December 18, 2015 07:38
Show Gist options
  • Save kikuchy/5747651 to your computer and use it in GitHub Desktop.
Save kikuchy/5747651 to your computer and use it in GitHub Desktop.
後でオセロに使うやつ(未検証)
#include <stdlib.h>
#include <string.h>
struct Point {
int x;
int y;
Point(int x, int y);
};
Point::Point(int x, int y)
{
this->x = x;
this->y = y;
};
class Stage {
public:
Stage(int width, int height);
~Stage();
bool canPut(Point p, int myColor);
int countTurn(Point nowPos, Point dif, int turned, int myColor);
bool isBlank(Point p);
bool isOutOfStage(Point p);
int colorOf(Point p);
int put(Point p, int myColor);
private:
int *m_stage;
int m_width;
int m_height;
};
Stage::Stage(int width, int height)
{
m_stage = (int *)malloc(sizeof(int) * width * height);
memset(m_stage, '\0', sizeof(int) * width * height);
m_width = width;
m_height = height;
}
Stage::~Stage()
{
free(m_stage);
}
int Stage::colorOf(Point p)
{
return m_stage[p.x + m_width * p.y];
}
bool Stage::isBlank(Point p)
{
return colorOf(p) == 0;
}
bool Stage::isOutOfStage(Point p)
{
return !((p.x < m_width) && (-1 < p.x) && (p.y < m_height) && (-1 < p.y));
}
bool Stage::canPut(Point p, int myColor)
{
if(!isBlank(p) || isOutOfStage(p)) return false;
for(int i = -1; i < 2; i++)
for(int j = -1; j < 2; j++)
if(!(i != 0 && j != 0))
if(countTurn(p, Point(i, j), 0, myColor) > 0)
return true;
return false;
}
int Stage::countTurn(Point nowPos, Point dif, int turned, int myColor)
{
Point newPos(nowPos.x + dif.x, nowPos.y + dif.y);
if(isBlank(newPos) || isOutOfStage(newPos)) return 0;
if(colorOf(newPos) == myColor) return turned;
return countTurn(newPos, dif, turned + 1, myColor);
}
int Stage::put(Point p, int myColor)
{
if(!canPut(p, myColor)) return 0;
return m_stage[p.x + m_width * p.y] = myColor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment