Advertisement
Korotkodul

client_OK

Sep 22nd, 2022 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.43 KB | None | 0 0
  1. import socket
  2.  
  3. sock = socket.socket()
  4. sock.connect(('10.8.128.157',9988))
  5. #sock.send(b'hello, world!')
  6.  
  7. data = sock.recv(1024)
  8. data = data.decode('utf-8')
  9. print(data)
  10. me=data
  11. enemy = 'x'
  12. if enemy == me:
  13.     enemy = '0'
  14.  
  15. import pygame
  16. import sys
  17.  
  18. pygame.init()
  19.  
  20. RED = (255, 0, 0)
  21. GREEN = (0, 255, 0)
  22. BLUE = (0, 0, 255)
  23. WHITE = (255, 255, 255)
  24. BLACK = (0, 0, 0)
  25.  
  26. size_block = 100
  27. margin = 10
  28. width = heigth = size_block * 3 + margin * 4
  29.  
  30. size_window = (width, heigth)
  31.  
  32. screen = pygame.display.set_mode(size_window)
  33.  
  34. mtr = []
  35. for i in range(3):
  36.     m = ['a'] * 3
  37.     mtr.append(m)
  38.  
  39. cnt_a = 9
  40.  
  41. def check():
  42.     for i in range(3):
  43.         if mtr[i][0] == mtr[i][1] == mtr[i][2]:
  44.             if mtr[i][0] == 'x':
  45.                 return 'x'
  46.             elif mtr[i][0] == '0':
  47.                 return '0'
  48.  
  49.     for j in range(3):
  50.         if mtr[0][j] == mtr[1][j] == mtr[2][j]:
  51.             if mtr[0][j] == 'x':
  52.                 return 'x'
  53.             elif mtr[0][j] == '0':
  54.                 return '0'
  55.  
  56.     if mtr[0][0] == mtr[1][1] == mtr[2][2]:
  57.         if mtr[0][0] == 'x':
  58.             return 'x'
  59.         elif mtr[0][0] == '0':
  60.             return '0'
  61.  
  62.     if mtr[0][2] == mtr[1][1] == mtr[2][0]:
  63.         if mtr[0][2] == 'x':
  64.             return 'x'
  65.         elif mtr[0][2] == '0':
  66.             return '0'
  67.  
  68.     draw = True
  69.     for i in range(3):
  70.         for j in range(3):
  71.             if mtr[i][j] == 'a':
  72.                 draw = False
  73.     if draw:
  74.         return 'draw'
  75.  
  76.     return 'a'
  77.  
  78.  
  79. #clock = pygame.time.Clock()
  80.  
  81. while check() == 'a':
  82.     #clock.tick(120)
  83.     print("ITERATION")
  84.     for event in pygame.event.get():
  85.         if event.type == pygame.QUIT:
  86.             sys.exit()
  87.  
  88.         for i in range(3):
  89.             for j in range(3):
  90.                 x = i * size_block + (i + 1) * margin
  91.                 y = j * size_block + (j + 1) * margin
  92.                 color = WHITE
  93.                 if mtr[i][j] == '0':
  94.                     color = BLUE
  95.                 elif mtr[i][j] == 'x':
  96.                     color = RED
  97.                 pygame.draw.rect(screen, color, (x, y, size_block, size_block))
  98.                 if color == RED:
  99.                     pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
  100.                     pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
  101.                 elif color == BLUE:
  102.                     pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
  103.                 pygame.display.update()
  104.  
  105.     data = sock.recv(1024)
  106.     data = data.decode('utf-8')
  107.     print("DATA = ", data)
  108.     if data == "move":
  109.         print("WE MOVE")
  110.         old_cnt_a = cnt_a
  111.         while cnt_a == old_cnt_a:#против перезакрашивания клетки
  112.             for event in pygame.event.get():
  113.                 if event.type == pygame.MOUSEBUTTONDOWN:
  114.                     x, y = pygame.mouse.get_pos()
  115.                 # print("x y = ", x, y)
  116.                 col = x // (size_block + margin)
  117.                 row = y // (size_block + margin)
  118.                 # print("cnt = ", cnt)
  119.  
  120.                 if mtr[col][row] == 'a':
  121.                     mtr[col][row] = me
  122.                     cnt_a -= 1
  123.         to_server = (str(col) + ' ' + str(row)).encode('utf-8')
  124.         sock.send(to_server)
  125.     else:
  126.         print("HE HAS MOVED")
  127.         col, row = data.split()
  128.         col = int(col)
  129.         row = int(row)
  130.         #print("col row = ", col, row, type(col), type(row))
  131.         mtr[col][row] = enemy
  132.  
  133.     for event in pygame.event.get():
  134.         for i in range(3):
  135.             for j in range(3):
  136.                 x = i * size_block + (i + 1) * margin
  137.                 y = j * size_block + (j + 1) * margin
  138.                 color = WHITE
  139.                 if mtr[i][j] == '0':
  140.                     color = BLUE
  141.                 elif mtr[i][j] == 'x':
  142.                     color = RED
  143.                 pygame.draw.rect(screen, color, (x, y, size_block, size_block))
  144.                 if color == RED:
  145.                     pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
  146.                     pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
  147.                 elif color == BLUE:
  148.                     pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
  149.                 pygame.display.update()
  150.     print()
  151.  
  152. print("END A")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement