概述
准备工作:
(1)首先创建两个记事本,其目的是用来保存玩家和管理员信息的:
上面的数据都是在运行代码时添加的,使用文件保存测原因就是在模拟一个数据库,将这个文件保存的数据当成是数据库在保存数据,所有管理员对玩家信息的修改等操作都是记录在文件中的;玩家对自己的信息的修改也是记录在文件中的。文件HU.txt是用来辅助文件HUBEIUN.txt进行修改等操作的。
(2)对于刚开使用Dev-C++,可能在运行这段代码的时候会出现问题:
解决方法是:
打开工具->选择编译选项->在连接器命令行加入如下命令:
-lwsock32
以下代码是代码还是存在很多不足的地方,此代码没有基于控制台来写,所以看起来效果比较差,但是基本的功能实现了。
服务端代码(Server):
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<ctime>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<fstream>
#include<conio.h>
#include<iomanip>
#include<iostream>
#include<algorithm>
#include<WinSock2.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
const int maxx=105;
char chess_1='#';//玩家1
char chess_2='@';//玩家2 或者PC
char chess_layout[maxx][maxx];//用来记录棋子
char chess[maxx][maxx];//用来客服端和服务端进行棋盘信息的传输
int rate[28]={4,6,7,9,11,13,15,17,21,24,27,31,35,39,43,48,53,58,64,69,75,81,88,94,101,108,115,123};//当棋盘中的棋子数量达到一定的程度之后
int n;
string Model;
//菜单的选择
void Menu_1(){
system("color F4");
cout<<"***************************************"<<endl;
cout<<"* A -添加玩家 *"<<endl;
cout<<"* DBA-授权管理 *"<<endl;
cout<<"* T-管理信息查询 *"<<endl;
cout<<"* D-删除玩家信息 *"<<endl;
cout<<"* U-修改玩家信息 *"<<endl;
cout<<"****************************************"<<endl;
}
void Menu_2(){
system("color F4");
cout<<"***************************************"<<endl;
cout<<"* E-进入游戏 *"<<endl;
cout<<"* Q-退出游戏 *"<<endl;
cout<<"****************************************"<<endl;
}
//菜单的选择
void Select(){
system("color F4");
cout<<"***************************************"<<endl;
cout<<"* PC-人机对弈 *"<<endl;
cout<<"* P -玩家对弈 *"<<endl;
cout<<"***************************************"<<endl;
}
//对棋盘初始化
void init_chess_layout(){
system("color F4");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
chess_layout[i][j]=' ';
}
}
}
//判断玩家放置的棋子是否合法
int check(int x,int y){
system("color F4");
if(x>=1&&x<=n&&y>=1&&y<=n&&chess_layout[x][y]!='#'&&chess_layout[x][y]!='@'){
return 1;
}
return 0;
}
//棋子的放置位置
void Position(int x,int y,int flag){
system("color F0");
if(chess_layout[x][y]==' '){
if(flag==1){
chess_layout[x][y]=chess_1;
chess[2*x][5*y+1]=chess_1;
}else if(flag==2){
chess_layout[x][y]=chess_2;
chess[2*x][5*y+1]=chess_2;
}
}
}
//判断赢者
int Judge_Win(int x,int y,int flag){
system("color F0");
int win=0;
if(flag==1){
//行判断
for(int i=1;i<=n;i++){
if(chess_layout[x][i]=='#'){
win++;
}else{
win=0;
}
if(win>=5)break;
}
if(win>=5){
return flag;
}
win=0;
//列判断
for(int i=1;i<=n;i++){
if(chess_layout[i][y]=='#'){
win++;
}else{
win=0;
}
if(win>=5)break;
}
if(win>=5){
return flag;
}
//斜线判断
win=0;
//从这条斜线的起始位置判断
//当斜率为正时
int tx=x;
int ty=y;
while(tx>1&&ty>1){
tx--;
ty--;
}
while(tx<=n&&ty<=n){
if(chess_layout[tx][ty]=='#'){
win++;
}else{
win=0;
}
tx++;
ty++;
if(win>=5)break;
}
if(win>=5){
return flag;
}
//当斜率为负时
win=0;
int rx=x;
int ry=y;
while(rx>1&&ry<n){
rx--;
ry++;
}
while(rx<=n&&ry>=1){
if(chess_layout[rx][ry]=='#'){
win++;
}else{
win=0;
}
rx++;
ry--;
if(win>=5)break;
}
if(win>=5){
return flag;
}
}else if(flag==2){
//行判断
for(int i=1;i<=n;i++){
if(chess_layout[x][i]=='@'){
win++;
}else{
win=0;
}
if(win>=5)break;
}
if(win>=5){
return flag;
}
win=0;
//列判断
for(int i=1;i<=n;i++){
if(chess_layout[i][y]=='@'){
win++;
}else{
win=0;
}
if(win>=5)break;
}
if(win>=5){
return flag;
}
//斜线判断
win=0;
//从这条斜线的起始位置判断
//当斜率为正时
int tx=x;
int ty=y;
while(tx>1&&ty>1){
tx--;
ty--;
}
while(tx<=n&&ty<=n){
if(chess_layout[tx][ty]=='@'){
win++;
}else{
win=0;
}
tx++;
ty++;
if(win>=5)break;
}
if(win>=5){
return flag;
}
//当斜率为负时
win=0;
int rx=x;
int ry=y;
while(rx>1&&ry<n){
rx--;
ry++;
}
while(rx<=n&&ry>=1){
if(chess_layout[rx][ry]=='#'){
win++;
}else{
win=0;
}
rx++;
ry--;
if(win>=5)break;
}
if(win>=5){
return flag;
}
}
return 0;
}
//输出棋盘
void Display(){
system("color F0");
cout<<" ";
for(int i=1;i<=n;i++){
cout<<i<<" ";
}
cout<<endl;
for(int j=1;j<=n+1;j++){
cout<<" |";
for(int i=1;i<=n;i++){
cout<<"----|";
}
if(j==n+1)break;
cout<<endl;
if((int)log10(j)==1){
cout<<j<<" |";
}else{
cout<<j<<" |";
}
for(int i=1;i<=n;i++){
if(chess_layout[j][i]=='#'){
cout<<" "<<"●"<<" |";
}else if(chess_layout[j][i]=='@'){
cout<<" "<<"■"<<" |";
}else{
cout<<" "<<" "<<" |";
}
}
cout<<endl;
}
cout<<endl;
}
void Switch_Display_2(){
system("color F4");
for(int i=0;i<4;i++){
chess[0][i]=' ';
}
int index=4;
for(int i=1;i<=n;i++){
if(i>=10){
chess[0][index]=(char)(i/10+48);
chess[0][index+1]=(char)(i%10+48);
index++;
}else{
chess[0][index]=(char)(i+48);
}
for(int j=index+1;j<=index+4;j++){
chess[0][j]=' ';
}
index=index+5;
}
cout<<endl;
for(int j=1;j<=2*n+1;j++){
int indexs=0;
for(int i=indexs;i<indexs+3;i++){
chess[j][i]=' ';
}
chess[j][indexs+3]='|';
indexs+=4;
for(int k=1;k<=n;k++){
for(int i=indexs;i<=indexs+3;i++){
chess[j][i]='-';
}
chess[j][indexs+4]='|';
indexs+=5;
}
if(j==2*n+1)break;
indexs=0;
j++;
if((int)log10(j/2)==1){
if(j>=20){
chess[j][indexs]=(char)(j/10/2+48);
chess[j][indexs+1]=(char)(j/2%10+48);
chess[j][indexs+2]=' ';
chess[j][indexs+3]='|';
indexs+=4;
}else{
chess[j][indexs]=(char)(j/2+48);
chess[j][indexs+1]=' ';
chess[j][indexs+2]=' ';
chess[j][indexs+2]='|';
indexs+=3;
}
}else{
if(j>=20){
chess[j][indexs]=(char)(j/10/2+48);
chess[j][indexs+1]=(char)(j/2%10+48);
chess[j][indexs+2]=' ';
chess[j][indexs+3]=' ';
chess[j][indexs+4]='|';
indexs+=5;
}else{
chess[j][indexs]=(char)(j/2+48);
chess[j][indexs+1]=' ';
chess[j][indexs+2]=' ';
chess[j][indexs+3]='|';
indexs+=4;
}
}
for(int i=1;i<=n;i++){
chess[j][indexs+1]=' ';
chess[j][indexs+2]=' ';
chess[j][indexs+3]=' ';
chess[j][indexs+4]='|';
indexs+=5;
}
}
}
void init_chess(){
system("color F4");
for(int i=0;i<2*n+2;i++){
for(int j=0;j<6*n+2;j++){
chess[i][j]=' ';
}
}
Switch_Display_2();
}
//判断棋盘是否放满
int OverFlow(){
system("color F0");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(chess_layout[i][j]==' '){
return 1;
}
}
}
return 0;
}
//查找整个棋盘可以放置棋子的个数
int chess_num(){
system("color F0");
int num=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(chess_layout[i][j]==' '){
num++;
}
}
}
return num;
}
//查找为空的位置
int findposition(){
system("color F0");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(chess_layout[i][j]==' '){
return i,j;
}
}
}
}
/*
0 = 黑色 8 = 灰色
1 = 蓝色 9 = 淡蓝色
2 = 绿色 A = 淡绿色
3 = 浅绿色 B = 淡浅绿色
4 = 红色 C = 淡红色
5 = 紫色 D = 淡紫色
6 = 黄色 E = 淡黄色
7 = 白色 F = 亮白色
*/
string manager_id="19103001";
string manager_name="张文豪";
struct DBA{
int vis_id;
string ID;
string name;
string code_word;
DBA(){
vis_id=0;
}
}dba[maxx];
fstream fin;
fstream fout;
int man_num=0;//玩家数量
map<string ,string>mp;
set<string>st;
//添加玩家
void ADD_player(string id,string code_word,string name){
system("color F1");
if(st.find(id)!=st.end()){
cout<<"该用户已经存在!请重新输入!"<<endl;
return ;
}
mp[name]=id;
st.insert(id);
dba[++man_num].ID=id;
dba[man_num].name=name;
dba[man_num].code_word=code_word;
fout.open("D:\HUBEIUN.txt",ios_base::app);
fout<<0<<" "<<id<<" "<<code_word<<" "<<" "<<name<<endl;
fout.close();
cout<<"添加玩家成功!"<<endl;
}
/*
19103001
张文豪
*/
//超级管理员给二级管理员授权
//超级管理员给二级管理员授权
void Empower(string id,string code_word,string name){
system("color F1");
int flag=0;
for(int i=1;i<=man_num;i++){
if(dba[i].ID==id&&dba[i].name==name&&dba[i].code_word==code_word){
dba[i].vis_id=1;
flag=1;
break;
}
}
if(flag==0){
cout<<"授权失败!请重新输入"<<endl;
return ;
}
fstream fin_s;
fstream fout_s;
fin.open("D:\HUBEIUN.txt",ios_base::in);
fout_s.open("D:\HU.txt",ios_base::out);
string str;
string index="";
string id_1="";
string name_1="";
str=index.append("1").append(" ").append(id).append(" ").append(code_word).append(" ").append(name);
string estr="";
char tt[maxx];
while(fin.getline(tt,100)){
estr="";
for(int i=0;i<strlen(tt);i++){
estr+=tt[i];
}
id_1="";
for(int i=2;i<10;i++){
id_1+=estr[i];
}
if(id_1==id){
fout_s<<str<<endl;
}else{
fout_s<<estr<<endl;
}
}
fin.close();
fout_s.close();
string Data;
fin_s.open("D:\HU.txt",ios_base::in);
fout.open("D:\HUBEIUN.txt",ios_base::out);
char et[maxx];
while(fin_s.getline(et,100)){
Data="";
for(int i=0;i<strlen(et);i++){
Data+=et[i];
}
fout<<Data<<endl;
}
cout<<"授权成功!"<<endl;
fin_s.close();
fout.close();
}
//管理员查询玩家的信息
void Query_player(){
system("color F1");
cout<<"----------------------------------------------------------------"<<endl;
cout<<"是否为二级管理员 | 玩家账号 | 玩家密码 | 玩家姓名"<<endl;
cout<<"----------------------------------------------------------------"<<endl;
map<string,string>::iterator it;
for(int i=1;i<=man_num;i++){
cout<<" "<<dba[i].vis_id<<" |"<<" "<<dba[i].ID<<" | "<<dba[i].code_word<<" | "<<dba[i].name<<endl;
}
cout<<"----------------------------------------------------------------"<<endl;
}
//查询指定的玩家信息
int Find_player(string id,string code_word){
system("color F1");
for(int i=1;i<=man_num;i++){
if(dba[i].ID==id&&dba[i].code_word==code_word){
return i;
}
}
return 0;
}
//修改
void Update(int index,string id,string code_word,string name){
system("color F1");
fstream fin_s;
fstream fout_s;
fin.open("D:\HUBEIUN.txt",ios_base::in);
fout_s.open("D:\HU.txt",ios_base::out);
string str;
string index_str="";
string indexs="";
string indext="";
if(dba[index].vis_id==1){
str=indexs.append("1").append(" ").append(dba[index].ID).append(" ").append(dba[index].code_word).append(" ").append(dba[index].name);
index_str=indext.append("1").append(" ").append(id).append(" ").append(code_word).append(" ").append(name);
}else{
str=indexs.append("0").append(" ").append(dba[index].ID).append(" ").append(dba[index].code_word).append(" ").append(dba[index].name);
index_str=indext.append("0").append(" ").append(id).append(" ").append(code_word).append(" ").append(name);
}
string estr="";
string stdc="";
char tt[maxx];
while(fin.getline(tt,100)){
estr="";
for(int i=0;i<strlen(tt);i++){
estr+=tt[i];
}
stdc="";
for(int i=0;i<estr.length();i++){
stdc+=estr[i];
}
if(stdc==str){
fout_s<<index_str<<endl;
}else{
fout_s<<estr<<endl;
}
}
fin.close();
fout_s.close();
string Data;
fin_s.open("D:\HU.txt",ios_base::in);
fout.open("D:\HUBEIUN.txt",ios_base::out);
char ty[maxx];
while(fin_s.getline(ty,100)){
Data="";
for(int i=0;i<strlen(ty);i++){
Data+=ty[i];
}
fout<<Data<<endl;
}
fin_s.close();
fout.close();
}
//玩家修改信息
int Player_Update(string id,string code_word,string name){
system("color F1");
int index;
int flag=0;
for(int i=1;i<=man_num;i++){
if(dba[i].ID==id&&dba[i].code_word==code_word&&dba[i].name==name){
index=i;
flag=1;
}
}
if(flag==1){
cout<<"请玩家输入新的密码和姓名(code/name): ";
string code_word_1;
string name_1;
cin>>code_word_1>>name_1;
Update(index,id,code_word_1,name_1);
cout<<"玩家修改个人信息成功!"<<endl;
return 1;
}else{
cout<<"密码或者账户错误! 请玩家重新输入个人账号 密码和姓名!"<<endl;
return 0;
}
}
//首选需要输入超级管理员的身份,查看当前二级管理员(或普通玩家)并作修改
void Manager(){
system("color F1");
string id_1;
string name_1;
string code_word="";
int flag_1=0;
int flag_2=0;
cout<<"请问您需要查看当前二级管理员的信息吗?(YES(yes) or NO(no)): ";
cin>>flag_1;
if(flag_1==1){
cout<<"------------------------------"<<endl;
cout<<"二级玩家账号 | 玩家姓名 "<<endl;
cout<<"------------------------------"<<endl;
for(int i=1;i<=man_num;i++){
if(dba[i].vis_id==1){
cout<<dba[i].ID<<" | "<<dba[i].name<<endl;
}
}
cout<<"------------------------------"<<endl;
}
cout<<"请问您需要查看当前普通玩家的信息吗?(YES(yes) or NO(no)): ";
cin>>flag_2;
if(flag_2==1){
Query_player();
}
cout<<"请问您需要修改玩家信息吗?(yes(YES) or no(NO))"<<endl;
string flag;
cin>>flag;
if(flag=="no"||flag=="NO"){
return ;
}else{
cout<<"请输入需要修改的玩家账号(id): ";
cin>>id_1;
map<string,string>::iterator it;
for(int i=1;i<=man_num;i++){
if(dba[i].ID==id_1){
cout<<"请输入修改之后的玩家姓名和密码(code/name): ";
cin>>code_word>>name_1;
cout<<"请问您确定要修改玩家的信息吗?(YES(yes) or NO(no))"<<endl;
string rt;
cin>>rt;
if(rt=="YES"||rt=="yes"){
for(it=mp.begin();it!=mp.end();it++){
if(it->second==id_1){
mp.erase(it);
mp[name_1]=it->second;
Update(i,id_1,code_word,name_1);
dba[i].code_word=code_word;
dba[i].name=name_1;
cout<<"修改玩家信息成功!"<<endl;
break;
}
}
return ;
}else{
return ;
}
}
}
}
}
//请求帮助
void G(){
system("color F2");
cout<<"*****************************************************************"<<endl;
cout<<"| 同一种棋子必须在同一行或同一列或同一斜线的数等于5(包括大于5),|"<<endl;
cout<<"| 先达到的一方获胜. |"<<endl;
cout<<"*****************************************************************"<<endl;
}
void Code_Accounts(){
system("color F2");
cout<<"******************************************************************"<<endl;
cout<<"| 账户的位数必须是8位的数字或者大小字母混合. |"<<endl;
cout<<"| 密码的位数必须是6位的数字或者大小字母混合. |"<<endl;
cout<<"******************************************************************"<<endl;
}
void Help(){
system("color F2");
cout<<"以下是可能您要找的内容"<<endl;
cout<<"---------------------------------------------"<<endl;
cout<<" G-游戏规则 "<<endl;
cout<<" CA-账号8位 "<<endl;
cout<<"---------------------------------------------"<<endl;
}
//选择要对弈的玩家 ,如果匹配成功,则返回1,否则返回0
int Match_player(string name){
system("color F1");
for(int i=1;i<=man_num;i++){
if(dba[i].name==name){
cout<<"匹配成功!开始游戏!"<<endl;
return 1;
}
}
return 0;
}
//判断是不是一级管理员
int One_Manager(string id,string code_word,string name){
system("color F1");
if(id==manager_id&&name==manager_name&&code_word=="123456"){
return 1;
}
return 0;
}
//判断是不是二级管理员
int Judge_second_manager(string id,string code_word,string name){
system("color F1");
for(int i=1;i<=man_num;i++){
if((dba[i].ID==id&&dba[i].name==name&&dba[i].code_word==code_word)){
return 1;
}
}
return 0;
}
//查询玩家姓名
void query_name(){
system("color F1");
map<string,string>::iterator it;
it=mp.begin();it++;
for(;it!=mp.end();it++){
cout<<it->first<<endl;
}
}
//超级管理员对玩家的信息进行删除
void Delete_player(string id,string code_word,string name){
system("color F1");
map<string,string>::iterator it;
it=mp.begin();it++;
for(;it!=mp.end();it++){
if(it->second==id){
mp.erase(it);
break;
}
}
int index=1;
for(int i=1;i<=man_num;i++){
if(dba[i].ID==id){
index=i;
break;
}
}
for(int i=index;i<man_num;i++){
dba[i]=dba[i+1];
}
fstream fin_s;
fstream fout_s;
fin.open("D:\HUBEIUN.txt",ios_base::in);
fout_s.open("D:\HU.txt",ios_base::out);
string str;
string indexs="";
string id_1="";
string name_1="";
if(dba[index].vis_id==1){
str=indexs.append("1").append(" ").append(id).append(" ").append(code_word).append(" ").append(name);
}else{
str=indexs.append("0").append(" ").append(id).append(" ").append(code_word).append(" ").append(name);
}
string estr="";
char tt[maxx];
while(fin.getline(tt,100)){
for(int i=0;i<strlen(tt);i++){
estr+=tt[i];
}
if(!estr.compare(str)){
continue;
}
fout_s<<estr<<endl;
}
fin.close();
fout_s.close();
string Data;
fin_s.open("D:\HU.txt",ios_base::in);
fout.open("D:\HUBEIUN.txt",ios_base::out);
char ty[maxx];
while(fin_s.getline(ty,100)){
Data="";
for(int i=0;i<strlen(ty);i++){
Data+=ty[i];
}
fout<<Data<<endl;
}
fin_s.close();
fout.close();
cout<<"删除成功!"<<endl;
}
//将文件中的数据存储到数组中
void Switch(){
system("color F2");
char data[100];
string id="";
string name="";
string vis="";
string code_word="";
fin.open("D:\HUBEIUN.txt",ios_base::in);
while(!fin.eof()){
fin.getline(data,100);
id="";
name="";
code_word="";
for(int i=2;i<10;i++){
if(data[i]!=' '){
id+=data[i];
}
}
for(int i=11;i<=16;i++){
code_word+=data[i];
}
for(int i=17;i<strlen(data);i++){
if(data[i]!=' '){
name+=data[i];
}
}
dba[++man_num].ID=id;
dba[man_num].name=name;
dba[man_num].code_word=code_word;
if(data[0]=='1'){
dba[man_num].vis_id=1;
}else{
dba[man_num].vis_id=0;
}
mp[name]=id;
st.insert(id);
}
fin.close();
}
//超级管理员拥有的权力
void Manager_1(){
system("color F1");
string flag;
string begin="";
int overflow=0;
string win="";
string id_1;
string name_1;
string mag_id;
string mag_name;
string manager_1;
string manager_2;
cout<<"添加玩家: ";
string T1;
string code_word;
cin>>T1;
if(T1=="A"){
cout<<"请问您需要添加玩家吗?(YES(yes) or NO(no): ";
string sym;
cin>>sym;
int flags=0;
if(sym=="YES"||sym=="yes"){
while(true){
cout<<"请输入要添加的玩家账号 密码和姓名: ";
cin>>id_1>>code_word>>name_1;
ADD_player(id_1,code_word,name_1);
cout<<"请问您还需要添加玩家吗?(YES(yes) or NO(no): ";
string sym;
cin>>sym;
if(sym=="YES"||sym=="yes"){
continue;
}else{
break;
}
}
}
}
/*
19103001 张文豪
*/
cout<<"授权管理: ";
string T2;
cin>>T2;
if(T2=="DBA"){
while(true){
string id_mag,name_mag;
string id_player,name_player;
cout<<"请输入要授权的玩家身份(id/code/name): ";
cin>>id_player;
cin>>code_word;
cin>>name_player;
Empower(id_player,code_word,name_player);
cout<<"请问您要继续操作吗?(YES(yes) or NO(no)): ";
string flag_2;
cin>>flag_2;
if(flag_2=="YES"||flag_2=="yes"){
continue;
}else if(flag_2=="NO"||flag_2=="no"){
break;
}
}
}
cout<<"修改玩家信息查询: ";
string T3;
cin>>T3;
if(T3=="U"){
while(true){
cout<<"是否进行管理信息查询(YES(yes) or NO(no): ";
string sym;
cin>>sym;
if(sym=="YES"||sym=="yes"){
Manager();
}else if(sym=="no"||sym=="NO"){
break;
}
}
}
map<string,string>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
fout<<it->second<<" "<<it->first<<endl;
}
cout<<"管理信息查询: ";
string T6;
cin>>T6;
if(T6=="T"){
Query_player();
}
cout<<"删除玩家信息: ";
string T7;
cin>>T7;
if(T7=="D"){
cout<<"请输入玩家的身份(id/code/name): ";
string del_id;
string del_name;
while(true){
cin>>del_id>>code_word>>del_name;
Delete_player(del_id,code_word,del_name);
cout<<"是否继续操作?(YES(yes) or NO(no)): ";
string del;
cin>>del;
if(del=="YES"||del=="yes"){
continue;
}else{
break;
}
}
}
}
//二级管理员拥有的权力
void Manager_2(){
system("color F3");
cout<<"管理信息查询: ";
string T6;
cin>>T6;
if(T6=="T"){
while(true){
cout<<"请输入管理员信息: ";
string T_id;
string T_name;
cin>>T_id>>T_name;
Query_player();
}
}
}
void One_manager_operation(){
system("color F1");
Menu_1();
string id;
string name;
string code_word;
// Judge_second_manager
cout<<"请问您需要选择身份吗?(超级管理员/二级管理员)(yes(YES) or NO(no)): ";
string tt;
cin>>tt;
if(tt=="no"||tt=="NO"){
return ;
}
while(true){
cout<<"请输入您的账号 密码和姓名: ";
cin>>id>>code_word>>name;
if(One_Manager(id,code_word,name)){
Manager_1();
cout<<endl;
}else if(Judge_second_manager(id,code_word,name)){
Manager_2();
cout<<endl;
}
cout<<"请问您还需要继续上述操作吗?(YES(yes) or NO(no)): ";
string log;
cin>>log;
if(log=="YES"||log=="yes"){
continue;
}else{
break;
}
}
}
int Log(){
system("color F2");
string id_1;
string name_1;
Switch();
int Log_flag=0;
int Log_cnt=0;
string code_word="";
cout<<" 登录 "<<endl;
while(true){
cout<<"账号: ";
cin>>id_1;
cout<<"密码: ";
cin>>code_word;
cout<<"姓名: ";
cin>>name_1;
Log_cnt++;
int flag=0;
for(int i=1;i<=man_num;i++){
if(dba[i].ID==id_1&&dba[i].code_word==code_word&&dba[i].name==name_1){
flag=1;
break;
}
}
if(flag==0){
if(Log_cnt==3){
cout<<"抱歉,您已输入过三次,您将没有机会输入!"<<endl;
Log_flag=1;
break;
}
cout<<"您输入的账号或者姓名错误!"<<endl;
cout<<"您还有"<<3-Log_cnt<<"次登录机会,"<<"请重新输入您的账号和姓名!"<<endl;
continue;
}else{
cout<<"登录成功!"<<endl;
break;
}
}
cout<<endl;
if(Log_flag==0){
One_manager_operation();
}else{
cout<<"您不能再登录此系统!"<<endl;
return 0;
}
}
int Sever(){
system("color F1");
string name;
string flag;
//初始化WSA
init_chess_layout();
init_chess();
WORD sockVersion = MAKEWORD(2, 2);
WSADATA wsaData;
if (WSAStartup(sockVersion, &wsaData) != 0) {
return 0;
}
//创建套接字,构造监听SOCKET,SOCKET
SOCKET slisten =socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (slisten == INVALID_SOCKET) {
cout << "socket error!" << endl;
return 0;
}
//绑定IP和端口
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(4443);
sin.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR) {
cout << "bind error" << endl;
return 0;
}
//绑定成功之后开始监听
if (listen(slisten, 5) == SOCKET_ERROR) {
cout << "listen error" << endl;
return 0;
}
SOCKET sClient;
Switch_Display_2();
sockaddr_in remoteAddr;
int nAddrlen = sizeof(remoteAddr);
char revData[2550];
//循环接收数据
while (true) {
while(true){
cout<<"请输入要匹配的玩家姓名: ";
cin>>name;
init_chess_layout();
init_chess();
if(Match_player(name)){
int win_1[5];
int win_2[5];
memset(win_1,0,sizeof(win_1));
memset(win_2,0,sizeof(win_2));
while(true){
init_chess_layout();
init_chess();
memset(revData,0,sizeof(revData));
cout << "等待玩家1输入..." << endl;
sClient = accept(slisten, (SOCKADDR*)&remoteAddr, &nAddrlen);
if (sClient == INVALID_SOCKET) {
cout << "accept error!" << endl;
continue;
}
int st=0;
while(true){
int x,y;
int tag[5];
memset(tag,0,sizeof(tag));
if(!OverFlow())break;
memset(revData,0,sizeof(revData));
cout << "等待玩家1输入..." << endl;
sClient = accept(slisten, (SOCKADDR*)&remoteAddr, &nAddrlen);
if (sClient == INVALID_SOCKET) {
cout << "accept error!" << endl;
continue;
}
//接收数据
printf("接收到一个连接: %s rn", inet_ntoa(remoteAddr.sin_addr));
int ret = recv(sClient, revData, 2550, 0);
n=0;
if(ret!=0){
for(int i=0;i<strlen(revData)-2;i++){
if(revData[i]>='0'&&revData[i]<='9'){
int index=revData[i]-'0';
if(index>n){
n=index;
}
}
if(revData[i]=='P'){
cout<<endl;
}else{
cout<<revData[i];
}
}
}
int i=0,j=0;
for(int k=0;k<strlen(revData);k++){
if(revData[k]=='P'){
i++;
j=0;
}else{
chess[i][j]=revData[k];
j++;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(chess[2*i][5*j+1]=='#'||chess[2*i][5*j+1]=='@'){
chess_layout[i][j]=chess[2*i][5*j+1];
}else{
chess_layout[i][j]=' ';
}
}
}
//发送数据s
cout<<endl;
while(true){
Display();
int x,y;
cout<<"玩家2输入坐标: ";
cin>>x>>y;
if(check(x,y)){
Position(x,y,2);
tag[0]=Judge_Win(x,y,2);
if(tag[0]==2){
win_2[0]++;
}else if(tag[0]==1){
win_1[0]++;
}
system("cls");
break;
}else{
system("cls");
cout<<"玩家2输入的坐标不合法"<<endl;
}
}
Display();
char sendData[1024];
memset(sendData,0,sizeof(sendData));
int k=0;
for(int i=0;i<2*n+2;i++){
for(int j=0;j<6*n+2;j++){
if(chess[i][j]!='