前端分為python和c++部分
呼叫c++並與網頁互動
網頁互動:flask框架1
;
from flask import flask, render_template, request, jsonify
'/')
defindex()
:return render_template(
"/web/chess.js"
)'/start_game'
)def
start_game()
:global board
global turn
global ai0, ai1
turn =
0 board = board(
) a1 =
('human'
) b1 =
["./baseline"
] ai0, ai1 = ai(a1,0)
, ai(b1,1)
try_init(ai0, ai1)
return
"遊戲開始!"
'/send_message'
, methods=
['get'])
defsend_message()
: message_get =
"" message_get = request.args[
'message'
]global a, b
global ai0
global board
global turn
++turn
tmp = message_get.split(
) a =
int(tmp[0]
) b =
int(tmp[1]
)print
(a)print
(b) a, b = ai0.action(a, b)
board.action(
0, turn, a, b)
return
"收到訊息"
'/ai0_change_to_json'
, methods=
['get'])
defai0_change_to_json()
:global a, b
global board
global turn
global ai1
global ai0
if turn ==1:
a, b = ai0.action(-1
,-1)
else
: a, b = ai0.action(a, b)
++turn
board.action(
0, turn, a, b)
print
(a)print
(b)print
("hh");
message_back =
"".join(
str(a)
+" "
+str
(b))
message_json =
return jsonify(message_json)
呼叫c++classai:
def__init__
(self, path,id)
: self.path = path
if path ==
'human'
: self.human =
1else
: self.human =
0 self.id=
iddef
send
(self, message)
: value =
str(message)
+'\n'
value =
bytes
(value,
'utf-8'
) self.proc.stdin.write(value)
self.proc.stdin.flush(
)def
receive
(self)
:return self.proc.stdout.readline(
).strip(
).decode(
)def
init
(self)
:if self.human ==0:
self.proc = subprocess.popen(self.path,
stdin=subprocess.pipe,
stdout=subprocess.pipe)
self.send(self.id)
self.name = self.receive(
)def
action
(self, a, b)
:if self.human ==1:
value =
str(a)
+str
(b)else
: self.send(
str(a)
+' '
+str
(b))
value = self.receive(
).split(
' ')
return
int(value[0]
),int(value[1]
)
主要演算法評估函式,α-β剪枝,置換表(似乎沒有優化)
偽**:
int
alphabeta
(int depth,
int alpha,
int beta)
generatelegalmoves()
;while
(movesleft()
)if(val > alpha)
}return alpha;
}
評估函式多種多樣:如字串匹配,空連子數
vector<:pair>
int>> patterns =,,
,,,,
,,,,
,,,,
,,};
enum chesstype
;
主要為網頁部分
;跨域請求解決:使用外掛程式
2.flask環境配置
虛擬環境和選項配置
建立檔案.flaskenv並寫入
flask_env=development
下五子棋的bot 五子棋演算法
include include include include include include jsoncpp json.h c 編譯時預設包含此庫 define n 7 每個節點的分支數 以下為各棋型的識別碼 權重 define win 1 4000 define lose 2 4000 defi...
窮舉五子棋
本想窮舉五子棋必勝點,可惜呀,這貨窮舉太不現實了,寫出來了,根本沒辦法執行出來結果 include include include define rl 17 char s 14 int five rl rl void init void void print void int cs int i,in...
普通 五子棋
五子棋 五子棋是世界智力運動會競技專案之一,是一種兩人對弈的純策略型棋類遊戲,是世界智力運動會競技專案之一,通常雙方分別使用黑白兩色的棋子,下在棋盤直線與橫線的交叉點上,先形成5子連線者獲勝。棋具與圍棋通用,起源於中國上古時代的傳統黑白棋種之一。主要流行於華人和漢字文化圈的國家以及歐美一些地區,是世...