【发布时间】:2022-01-14 21:36:07
【问题描述】:
在我合并源之前它正在工作 这是我尝试的第一个来源
from flask import Flask, jsonify
from random import random
from flask.globals import request
from flask.templating import render_template
import numpy as np
from tensorflow.keras.models import load_model
model = load_model('models/20201213_202430.h5')
app = Flask(__name__, static_url_path='', static_folder='static')
@app.route('/omok')
def omok():
return render_template('omok05_ai.html')
@app.route('/ajaxtest')
def ajaxtest():
return render_template('ajaxtest.html')
#===============================================================================
@app.route('/ajax_omok', methods=['POST'])
def ajax_omok():
data = request.get_json()
stones = data['stones']
arr_stone = stones.split(',')
print(arr_stone)
input = []
for s in arr_stone:
input.append(int(s))
input_n = np.array(input)
input_n = np.reshape(input_n,(1,20,20,1))
output = model.predict(input_n).squeeze()
cnt = np.argmax(output)
i = int(cnt/20)
j = int(cnt%20)
print(i,j)
return jsonify(i = i, j = j)
#===============================================================================
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)
这是html源代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OMOK</title>
<script type="text/javascript" src="jquery-3.6.0.js"></script>
<script type="text/javascript">
// var comput_i = 0;
// var computs = [
// {"i":0,"j":0},
// {"i":0,"j":1},
// {"i":0,"j":2},
// {"i":0,"j":3},
// {"i":0,"j":4}
// ]
var flag_wb = true;
var flag_over = false;
var arr2d = [
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0]
];
function aiinit(){
}
function myinit() {
var stns = document.getElementsByClassName("stn");
for(var i=0; i<20; i++) {
for(var j=0; j<20; j++) {
stns[i*20+j].alt = i+","+j;
}
}
}
function myrender() {
var stns = document.getElementsByClassName("stn");
for(var i=0; i<20; i++) {
for(var j=0; j<20; j++) {
if(arr2d[i][j] == 0) {
stns[i*20+j].src = "0.png"
}
if(arr2d[i][j] == 1) {
stns[i*20+j].src = "1.png"
}
if(arr2d[i][j] == 2) {
stns[i*20+j].src = "2.png"
}
}
}
}
function myreset(){
var stns = document.getElementsByClassName("stn");
for(var i=0; i<20; i++) {
for(var j=0; j<20; j++) {
arr2d[i][j]=0;
stns[i*20+j].src = "0.png"
}
}
flag_over = false;
flag_wb = true;
}
function myclick(obj) {
if(flag_over) {
return;
}
var str = obj.alt;
var arr = str.split(",");
var i = parseInt(arr[0]);
var j = parseInt(arr[1]);
if(arr2d[i][j] > 0) {
return;
}
var stone = -1;
if (flag_wb) {
arr2d[i][j] = 1;
stone = 1;
} else {
arr2d[i][j] = 2;
stone = 2;
}
var up = checkUp(i,j,stone);
var dw = checkDw(i,j,stone);
var ri = checkRi(i,j,stone);
var le = checkLe(i,j,stone);
var ur = checkUr(i,j,stone);
var ul = checkUl(i,j,stone);
var dr = checkDr(i,j,stone);
var dl = checkDl(i,j,stone);
var d1 = up+dw+1;
var d2 = le+ri+1;
var d3 = ul+dr+1;
var d4 = ur+dl+1;
myrender();
if(d1==5 || d2==5 || d3==5 || d4==5) {
if(flag_wb) {
setTimeout(function(){
alert("흰돌 승리");
},300);
} else {
setTimeout(function(){
alert("흑돌 승리");
},300);
}
flag_over = true;
return
}
flag_wb = !flag_wb;
/*==============================*/
var stones = arr2d+"";
stones = stones.replace(/2/gi, "-1")
console.log(stones)
var postdata = {
'stones' : stones
}
console.log(postdata)
var iii
var jjj
$.ajax({
url : "ajax_omok",
data : JSON.stringify(postdata),
dataType : "json",
type : "post",
contentType : "application/json",
async : false,
statusCode : {
404 : function() {
alert("네트워크가 불안정합니다. 다시 시도부탁드립니다.");
}
},
success : function(data) {
console.log(data);
console.log(data['i']);
console.log(data['j']);
iii = data['i']
jjj = data['j']
}
});
console.log(iii)
console.log(jjj)
var i = iii;
var j = jjj;
var stone = -1;
if (flag_wb) {
arr2d[i][j] = 1;
stone = 1;
} else {
arr2d[i][j] = 2;
stone = 2;
}
var up = checkUp(i,j,stone);
var dw = checkDw(i,j,stone);
var ri = checkRi(i,j,stone);
var le = checkLe(i,j,stone);
var ur = checkUr(i,j,stone);
var ul = checkUl(i,j,stone);
var dr = checkDr(i,j,stone);
var dl = checkDl(i,j,stone);
var d1 = up+dw+1;
var d2 = le+ri+1;
var d3 = ul+dr+1;
var d4 = ur+dl+1;
myrender();
if(d1==5 || d2==5 || d3==5 || d4==5) {
if(flag_wb) {
setTimeout(function(){
alert("흰돌 승리");
},300);
} else {
setTimeout(function(){
alert("흑돌 승리");
},300);
}
flag_over = true;
return
}
flag_wb = !flag_wb;
comput_i++
}//end
function checkUp(i,j,stone) {
var cnt = 0;
try {
while(true) {
i--;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkDw(i,j,stone) {
var cnt = 0;
try {
while(true) {
i++;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkRi(i,j,stone) {
var cnt = 0;
try {
while(true) {
j++;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkLe(i,j,stone) {
var cnt = 0;
try {
while(true) {
j--;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkUr(i,j,stone) {
var cnt = 0;
try {
while(true) {
i--;
j++;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkUl(i,j,stone) {
var cnt = 0;
try {
while(true) {
i--;
j--;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkDr(i,j,stone) {
var cnt = 0;
try {
while(true) {
i++;
j++;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkDl(i,j,stone) {
var cnt = 0;
try {
while(true) {
i++;
j--;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
$.ajax({
url : "ajax_insert_sawon",
data : JSON.stringify(postdata),
dataType : "json",
type : "post",
contentType : "application/json",
async : false,
statusCode : {
404 : function() {
alert("네트워크가 불안정합니다. 다시 시도부탁드립니다.");
}
},
success : function(data) {
if (data.result == "ok") {
location.reload();
} else {
alert("문제 발생")
}
console.log(data)
}
});
</script>
<style type="text/css">
table {
border-spacing: 0px;
}
tr, td {
padding: 0px;
}
img {
display: block;
}
</style>
</head>
<body onload="myinit()">
<input type="button" onclick="myreset()" value="리셋">
<table>
<tr>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
</tr>
...200more
</table>
</body>
</html>
但是在我尝试合并第一个和套接字之后,ajax 不再起作用,它说 回溯(最近一次通话最后): wsgi_app 中的文件“D:\pythonMatarials\Anaconda3\Lib\site-packages\flask\app.py”,第 2450 行 响应 = self.handle_exception(e) 文件“D:\pythonMatarials\Anaconda3\Lib\site-packages\flask\app.py”,第 1867 行,在 handle_exception 中 reraise(exc_type, exc_value, tb) 文件“D:\pythonMatarials\Anaconda3\Lib\site-packages\flask_compat.py”,第 39 行,在 reraise 升值 wsgi_app 中的文件“D:\pythonMatarials\Anaconda3\Lib\site-packages\flask\app.py”,第 2447 行 响应 = self.full_dispatch_request() 文件“D:\pythonMatarials\Anaconda3\Lib\site-packages\flask\app.py”,第 1952 行,在 full_dispatch_request rv = self.handle_user_exception(e) 文件“D:\pythonMatarials\Anaconda3\Lib\site-packages\flask\app.py”,第 1821 行,在 handle_user_exception 中 reraise(exc_type, exc_value, tb) 文件“D:\pythonMatarials\Anaconda3\Lib\site-packages\flask_compat.py”,第 39 行,在 reraise 升值 文件“D:\pythonMatarials\Anaconda3\Lib\site-packages\flask\app.py”,第 1950 行,在 full_dispatch_request 中 rv = self.dispatch_request() 文件“D:\pythonMatarials\Anaconda3\Lib\site-packages\flask\app.py”,第 1936 行,在 dispatch_request 中 返回 self.view_functionsrule.endpoint ajax_omok 中的文件“D:\pythonMatarials\workspace_py\HELLO_AI\day15\myflask.py”,第 25 行 数据 = request.get_json() AttributeError: 'function' 对象没有属性 'get_json'
这是我的最后一个来源
from flask import Flask, jsonify
from random import random
from flask.globals import request
from flask.templating import render_template
import numpy as np
from tensorflow.keras.models import load_model
from flask_socketio import SocketIO, send
model = load_model('models/20201213_202430.h5')
dummy_n = np.zeros((400))
dummy_n = np.reshape(dummy_n,(1,20,20,1))
model.predict(dummy_n)
app = Flask(__name__, static_url_path='', static_folder='static')
app.secret_key = "mysecret"
socket_io = SocketIO(app)
@app.route('/omok')
def omok():
return render_template('omok05_ai_network.html')
#===============================================================================
@app.route('/ajax_omok', methods=['GET','POST'])
def ajax_omok():
data = request.get_json()
stones = data['stones']
arr_stone = stones.split(',')
print(arr_stone)
input = []
for s in arr_stone:
input.append(int(s))
input_n = np.array(input)
input_n = np.reshape(input_n,(1,20,20,1))
output = model.predict(input_n).squeeze()
cnt = np.argmax(output)
i = int(cnt/20)
j = int(cnt%20)
print(i,j)
return jsonify(i = i, j = j)
@app.route('/chat')
def chatting():
return render_template('chat_omok.html')
@socket_io.on("message")
def request(message):
print("message : "+ message)
to_client = dict()
to_client['message'] = message
send(to_client, broadcast=True)
#===============================================================================
if __name__ == '__main__':
# app.run(host="0.0.0.0", debug=True)
socket_io.run(app,host="0.0.0.0", debug=True, port=5000)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>OMOK</title>
<script type="text/javascript" src="jquery-3.6.0.js"></script>
<script type="text/javascript" src="socket.io.min.js"></script>
<script type="text/javascript">
// var comput_i = 0;
// var computs = [
// {"i":0,"j":0},
// {"i":0,"j":1},
// {"i":0,"j":2},
// {"i":0,"j":3},
// {"i":0,"j":4}
// ]
var flag_wb = true;
var flag_over = false;
var arr2d = [
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0]
];
function aiinit(){
}
function myinit() {
var stns = document.getElementsByClassName("stn");
for(var i=0; i<20; i++) {
for(var j=0; j<20; j++) {
stns[i*20+j].alt = i+","+j;
}
}
}
function myrender() {
var stns = document.getElementsByClassName("stn");
for(var i=0; i<20; i++) {
for(var j=0; j<20; j++) {
if(arr2d[i][j] == 0) {
stns[i*20+j].src = "0.png"
}
if(arr2d[i][j] == 1) {
stns[i*20+j].src = "1.png"
}
if(arr2d[i][j] == 2) {
stns[i*20+j].src = "2.png"
}
}
}
}
function myreset(){
var stns = document.getElementsByClassName("stn");
for(var i=0; i<20; i++) {
for(var j=0; j<20; j++) {
arr2d[i][j]=0;
stns[i*20+j].src = "0.png"
}
}
flag_over = false;
flag_wb = true;
}
function myclick(obj) {
if(flag_over) {
return;
}
var str = obj.alt;
var arr = str.split(",");
var i = parseInt(arr[0]);
var j = parseInt(arr[1]);
if(arr2d[i][j] > 0) {
return;
}
var stone = -1;
if (flag_wb) {
arr2d[i][j] = 1;
stone = 1;
} else {
arr2d[i][j] = 2;
stone = 2;
}
var up = checkUp(i,j,stone);
var dw = checkDw(i,j,stone);
var ri = checkRi(i,j,stone);
var le = checkLe(i,j,stone);
var ur = checkUr(i,j,stone);
var ul = checkUl(i,j,stone);
var dr = checkDr(i,j,stone);
var dl = checkDl(i,j,stone);
var d1 = up+dw+1;
var d2 = le+ri+1;
var d3 = ul+dr+1;
var d4 = ur+dl+1;
myrender();
if(d1==5 || d2==5 || d3==5 || d4==5) {
if(flag_wb) {
setTimeout(function(){
alert("흰돌 승리");
},300);
} else {
setTimeout(function(){
alert("흑돌 승리");
},300);
}
flag_over = true;
return
}
flag_wb = !flag_wb;
/*==============================*/
var stones = arr2d+"";
stones = stones.replace(/2/gi, "-1")
console.log(stones)
var postdata = {
'stones' : stones
}
var iii
var jjj
console.log(postdata)
$.ajax({
url : "ajax_omok",
data : JSON.stringify(postdata),
dataType : "json",
type : "post",
contentType : "application/json",
async : false,
statusCode : {
404 : function() {
alert("네트워크가 불안정합니다. 다시 시도부탁드립니다.");
}
},
success : function(data) {
console.log(data);
console.log(data['i']);
console.log(data['j']);
iii = data['i']
jjj = data['j']
}
});
var i = iii;
var j = jjj;
var stone = -1;
if (flag_wb) {
arr2d[i][j] = 1;
stone = 1;
} else {
arr2d[i][j] = 2;
stone = 2;
}
var up = checkUp(i,j,stone);
var dw = checkDw(i,j,stone);
var ri = checkRi(i,j,stone);
var le = checkLe(i,j,stone);
var ur = checkUr(i,j,stone);
var ul = checkUl(i,j,stone);
var dr = checkDr(i,j,stone);
var dl = checkDl(i,j,stone);
var d1 = up+dw+1;
var d2 = le+ri+1;
var d3 = ul+dr+1;
var d4 = ur+dl+1;
myrender();
if(d1==5 || d2==5 || d3==5 || d4==5) {
if(flag_wb) {
setTimeout(function(){
alert("흰돌 승리");
},300);
} else {
setTimeout(function(){
alert("흑돌 승리");
},300);
}
flag_over = true;
return
}
flag_wb = !flag_wb;
comput_i++
}//end
function checkUp(i,j,stone) {
var cnt = 0;
try {
while(true) {
i--;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkDw(i,j,stone) {
var cnt = 0;
try {
while(true) {
i++;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkRi(i,j,stone) {
var cnt = 0;
try {
while(true) {
j++;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkLe(i,j,stone) {
var cnt = 0;
try {
while(true) {
j--;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkUr(i,j,stone) {
var cnt = 0;
try {
while(true) {
i--;
j++;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkUl(i,j,stone) {
var cnt = 0;
try {
while(true) {
i--;
j--;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkDr(i,j,stone) {
var cnt = 0;
try {
while(true) {
i++;
j++;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
function checkDl(i,j,stone) {
var cnt = 0;
try {
while(true) {
i++;
j--;
if (arr2d[i][j] == stone) {
cnt++;
} else {
return cnt;
}
}
} catch(e) {
return cnt;
}
return cnt;
}
// $.ajax({
// url : "ajax_insert_sawon",
// data : JSON.stringify(postdata),
// dataType : "json",
// type : "post",
// contentType : "application/json",
// async : false,
// statusCode : {
// 404 : function() {
// alert("네트워크가 불안정합니다. 다시 시도부탁드립니다.");
// }
// },
// success : function(data) {
// if (data.result == "ok") {
// location.reload();
// } else {
// alert("문제 발생")
// }
// console.log(data)
// }
// });
</script>
<style type="text/css">
table {
border-spacing: 0px;
}
tr, td {
padding: 0px;
}
img {
display: block;
}
</style>
</head>
<body onload="myinit()">
<input type="button" onclick="myreset()" value="리셋">
<table>
<tr>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
<td><img src="0.png" class="stn" onclick="myclick(this)"></td>
...200 more
</body>
</html>
我该如何解决这个问题?
【问题讨论】: