准备工作:
(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 \r\n", 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]!='\0'){
sendData[k++]=chess[i][j];
}else{
sendData[k++]=' ';
}
}
sendData[k++]='P';
}
cout << endl;
send(sClient, sendData, strlen(sendData), 0);
cout<<endl;
closesocket(sClient);
}
cout<<"玩家1当前得分 —"<<win_1[0]<<"分"<<endl;
cout<<"玩家2当前得分 —"<<win_2[0]<<"分"<<endl;
cout<<"是否继续比赛(yes(YES) or no(NO))"<<endl;
cin>>flag;
if(flag=="no"||flag=="NO"){
if(win_1[0]==win_2[0]){
cout<<"玩家1和玩家2平局"<<endl;
}else if(win_1[0]>win_2[0]){
cout<<"恭喜玩家1获胜"<<endl;
}else if(win_1[0]<win_2[0]){
cout<<"恭喜玩家2获胜"<<endl;
}
win_1[0]=0;
win_2[0]=0;
break;
}else if(flag=="yes"||flag=="YES"){
init_chess_layout();
init_chess();
system("cls");
continue;
}
}
}else{
cout<<"匹配失败"<<endl;
}
cout<<"是否还要匹配其他玩家继续游戏!(YES(yes) or NO(no): ";
string come;
cin>>come;
if(come=="YES"||come=="yes"){
continue;
}else{
cout<<"结束游戏!"<<endl;
return 0;
}
}
}
closesocket(slisten);
WSACleanup();
}
int start(){
system("color F4");
string flag;
string begin="";
int overflow=0;
string win="";
if(!Log()){
return 0;
}
/*管理员
string manager_id="19103001";
string manager_name="张文豪";
*/
Menu_2();
cout<<"玩家是否进入游戏: ";
cin>>begin;
init_chess_layout();
init_chess();
if(begin=="E"){
Help();
cout<<"请问您是需要帮助吗?(YES(yes) or NO(no)): ";
cin>>flag;
if(flag=="YES"||flag=="yes"){
cout<<"请输入您需要的内容: ";
string g;
cin>>g;
if(g=="G"){
G();
}else if(g=="CA"){
Code_Accounts();
}
}
while(true){
cout<<"玩家需要修改个人信息吗?(YES(yes) or NO(no)): ";
cin>>flag;
if(flag=="YES"||flag=="yes"){
cout<<"请玩家输入账号 密码和姓名(id/code/name): ";
int tt=0;
while(!tt){
string id;
string name;
string code_word;
cin>>id>>code_word>>name;
tt=Player_Update(id,code_word,name);
}
}
cout<<"请输入棋盘的大小: ";
cin>>n;
cout<<" 请玩家选择游戏模式 "<<endl;
Select();
cin>>Model;
string name;
if(Model=="P"){
cout<<"请玩家是否要查看其他的玩家?(YES(yes) or NO(no))"<<endl;
string q;
cin>>q;
if(q=="YES"||q=="yes"){
cout<<"-----------"<<endl;
query_name();
cout<<"-----------"<<endl;
}
Sever();
}else if(Model=="PC"){
int win_1=0;
int win_2=0;
while(true){
init_chess_layout();
init_chess();
system("color F4");
overflow=0;
while(true){
int x,y;
int tag=0;
if(!OverFlow())break;
while(true){
Display();
cout<<"玩家输入坐标: ";
cin>>x>>y;
if(check(x,y)){
Position(x,y,1);
tag=Judge_Win(x,y,1);
if(tag==1){
win_1++;
}
system("cls");
break;
}else{
system("cls");
cout<<"玩家输入的坐标不合法"<<endl;
}
}
if(tag==0){
Display();
int x = 0;
int y = 0;
if(!OverFlow()){
overflow=1;
break;
}
if(chess_num()<=rate[n-5]){
x,y=findposition();
}else{
while (1){ //循环查找空位置
x = (rand() % n) + 1; //产生从1~N的随机数
srand((unsigned int)time(NULL));
y = (rand() % n) + 1; //产生从1~N的随机数
srand((unsigned int)time(NULL));
if (check(x,y)){ //如果这个位置为空(没有棋子),跳出循环,下棋
break;
}
}
}
Position(x,y,2);
tag=Judge_Win(x,y,2);
if(tag==2){
win_2++;
}
system("cls");
}
if(tag==1||tag==2||overflow==1)break;
}
Display();
cout<<"玩家当前得分 —"<<win_1<<"分"<<endl;
cout<<"PC-当前得分 —"<<win_2<<"分"<<endl;
cout<<"是否继续比赛(yes(YES) or no(NO))"<<endl;
cin>>flag;
if(flag=="no"||flag=="NO"){
if(win_1==win_2){
cout<<"玩家和PC平局"<<endl;
}else if(win_1>win_2){
cout<<"恭喜玩家获胜"<<endl;
}else if(win_1<win_2){
cout<<"PC获胜"<<endl;
}
win_1=0;
win_2=0;
break;
}else if(flag=="YES"||flag=="yes"){
init_chess_layout();
init_chess();
system("cls");
continue;
}
}
}
cout<<"请问您要退出游戏吗?(yes(YES) or NO(no): ";
string rt;
cin>>rt;
if(rt=="YES"||rt=="yes"){
break;
}
One_manager_operation();
}
}
}
int main(){
start();
return 0;
}
客户端代码(Socket):
#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){
system("color F");
chess[2*x][5*y+1]=chess_1;
chess_layout[x][y]=chess_1;
}else if(flag==2){
system("color F");
chess[2*x][5*y+1]=chess_2;
chess_layout[x][y]=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();
}
void show(){
system("color F0");
for(int i=0;i<2*n+2;i++){
for(int j=0;j<6*n+2;j++){
if(chess_layout[i][j]=='#'){
cout<<"●";
}else if(chess_layout[i][j]=='@'){
cout<<"■";
}else{
cout<<chess_layout[i][j];
}
}
cout<<endl;
}
}
//判断棋盘是否放满
int OverFlow(){
system("color F0");
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(chess[2*i][5*j+1]==' '){
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];
}
cout<<id_1<<endl;
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="";
string flag_1;
string flag_2;
cout<<"请问您需要查看当前二级管理员的信息吗?(YES(yes) or NO(no)): ";
cin>>flag_1;
if(flag_1 == "YES"||flag_1=="yes"){
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 == "YES"||flag_2=="yes"){
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 Socket(){
system("color F1");
string name;
string flag;
WORD sockVersion = MAKEWORD(2, 2);
WSADATA data;
if (WSAStartup(sockVersion, &data) != 0) {
return 0;
}
SOCKET sclient;
while (true) {
//构造通讯SOCKET 流式SOCKET
sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sclient == INVALID_SOCKET) {
cout << "invlaid socket!" << endl;
return 0;
}
//配置要连接的IP和端口
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(4443);
serAddr.sin_addr.S_un.S_addr = inet_addr("192.168.43.134");
//连接失败
if (connect(sclient, (sockaddr*)&serAddr, sizeof(serAddr)) == SOCKET_ERROR) {
cout << "connect error!" << endl;
closesocket(sclient);
return 0;
}
int vis=0;
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){
int win=0;
while(true){
system("color 0F");
//构造通讯SOCKET 流式SOCKET
sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sclient == INVALID_SOCKET) {
cout << "invlaid socket!" << endl;
return 0;
}
//配置要连接的IP和端口
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(4443);
serAddr.sin_addr.S_un.S_addr = inet_addr("192.168.43.134");
//连接失败
if (connect(sclient, (sockaddr*)&serAddr, sizeof(serAddr)) == SOCKET_ERROR) {
cout << "connect error!" << endl;
closesocket(sclient);
return 0;
}
int x,y;
int tag[maxx];
memset(tag,0,sizeof(tag));
if(!OverFlow())break;
while(true){
Display();
int x,y;
cout<<"玩家1输入坐标: ";
cin>>x>>y;
if(check(x,y)){
Position(x,y,1);
tag[0]=Judge_Win(x,y,1);
if(tag[0]==1){
win_1[0]++;
}else if(tag[0]==2){
win_2[0]++;
}
system("cls");
break;
}else{
system("cls");
cout<<"玩家1输入的坐标不合法"<<endl;
}
}
char sendData[2550];
memset(sendData,0,sizeof(sendData));
Display();
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]!='\0'){
sendData[k++]=chess[i][j];
}else{
sendData[k++]=' ';
}
}
sendData[k++]='P';
}
send(sclient, sendData,strlen(sendData), 0);
if(tag[0]==1||tag[0]==2){
break;
}
cout<<"等待玩家2输入..."<<endl;
char revData[1024];
memset(revData,0,sizeof(revData));
int ret = recv(sclient, revData, 2550, 0);
if(ret!=0){
for(int i=0;i<strlen(revData)-2;i++){
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]=' ';
}
}
}
cout<<endl;
}
cout<<endl;
cout<<"玩家1当前得分 —"<<win_1[0]<<"分"<<endl;
cout<<"玩家2当前得分 —"<<win_2[0]<<"分"<<endl;
cout<<"是否继续比赛(yes(YES) or no(NO))"<<endl;
cin>>flag;
if(flag=="no"||flag=="NO"){
if(win_1[0]==win_2[0]){
cout<<"玩家1和玩家2平局"<<endl;
}else if(win_1[0]>win_2[0]){
cout<<"恭喜玩家1获胜"<<endl;
}else if(win_1[0]<win_2[0]){
cout<<"恭喜玩家2获胜"<<endl;
}
win_1[0]=0;
win_2[0]=0;
break;
}else if(flag=="yes"||flag=="YES"){
init_chess_layout();
init_chess();
system("cls");
continue;
}
}
}else{
cout<<"匹配失败"<<endl;
}
cout<<"是否还要匹配其他玩家继续游戏!(YES(yes) or NO(no): ";
string come;
cin>>come;
if(come=="YES"||come=="yes"){
continue;
}else{
cout<<"结束游戏!"<<endl;
return 0;
}
}
}
closesocket(sclient);
WSACleanup();
}
int start(){
system("color F4");
string flag;
string begin="";
int overflow=0;
string win="";
if(!Log()){
return 0;
}
/*管理员
string manager_id="19103001";
string manager_name="张文豪";
*/
Menu_2();
cout<<"玩家是否进入游戏: ";
cin>>begin;
init_chess_layout();
init_chess();
if(begin=="E"){
Help();
cout<<"请问您是需要帮助吗?(YES(yes) or NO(no)): ";
cin>>flag;
if(flag=="YES"||flag=="yes"){
cout<<"请输入您需要的内容: ";
string g;
cin>>g;
if(g=="G"){
G();
}else if(g=="CA"){
Code_Accounts();
}
}
while(true){
cout<<"玩家需要修改个人信息吗?(YES(yes) or NO(no)): ";
cin>>flag;
if(flag=="YES"||flag=="yes"){
cout<<"请玩家输入账号 密码和姓名(id/code/name): ";
int tt=0;
while(!tt){
string id;
string name;
string code_word;
cin>>id>>code_word>>name;
tt=Player_Update(id,code_word,name);
}
}
cout<<"请输入棋盘的大小: ";
cin>>n;
cout<<" 请玩家选择游戏模式 "<<endl;
Select();
cin>>Model;
string name;
if(Model=="P"){
cout<<"请玩家是否要查看其他的玩家?(YES(yes) or NO(no))"<<endl;
string q;
cin>>q;
if(q=="YES"||q=="yes"){
cout<<"-----------"<<endl;
query_name();
cout<<"-----------"<<endl;
}
Socket();
}else if(Model=="PC"){
int win_1=0;
int win_2=0;
while(true){
init_chess_layout();
init_chess();
system("color F4");
overflow=0;
while(true){
int x,y;
int tag=0;
if(!OverFlow())break;
while(true){
Display();
cout<<"玩家输入坐标: ";
cin>>x>>y;
if(check(x,y)){
Position(x,y,1);
tag=Judge_Win(x,y,1);
if(tag==1){
win_1++;
}
system("cls");
break;
}else{
system("cls");
cout<<"玩家输入的坐标不合法"<<endl;
}
}
if(tag==0){
Display();
int x = 0;
int y = 0;
if(!OverFlow()){
overflow=1;
break;
}
if(chess_num()<=rate[n-5]){
x,y=findposition();
}else{
while (1){ //循环查找空位置
x = (rand() % n) + 1; //产生从1~N的随机数
srand((unsigned int)time(NULL));
y = (rand() % n) + 1; //产生从1~N的随机数
srand((unsigned int)time(NULL));
if (check(x,y)){ //如果这个位置为空(没有棋子),跳出循环,下棋
break;
}
}
}
Position(x,y,2);
tag=Judge_Win(x,y,2);
if(tag==2){
win_2++;
}
system("cls");
}
if(tag==1||tag==2||overflow==1)break;
}
Display();
cout<<"玩家当前得分 —"<<win_1<<"分"<<endl;
cout<<"PC-当前得分 —"<<win_2<<"分"<<endl;
cout<<"是否继续比赛(yes(YES) or no(NO))"<<endl;
cin>>flag;
if(flag=="no"||flag=="NO"){
if(win_1==win_2){
cout<<"玩家和PC平局"<<endl;
}else if(win_1>win_2){
cout<<"恭喜玩家获胜"<<endl;
}else if(win_1<win_2){
cout<<"PC获胜"<<endl;
}
win_1=0;
win_2=0;
break;
}else if(flag=="YES"||flag=="yes"){
init_chess_layout();
init_chess();
system("cls");
continue;
}
}
}
cout<<"请问您要退出游戏吗?(yes(YES) or NO(no): ";
string rt;
cin>>rt;
if(rt=="YES"||rt=="yes"){
break;
}
One_manager_operation();
}
}
}
int main(){
start();
return 0;
}
首先是服务端运行其运行起来,以下是服务端首先运行的结果(Server):
客户端运行起来(Socket):
下面是服务端客户端进行下棋的结果:
如果有什么错误的地方请指出!谢谢!