Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <netdb.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include "message.h"
  12.  
  13. #define true 1
  14. #define false 0
  15. #define bool uint8_t
  16.  
  17. #define SERVER "127.0.0.1"
  18. #define SERVPORT 9876
  19.  
  20. #define BUFLEN 1024
  21.  
  22. typedef struct {
  23. uint8_t board[9];
  24. } local_board_t;
  25.  
  26. int do_move(local_board_t *board, figure_t player, uint8_t *field);
  27.  
  28. bool send_move_msg(int fd, uint8_t x, uint8_t y, figure_t player){
  29. struct msg message;
  30. message.type = MOVE;
  31. message.len = HDR_SIZE + 3;
  32. message.move.x = x;
  33. message.move.y = y;
  34. message.move.player = player;
  35. printf("Sending MOVE message to fd %d: x=%d, y=%d, player=%d\n", fd, x, y, player);
  36. return send(fd, &message, message.len, 0) == message.len;
  37. }
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41. int serverPort = SERVPORT;
  42. struct sockaddr_in serveraddr;
  43. char msg_buf[BUFLEN];
  44. char server[255];
  45. int unread;
  46. int sd = socket(AF_INET, SOCK_STREAM, 0); // socket descriptor
  47. if(sd < 0)
  48. {
  49. perror("Creating socket error");
  50. exit(-1);
  51. }
  52.  
  53. if(argc > 1)
  54. strcpy(server, argv[1]);
  55. else
  56. strcpy(server, SERVER);
  57.  
  58. if (argc > 2)
  59. serverPort = atoi(argv[2]);
  60.  
  61. memset(&serveraddr, 0x00, sizeof(struct sockaddr_in));
  62. serveraddr.sin_family = AF_INET;
  63. serveraddr.sin_port = htons(serverPort);
  64.  
  65. if((serveraddr.sin_addr.s_addr = inet_addr(server)) == (unsigned long)INADDR_NONE){
  66. struct hostent *hostp = gethostbyname(server);
  67. if(hostp == (struct hostent *)NULL)
  68. {
  69. printf("404 Host not found \n");
  70. shutdown(sd,0);
  71. exit(-1);
  72. }
  73. memcpy(&serveraddr.sin_addr, hostp->h_addr, sizeof(serveraddr.sin_addr));
  74. }
  75.  
  76. struct msg *message = (struct msg *) msg_buf;
  77. printf("Insert bot's nickname:\n");
  78. fgets(message->join.nickname, 20, stdin);
  79. message->join.nickname[strlen(message->join.nickname)-1] = 0; // remove \n from the end
  80. message->type = JOIN;
  81. message->len = strlen(message->join.nickname)+1+HDR_SIZE;
  82.  
  83. if(connect(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){
  84. perror("Connection error");
  85. shutdown(sd, 0);
  86. exit(EXIT_FAILURE);
  87. } else printf("Bot connected to server\n");
  88.  
  89. if(send(sd, message, message->len, 0) == -1){
  90. perror("Socket send error");
  91. exit(EXIT_FAILURE);
  92. }
  93.  
  94. figure_t player = NONE_FIGURE;
  95. local_board_t board;
  96. memset(board.board, NONE_FIGURE, 9);
  97.  
  98. while(1){
  99. fd_set rfds;
  100. FD_ZERO(&rfds);
  101. FD_SET(sd, &rfds);
  102. select(sd+1, &rfds, NULL, NULL, NULL);
  103.  
  104. // handle networking
  105. unread = recv(sd, msg_buf, BUFLEN, 0);
  106. message = (struct msg *) msg_buf;
  107. if(unread < 1){
  108. perror("Connection error, exiting");
  109. shutdown(sd, 0);
  110. exit(0);
  111. }
  112.  
  113. while(unread > 0){
  114. switch(message->type){
  115. case CHAT:
  116. printf("[CHAT] %s: %s\n", message->chat.nickname, message->chat.msg);
  117. break;
  118. case MOVE:
  119. // no need to check if < 0, we use unsigned type
  120. if(message->move.x > 2 || message->move.y > 2)
  121. break;
  122. board.board[3*message->move.y+message->move.x] = message->move.player;
  123. break;
  124. case MOVE_YOUR_ASS:
  125. if(player == NONE_FIGURE) {
  126. player = message->move_your_ass.you;
  127. if(player == CIRCLE) printf("I'm a circle!\n");
  128. else printf("I'm a cross!\n");
  129. }
  130.  
  131. uint8_t field = 0;
  132. do_move(&board, player, &field);
  133. if (!send_move_msg(sd, field % 3, field / 3, player)) {
  134. perror("Sending through socket failed");
  135. exit(EXIT_FAILURE);
  136. }
  137. break;
  138. case FINISH:
  139. switch(message->finish.result){
  140. case WIN_CIRCLE:
  141. printf("Circle wins\n");
  142. break;
  143. case WIN_CROSS:
  144. printf("Cross wins\n");
  145. break;
  146. case DRAW:
  147. printf("Draw\n");
  148. break;
  149. case JEDEN_RABIN_POWIE_TAK_DRUGI_RABIN_POWIE_NIE:
  150. printf("JEDEN RABIN POWIE TAK, DRUGI RABIN POWIE NIE\n");
  151. break;
  152. }
  153. shutdown(sd, 0);
  154. exit(EXIT_SUCCESS);
  155. }
  156. // handling of multiple messages in one buffer
  157. int len = message->len;
  158. unread -= len;
  159. message = (struct msg *)(((char*) message) + len);
  160. }
  161. }
  162. }
  163.  
  164. int set_move(local_board_t *board, figure_t player, uint8_t field) {
  165. if (board->board[field] != NONE_FIGURE)
  166. return 0;
  167. board->board[field] = (player == CIRCLE) ? CIRCLE : CROSS;
  168. return 1;
  169. }
  170.  
  171. int unset_move(local_board_t *board, uint8_t field)
  172. {
  173. if (board->board[field] == NONE_FIGURE)
  174. return 0;
  175. board->board[field] = NONE_FIGURE;
  176. return 1;
  177. }
  178.  
  179. void change_player(figure_t *player)
  180. {
  181. *player = (*player == CIRCLE) ? CROSS : CIRCLE;
  182. }
  183.  
  184. result_t check_game(local_board_t *board) {
  185. //Sprawdzanie wierszy
  186. for (int y=0; y<3; y++)
  187. {
  188. if ((board->board[3*y] == board->board[3*y + 1]) && (board->board[3*y] == board->board[3*y + 2]))
  189. {
  190. if (board->board[3*y] == CIRCLE)
  191. return WIN_CIRCLE;
  192. if (board->board[3*y] == CROSS)
  193. return WIN_CROSS;
  194. }
  195. }
  196. //Sprawdzanie kolumn
  197. for (int x=0; x<3; x++)
  198. {
  199. if ((board->board[x] == board->board[x + 3]) && (board->board[x] == board->board[x + 6]))
  200. {
  201. if (board->board[x] == CIRCLE)
  202. return WIN_CIRCLE;
  203. if (board->board[x] == CROSS)
  204. return WIN_CROSS;
  205. }
  206. }
  207. //Sprawdzanie przekątnych
  208. if ((board->board[0] == board->board[4]) && (board->board[0] == board->board[8]))
  209. {
  210. if (board->board[4] == CIRCLE)
  211. return WIN_CIRCLE;
  212. if (board->board[4] == CROSS)
  213. return WIN_CROSS;
  214. }
  215. if ((board->board[2] == board->board[4]) && (board->board[2] == board->board[6]))
  216. {
  217. if (board->board[4] == CIRCLE)
  218. return WIN_CIRCLE;
  219. if (board->board[4] == CROSS)
  220. return WIN_CROSS;
  221. }
  222. //sprawdzanie możliwości wykonania ruchu
  223.  
  224. for (int i=0; i<9; i++)
  225. if (board->board[i] == NONE_FIGURE)
  226. return JEDEN_RABIN_POWIE_TAK_DRUGI_RABIN_POWIE_NIE;
  227.  
  228. return DRAW;
  229. }
  230.  
  231. int review_result(figure_t player, result_t result) {
  232. if (result == DRAW) return 0;
  233.  
  234. if (((player == CIRCLE) && (result == WIN_CIRCLE)) || ((player == CROSS) && (result == WIN_CROSS)))
  235. return 1;
  236.  
  237. return -1;
  238. }
  239.  
  240. /**
  241. * @return -1 przegrana, 0 remis, 1 wygrana
  242. */
  243. int do_move(local_board_t *board, figure_t player, uint8_t *field) {
  244. uint8_t best_field = 0;
  245. int best_result = -2;
  246.  
  247. int result;
  248. for (uint8_t i=0; i<9; i++)
  249. {
  250. if (set_move(board, player, i) == 0) continue;
  251.  
  252. result_t tmp_result = check_game(board);
  253. if (tmp_result != JEDEN_RABIN_POWIE_TAK_DRUGI_RABIN_POWIE_NIE)
  254. {
  255. result = review_result(player, tmp_result);
  256. }
  257. else
  258. {
  259. figure_t enemy = player;
  260. change_player(&enemy);
  261. result = (-1) * do_move(board, enemy, NULL);
  262. }
  263. if (result > best_result)
  264. {
  265. best_result = result;
  266. best_field = i;
  267. }
  268. unset_move(board, i);
  269. }
  270.  
  271. if (field != NULL)
  272. {
  273. *field = best_field;
  274. }
  275. return best_result;
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement