-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgBotc.cpp
121 lines (103 loc) · 2.01 KB
/
ProgBotc.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
#include "ProgBot.h"
void ProgBot::doCmd()
{
IRCBot::doCmd();
if(input["cmd"] == "say")
{
say();
}
else
if(input["cmd"] == "ttt")
{
ttt();
}
else
if(input["cmd"] == "gnum")
{
gNum();
}
}
void ProgBot::say()
{
writeSocket("PRIVMSG #" + input["room"] + ":" + input["input"] + "\r\n");
}
void ProgBot::ttt()
{
stringstream ss;
try
{
int pos = atoi(input["input"].c_str());
tic.chaPiece(pos,'x');
}
catch(exception e)
{
cout << "Tic-tac-toe error: " << e.what() << endl;
}
switch(tic.checkWinner())
{
case 'x':
writeSocket("PRIVMSG #" + input["room"] + ":" + "You win!" + "\r\n");
tic.clear();
return;
case 'o':
writeSocket("PRIVMSG #" + input["room"] + ":" + "I win!" + "\r\n");
tic.clear();
return;
}
char c[8];
ss << tic;
for(int i = 0; i < tic.width; ++i)
{
ss.read(c,ss.str().find('\n',0) + 1);
writeSocket("PRIVMSG #" + input["room"] + ":" + c + "\r\n");
}
writeSocket("PRIVMSG #" + input["room"] + ":" + "My turn!" + "\r\n");
tic.makeMove();
ss << tic;
for(int i = 0; i < tic.width; ++i)
{
ss.read(c,ss.str().find('\n',0) + 1);
writeSocket("PRIVMSG #" + input["room"] + ":" + c + "\r\n");
}
switch(tic.checkWinner())
{
case 'x':
writeSocket("PRIVMSG #" + input["room"] + ":" + "You win!" + "\r\n");
tic.clear();
return;
case 'o':
writeSocket("PRIVMSG #" + input["room"] + ":" + "I win!" + "\r\n");
tic.clear();
return;
}
}
void ProgBot::gNum()
{
srand(time(NULL));
if(gInt == 0) { gInt = rand()%21 + 1; }
try
{
int guess = atoi(input["input"].c_str());
if(guess == gInt)
{
writeSocket("PRIVMSG #" + input["room"] + ":You Win!" + "\r\n");
gInt = 0;
return;
}
else
if(guess < gInt)
{
writeSocket("PRIVMSG #" + input["room"] + ":Higher!" + "\r\n");
}
else
if(guess > gInt)
{
writeSocket("PRIVMSG #" + input["room"] + ":Lower!" + "\r\n");
}
}
catch(exception e)
{
cout << "Guess a number error: " << e.what() << endl;
}
writeSocket("PRIVMSG #" + input["room"] + ":Guess a number from 1 to 21" + "\r\n");
}