-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFour.cpp
More file actions
149 lines (140 loc) · 3.23 KB
/
ConnectFour.cpp
File metadata and controls
149 lines (140 loc) · 3.23 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
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
#include <iostream>
using namespace std;
// implementation for list object to be used
class List
{
private:
// implementation of ListNode class
class ListNode
{
public:
char val;
ListNode *next;
ListNode(char v, ListNode *n=nullptr)
{
val = v;
next = n;
}
};
ListNode *head;
ListNode *tail;
public:
// iterator class
class iterator
{
private:
ListNode *node;
public:
iterator(ListNode *n=nullptr) { node = n; }
// return val of iterator
char& getVal() { return node->val; }
void next() { node = node->next; }
bool operator==(const iterator &other)
{
return node == other.node;
}
bool operator != (const iterator &other)
{
return node != other.node;
}
//code allowing to increment iterator by overloading ++
iterator& operator++()
{
next();
return *this;
}
};
// get begining and end of a list
iterator begin() { return iterator(head); }
iterator end() { return iterator(); }
// code to append to the list
// all that is needed in managing the connect 4 board
void append(char a)
{
ListNode *node = new ListNode(a);
if (head==nullptr)
{
head = node;
tail = node;
}
else
{
tail->next = node;
tail = node;
}
}
};
// classs for Board using row and coloumn
// to define how big the board is
class Board
{
protected:
int row;
int coloumn;
public:
// set default rows to 7 and coloumns to 6
Board(int r = 7,int c = 6)
{
row = r;
coloumn = c;
}
};
// derive BoardHT from Board
class BoardHT : public Board
{
protected:
// dist will be amount of coloums and be max length of lists in each bucket
int dist;
// one bucket per row, buckets will hold lists
struct bucket
{
List list;
bucket() { list; }
};
// hash function, will be updated more when insert is implemented, somewhat a placeholder for now
int hash(int key) const
{
int val = key - 1;
return std::abs(val);
}
public:
// made public to be able to test locally in main(), will be protected when fully implemented
bucket *table;
// constructor for board using HT
BoardHT()
{
dist = coloumn;
table = new bucket [row];
}
};
// used for local testing, not important to actual project design
// creates and prints a table of 7 rows and 6 coloumns all holding 'R'
int main()
{
BoardHT F;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 6; j++)
{
F.table[i].list.append('R');
}
}
List::iterator it;
for (int j = 0; j < 6; j++)
{
for (int i = 0; i < 7; i++)
{
if (j == 0) it = F.table[i].list.begin();
else
{
for (int k = 0; k < j; k++)
{
it = F.table[i].list.begin();
++it;
}
}
cout << it.getVal() << " ";
}
cout << endl;
}
}