Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- sock = socket.socket()
- sock.connect(('10.8.128.157',9988))
- #sock.send(b'hello, world!')
- data = sock.recv(1024)
- data = data.decode('utf-8')
- print(data)
- me=data
- enemy = 'x'
- if enemy == me:
- enemy = '0'
- import pygame
- import sys
- pygame.init()
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- size_block = 100
- margin = 10
- width = heigth = size_block * 3 + margin * 4
- size_window = (width, heigth)
- screen = pygame.display.set_mode(size_window)
- mtr = []
- for i in range(3):
- m = ['a'] * 3
- mtr.append(m)
- cnt_a = 9
- def check():
- for i in range(3):
- if mtr[i][0] == mtr[i][1] == mtr[i][2]:
- if mtr[i][0] == 'x':
- return 'x'
- elif mtr[i][0] == '0':
- return '0'
- for j in range(3):
- if mtr[0][j] == mtr[1][j] == mtr[2][j]:
- if mtr[0][j] == 'x':
- return 'x'
- elif mtr[0][j] == '0':
- return '0'
- if mtr[0][0] == mtr[1][1] == mtr[2][2]:
- if mtr[0][0] == 'x':
- return 'x'
- elif mtr[0][0] == '0':
- return '0'
- if mtr[0][2] == mtr[1][1] == mtr[2][0]:
- if mtr[0][2] == 'x':
- return 'x'
- elif mtr[0][2] == '0':
- return '0'
- draw = True
- for i in range(3):
- for j in range(3):
- if mtr[i][j] == 'a':
- draw = False
- if draw:
- return 'draw'
- return 'a'
- #clock = pygame.time.Clock()
- while check() == 'a':
- #clock.tick(120)
- print("ITERATION")
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- for i in range(3):
- for j in range(3):
- x = i * size_block + (i + 1) * margin
- y = j * size_block + (j + 1) * margin
- color = WHITE
- if mtr[i][j] == '0':
- color = BLUE
- elif mtr[i][j] == 'x':
- color = RED
- pygame.draw.rect(screen, color, (x, y, size_block, size_block))
- if color == RED:
- pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
- pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
- elif color == BLUE:
- pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
- pygame.display.update()
- data = sock.recv(1024)
- data = data.decode('utf-8')
- print("DATA = ", data)
- if data == "move":
- print("WE MOVE")
- old_cnt_a = cnt_a
- while cnt_a == old_cnt_a:#против перезакрашивания клетки
- for event in pygame.event.get():
- if event.type == pygame.MOUSEBUTTONDOWN:
- x, y = pygame.mouse.get_pos()
- # print("x y = ", x, y)
- col = x // (size_block + margin)
- row = y // (size_block + margin)
- # print("cnt = ", cnt)
- if mtr[col][row] == 'a':
- mtr[col][row] = me
- cnt_a -= 1
- to_server = (str(col) + ' ' + str(row)).encode('utf-8')
- sock.send(to_server)
- else:
- print("HE HAS MOVED")
- col, row = data.split()
- col = int(col)
- row = int(row)
- #print("col row = ", col, row, type(col), type(row))
- mtr[col][row] = enemy
- for event in pygame.event.get():
- for i in range(3):
- for j in range(3):
- x = i * size_block + (i + 1) * margin
- y = j * size_block + (j + 1) * margin
- color = WHITE
- if mtr[i][j] == '0':
- color = BLUE
- elif mtr[i][j] == 'x':
- color = RED
- pygame.draw.rect(screen, color, (x, y, size_block, size_block))
- if color == RED:
- pygame.draw.line(screen, WHITE, (x,y), (x+size_block, y+size_block))
- pygame.draw.line(screen, WHITE, (x + size_block, y), (x, y + size_block))
- elif color == BLUE:
- pygame.draw.circle(screen, WHITE, (x + size_block / 2, y + size_block / 2), size_block / 2 - 30)
- pygame.display.update()
- print()
- print("END A")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement