前言

首先说明一下,本人方向是java后端,只因老师布置了一个作业,要用flask来做一个五子棋,没办法被逼上梁山,程序不太美观,但是应付作业还是够了的。

废话不多说,下面开锤!

首先整个程序是一个web应用,前端html+css+javaScript(有用到jquery)(基本都是现学的,所以程序很多注释也很丑),后端用的flask框架。

准备工作

**1mit() return '这是首页' def queryUser(username,password): user = Usermit() return user @app.route('/replay',methods=["POST"]) def replay(): global map map = [[0 for i in range(15)] for j in range(15)] return {"code": 200,"msg":"成功"} @app.route('/yes',methods=["POST"]) def yes(): print('this is yes ') t = int(request.form['t']) print(t) tmprow = request.form['row'] print(tmprow) tmpcol = request.form['col'] print(tmpcol) row = int(tmprow) col = int(tmpcol) total = 1 map[int(row)][int(col)] = t chessboard = map print(chessboard) print('this is yes ') print(t) print(tmprow) print(tmpcol) #不写注释真容易看混,本少侠就勉强写一点吧 #这里是要判断水平方向上是否满足获胜条件 while col - 1 > 0 and chessboard[row][col - 1] == t: total = total + 1 col = col - 1 row = int(tmprow) col = int(tmpcol) while col + 1 < 15 and chessboard[row][col + 1] == t: total = total + 1 col = col + 1 if total >= 5: if t == 1: return {"code": 201, "msg": "黑棋获胜"} else: return {"code": 202, "msg": "白棋获胜"} #判断垂直方向上是否满足获胜条件 row = int(tmprow) col = int(tmpcol) while row - 1 > 0 and chessboard[row - 1][col] == t: total = total + 1 row = row - 1 row = int(tmprow) col = int(tmpcol) while row + 1 < 15 and chessboard[row + 1][col] == t: total = total + 1 row = row + 1 if total >= 5: if t == 1: return {"code": 201, "msg": "黑棋获胜"} else: return {"code": 202, "msg": "白棋获胜"} #判断pie上是否满足获胜条件 row = int(tmprow) col = int(tmpcol) while row - 1 > 0 and col + 1 < 15 and chessboard[row - 1][col + 1] == t: total = total + 1 row = row - 1 col = col + 1 row = int(tmprow) col = int(tmpcol) while row + 1 < 15 and col - 1 > 0 and chessboard[row + 1][col - 1] == t: total = total + 1 row = row + 1 col = col - 1 if total >= 5: if t == 1: return {"code": 201, "msg": "黑棋获胜"} else: return {"code": 202, "msg": "白棋获胜"} #判断na上是否满足获胜条件 row = int(tmprow) col = int(tmpcol) while row - 1 > 0 and col - 1 > 0 and chessboard[row - 1][col - 1] == t: total = total + 1 row = row - 1 col = col - 1 row = int(tmprow) col = int(tmpcol) while row + 1 < 15 and col + 1 < 15 and chessboard[row + 1][col + 1] == t: total = total + 1 row = row + 1 col = col + 1 if total >= 5: if t == 1: return {"code": 201, "msg": "黑棋获胜"} else: return {"code": 202, "msg": "白棋获胜"} return {"code": 203, "msg": "继续"} #会运行起一个小型服务器,就会将我们的flask程序运行在一个简单的服务器上,服务器由flask提供,用于测试 if __name__ == '__main__': app.run()

数据库表

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `password` varchar(255) NOT NULL,
  `id` int(16) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('yokna', '123456', '1');
INSERT INTO `user` VALUES ('你好你好', '456456', '2');
INSERT INTO `user` VALUES ('你好你好', '456456', '3');
INSERT INTO `user` VALUES ('orange', '123456', '4');

声明:有的资源均来自网络转载,版权归原作者所有,如有侵犯到您的权益 请联系邮箱:312334557@qq.com 我们将配合处理!

原文地址:《基于flask实现五子棋小游戏》发布于2022-03-31 04:21:06