build: split user and data folder, moving user data to Application Support on Mac

This commit is contained in:
Eberhard Graether
2016-02-16 16:44:42 +01:00
parent 89b8d51ab8
commit 86cf1ee62d
51 changed files with 176 additions and 1809 deletions
-16
View File
@@ -1,16 +0,0 @@
void lambdaCaller()
{
[](){}();
}
void lambdaSuperCaller()
{
lambdaCaller();
}
void lambdaMetaCaller()
{
[](){
lambdaCaller();
}();
}
-17
View File
@@ -1,17 +0,0 @@
/* hallo */
int a; /* hallo */
int b = 6/3/2; /* hallo */
//*
int c;
//*/
// /*
int d;
// */
/*
hallo
// */ int e = 0;
-129
View File
@@ -1,129 +0,0 @@
class Point
{
public:
Point(int x, int y)
: x(x)
, y(y)
{
}
int x;
int y;
};
class Shape
{
public:
virtual void draw() = 0;
};
class Line
: public Shape
{
public:
Line(Point start, Point end)
: start(start)
, end(end)
{
}
void draw()
{
// line drawing
}
private:
Point start;
Point end;
};
class Circle
: public Shape
{
public:
Circle(Point center, int radius)
: center(center)
, radius(radius)
{}
void draw()
{
// circle drawing
}
private:
Point center;
int radius;
};
class Image
{
public:
Image(int width, int height)
: width(width)
, height(height)
, idx(0)
{
shapes = new Shape*[10];
}
~Image()
{
for (int i = 0; i < idx; i++)
{
delete shapes[i];
}
delete [] shapes;
}
void addShape(Shape* shape)
{
if (idx < 10)
{
shapes[idx] = shape;
idx++;
}
}
void draw()
{
for (int i = 0; i < idx; i++)
{
shapes[i]->draw();
}
}
private:
Shape** shapes;
int idx;
int width;
int height;
};
void drawStickMan()
{
Image stickman(100, 200);
// head
stickman.addShape(new Circle(Point(50, 50), 25));
// torso
stickman.addShape(new Line(Point(50, 75), Point(50, 180)));
// arms
stickman.addShape(new Line(Point(50, 100), Point(10, 125)));
stickman.addShape(new Line(Point(50, 100), Point(90, 125)));
// legs
stickman.addShape(new Line(Point(50, 100), Point(10, 125)));
stickman.addShape(new Line(Point(50, 100), Point(90, 125)));
stickman.draw();
}
-3
View File
@@ -1,3 +0,0 @@
// nothing here
// except for text
-34
View File
@@ -1,34 +0,0 @@
class Player {
public:
void Do() {}
};
class Base {
protected:
void Init() {}
};
class Game : public Base {
public:
Game() {
Init();
}
void Run() {
player.Do();
}
private:
Player player;
};
Game* game;
int main() {
game = new Game();
game->Run();
delete game;
return 0;
}
-198
View File
@@ -1,198 +0,0 @@
int number = 3;
int calculate();
int calculate(long i)
{
int a = 1 + 2;
return a;
}
int calculate()
{
int a = 1 + 2;
return a;
}
int calculate(int i)
{
int a = 1 + 2;
return a;
}
#define PI 3.1415
#define CALL_CALCULATE() \
do \
{ \
calculate(); \
} \
while(0) \
int main()
{
CALL_CALCULATE();
}
int count = 0;
void countUp()
{
count += 1;
}
void countDown()
{
count -= 1;
}
void countUpTen()
{
for (int i = 0; i < 10; i++)
{
countUp();
}
}
class Person
{
public:
Person(char* name)
: name(name)
, age(0)
, weight(3)
, height(50)
{
}
char* askForName() const
{
return name;
}
void gainWeight(int gain)
{
weight += gain;
}
void grow(int gain)
{
height += gain;
}
void celebrateBirthday()
{
age += 1;
grow(5);
gainWeight(3);
}
private:
char* name;
int age;
int height;
int weight;
};
class Vehicle
{
protected:
int wheelCount;
int seatCount;
};
class Car
: public Vehicle
{
public:
Car()
{
wheelCount = 4;
seatCount = 5;
}
};
class Truck
: public Vehicle
{
public:
Truck()
{
wheelCount = 6;
seatCount = 2;
}
};
template<typename T>
T sum(T a, T b)
{
return a + b;
}
class Apple {};
class Pear {};
template<typename Fruit>
class Basket
{
public:
Fruit** field;
};
Basket<Apple> apples;
Basket<Pear> pears;
class Dog {};
class Cat
{
Dog getDog();
};
class Bird
{
Dog getDog();
Cat getCat();
};
class Fish
{
Dog getDog();
Cat getCat();
Bird getBird();
};
class Horse
: public Dog
{
Dog getDog();
Cat getCat();
Bird getBird();
Fish getFish();
};
class Building {};
class House : public Building {};
class Tower : public Building {};
class SkyScrapper : public Building {};
class Mansion : public Building {};
class Shard : public House, public Tower, public SkyScrapper, public Mansion {};
typedef unsigned int uint;
uint a = 1 + 2;
enum Size
{
SMALL,
MEDIUM,
LARGE
};
-272
View File
@@ -1,272 +0,0 @@
const bool *abd(int abc, int bca);
bool const *abc(int a, int b);
bool *const abcd(int a, int b);
int main();
void foo();
int sum(int a, int b);
int diff(int a, int b);
void funk();
class A
{
public:
A(char c)
: m_importantValue(42)
, m_importanterValue(c)
, m_importantestValue(3.14159265359f)
{
}
~A()
{
}
void doImportantStuff()
{
int a = 1;
m_importantValue *= a;
}
void doModeratelyImportantStuff()
{
m_importanterValue = 'R';
int answer = sum(21, 21);
}
private:
int m_importantValue;
char m_importanterValue;
const float m_importantestValue;
};
A globalA(' ');
class B : public A
{
public:
B()
: A('b')
{
}
virtual ~B()
{
}
protected:
virtual float getNumber() = 0;
};
class C
{
public:
C()
: m_valuable(0)
{
globalA.doImportantStuff();
s_count++;
}
~C()
{
s_count--;
}
static int getCount()
{
return s_count;
}
void solveAllProblems() const
{
A aInstance('a');
aInstance.doImportantStuff();
aInstance.doModeratelyImportantStuff();
}
private:
static int s_count;
int m_valuable;
};
namespace
{
namespace noname
{
struct V
{
int x;
int y;
};
}
enum E
{
X,
Y
};
}
noname::V voo;
typedef A* D;
D globalD;
class E
{
public:
E(){}
~E(){}
class SubE
{
public:
SubE(){}
~SubE(){}
};
private:
int m_importantInt;
};
class AnotherClass
: public A
{
public:
AnotherClass()
: A('x')
{
}
int publicInt;
protected:
int protectedInt;
private:
int privateInt;
};
class circleA
{
public:
circleA()
{
}
private:
//circleB m_b;
};
class circleB
{
public:
circleB()
{
m_a = circleA();
}
circleA foo(){return circleA();}
private:
circleA m_a;
};
struct circleC
{
public:
circleC()
{
m_b = circleB();
}
circleA foo0(){return circleA();}
circleB foo1(){return circleB();}
private:
circleB m_b;
};
class circleD
{
public:
circleD()
{
m_c = circleC();
}
circleC foo(){return circleC();}
circleA foo2(){return circleA();}
circleB foo3(){return circleB();}
private:
circleC m_c;
};
class circleE
{
public:
circleE()
{
m_d = circleD();
}
circleC foo(){return circleC();}
circleB foo1(){return circleB();}
circleD foo2(){return circleD();}
circleA foo3(){return circleA();}
private:
circleD m_d;
};
class circleF
{
public:
circleF()
{
m_e = circleE();
}
circleC foo(){return circleC();}
circleB foo1(){return circleB();}
circleD foo2(){return circleD();}
circleA foo3(){return circleA();}
circleE foo4(){return circleE();}
private:
circleE m_e;
};
class circleCenter
{
public:
circleCenter()
{
m_a = circleA();
m_b = circleB();
m_c = circleC();
m_d = circleD();
}
circleA foo0(){return circleA();}
circleB foo1(){return circleB();}
circleC foo2(){return circleC();}
circleD foo3(){return circleD();}
circleE foo4(){return circleE();}
circleF foo5(){return circleF();}
private:
circleA m_a;
circleB m_b;
circleC m_c;
circleD m_d;
//AnotherClass m_anotherClass;
};
-48
View File
@@ -1,48 +0,0 @@
#include "header.h"
int main();
void foo();
int sum(int a, int b);
int sum(int* a, int* b);
int diff(int a, int b);
int main()
{
A aInstance('m');
aInstance.doImportantStuff();
int a = sum(1, 2);
int b = diff(a, 3);
int c = a * b;
int x = 0;
return 0;
}
int sum(int a, int b);
int diff(int a, int b);
void foo()
{
const char* foo = "bar";
}
int diff(int a, int b);
int sum(int a, int b)
{
return a + b;
}
int diff(int a, int b)
{
return a - b;
}
void funk()
{
A aInstance('x');
aInstance.doModeratelyImportantStuff();
}
-13
View File
@@ -1,13 +0,0 @@
#include "header.h"
void test(int i, int b)
{
int x;
A instanceOfA('v');
return;
}
-23
View File
@@ -1,23 +0,0 @@
#include <memory>
class A
{
};
template <typename T>
class B
{
};
int main()
{
A a;
A* aPtr = nullptr;
std::shared_ptr<A> aSharedPtr = std::make_shared<A>();
B<A> b;
return 0;
}
-35
View File
@@ -1,35 +0,0 @@
template <bool B, typename, class F>
class Foo
{
public:
void bar()
{
}
};
template <typename T>
class Fooo
{
public:
void bar()
{
}
};
template <typename T, typename T::a::type U>
class Foooo1
{
public:
void bar()
{
}
};
template <typename T, typename T::type U>
class Foooo2
{
public:
void bar()
{
}
};
-13
View File
@@ -1,13 +0,0 @@
#include "header.h"
int main()
{
Foo<true, int, float> ft;
ft.bar();
return 0;
}
@@ -1,67 +0,0 @@
#include "artificial_player.h"
ArtificialPlayer::ArtificialPlayer( Field::Token token, const char* name )
: Player( token, name ) {
}
ArtificialPlayer::~ArtificialPlayer() {
}
Field::Move ArtificialPlayer::Turn( const Field& field ) const {
Field fakeField = field.Clone();
Node node = MinMax( fakeField, token_ );
return node.move;
}
ArtificialPlayer::Node ArtificialPlayer::MinMax( Field& field, Field::Token token ) const {
Node node;
node.value = -10000;
Field::Move move;
int sameMove;
for ( int i = 0; i < 3; i++ ) {
move.row = i;
for ( int j = 0; j < 3; j++ ) {
move.col = j;
if ( !field.IsEmpty( move ) ) {
continue;
}
field.MakeMove( move, token );
int turnValue = Evaluate( field, token );
if ( !turnValue && !field.IsFull() ) {
turnValue = -MinMax( field, Field::Opponent( token ) ).value;
}
field.ClearMove( move );
if ( turnValue > node.value ) {
node.move = move;
node.value = turnValue;
sameMove = 1;
} else if ( turnValue == node.value ) {
sameMove++;
if ( 1 % sameMove == 0 ) {
node.move = move;
}
}
}
}
return node;
}
int ArtificialPlayer::Evaluate( const Field& field, Field::Token token ) const {
if ( field.SameInRow( token, 3 ) ) {
return 2;
} else if ( field.SameInRow( Field::Opponent( token ), 2 ) ) {
return -1;
} else if ( field.SameInRow( token, 2 ) > 1 ) {
return 1;
}
return 0;
}
@@ -1,24 +0,0 @@
#ifndef _ARTIFICIAL_PLAYER_
#define _ARTIFICIAL_PLAYER_
#include "player.h"
class ArtificialPlayer : public Player {
public:
ArtificialPlayer( Field::Token token, const char* name );
virtual ~ArtificialPlayer();
virtual Field::Move Turn( const Field& field ) const;
private:
struct Node {
Field::Move move;
int value;
};
Node MinMax( Field& field, Field::Token token ) const;
int Evaluate( const Field& field, Field::Token token ) const;
};
#endif // _ARTIFICIAL_PLAYER_
-126
View File
@@ -1,126 +0,0 @@
#include "field.h"
#include "io.h"
Field::Token Field::Opponent( Token token ) {
if ( token == PlayerA ) {
return PlayerB;
} else if (token == PlayerB){
return PlayerA;
} else {
return None;
}
}
Field::Field()
: left_( 9 ) {
grid_ = new Token* [3];
for ( int i = 0; i < 3 ; i++ ) {
grid_[i] = new Token[3];
}
}
Field::~Field() {
for ( int i = 0; i < 3; i++ ) {
delete [] grid_[i];
}
delete [] grid_;
}
Field Field::Clone() const {
Field field;
for ( int i = 0; i < 3; i++ ) {
for ( int j = 0; j < 3; j++ ) {
field.grid_[i][j] = grid_[i][j];
}
}
field.left_ = left_;
return field;
}
void Field::Clear() {
for ( int i = 0; i < 3; i++ ) {
for ( int j = 0; j < 3; j++ ) {
grid_[i][j] = None;
}
}
left_ = 9;
}
void Field::Show() const {
io::stringOut(" 1 2 3\n");
for ( int row = 0; row < 3; row++ ) {
io::numberOut(row + 1);
io::stringOut(" ");
for ( int col = 0; col < 3; col++ ) {
if ( grid_[row][col] == PlayerA ) {
io::stringOut(" X ");
} else if ( grid_[row][col] == PlayerB ) {
io::stringOut(" O ");
} else {
io::stringOut(" ");
}
if ( col < 2 ) {
io::stringOut("|");
}
}
if ( row < 2 ) {
io::stringOut("\n -----------\n");
}
}
io::stringOut("\n\n");
}
int Field::SameInRow( Token token, int amount ) const {
int sum = amount * token;
int count = 0;
for ( int i = 0; i < 3; i++ ) {
if ( grid_[i][0] + grid_[i][1] + grid_[i][2] == sum ) {
count++;
} else if ( grid_[0][i] + grid_[1][i] + grid_[2][i] == sum ) {
count++;
}
}
if ( grid_[0][0] + grid_[1][1] + grid_[2][2] == sum ) {
count++;
} else if ( grid_[2][0] + grid_[1][1] + grid_[0][2] == sum ) {
count++;
}
return count;
}
bool Field::InRange( const Move& move ) const {
return move.row >= 0 && move.row < 3 && move.col >= 0 && move.col < 3;
}
bool Field::IsEmpty( const Move& move ) const {
return grid_[move.row][move.col] == None;
}
bool Field::IsFull() const {
return left_ == 0;
}
void Field::MakeMove( const Move& move, Token token ) {
if ( !InRange( move ) || !IsEmpty( move ) || token == None || !left_ ) {
return;
}
grid_[move.row][move.col] = token;
left_--;
}
void Field::ClearMove( const Move& move ) {
if ( !InRange( move ) || !IsEmpty( move ) || left_ == 9 ) {
return;
}
grid_[move.row][move.col] = None;
left_++;
}
-41
View File
@@ -1,41 +0,0 @@
#ifndef _FIELD_
#define _FIELD_
class Field {
public:
enum Token {
None = 0,
PlayerA = 1,
PlayerB = 4
};
static Token Opponent( Token token );
struct Move {
int row;
int col;
};
Field();
~Field();
Field Clone() const;
void Clear();
void Show() const;
int SameInRow( Token token, int amount ) const;
bool InRange( const Move& move ) const;
bool IsEmpty( const Move& move ) const;
bool IsFull() const;
void MakeMove( const Move& move, Token token );
void ClearMove( const Move& move );
private:
Token** grid_;
int left_;
};
#endif // _FIELD_
@@ -1,49 +0,0 @@
#include "human_player.h"
#include "io.h"
HumanPlayer::HumanPlayer( Field::Token token, const char* name )
: Player( token, name ) {
}
HumanPlayer::~HumanPlayer() {
}
Field::Move HumanPlayer::Turn( const Field& field ) const {
Field::Move move;
io::stringOut(name_);
io::stringOut("\n");
do {
move = Input();
move.row -= 1;
move.col -= 1;
} while ( !Check( field, move ) );
return move;
}
Field::Move HumanPlayer::Input() const {
Field::Move move;
move.row = -1;
move.col = -1;
io::stringOut("Insert row: ");
move.row = io::numberIn();
io::stringOut("Insert column: ");
move.col = io::numberIn();
io::stringOut("\n");
return move;
}
bool HumanPlayer::Check( const Field& field, const Field::Move& move ) const {
if ( !field.InRange( move ) ) {
io::stringOut("Wrong input!\n");
return false;
} else if ( !field.IsEmpty( move ) ) {
io::stringOut("Is occupied!\n");
return false;
}
return true;
}
-19
View File
@@ -1,19 +0,0 @@
#ifndef _HUMAN_PLAYER_
#define _HUMAN_PLAYER_
#include "player.h"
class HumanPlayer : public Player {
public:
HumanPlayer( Field::Token token, const char* name );
virtual ~HumanPlayer();
virtual Field::Move Turn( const Field& field ) const;
private:
Field::Move Input() const;
bool Check( const Field& field, const Field::Move& move ) const;
};
#endif // _HUMAN_PLAYER_
-13
View File
@@ -1,13 +0,0 @@
#ifndef _IO_
#define _IO_
namespace io {
int numberIn();
void numberOut(int num);
void stringOut(const char* str);
}
#endif // _IO_
-11
View File
@@ -1,11 +0,0 @@
#include "tictactoe.h"
int main() {
TicTacToe tictactoe;
while ( tictactoe.Start() ) {
tictactoe.Run();
}
return 0;
}
-19
View File
@@ -1,19 +0,0 @@
#include "player.h"
Player::Player( Field::Token token, const char* name )
: token_( token )
, name_( name ) {
}
Player::~Player() {
}
const Field::Token& Player::getToken() const
{
return token_;
}
const char* Player::getName() const
{
return name_;
}
-21
View File
@@ -1,21 +0,0 @@
#ifndef _PLAYER_
#define _PLAYER_
#include "field.h"
class Player {
public:
Player( Field::Token token, const char* name );
virtual ~Player();
virtual Field::Move Turn( const Field& field ) const = 0;
const Field::Token& getToken() const;
const char* getName() const;
protected:
const Field::Token token_;
const char* name_;
};
#endif // _PLAYER_
-85
View File
@@ -1,85 +0,0 @@
#include "tictactoe.h"
#include "artificial_player.h"
#include "human_player.h"
#include "io.h"
TicTacToe::TicTacToe() {
players_[0] = 0;
players_[1] = 0;
}
TicTacToe::~TicTacToe() {
Reset();
}
bool TicTacToe::Start() {
Reset();
io::stringOut("Tic Tac Toe\n\n[1] Human\n[2] Computer\n[3] Quit\n\n");
players_[0] = SelectPlayer( Field::PlayerA, "Player A" );
if ( !players_[0] ) {
return false;
}
players_[1] = SelectPlayer( Field::PlayerB, "Player B" );
if ( !players_[1] ) {
return false;
}
io::stringOut("\n");
return true;
}
void TicTacToe::Run() {
field_.Show();
int playerIndex = 0;
for ( int i = 0; i < 9; i++ ) {
Player& player = *players_[playerIndex];
field_.MakeMove( player.Turn( field_ ), player.getToken() );
field_.Show();
if ( field_.SameInRow( player.getToken(), 3 ) ) {
io::stringOut(player.getName());
io::stringOut(" won!\n\n");
return;
}
playerIndex = ( playerIndex + 1 ) % 2;
}
io::stringOut("Game ends in draw!\n\n");
}
void TicTacToe::Reset() {
field_.Clear();
for ( int i = 0; i < 2; i++ ) {
if ( players_[i] ) {
delete players_[i];
players_[i] = 0;
}
}
}
Player* TicTacToe::SelectPlayer( Field::Token token, const char* name ) const {
int selection = 0;
while ( true ) {
io::stringOut("Choose ");
io::stringOut(name);
io::stringOut(": ");
selection = io::numberIn();
switch ( selection ) {
case 1 : return new HumanPlayer( token, name );
case 2 : return new ArtificialPlayer( token, name );
case 3 : return 0;
default : io::stringOut("Wrong input!\n");
}
}
}
-23
View File
@@ -1,23 +0,0 @@
#ifndef _TIC_TAC_TOE_
#define _TIC_TAC_TOE_
#include "field.h"
#include "player.h"
class TicTacToe {
public:
TicTacToe();
~TicTacToe();
bool Start();
void Run();
private:
void Reset();
Player* SelectPlayer( Field::Token token, const char* name ) const;
Player* players_[2];
Field field_;
};
#endif // _TIC_TAC_TOE_
-82
View File
@@ -1,82 +0,0 @@
#ifndef _ARTIFICIAL_
#define _ARTIFICIAL_
#include <stdlib.h>
#include <time.h>
#include "player.h"
class ArtificialPlayer : public Player {
public:
ArtificialPlayer( Field::Token token, const std::string& name )
: Player( token, name ) {
}
virtual ~ArtificialPlayer() {}
virtual Field::Move Turn( const Field& field ) const {
// srand( static_cast<unsigned int>( time( NULL ) ) );
Field fakeField = field.Clone();
Node node = MinMax( fakeField, token_ );
return node.move;
}
private:
struct Node {
Field::Move move;
int value;
};
Node MinMax( Field& field, Field::Token token ) const {
Node node;
node.value = -10000;
Field::Move move;
int sameMove;
for ( int i = 0; i < 3; i++ ) {
move.row = i;
for ( int j = 0; j < 3; j++ ) {
move.col = j;
if ( !field.IsEmpty( move ) ) {
continue;
}
field.MakeMove( move, token );
int turnValue = Evaluate( field, token );
if ( !turnValue && !field.IsFull() ) {
turnValue = -MinMax( field, Field::Opponent( token ) ).value;
}
field.ClearMove( move );
if ( turnValue > node.value ) {
node.move = move;
node.value = turnValue;
sameMove = 1;
} else if ( turnValue == node.value ) {
sameMove++;
if ( 1 % sameMove == 0 ) {
node.move = move;
}
}
}
}
return node;
}
int Evaluate( const Field& field, Field::Token token ) const {
if ( field.SameInRow( token, 3 ) ) {
return 2;
} else if ( field.SameInRow( Field::Opponent( token ), 2 ) ) {
return -1;
} else if ( field.SameInRow( token, 2 ) > 1 ) {
return 1;
}
return 0;
}
};
#endif // _ARTIFICIAL_
-147
View File
@@ -1,147 +0,0 @@
#ifndef _FIELD_
#define _FIELD_
#include <cassert>
#include <iostream>
class Field {
public:
enum Token {
None = 0,
PlayerA = 1,
PlayerB = 4
};
static Token Opponent( Token token ) {
assert( token != None );
if ( token == PlayerA ) {
return PlayerB;
} else {
return PlayerA;
}
}
struct Move {
int row;
int col;
};
Field()
: left_( 9 ) {
grid_ = new Token* [3];
for ( int i = 0; i < 3 ; i++ ) {
grid_[i] = new Token[3];
}
}
~Field() {
for ( int i = 0; i < 3; i++ ) {
delete grid_[i];
}
delete [] grid_;
}
Field Clone() const {
Field field;
for ( int i = 0; i < 3; i++ ) {
for ( int j = 0; j < 3; j++ ) {
field.grid_[i][j] = grid_[i][j];
}
}
field.left_ = left_;
return field;
}
void Clear() {
for ( int i = 0; i < 3; i++ ) {
for ( int j = 0; j < 3; j++ ) {
grid_[i][j] = None;
}
}
left_ = 9;
}
void Show() const {
std::cout << " 1 2 3\n";
for ( int row = 0; row < 3; row++ ) {
std::cout << row + 1 << " ";
for ( int col = 0; col < 3; col++ ) {
if ( grid_[row][col] == PlayerA ) {
std::cout << " X ";
} else if ( grid_[row][col] == PlayerB ) {
std::cout << " O ";
} else {
std::cout << " ";
}
if ( col < 2 ) {
std::cout << "|";
}
}
if ( row < 2 ) {
std::cout << "\n -----------\n";
}
}
std::cout << "\n\n";
}
int SameInRow( Token token, int amount ) const {
int sum = amount * token;
int count = 0;
for ( int i = 0; i < 3; i++ ) {
if ( grid_[i][0] + grid_[i][1] + grid_[i][2] == sum ) {
count++;
} else if ( grid_[0][i] + grid_[1][i] + grid_[2][i] == sum ) {
count++;
}
}
if ( grid_[0][0] + grid_[1][1] + grid_[2][2] == sum ) {
count++;
} else if ( grid_[2][0] + grid_[1][1] + grid_[0][2] == sum ) {
count++;
}
return count;
}
bool InRange( const Move& move ) const {
return move.row >= 0 && move.row < 3 && move.col >= 0 && move.col < 3;
}
bool IsEmpty( const Move& move ) const {
return grid_[move.row][move.col] == None;
}
bool IsFull() const {
return left_ == 0;
}
void MakeMove( const Move& move, Token token ) {
assert( InRange( move ) );
assert( IsEmpty( move ) );
assert( token != None );
assert( left_ );
grid_[move.row][move.col] = token;
left_--;
}
void ClearMove( const Move& move ) {
assert( InRange(move) );
assert( !IsEmpty(move) );
assert( left_ < 9 );
grid_[move.row][move.col] = None;
left_++;
}
private:
Token** grid_;
int left_;
};
#endif // _FIELD_
-51
View File
@@ -1,51 +0,0 @@
#ifndef _HUMAN_
#define _HUMAN_
#include "input.h"
#include "player.h"
class HumanPlayer : public Player {
public:
HumanPlayer( Field::Token token, const std::string& name )
: Player( token, name ) {
}
virtual ~HumanPlayer() {}
virtual Field::Move Turn( const Field& field ) const {
Field::Move move;
std::cout << name_ << '\n';
do {
move = Input();
move.row -= 1;
move.col -= 1;
} while ( !Check( field, move ) );
return move;
}
private:
Field::Move Input() const {
Field::Move move;
std::cout << "Insert row: ";
input::number( &move.row );
std::cout << "Insert column: ";
input::number( &move.col );
std::cout << '\n';
return move;
}
bool Check( const Field& field, const Field::Move& move ) const {
if ( !field.InRange( move ) ) {
std::cout << "Wrong input!\n";
return false;
} else if ( !field.IsEmpty( move ) ) {
std::cout << "Is occupied!\n";
return false;
}
return true;
}
};
#endif // _HUMAN_
-20
View File
@@ -1,20 +0,0 @@
#ifndef _INPUT_
#define _INPUT_
#include <string>
#include <iostream>
#include <sstream>
namespace input {
template<typename T>
void number( T* number ) {
std::string input;
getline( std::cin, input );
std::stringstream stream( input );
stream >> *number;
}
}
#endif // _INPUT_
-11
View File
@@ -1,11 +0,0 @@
#include "tictactoe.h"
int main() {
TicTacToe tictactoe;
while ( tictactoe.Start() ) {
tictactoe.Run();
}
return 0;
}
-33
View File
@@ -1,33 +0,0 @@
#ifndef _PLAYER_
#define _PLAYER_
#include <string>
#include "field.h"
class Player {
public:
Player( Field::Token token, const std::string& name )
: token_( token )
, name_( name ) {
}
virtual ~Player() {}
virtual Field::Move Turn( const Field& field ) const = 0;
const Field::Token& getToken() const
{
return token_;
}
const std::string getName() const
{
return name_;
}
protected:
const Field::Token token_;
const std::string name_;
};
#endif // _PLAYER_
@@ -1,93 +0,0 @@
#ifndef _TIC_TAC_TOE_
#define _TIC_TAC_TOE_
#include "computer.h"
#include "field.h"
#include "human.h"
#include "input.h"
#include "player.h"
class TicTacToe {
public:
TicTacToe() {
players_[0] = NULL;
players_[1] = NULL;
}
~TicTacToe() {
Reset();
}
bool Start() {
Reset();
std::cout << "Tic Tac Toe\n\n[1] Human\n[2] Computer\n[3] Quit\n\n";
players_[0] = SelectPlayer( Field::PlayerA, "PlayerA" );
if ( !players_[0] ) {
return false;
}
players_[1] = SelectPlayer( Field::PlayerB, "PlayerB" );
if ( !players_[1] ) {
return false;
}
std::cout << '\n';
return true;
}
void Run() {
field_.Show();
int playerIndex = 0;
for ( int i = 0; i < 9; i++ ) {
Player& player = *players_[playerIndex];
field_.MakeMove( player.Turn( field_ ), player.getToken() );
field_.Show();
if ( field_.SameInRow( player.getToken(), 3 ) ) {
std::cout << player.getName() << " won!\n\n";
return;
}
playerIndex = ( playerIndex + 1 ) % 2;
}
std::cout << "Game ends in draw!\n\n";
}
private:
void Reset() {
field_.Clear();
for ( int i = 0; i < 2; i++ ) {
if ( players_[i] ) {
delete players_[i];
players_[i] = NULL;
}
}
}
Player* SelectPlayer( Field::Token token, const std::string& name ) const {
int selection;
while ( true ) {
std::cout << "Choose " << name << ": ";
input::number( &selection );
switch ( selection ) {
case 1 : return new HumanPlayer( token, name );
case 2 : return new ArtificialPlayer( token, name );
case 3 : return NULL;
default : std::cout << "Wrong input!\n";
}
}
}
Player* players_[2];
Field field_;
};
#endif // _TIC_TAC_TOE_