-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.cpp
123 lines (117 loc) · 2.21 KB
/
bot.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
#include "Bot.h"
#include <random>
#include <ctime>
#include <iostream>
Bot::Bot(char m)
{
setMark(m);
}
//void Bot::play()
//{
//
//}
int Bot::addMove(char game[], Bot bot, Game var)
{
int position, random;
srand(time(NULL));
// If the center spot is taken, bot will check to see if random corner spots are taken
if (var.taken(game, 5))
{
random = rand() % 4 + 1;
if (random == 1 && !var.taken(game, 1))
{
game[1] = bot.getMark();
return 1;
}
else if (random == 2 && !var.taken(game, 3))
{
game[3] = bot.getMark();
return 3;
}
else if (random == 3 && !var.taken(game, 7))
{
game[7] = bot.getMark();
return 7;
}
else if (random == 4 && !var.taken(game, 9))
{
game[9] = bot.getMark();
return 9;
}
else if (!var.taken(game, 2))
{
game[2] = bot.getMark();
return 2;
}
else if (!var.taken(game, 4))
{
game[4] = bot.getMark();
return 4;
}
else if (!var.taken(game, 6))
{
game[6] = bot.getMark();
return 6;
}
else if (!var.taken(game, 8))
{
game[8] = bot.getMark();
return 8;
}
else
std::cout << "Bot Error: All spots taken" << std::endl;
}
// If any corner spots are taken, try to take the center spot, otherwise try another corner, and then edge spots after that.
/*else if (var.taken(game, 1) || var.taken(game, 3) || var.taken(game, 7) || var.taken(game, 9))*/
else
{
random = rand() % 4 + 1;
if (!var.taken(game, 5))
{
game[5] = bot.getMark();
return 5;
}
else if (random == 1 && !var.taken(game, 1))
{
game[1] = bot.getMark();
return 1;
}
else if (random == 2 && !var.taken(game, 3))
{
game[3] = bot.getMark();
return 3;
}
else if (random == 3 && !var.taken(game, 7))
{
game[7] = bot.getMark();
return 7;
}
else if (random == 4 && !var.taken(game, 9))
{
game[9] = bot.getMark();
return 9;
}
else if (!var.taken(game, 2))
{
game[2] = bot.getMark();
return 2;
}
else if (!var.taken(game, 4))
{
game[4] = bot.getMark();
return 4;
}
else if (!var.taken(game, 6))
{
game[6] = bot.getMark();
return 6;
}
else if (!var.taken(game, 8))
{
game[8] = bot.getMark();
return 8;
}
else
std::cout << "Bot Error: All spots taken" << std::endl;
}
}