Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CaroServer.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <conio.h>
- #include <WinSock2.h>
- #include <WS2tcpip.h>
- #pragma comment(lib,"ws2_32.lib")
- struct Player
- {
- SOCKET c;
- SOCKADDR_IN cAddr;
- char nickname[32];
- bool isState = 0;
- int roomNum;
- bool permission = 0;
- };
- struct Room
- {
- Player * player1 = 0;
- Player * player2 = 0;
- int map[100][100];
- };
- SOCKET s;
- SOCKADDR_IN sAddr;
- Player * player[1024];
- Room * room[512];
- int nPlayer = 0, nRoom = 0;
- DWORD WINAPI PlayerThread(LPVOID Param);
- int isGameOver(int x, int y, int roomNum);
- int main()
- {
- WSADATA wsadata;
- int ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
- s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- sAddr.sin_family = AF_INET;
- sAddr.sin_port = htons(8888);
- sAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- ret = bind(s, (sockaddr*)&sAddr, sizeof(sAddr));
- ret = listen(s, 16);
- int i, cAddrLen = sizeof(SOCKADDR_IN);
- for (i = 0; i < 1024; i++) player[i] = 0;
- for (i = 0; i < 512; i++) room[i] = 0;
- while (1) {
- for (i = 0; i < nPlayer; i++) if (player[i] == 0) break;
- player[i] = new Player;
- player[i]->c = accept(s, (sockaddr*)&player[i]->cAddr, &cAddrLen);
- CreateThread(0, 0, PlayerThread, (LPVOID)i, 0, 0);
- if (i == nPlayer) nPlayer++;
- }
- closesocket(s);
- WSACleanup();
- return 0;
- }
- DWORD WINAPI PlayerThread(LPVOID Param) {
- int i = (int)Param;
- char buf[1024], response[1024];
- int len, j;
- bool check;
- while (1) {
- len = recv(player[i]->c, buf, 1024, 0);
- if (len > 0) {
- buf[len - 1] = 0;
- if (strnicmp(buf, "PLAY ", 5) == 0 && player[i]->isState == 0) {
- strcpy(player[i]->nickname, buf + 5);
- check = 0;
- for (j = 0; j < nPlayer;j++)
- if(player[i]!=0 && player[j]->isState==1 && j!=i)
- if (strcmp(player[i]->nickname, player[j]->nickname) == 0) {
- check = 1;
- break;
- }
- if (check != 1) {
- player[i]->isState = 1;
- for (j = 0; j < nRoom; j++) {
- if (room[j] == 0) {
- break;
- }
- if (room[j]->player2 == 0) {
- room[j]->player2 = new Player;
- room[j]->player2 = player[i];
- player[i]->roomNum = j;
- printf("%s login\n", room[j]->player2->nickname);
- sprintf(response, "READY %s %s\n",room[j]->player1->nickname, room[j]->player2->nickname);
- send(room[j]->player1->c, response, strlen(response), 0);
- send(room[j]->player2->c, response, strlen(response), 0);
- goto ENDLOG;
- }
- }
- room[j] = new Room;
- room[j]->player1 = player[i];
- player[i]->roomNum = j;
- player[i]->permission = 1;
- printf("%s login\n", room[j]->player1->nickname);
- if (j == nRoom) nRoom++;
- }
- else {
- sprintf(response, "%s da dang nhap!\n", player[i]->nickname);
- send(player[i]->c, response, strlen(response), 0);
- }
- ENDLOG:
- continue;
- }
- if (strnicmp(buf, "MARK ", 5) == 0) {
- int x, y;
- sscanf(buf, "MARK %d %d", &x, &y);
- int index = player[i]->roomNum;
- if (strcmp(player[i]->nickname, room[index]->player1->nickname) == 0 && room[index]->player2 != 0 ) {
- if (player[i]->permission == 1) {
- printf("Player %s in Room[%d] MARK %d %d\n", room[index]->player1->nickname, index, x, y);
- room[index]->player1->permission = 0;
- room[index]->player2->permission = 1;
- room[index]->map[x][y] = 1;
- }
- else {
- sprintf(response, "WATING %s MARK\n", room[index]->player2->nickname);
- send(room[j]->player1->c, response, strlen(response), 0);
- }
- }
- else {
- sprintf(response, "WATING FOR OTHER PLAYER\n", room[index]->player2->nickname);
- send(room[j]->player1->c, response, strlen(response), 0);
- continue;
- }
- if (strcmp(player[i]->nickname, room[index]->player2->nickname) == 0) {
- if (player[i]->permission == 1) {
- printf("Player %s in Room[%d] MARK %d %d\n", room[index]->player2->nickname, index, x, y);
- room[index]->player1->permission = 1;
- room[index]->player2->permission = 0;
- room[index]->map[x][y] = 2;
- }
- else {
- sprintf(response, "WATING %s MARK\n", room[index]->player1->nickname);
- send(room[j]->player2->c, response, strlen(response), 0);
- }
- }
- int a = isGameOver(x, y, index);
- if (a != 0) {
- if (a == 1) {
- sprintf(response, "PLAYER %s WIN\n", room[index]->player1->nickname);
- send(room[index]->player1->c, response, strlen(response), 0);
- send(room[index]->player2->c, response, strlen(response), 0);
- }
- if (a == 2) {
- sprintf(response, "PLAYER %s WIN\n", room[index]->player2->nickname);
- send(room[index]->player1->c, response, strlen(response), 0);
- send(room[index]->player2->c, response, strlen(response), 0);
- }
- }
- continue;
- }
- sprintf(response, "Command error!\n");
- send(player[i]->c, response, strlen(response), 0);
- }
- else {
- closesocket(player[i]->c);
- delete player[i];
- player[i] = 0;
- break;
- }
- }
- return 0;
- }
- int isGameOver(int x, int y, int roomNum) {
- int row = 0, col = 0;
- int i;
- bool player1, player2;
- while (col < 100 - 5) {
- player1 = 1; player2 = 1;
- for (i = 0; i < 5; i++) {
- if (room[roomNum]->map[x][col + i] != 1) player1 = 0;
- if (room[roomNum]->map[x][col + i] != 2) player2 = 0;
- }
- if (player1) return 1;
- if (player2) return 2;
- col++;
- }
- while (row < 100 - 5) {
- player1 = 1; player2 = 1;
- for (i = 0; i < 5; i++) {
- if (room[roomNum]->map[row + i][y] != 1) player1 = 0;
- if (room[roomNum]->map[row + i][y] != 2) player2 = 0;
- }
- if (player1) return 1;
- if (player2) return 2;
- row++;
- }
- col = x; row = y;
- while (row > 0 && col > 0) { row--; col--; }
- while (row <= 100 - 5 && col <= 100 - 5) {
- player1 = 1; player2 = 1;
- for (i = 0; i < 5; i++) {
- if (room[roomNum]->map[col + i][row + i] != 1) player1 = 0;
- if (room[roomNum]->map[col + i][row + i] != 2) player2 = 0;
- }
- if (player1) return 1;
- if (player2) return 2;
- col++; row++;
- }
- col = x; row = y;
- while (row < 100-1 && col > 0) { row++; col--; }
- while (row >= 4 && col <= 100 - 5) {
- player1 = 1; player2 = 1;
- for (i = 0; i < 5; i++) {
- if (room[roomNum]->map[col - i][row + i] != 1) player1 = 0;
- if (room[roomNum]->map[col - i][row + i] != 2) player2 = 0;
- }
- if (player1) return 1;
- if (player2) return 2;
- col++; row--;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement