Advertisement
tuanln

CaroServer

Dec 24th, 2015
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.42 KB | None | 0 0
  1. // CaroServer.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <WinSock2.h>
  8. #include <WS2tcpip.h>
  9.  
  10. #pragma comment(lib,"ws2_32.lib")
  11.  
  12. struct Player
  13. {
  14.     SOCKET c;
  15.     SOCKADDR_IN cAddr;
  16.     char nickname[32];
  17.     bool isState = 0;
  18.     int roomNum;
  19.     bool permission = 0;
  20. };
  21.  
  22. struct Room
  23. {
  24.     Player * player1 = 0;
  25.     Player * player2 = 0;
  26.     int map[100][100];
  27. };
  28.  
  29. SOCKET s;
  30. SOCKADDR_IN sAddr;
  31. Player * player[1024];
  32. Room * room[512];
  33. int nPlayer = 0, nRoom = 0;
  34.  
  35. DWORD WINAPI PlayerThread(LPVOID Param);
  36.  
  37. int isGameOver(int x, int y, int roomNum);
  38.  
  39. int main()
  40. {
  41.     WSADATA wsadata;
  42.     int ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
  43.    
  44.     s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  45.     sAddr.sin_family = AF_INET;
  46.     sAddr.sin_port = htons(8888);
  47.     sAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  48.  
  49.     ret = bind(s, (sockaddr*)&sAddr, sizeof(sAddr));
  50.     ret = listen(s, 16);
  51.  
  52.     int i, cAddrLen = sizeof(SOCKADDR_IN);
  53.     for (i = 0; i < 1024; i++) player[i] = 0;
  54.     for (i = 0; i < 512; i++) room[i] = 0;
  55.  
  56.     while (1) {
  57.         for (i = 0; i < nPlayer; i++) if (player[i] == 0) break;
  58.         player[i] = new Player;
  59.         player[i]->c = accept(s, (sockaddr*)&player[i]->cAddr, &cAddrLen);
  60.         CreateThread(0, 0, PlayerThread, (LPVOID)i, 0, 0);
  61.         if (i == nPlayer) nPlayer++;
  62.     }
  63.     closesocket(s);
  64.     WSACleanup();
  65.     return 0;
  66. }
  67.  
  68. DWORD WINAPI PlayerThread(LPVOID Param) {
  69.     int i = (int)Param;
  70.     char buf[1024], response[1024];
  71.     int len, j;
  72.     bool check;
  73.     while (1) {
  74.         len = recv(player[i]->c, buf, 1024, 0);
  75.         if (len > 0) {
  76.             buf[len - 1] = 0;
  77.             if (strnicmp(buf, "PLAY ", 5) == 0 && player[i]->isState == 0) {
  78.                 strcpy(player[i]->nickname, buf + 5);
  79.                 check = 0;
  80.                 for (j = 0; j < nPlayer;j++)
  81.                     if(player[i]!=0 && player[j]->isState==1 && j!=i)
  82.                         if (strcmp(player[i]->nickname, player[j]->nickname) == 0) {
  83.                             check = 1;
  84.                             break;
  85.                         }
  86.                 if (check != 1) {
  87.                     player[i]->isState = 1;
  88.                     for (j = 0; j < nRoom; j++) {
  89.                         if (room[j] == 0) {
  90.                             break;
  91.                         }
  92.                         if (room[j]->player2 == 0) {
  93.                             room[j]->player2 = new Player;
  94.                             room[j]->player2 = player[i];
  95.                             player[i]->roomNum = j;
  96.                             printf("%s login\n", room[j]->player2->nickname);
  97.                             sprintf(response, "READY %s %s\n",room[j]->player1->nickname, room[j]->player2->nickname);
  98.                             send(room[j]->player1->c, response, strlen(response), 0);
  99.                             send(room[j]->player2->c, response, strlen(response), 0);
  100.                             goto ENDLOG;
  101.                         }
  102.                     }
  103.                     room[j] = new Room;
  104.                     room[j]->player1 = player[i];
  105.                     player[i]->roomNum = j;
  106.                     player[i]->permission = 1;
  107.                     printf("%s login\n", room[j]->player1->nickname);
  108.                     if (j == nRoom) nRoom++;
  109.                 }
  110.                 else {
  111.                     sprintf(response, "%s da dang nhap!\n", player[i]->nickname);
  112.                     send(player[i]->c, response, strlen(response), 0);
  113.                 }
  114.                 ENDLOG:
  115.                 continue;
  116.             }
  117.             if (strnicmp(buf, "MARK ", 5) == 0) {
  118.                 int x, y;
  119.                 sscanf(buf, "MARK %d %d", &x, &y);
  120.                 int index = player[i]->roomNum;
  121.                 if (strcmp(player[i]->nickname, room[index]->player1->nickname) == 0 && room[index]->player2 != 0 ) {
  122.                     if (player[i]->permission == 1) {
  123.                         printf("Player %s in Room[%d] MARK %d %d\n", room[index]->player1->nickname, index, x, y);
  124.                         room[index]->player1->permission = 0;
  125.                         room[index]->player2->permission = 1;
  126.                         room[index]->map[x][y] = 1;
  127.                     }
  128.                     else {
  129.                         sprintf(response, "WATING %s MARK\n", room[index]->player2->nickname);
  130.                         send(room[j]->player1->c, response, strlen(response), 0);
  131.                     }                  
  132.                 }
  133.                 else {
  134.                     sprintf(response, "WATING FOR OTHER PLAYER\n", room[index]->player2->nickname);
  135.                     send(room[j]->player1->c, response, strlen(response), 0);
  136.                     continue;
  137.                 }
  138.                 if (strcmp(player[i]->nickname, room[index]->player2->nickname) == 0) {
  139.                     if (player[i]->permission == 1) {
  140.                         printf("Player %s in Room[%d] MARK %d %d\n", room[index]->player2->nickname, index, x, y);
  141.                         room[index]->player1->permission = 1;
  142.                         room[index]->player2->permission = 0;
  143.                         room[index]->map[x][y] = 2;
  144.                     }
  145.                     else {
  146.                         sprintf(response, "WATING %s MARK\n", room[index]->player1->nickname);
  147.                         send(room[j]->player2->c, response, strlen(response), 0);
  148.                     }
  149.                 }
  150.                 int a = isGameOver(x, y, index);
  151.                 if (a != 0) {
  152.                     if (a == 1) {
  153.                         sprintf(response, "PLAYER %s WIN\n", room[index]->player1->nickname);
  154.                         send(room[index]->player1->c, response, strlen(response), 0);
  155.                         send(room[index]->player2->c, response, strlen(response), 0);
  156.                     }
  157.                     if (a == 2) {
  158.                         sprintf(response, "PLAYER %s WIN\n", room[index]->player2->nickname);
  159.                         send(room[index]->player1->c, response, strlen(response), 0);
  160.                         send(room[index]->player2->c, response, strlen(response), 0);
  161.                     }
  162.                 }
  163.                 continue;
  164.             }
  165.             sprintf(response, "Command error!\n");
  166.             send(player[i]->c, response, strlen(response), 0);
  167.         }
  168.         else {
  169.             closesocket(player[i]->c);
  170.             delete player[i];
  171.             player[i] = 0;
  172.             break;
  173.         }
  174.     }
  175.     return 0;
  176. }
  177.  
  178. int isGameOver(int x, int y, int roomNum) {
  179.     int row = 0, col = 0;
  180.     int i;
  181.     bool player1, player2;
  182.  
  183.     while (col < 100 - 5) {
  184.         player1 = 1; player2 = 1;
  185.         for (i = 0; i < 5; i++) {
  186.             if (room[roomNum]->map[x][col + i] != 1) player1 = 0;
  187.             if (room[roomNum]->map[x][col + i] != 2) player2 = 0;
  188.         }
  189.         if (player1) return 1;
  190.         if (player2) return 2;
  191.         col++;
  192.     }
  193.  
  194.     while (row < 100 - 5) {
  195.         player1 = 1; player2 = 1;
  196.         for (i = 0; i < 5; i++) {
  197.             if (room[roomNum]->map[row + i][y] != 1) player1 = 0;
  198.             if (room[roomNum]->map[row + i][y] != 2) player2 = 0;
  199.         }
  200.         if (player1) return 1;
  201.         if (player2) return 2;
  202.         row++;
  203.     }
  204.  
  205.     col = x; row = y;
  206.     while (row > 0 && col > 0) { row--; col--; }
  207.     while (row <= 100 - 5 && col <= 100 - 5) {
  208.         player1 = 1; player2 = 1;
  209.         for (i = 0; i < 5; i++) {
  210.             if (room[roomNum]->map[col + i][row + i] != 1) player1 = 0;
  211.             if (room[roomNum]->map[col + i][row + i] != 2) player2 = 0;
  212.         }
  213.         if (player1) return 1;
  214.         if (player2) return 2;
  215.         col++; row++;
  216.     }
  217.  
  218.     col = x; row = y;
  219.     while (row < 100-1 && col > 0) { row++; col--; }
  220.     while (row >= 4 && col <= 100 - 5) {
  221.         player1 = 1; player2 = 1;
  222.         for (i = 0; i < 5; i++) {
  223.             if (room[roomNum]->map[col - i][row + i] != 1) player1 = 0;
  224.             if (room[roomNum]->map[col - i][row + i] != 2) player2 = 0;
  225.         }
  226.         if (player1) return 1;
  227.         if (player2) return 2;
  228.         col++; row--;
  229.     }
  230.     return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement