// Sever.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <time.h>
#include <Winsock2.h>
#pragma once
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define AF_INET 2 /* internetwork: UDP, TCP, etc. */
#define SOCK_STREAM 1 /* stream socket */
#define SOCK_DGRAM 2 /* datagram socket */
#define SOCK_RAW 3 /* raw-protocol interface */
#define SOCK_RDM 4 /* reliably-delivered message */
#define SOCK_SEQPACKET 5 /* sequenced packet stream */
typedef struct sockaddr_in SOCKADDR_IN;
int SocketInit();
#define MaxSize 1024
void sendToClient(SOCKET sockClient, const char s[MaxSize]);
int SocketInit()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData); //返回0,成功,否则就是错误码
if (err != 0)
{
printf("WinSock DLL版本不足要求n");
return 0;
}
if (LOBYTE(wsaData.wVersion) != 2 ||
HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
return 0;
}
return 1;
}
/**
* [sendToClient description]:向客户端发送数据
* @param sockClient [description]:客户端
* @param s [description]:消息
*/
void sendToClient(SOCKET sockClient, const char s[MaxSize])
{
char sendBuf[MaxSize];
strcpy_s(sendBuf, s);
int byte = send(sockClient, sendBuf, strlen(sendBuf), 0);
if (byte <= 0)
cout << "send error\n";
}
class Game {
private:
public:
static vector<vector<string>> wordList;//被添加的单词列表
int level;
Game(int l = 0) { level = l; }
~Game() {}
static vector<vector<string>>& getWordList() { return wordList; }
static void InitFromFile(SOCKET sockClient);
int Display(SOCKET sockClient);
int getLevel() { return level; }
void setLevel(int a) { level = a; }
};
vector<vector<string>> Game::wordList = {};
int Game::Display(SOCKET sockClient)
{
char revBuf[MaxSize];
sendToClient(sockClient, "The level is: ");
Sleep(20);
char l[MaxSize];
_itoa_s(level + 1, l, 10);
sendToClient(sockClient, l);
Sleep(20);
sendToClient(sockClient, "\n");
Sleep(20);
if ((unsigned int)level < wordList.size() && wordList[level].size() > 0) {
srand((unsigned int)time(NULL));
int pos = rand() % wordList[level].size();
char w[MaxSize];
strcpy_s(w, (wordList[level][pos]).c_str());
send(sockClient, w, strlen(w), 0);
Sleep(1000);
char sendBuf[MaxSize];
strcpy_s(sendBuf, "\r \r");
send(sockClient, sendBuf, strlen(sendBuf), 0);
time_t timeBegin;
timeBegin = time(NULL);
string answer;
int n = recv(sockClient, revBuf, MaxSize, 0);
revBuf[n] = '\0';
if (n < 0)
cout << "Rec Error" << endl;
answer = revBuf;
if (wordList[level][pos] == answer) {
time_t timeEnd;
timeEnd = time(NULL);
char l[MaxSize];
_itoa_s((int)(timeEnd - timeBegin), l, 10);
sendToClient(sockClient, l);
Sleep(20);
sendToClient(sockClient, "\n");
Sleep(20);
sendToClient(sockClient, "Your answer is right. \n");
Sleep(20);
level++;
int point;
if (level > 7 && (timeEnd - timeBegin) < 10)
point = 3;
else if (level > 4 && (timeEnd - timeBegin) < 4)
point = 2;
else
point = 1;
return point;
}
else {
sendToClient(sockClient, "Your answer is wrong.\n");
Sleep(20);
return 0;
}
}
else {
sendToClient(sockClient, "Congratulations! You have reached the last level.\n");
return 0;
}
}
void Game::InitFromFile(SOCKET sockClient)
{
string w;
ifstream infile("wordList.csv");
if (!infile) {
sendToClient(sockClient, "File wordList.csv open failed!\n");
abort();
}
unsigned int i = 0;
while (!infile.eof())
{
infile >> w;
for (; i < wordList.size(); i++)
if (w.size() == wordList[i][0].size())
break;
if (i < wordList.size())
wordList[i].push_back(w);
else {
vector <string> add;
add.push_back(w);
wordList.push_back(add);
}
}
infile.close();
}
class Battle :
public Game
{
private:
public:
string word;//题目
time_t t1;//客户端A挑战用时
time_t t2;//客户端B挑战用时
time_t timeBegin;//挑战开始时间
SOCKET sockClientA;//A挑战者
SOCKET sockClientB;//B挑战者
Battle() { t1 = 1000000; t2 = 1000000; }
~Battle() {}
void resetTime() { t1 = 1000000; t2 = 1000000; }//重置时间参数
void givePro();//出题函数
int onBattle(SOCKET sockClient);//客户端答题
};
void Battle::givePro()
{
if ((unsigned int)level < wordList.size() && wordList[level].size() > 0) {
srand((unsigned int)time(NULL));
int pos = rand() % wordList[level].size();
word = wordList[level][pos];
sendToClient(sockClientA, (wordList[level][pos]).c_str());
sendToClient(sockClientB, (wordList[level][pos]).c_str());
Sleep(1000);
sendToClient(sockClientA, "\r \r");
sendToClient(sockClientB, "\r \r");
}
else {
sendToClient(sockClientA, "Congratulations! You have reached the last level.\n");
sendToClient(sockClientB, "Congratulations! You have reached the last level.\n");
}
level++;
}
int Battle::onBattle(SOCKET sockClient)
{
char revBuf[MaxSize];
string answer;
int n = recv(sockClient, revBuf, MaxSize, 0);
revBuf[n] = '\0';
if (n < 0)
cout << "Rec Error" << endl;
answer = revBuf;
if (word == answer) {
time_t timeEnd;
timeEnd = time(NULL);
if (sockClientA == sockClient)
t1 = timeEnd - timeBegin;
else
t2 = timeEnd - timeBegin;
if (t1 > t2) {
sendToClient(sockClientA, "You lose the battle! \n");
sendToClient(sockClientB, "You win the battle! \n");
return 2;
}
else if (t1 < t2) {
sendToClient(sockClientA, "You win the battle! \n");
sendToClient(sockClientB, "You lose the battle! \n");
return 1;
}
else {
sendToClient(sockClientA, "It is a draw! \n");
sendToClient(sockClientB, "It is a draw! \n");
return 0;
}
}
else {
sendToClient(sockClient, "Your answer is wrong.\n");
if (sockClientA == sockClient)
return 2;
else
return 1;
}
}
class User {
private:
string name;
string password;
public:
User(const string& n, const string& p) : name(n), password(p) {}
~User() {}
string getName() { return name; }
string getPassword() { return password; }
virtual void Register(SOCKET sockClient) {}
virtual int Login(SOCKET sockClient) { return false; }
};
vector<string> split(const string& s, const string& delim);
vector<string> split(const string& s, const string& delim)
{
vector<std::string> elems;
size_t pos = 0;
size_t len = s.length();
size_t delim_len = delim.length();
if (delim_len == 0) return elems;
while (pos < len)
{
int find_pos = s.find(delim, pos);
if (find_pos < 0)
{
elems.push_back(s.substr(pos, len - pos));
break;
}
elems.push_back(s.substr(pos, find_pos - pos));
pos = find_pos + delim_len;
}
return elems;
}
class TestBuilder :
public User
{
private:
int problemsNumbers;
int testBuilderLevel;
static bool isLogin;
static vector <TestBuilder> testBuilderList;//registered player list
public:
TestBuilder(const string& n, const string& p, int pNum = 0, int g = 0) : User(n, p)
{
problemsNumbers = pNum;
testBuilderLevel = g;
}
~TestBuilder() {}
static vector<TestBuilder>& getTestBuilderList() { return testBuilderList; }
static void InitFromFile(SOCKET sockClient);
void updateInfo(SOCKET sockClient);
void Register(SOCKET sockClient);
int Login(SOCKET sockClient);
void

onnx
- 粉丝: 1w+
最新资源
- excel绘制蛋白质浓度标准曲线.ppt
- 基于互联网+的气象服务行政审批系统设计与实现.docx
- 安徽省移动通信公司内部控制系统构建研究的开题报告.docx
- 软件测试在电子信息工程建设中的应用分析.docx
- 我国电力行业电子商务应用研究.docx
- 第二届全国高校云计算应用创新大赛宣讲PPT.pptx
- 嵌入式HMI组态软件研究与设计的开题报告.docx
- 西门子S7300PLC05教程文件.ppt
- 4-1-第4章-S7-300PLC-第1节-硬件-配置方式及地址分配.ppt
- 基于本体的计算机取证的研究的开题报告.docx
- 操作系统(第四版)教材配套资源ppt课件(完整版).zip
- 计算机应用基础及实训教案资料.ppt
- 营销型网站建设必备的七点(1).doc
- 浅谈计算机网络安全漏洞及防范措施(1).docx
- Excel表格通用模板:生产成本核算.xlsx
- Java语言基础.ppt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


