-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.cpp
More file actions
124 lines (106 loc) · 2.87 KB
/
Pawn.cpp
File metadata and controls
124 lines (106 loc) · 2.87 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream> // input-output library
#include <string> // string library
#include "Chessman.h"
#include "Pawn.h"
#include "Game.h"
Pawn::Pawn(char type, string place) : Chessman(type, place)
{
}
Pawn::~Pawn()
{
}
string Pawn::validMove(string movement, Game game)
{
const int RET_INDEX = 0;
const int LETTER = 0, NUM = 1;
const int TO1 = 2, TO2 = 4, FROM1 = 0, FROM2 = 2;
const string TO = movement.substr(TO1, TO2);
const string FROM = movement.substr(FROM1, FROM2);
string ret = "*";
const char LETTER_TO = TO[LETTER], NUM_TO = TO[NUM];
const char LETTER_FROM = FROM[LETTER], NUM_FROM = FROM[NUM];
char cheekMove[] = { LETTER_TO, "*" };
if (TO != FROM) // if src and dst different
{
if (game.isBlack(game.hasChessman(FROM)) != game.isBlack(game.hasChessman(TO))) // if is not same color
{
if (LETTER_FROM == LETTER_TO)
{
if (this->getType() == PAWN_W)
{
cheekMove[1] = NUM_TO + 1;
if (NUM_TO + 1 == NUM_FROM && game.hasChessman(TO) == NULL)
ret[RET_INDEX] = VALID_MOVEMENT;
else if (NUM_TO + 2 == NUM_FROM && game.hasChessman(TO) == NULL)
if (NUM_FROM == WHITE_START_NUM && game.hasChessman(string(cheekMove)) == NULL)
ret[RET_INDEX] = VALID_MOVEMENT;
else
ret[RET_INDEX] = INVALID_MOVEMENT;
}
else // if (this->getType() == PAWN_B)
{
cheekMove[1] = NUM_TO - 1;
if (NUM_TO - 1 == NUM_FROM && game.hasChessman(TO) == NULL)
ret[RET_INDEX] = VALID_MOVEMENT;
else if (NUM_TO - 2 == NUM_FROM && game.hasChessman(TO) == NULL)
if (NUM_FROM == BLACK_START_NUM && game.hasChessman(string(cheekMove)) == NULL)
ret[RET_INDEX] = VALID_MOVEMENT;
else
ret[RET_INDEX] = INVALID_MOVEMENT;
}
}
else if (LETTER_FROM == LETTER_TO + 1 || LETTER_FROM == LETTER_TO - 1)
{
if (this->getType() == PAWN_W)
{
if (NUM_FROM == NUM_TO + 1)
{
if (game.isBlack(game.hasChessman(TO)) == BLACK_CH)
{
ret[RET_INDEX] = VALID_MOVEMENT;
}
else
{
ret[RET_INDEX] = INVALID_MOVEMENT; // error - invalid movement
}
}
else
{
ret[RET_INDEX] = INVALID_MOVEMENT; // error - invalid movement
}
}
else // if (this->getType() == PAWN_B)
{
if (NUM_FROM == NUM_TO - 1)
{
if (game.isBlack(game.hasChessman(TO)) == WHITE_CH)
{
ret[RET_INDEX] = VALID_MOVEMENT;
}
else
{
ret[RET_INDEX] = INVALID_MOVEMENT; // error - invalid movement
}
}
else
{
ret[RET_INDEX] = INVALID_MOVEMENT; // error - invalid movement
}
}
}
else
{
ret[RET_INDEX] = INVALID_MOVEMENT; // error - invalid movement
}
}
else // if there same color
{
ret[RET_INDEX] = DEST_PLACE_INVALID; // error - try to eat chessman with same color
}
}
else // if (TO == FROM)
{
ret[RET_INDEX] = SAME_SOURCE_AND_DEST;
}
return ret;
}