-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhorse.cpp
188 lines (147 loc) · 4.39 KB
/
horse.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// horse.cpp
// chessPieces
//
// Created by Miguel Sacristán on 13/1/17.
//
//
#include "horse.h"
using namespace std;
horse::horse(){
position[0]=0;
position[1]=0;
}
horse::horse(int x, int y){
if ( x>8 || x<0 || y>8 || y<0 ) {
cout<<"Values outside the range, initializing to (0,0)"<<endl;
position[0]=0;
position[1]=0;
} else{
position[0]=x;
position[1]=y;
}
}
horse::~horse(){
}
void horse::move(int dx, int dy){
if (moveIsLegal(dx, dy)) {
position[0]+=dx;
position[1]+=dy;
} else{
cout<<"Illegal move!!"<<endl;
}
}
bool horse::moveIsLegal(int dx, int dy){
return (position[0] + dx < 8 && position[0] + dx > -1) &&
(position[1]+dy < 8 && position[1]+dy > -1);
}
void horse::randomMove(){
int dx=9;
int dy=9;
while (!moveIsLegal(dx, dy)) {
int firstDirection = rand() % 4;
int secondDirection = rand() % 2;
//Decision on what movement
//[WIP] to put in another function
if (firstDirection == 0) {
dx=2;
if (secondDirection == 0) {
// |
dy=1; // ---
//
} else {
//
dy=-1; // ---
// |
}
} else if (firstDirection == 1) {
dy=2;
if (secondDirection == 0) {
// --
dx=1; // |
// |
} else {
// --
dx=-1; // |
// |
}
} else if (firstDirection == 2){
dx=-2;
if (secondDirection == 0) {
// |
dy=1; // ---
//
} else {
//
dy=-1; // ---
// |
}
} else if (firstDirection == 3){
dy=-2;
if (secondDirection == 0) {
// |
dx=1; // |
// --
} else {
// |
dx=-1; // |
// --
}
}
}
move(dx, dy);
}
int horse::getPositionX(){
return position[0];
}
int horse::getPositionY(){
return position[1];
}
void horse::printPosition(){
system("clear");
for (int i=7; i>=0; i--) {
for (int j=0; j<8; j++) {
cout<<" ::::::::::::::";
}
cout<<endl;
for (int n=0; n<6; n++) {
for (int j=0; j<9; j++) {
if (position[0]==j && position[1]==i) {
if (n==0) {
cout<<": |\\. ";
}else if (n==1){
cout<<": /* '. ";
}else if (n==2){
cout<<": /_.'- \\ ";
}else if (n==3){
cout<<": / | ";
}else if (n==4){
cout<<": /____| ";
}else if (n==5){
cout<<": `.____.´ ";
}
}else{
cout<<": ";
}
}
cout<<endl;
}
}
for (int j=0; j<8; j++) {
cout<<" ::::::::::::::";
}
cout<<endl;
sleep(1);
}
int horse::numberOfMovesToComeBack(){
int n=0;
int x = getPositionX();
int y = getPositionY();
randomMove();
n++;
while ((position[0] != x) || position[1] != y) {
randomMove();
n++;
}
return n;
}