-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.cpp
More file actions
56 lines (46 loc) · 1.32 KB
/
King.cpp
File metadata and controls
56 lines (46 loc) · 1.32 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
#include <iostream> // input-output library
#include <string> // string library
#include "Chessman.h"
#include "King.h"
King::King(char type, string place) : Chessman(type, place)
{
}
King::~King()
{
}
string King::validMove(string movement, Game game)
{
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 = "*";
ret[0] = INVALID_MOVEMENT;
const char LETTER_TO = TO[LETTER], NUM_TO = TO[NUM];
const char LETTER_FROM = FROM[LETTER], NUM_FROM = FROM[NUM];
if (TO == FROM)
{
ret[0] = SAME_SOURCE_AND_DEST;
}
else if ((LETTER_TO == LETTER_FROM) && ((NUM_TO == NUM_FROM +1) || (NUM_TO == NUM_FROM -1)))
{
ret[0] = VALID_MOVEMENT;
}
else if ((NUM_TO == NUM_FROM) && ((LETTER_TO == LETTER_FROM + 1) || (LETTER_TO == LETTER_FROM - 1)))
{
ret[0] = VALID_MOVEMENT;
}
else if ((NUM_TO == NUM_FROM + 1) && ((LETTER_TO == LETTER_FROM + 1) || (LETTER_TO == LETTER_FROM - 1)))
{
ret[0] = VALID_MOVEMENT;
}
else if ((NUM_TO == NUM_FROM - 1) && ((LETTER_TO == LETTER_FROM + 1) || (LETTER_TO == LETTER_FROM - 1)))
{
ret[0] = VALID_MOVEMENT;
}
if ((ret[0] == VALID_MOVEMENT) && (game.isBlack(game.hasChessman(TO)) == game.getTurn()))
{
ret[0] = DEST_PLACE_INVALID;
}
return ret;
}