日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

用python做双人五子棋_基于python的socket实现单机五子棋到双人对战

發(fā)布時(shí)間:2024/1/23 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用python做双人五子棋_基于python的socket实现单机五子棋到双人对战 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

基于python的socket實(shí)現(xiàn)單機(jī)五子棋到雙人對(duì)戰(zhàn),供大家參考,具體內(nèi)容如下

本次實(shí)驗(yàn)使用python語言。通過socket進(jìn)行不同機(jī)器見的通信,具體可以分為以下四步:1.創(chuàng)建ServerSocket和Socket;2.打開鏈接到Socket的輸入/輸出流;3.按照協(xié)議對(duì)Socket進(jìn)行讀/寫操作;4.關(guān)閉輸入輸出流、關(guān)閉Socket。

由于是雙人對(duì)戰(zhàn),服務(wù)器必須應(yīng)對(duì)多人及以上的客戶端的連接,因此本實(shí)驗(yàn)還引入了python的threading多線程模塊,通過監(jiān)聽實(shí)時(shí)監(jiān)控網(wǎng)絡(luò)狀態(tài),同時(shí)利用socket.listen(2)引入排隊(duì)等待機(jī)制。

chess類

#五子棋類

import os

class chessboard(object):

def __init__(self):

self.size = 16

#初始化棋盤

self.__board=[[' ' for n in range(self.size)] for m in range(self.size)]

n = 0

#添加桌面標(biāo)簽

while n < self.size:

ntr=str(n)

self.__board[0][n] = ntr.zfill(2)

self.__board[n][0] = ntr.zfill(2)

n=n+1

self.id=0

#勝利條件

def is_end(self):

ch_stack=[]

#行檢查

for i in range(self.size):

for j in range(self.size):

#判斷是否結(jié)束

chess=self.__board[i][j]

if len(ch_stack)==5 and ch_stack[-1]=='* ':

print('winner=id 1')

return 1

elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':

print('winner=id 2')

return 2

if chess==' ':

ch_stack.clear()

else:

if (not ch_stack) or ch_stack[-1] == chess:

ch_stack.append(chess)

else:

ch_stack.clear()

ch_stack.append(chess)

ch_stack.clear()

ch_stack.clear()

#列檢查

for j in range(self.size):

for i in range(self.size):

#判斷是否結(jié)束

if len(ch_stack)==5 and ch_stack[-1]=='* ':

print('winner=id 1')

return 1

elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':

print('winner=id 2')

return 2

chess=self.__board[i][j]

if chess==' ':

ch_stack.clear()

else:

if (not ch_stack) or ch_stack[-1] == chess:

ch_stack.append(chess)

else:

ch_stack.clear()

ch_stack.append(chess)

ch_stack.clear()

ch_stack.clear()

#左斜檢查

#下三角

for i in range(self.size):

for j in range(1,self.size):

#判斷是否結(jié)束

if len(ch_stack)==5 and ch_stack[-1]=='* ':

print('winner=id 1')

return 1

elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':

print('winner=id 2')

return 2

if i+j

chess=self.__board[i+j][j]

if chess==' ':

ch_stack.clear()

else:

if (not ch_stack) or ch_stack[-1] == chess:

ch_stack.append(chess)

else:

ch_stack.clear()

ch_stack.append(chess)

else:

break

ch_stack.clear()

ch_stack.clear()

#上三角

for i in range(self.size):

for j in range(1,self.size):

#判斷是否結(jié)束

if len(ch_stack)==5 and ch_stack[-1]=='* ':

print('winner=id 1')

return 1

elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':

print('winner=id 2')

return 2

if i+j

chess=self.__board[j][j+i]

if chess==' ':

ch_stack.clear()

else:

if (not ch_stack) or ch_stack[-1] == chess:

ch_stack.append(chess)

else:

ch_stack.clear()

ch_stack.append(chess)

else:

break

ch_stack.clear()

ch_stack.clear()

#右斜檢查

#上三角

for i in range(self.size):

for j in range(1,self.size):

#判斷是否結(jié)束

if len(ch_stack)==5 and ch_stack[-1]=='* ':

print('winner=id 1')

return 1

elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':

print('winner=id 2')

return 2

if self.size-i-j+1>0:

chess=self.__board[self.size-i-j][j]

if chess==' ':

ch_stack.clear()

elif not chess:

break

else:

if (not ch_stack) or ch_stack[-1] == chess:

ch_stack.append(chess)

else:

ch_stack.clear()

ch_stack.append(chess)

else:

break

ch_stack.clear()

ch_stack.clear()

#下三角

for i in range(self.size):

for j in range(1,self.size):

# 判斷是否結(jié)束

if len(ch_stack) == 5 and ch_stack[-1] == '* ':

print('winner=id 1')

return 1

elif len(ch_stack) == 5 and ch_stack[-1] == '@ ':

print('winner=id 2')

return 2

if self.size-i-j> 0:

chess = self.__board[j][self.size-i-j]

if chess == ' ':

ch_stack.clear()

elif not chess:

break

else:

if (not ch_stack) or ch_stack[-1] == chess:

ch_stack.append(chess)

else:

ch_stack.clear()

ch_stack.append(chess)

else:

break

ch_stack.clear()

ch_stack.clear()

return 0

def draw(self):

#clear()

for x in self.__board:

print(x)

return 0

def drop_chess(self,x,y,id):

if id==1 and self.__board[x][y]==' ':

self.__board[x][y]='* '

return 1

elif id==2 and self.__board[x][y]==' ':

self.__board[x][y]='@ '

return 1

else:

return 0

然后是用while循環(huán)實(shí)現(xiàn)的單機(jī)版五子棋

# -*- coding: utf-8 -*-

#單機(jī)版五子棋

from chess import chessboard

def changeid(id):

if id==1:

return 2

elif id==2:

return 1

else:

return 0

t=chessboard()

id=1#初始化id

t.draw()

while (not t.is_end()):#end函數(shù)

print('your id is %d,input your next drop(x,y)'% id)

x=input()

y=input()

x=int(x)

y=int(y)

if t.drop_chess(x,y,id):

t.draw()

else:

print('_________Illegal Input,Please Check Again_________')

continue

id=changeid(id)

———————分割線———————

由于要實(shí)現(xiàn)雙人對(duì)戰(zhàn),所以服務(wù)器端必須要用多線程使其服務(wù)多個(gè)客戶端,因此使用threading

服務(wù)器端

# -*- coding: utf-8 -*-

#服務(wù)器

import os

import socket

import json

import threading

import time

import sys

from chess import chessboard

t=chessboard()

id=1#初始化id

def handle():

while (not t.is_end()):

for c in socks:

global id

json_string0 = json.dumps(t._chessboard__board)

c.sendto(json_string0.encode('utf-8'), address)

msg1 = 'Your id is %d,input your next drop' % id + "\r\n"

c.send(msg1.encode('utf-8'))

msg2x = c.recv(1024)

msg2y = c.recv(1024)

x = int(msg2x.decode('utf-8'))

y = int(msg2y.decode('utf-8'))

print('processing......\n')

if t.drop_chess(x, y, id):

json_string = json.dumps(t._chessboard__board)

c.sendto(json_string.encode('utf-8'), address)

else:

msg3 = '_________Illegal Input,Please Check Again_________'

c.send(msg3.encode('utf-8'))

continue

id = changeid(id)

def clear():

os.system('cls')

def changeid(id):

if id==1:

return 2

elif id==2:

return 1

else:

return 0

# 創(chuàng)建 socket 對(duì)象

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 獲取本地主機(jī)名

host = socket.gethostname()

port = 9999

# 綁定端口號(hào)

s.bind((host, port))

address=(host, port)

# 設(shè)置最大連接數(shù),超過后排隊(duì)

s.listen(2)

socks=[]

th = threading.Thread(target=handle)

th.start()

while 1:

c, addr = s.accept()

print

'connected from:', addr

socks.append(c)

s.close()

然后是客戶端

# -*- coding: utf-8 -*-

#客戶端

import socket

import time

# 創(chuàng)建 socket 對(duì)象

c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 獲取本地主機(jī)名

#host = socket.gethostname()

host='10.41.114.198'

# 設(shè)置端口號(hào)

port = 9999

# 連接服務(wù),指定主機(jī)和端口

c.connect((host, port))

address=(host, port)

while 1:

#s=c.accept()

print('')

print('__________________wait__________________')

msg0 = c.recv(2048).decode('utf-8') # 棋盤大于1024

for x in msg0:

if x == '[':

print('')

else:

print(x, end='')

print('')

msg1 = c.recv(1024)#接收輸入提示

print (msg1.decode('utf-8'))

time.sleep(1)

x = input('x=')

y = input('y=')

c.send(x.encode('utf-8'))

c.send(y.encode('utf-8'))

msg3 = c.recv(2048).decode('utf-8')#棋盤大于1024

if msg3=='_________Illegal Input,Please Check Again_________':

print(msg3)

continue

else:

#print(msg3)

for x in msg3:

if x=='[':

print('')

else:

print(x, end='')

print('')

print('__________________wait__________________')

print('')

c.close()

注意socket傳輸時(shí)只能傳送bytes,因此list先用json轉(zhuǎn)成str,再encode編碼

使用方法:先更改客戶端host為自己地址,然后先打開服務(wù)端,然后打開多個(gè)客戶端(大于2個(gè)開始排隊(duì)),然后開始輸入X,Y坐標(biāo)開始游戲。

由于時(shí)間緊急,暫時(shí)未處理單個(gè)客戶端退出后的程序初始化問題,可能過幾日會(huì)補(bǔ)上。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的用python做双人五子棋_基于python的socket实现单机五子棋到双人对战的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。