"""
-*- coding: utf-8 -*-
@File : ServerGobang.py
@author: 刘子忻
@CSDN : 山河之书Liu_Zixin
@Time : 2023/01/02 1:41
"""
import pygame
import sys
from pygame.locals import *
from collections import Counter
import json
import select
import socket
def ServerGobang(ip, port):
# 界面初始化
screen = pygame.display.set_mode((1200, 720))
pygame.display.set_caption('Gobang--Server')
pygame.init()
# 图片导入
img_board = pygame.image.load('../Client/chess_board.png') # 棋盘
img_bchess = pygame.image.load('../Client/black_chess.jpg') # 黑子
img_wchess = pygame.image.load('../Client/white_chess.jpg') # 白子
# 后面的字和字的背景颜色,之所以设置字的背景颜色就是为了遮盖之前的字
white = (255, 255, 255)
black = (0, 0, 0)
# 用于传送的数据
msg = []
# 棋盘定义——二元组
chess_board = [[]]
def SetChessBoard(): # 根据尺寸信息设置棋盘网格
x, y = 0, 0
while True:
if x == 1200: # 长边界
x = 0
y += 40
if y < 720:
chess_board.append([])
if y == 720: # 宽边界
break
chess_board[-1].append([x, y])
x += 40
SetChessBoard()
# 棋盘格子是否落子
chess_exist = [[0 for _ in range(30)] for _ in range(18)] # 棋盘边界
# 黑白棋子初始化
black_chess, white_chess = [], []
wcx, wcy, bcx, bcy = [], [], [], []
# 绘制棋盘的图像
def DrawBoard():
for i in chess_board:
for j in i:
screen.blit(img_board, (j[0], j[1]))
pygame.display.update()
# 默认棋子类型为1(黑棋)
def SetChess():
if event.type == MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
for i in range(len(chess_board)):
for j in range(len(chess_board[i])):
if chess_board[i][j][0] < pos[0] < chess_board[i][j][0] + 40 and chess_board[i][j][1] < pos[1] < \
chess_board[i][j][1] + 40:
if chess_exist[i][j] == 0:
black_chess.append([i, j])
bcx.append(black_chess[-1][0])
bcy.append(black_chess[-1][1])
msg.extend((i, j))
chess_exist[i][j] = 1
pygame.display.update()
return 1
# 绘制棋子
def DrawChess():
for i in white_chess:
screen.blit(img_wchess, (i[1] * 40, i[0] * 40))
for i in black_chess:
screen.blit(img_bchess, (i[1] * 40, i[0] * 40))
pygame.display.update()
# 判定行或者列的胜利
def RowColumnWin(x, m, n, chess):
for i in x:
if x[i] >= 5:
xy = []
for j in chess:
if j[m] == i:
xy.append(j[n])
xy.sort()
count = 0
for j in range(len(xy) - 1):
if xy[j] + 1 == xy[j + 1]:
count += 1
else:
count = 0
if count >= 4:
return 1
# 判定对角线的胜利
def DiagonalWin(chess):
x, y = [], []
chess.sort()
for i in chess:
x.append(i[0])
y.append(i[1])
c, first, last = 0, 0, 0
for i in range(len(x) - 1):
if x[i + 1] != x[i]:
if x[i] + 1 == x[i + 1]:
c += 1
last = i + 1
else:
if c < 4:
first = i + 1
c = 0
else:
last = i
print(last)
break
else:
last = i + 1
if c >= 4:
dis = []
for i in range(first, last + 1):
dis.append(x[i] - y[i])
count = Counter(dis)
for i in count:
if count[i] >= 5:
return 1
dis = []
x2 = [i * (-1) for i in x]
for i in range(first, last + 1):
dis.append(x2[i] - y[i])
count = Counter(dis)
for i in count:
if count[i] >= 5:
return 1
# 利用上述的;两个判别函数,对不同情况下的胜利进行判定并返回胜利或失败
def GameOver():
wcx_count, wcy_count, bcx_count, bcy_count = Counter(wcx), Counter(wcy), Counter(bcx), Counter(bcy)
if RowColumnWin(wcx_count, 0, 1, white_chess) == 1:
return 0
elif RowColumnWin(bcx_count, 0, 1, black_chess) == 1:
return 1
elif RowColumnWin(wcy_count, 1, 0, white_chess) == 1:
return 0
elif RowColumnWin(bcy_count, 1, 0, black_chess) == 1:
return 1
elif DiagonalWin(white_chess) == 1:
return 0
elif DiagonalWin(black_chess) == 1:
return 1
# 为后续显示提示做准备
def DrawText(text, x, y, size):
pygame.font.init()
fontObj = pygame.font.SysFont('SimHei', size)
textSurfaceObj = fontObj.render(text, True, white, black)
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (x, y)
screen.blit(textSurfaceObj, textRectObj)
pygame.display.update()
buf_size = 1024
addr = (ip, port)
# 定义服务器属性
conn_gobang = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn_gobang.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 对socket的配置重用ip和端口号
conn_gobang.bind(addr)
conn_gobang.listen(1)
inputs = [conn_gobang]
DrawBoard()
settable = 1
link = False
while True:
rs, ws, es = select.select(inputs, [], [], 0) # 获取连接信息
for r in rs:
if r is conn_gobang:
link = True
udp, addr = conn_gobang.accept()
inputs.append(udp)
else:
data, addr = r.recvfrom(buf_size)
disconnected = not data
DrawText(" YOUR TURN ", 600, 20, 40)
if disconnected:
inputs.remove(r)
DrawText("LOSE CONNECTION", 600, 360, 40)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
else:
data = json.loads(data)
settable = 1
white_chess.append(data)
wcx.append(data[0])
wcy.append(data[1])
for event in pygame.event.get():
if event.type == QUIT:
conn_gobang.close()
pygame.quit()
sys.exit()
if link:
if settable == 1:
if SetChess() == 1:
DrawText("WAITING FOR OPPONENT", 600, 20, 40)
settable = 0
msg1 = json.dumps(msg)
udp.send(msg1.encode())
msg = []
DrawChess()
if GameOver() == 1:
DrawText("YOU WIN", 600, 360, 40)
while True:
for event in pygame.event.get():
if event.type == QUIT:
山河之书Liu_Zixin
- 粉丝: 176
- 资源: 8
最新资源
- 01-【管理制度】-55-公司员工培训手册管理人力资源管理制度.docx
- 01-【管理制度】-59-人力资源管理制度汇编.doc
- 01-【管理制度】-60-人力资源管理制度汇编.doc
- 02-【管理流程】-01-人力资源管理流程图.docx
- 02-【管理流程】-04-招聘录用流程图.docx
- 02-【管理流程】-02-人力资源管理流程.doc
- 02-【管理流程】-03-HR工作流程图.docx
- 02-【管理流程】-05-招聘流程图.docx
- 02-【管理流程】-06-员工入职流程图.docx
- 02-【管理流程】-07-员工入职流程图.docx
- 02-【管理流程】-10-员工转正流程图.docx
- 02-【管理流程】-08-入职流程图.docx
- 02-【管理流程】-09-新员工入职培训流程图.docx
- 02-【管理流程】-11-试用期转正流程图.docx
- 02-【管理流程】-13-员工晋升流程图.docx
- 02-【管理流程】-12-晋升管理流程图.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈