-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchess.h
71 lines (65 loc) · 1.05 KB
/
chess.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
#include <iostream>
#include <map>
using namespace std;
enum Color {
WHITE, BLACK
};
struct Point {
int x, y;
};
class Chess {
public:
void draw() {
cout << "Chess " << (m_color==WHITE?"white":"black") << " at position:("<< m_pos.x<<","<< m_pos.y<<")" << endl;
}
Color getColor() {
return m_color;
}
Point getPoint() {
return m_pos;
}
void setPoint(int x, int y) {
m_pos.x = x;
m_pos.y = y;
}
protected:
Color m_color;
private:
Point m_pos;
};
class WhiteChess :public Chess {
public:
WhiteChess() {
m_color = WHITE;
}
};
class BlackChess :public Chess {
public:
BlackChess() {
m_color = BLACK;
}
};
class ChessFactory {
public:
Chess* getChess(Color color) {
map<Color, Chess*>::iterator l_it = m_chesses.find(color);
Chess* ret = nullptr;
if (l_it == m_chesses.end()) {
switch (color)
{
case WHITE:
ret = new WhiteChess;
break;
default:
ret = new BlackChess;
break;
}
m_chesses[color] = ret;
return ret;
}
return (*l_it).second;
}
private:
map<Color, Chess*> m_chesses;
};