-
Notifications
You must be signed in to change notification settings - Fork 0
/
TickTackToe_Algorithm.h.save
173 lines (170 loc) · 3.86 KB
/
TickTackToe_Algorithm.h.save
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
#ifndef TICKTACKTOE_ALGORITHM_H_INCLUDED
#define TICKTACKTOE_ALGORITHM_H_INCLUDED
#include<string>
int ctoi(char x) //if fails, return an integer greater than 10;
{
switch (x)
{
case '1':return 1;
case '2':return 2;
case '3':return 3;
case '4':return 4;
case '5':return 5;
case '6':return 6;
case '7':return 7;
case '8':return 8;
case '9':return 9;
case '0':return 0;
default: return 12;
}
}
int stoi(string x)
{
int y=0,b=x.length()-1;
while(b>=0)
{
y=y+pow(10,b)*ctoi(x[x.length()-b-1]);
b--;
}
return y;
}
string itos(double i)
{
int j=ceil(log10(i));
string s;
char d[j];
itoa(i,d,10);
s=d;
return s;
}
class data
{
public:
string me;
int totalNums;
data(string s)
{
me=s;
}
void append (int i)
{
me.append("-");
me.append(itos(i));
totalNums++;
}
int ExtractNumAfter(int i)
{
int tobeReturned=0,p=0,q=0;
bool started=false;
while(true)
{
if(me[i]>=48&&me[i]<=57) // '0' er int man 48.... '9' er 57
{
if (started)
continue;
else if(!started)
{
started=true;
p=i;
}
}
else
{
if(!started)
continue;
else if (started)
{
q=i;
break;
}
}
i++;
}
return stoi(me.substr(p,q));
}
};
class Algorithm
{
int nr=0, nc=0;
public:Block * blk;
int totalInputs=0;
bool init(int nRow, int nColoumn)
{
nr=nRow;nc=nColoumn;
blk= new Block[nc*nr];
for(int i=0;i<nColoumn*nRow;i++)
{
blk[i+1].myRow=ceil((i+1.0)/nColoumn);
blk[i+1].myColoumn=(i+1)-(ceil((i+1.0)/nColoumn)-1)*nColoumn;
}
return true;
};
int rowOf(int j)
{
double i=j,nC=nc;
return ceil(i/nC);
}
int coloumnOf(int i)
{
return i-(rowOf(i)-1)*nc;
}
int up(double i)
{
if(i>nc) return i-nc;
else return 0;
}
int down(int i)
{
if(i<=nc*nr-nc) return i+nc;
else return 0;
}
bool isCornered(int i)
{
if(i==1||i==nc||i==nc*nRow||i==nc*nr-nc+1) return true;
return false;
}
bool isAlly(int i, int j)
{
if(coloumnOf(i)==coloumnOf(j)||rowOf(i)==rowOf(j)||rowOf(i)==coloumnOf(i)&&rowOf(j)==coloumnOf(j)||rowOf(i)+coloumnOf(i)==rowOf(j)+coloumnOf(j))
return true;
return false;
}
DWORD AllyType(int i,int j)
{
if(nc!=nr)
return NOT_ALLY;
if(coloumnOf(i)==coloumnOf(j))
return SAME_COLOUMN;
if(rowOf(i)==rowOf(j))
return SAME_ROW;
if(rowOf(i)==coloumnOf(i)&&rowOf(j)==coloumnOf(j))
return CROSS_ALLY_TAN_NEG;
if(rowOf(i)+coloumnOf(i)==rowOf(j)+coloumnOf(j))
return CROSS_ALLY_TAN_POS;
return NOT_ALLY;
}
/*void CloneAt(TickTackToe* ttt)
{
ttt=&me;
}*/
data neighbours_of(int i)
{
data s("");
if(up(i)!=0) s.append(up(i));
if(down(i)!=0) s.append(down(i));
if(i%nc!=0) s.append(i+1);// right block
if((i-1)%nc!=0) s.append(i-1);// left block
if(up(i)!=0&&i%nc!=0) s.append(up(i)+1);
if(down(i)!=0&&i%nc!=0) s.append(down(i)+1);// lower right block
if((i-1)%nc!=0&&down(i)!=0) s.append(down(i)-1);// lower left block
if(up(i)!=0&&(i-1)%nc!=0) s.append(up(i)-1);
return s;
}
int output(int id)
{
if(totalInputs==1)
{
}
}
};
#endif // TICKTACKTOE_ALGORITHM_H_INCLUDED