-
Notifications
You must be signed in to change notification settings - Fork 0
/
column.cpp
86 lines (81 loc) · 1.8 KB
/
column.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
#include "column.h"
#include <algorithm>
#include <iostream>
Column* SetColumn(int n, int x, int y){
Column *c = new Column;
std::vector<Slot*> slots;
for(int i = 0; i<n; i++){
Slot *s = new Slot;
s->x = x;
s->y = y-(SLOTH*n/2)+SLOTH*i;
int type = TYPE_COUNT-1;
int chance = pow(2, TYPE_COUNT/2+1);
while(rand()%chance!=0&&chance!=1&&type>0){
type--;
if(type%2){
chance/=2;
}
}
s->type = type;
slots.push_back(s);
}
c->Set(n, x, y , slots);
return c;
}
void Column::Set(int n, int x, int y, std::vector<Slot*> s){
count = n;
cX = x;
cY = y;
slots = s;
}
std::vector <Slot*> Column::SetVisible(){
std::vector<Slot*> v;
for(int i = 0; i<count; i++){
if(abs(slots[i]->y-cY)<=3*SLOTH){
v.push_back(slots[i]);
}
}
return v;
}
bool Column::NextSpin(bool trytostop){
if(forcestop){
return true;
}
bool CANSTOP = false;
if(trytostop){
speed -= rand()%5;
if(speed<1){
speed = 1;
}
}
for(Slot *s: slots){
s->y = (s->y+speed);
if(s->y>cY+count/2*SLOTH){
s->y = s->y-count*SLOTH;
}
if(s->y==cY&&trytostop){
CANSTOP = true;
forcestop = true;
}
}
return CANSTOP;
}
bool compareByY(const Slot &a, const Slot &b){
return a.y>b.y;
}
std::vector<Slot> Column::WinningSlots(){
std::vector<Slot> wS;
for(Slot *s : slots){
if(abs(s->y-cY)<=(SLOTH)){
wS.push_back(*s);
}
}
if(wS.size()==3){
running = false;
std::sort(wS.begin(), wS.end(), compareByY);
}
return wS;
}
bool Column::IsRunning(){
return running;
}