服务器
#include <stdio.h>
#include <getch.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
char board[15][15];
char role = '@';
char key_x = 7,key_y = 7;
int sockfd;
struct sockaddr_in from_addr;
socklen_t addrlen = sizeof(from_addr);
typedef struct sockaddr* SP;
int init_board(short port)
{
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(0 > sockfd)
{
perror("socket");
return EXIT_SUCCESS;
}
struct sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if(bind(sockfd,(SP)&addr,sizeof(addr)))
{
perror("bind");
return EXIT_FAILURE;;
}
for(int i=0; i<15; i++)
{
for(int j=0; j<15; j++)
{
board[i][j] = '*';
}
}
return sockfd;
}
void show_board(void)
{
system("clear");
for(int i=0; i<15; i++)
{
for(int j=0; j<15; j++)
{
printf(" %c",board[i][j]);
}
printf("\n");
}
}
void get_key(void)
{
char buf[256] = {};
if(role == '@')
{
printf("等待对方落子...\n");
int ret = recvfrom(sockfd,buf,sizeof(buf),0,(SP)&from_addr,&addrlen);
if(0 >= ret)
{
printf("对方已逃跑,你赢了!\n");
close(sockfd);
exit(EXIT_SUCCESS);
}
sscanf(buf,"%hhd %hhd",&key_x,&key_y);
board[key_x][key_y] = '$';
}
else
{
printf("请你落子...\n");
for(;;)
{
printf("\33[%d;%dH",key_x+1,(key_y+1)*2);
switch(getch())
{
case 183:
key_x > 0 && key_x--; break;
case 184:
key_x < 14 && key_x++; break;
case 185:
key_y < 14 && key_y++; break;
case 186:
key_y > 0 && key_y--; break;
case 10:
if('*' == board[key_x][key_y])
{
board[key_x][key_y] = '@';
sprintf(buf,"%hhd %hhd",key_x,key_y);
int ret = sendto(sockfd,buf,strlen(buf)+1,0,(SP)&from_addr,addrlen);
if(0 >= ret)
{
printf("你已掉线,等待重新连接...\n");
close(sockfd);
exit(EXIT_FAILURE);
}
return;
}
}
}
}
}
int count_board(int go_x,int go_y)
{
int count = 0;
for(int x=key_x+go_x,y=key_y+go_y;
x>=0 && y>=0 && x<15 && y<15;
x+=go_x,y+=go_y)
{
if(board[x][y] == board[key_x][key_y])
{
count++;
}
else
{
break;
}
}
return count;
}
bool is_win(void)
{
if(count_board(0,-1) + count_board(0,1)>=4)
{
return true;
}
if(count_board(-1,0) + count_board(1,0)>=4)
{
return true;
}
if(count_board(-1,-1) + count_board(1,1)>=4)
{
return true;
}
if(count_board(1,-1) + count_board(-1,1)>=4)
{
return true;
}
return false;
}
int main(int argc,const char* argv[])
{
if(2 != argc)
{
printf("USER: ./gobang port\n");
return EXIT_SUCCESS;
}
if(0 >= init_board(atoi(argv[1])))
{
perror("初始化网络错误!\n");
return EXIT_FAILURE;
}
for(int i=0; i<225; i++)
{
show_board();
get_key();
if(is_win())
{
show_board();
printf("游戏胜利,%c赢了\n",board[key_x][key_y]);
return 0;
}
role = role == '@'?'$':'@';
}
}
客户端
#include <stdio.h>
#include <getch.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
char board[15][15];
char role = '$';
char key_x = 7,key_y = 7;
int sockfd;
struct sockaddr_in dest_addr;
socklen_t addrlen = sizeof(dest_addr);
typedef struct sockaddr* SP;
int init_board(const char* ip,short port)
{
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(0 > sockfd)
{
perror("socket");
return EXIT_FAILURE;
}
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(port);
dest_addr.sin_addr.s_addr = inet_addr(ip);
for(int i=0; i<15; i++)
{
for(int j=0; j<15; j++)
{
board[i][j] = '*';
}
}
return sockfd;
}
void show_board(void)
{
system("clear");
for(int i=0; i<15; i++)
{
for(int j=0; j<15; j++)
{
printf(" %c",board[i][j]);
}
printf("\n");
}
}
void get_key(void)
{
char buf[256] = {};
if(role == '@')
{
printf("等待对方落子...\n");
int ret = recvfrom(sockfd,buf,sizeof(buf),0,(SP)&dest_addr,&addrlen);
if(0 >= ret)
{
printf("对方已逃跑,你赢了!\n");
close(sockfd);
exit(EXIT_SUCCESS);
}
sscanf(buf,"%hhd %hhd",&key_x,&key_y);
board[key_x][key_y] = '@';
}
else
{
printf("请你落子...\n");
for(;;)
{
printf("\33[%d;%dH",key_x+1,(key_y+1)*2);
switch(getch())
{
case 183:
key_x > 0 && key_x--; break;
case 184:
key_x < 14 && key_x++; break;
case 185:
key_y < 14 && key_y++; break;
case 186:
key_y > 0 && key_y--; break;
case 10:
if('*' == board[key_x][key_y])
{
board[key_x][key_y] = '$';
sprintf(buf,"%hhd %hhd",key_x,key_y);
int ret = sendto(sockfd,buf,strlen(buf)+1,0,(SP)&dest_addr,addrlen);
if(0 >= ret)
{
printf("你已掉线,等待重新连接...\n");
close(sockfd);
exit(EXIT_FAILURE);
}
return;
}
}
}
}
}
int count_board(int go_x,int go_y)
{
int count = 0;
for(int x=key_x+go_x,y=key_y+go_y;
x>=0 && y>=0 && x<15 && y<15;
x+=go_x,y+=go_y)
{
if(board[x][y] == board[key_x][key_y])
{
count++;
}
else
{
break;
}
}
return count;
}
bool is_win(void)
{
if(count_board(0,-1) + count_board(0,1)>=4)
{
return true;
}
if(count_board(-1,0) + count_board(1,0)>=4)
{
return true;
}
if(count_board(-1,-1) + count_board(1,1)>=4)
{
return true;
}
if(count_board(1,-1) + count_board(-1,1)>=4)
{
return true;
}
return false;
}
int main(int argc,const char* argv[])
{
if(3 != argc)
{
printf("USER: ./gobang ip port\n");
return EXIT_SUCCESS;
}
if(0 >= init_board(argv[1],atoi(argv[2])))
{
perror("初始化网络错误!\n");
return EXIT_FAILURE;
}
for(int i=0; i<225; i++)
{
show_board();
get_key();
if(is_win())
{
show_board();
printf("游戏胜利,%c赢了\n",board[key_x][key_y]);
return 0;
}
role = role == '@'?'$':'@';
}
}
|