Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Created November 2, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save highfestiva/64cd22d952f5ce73c03750e51ed1bed4 to your computer and use it in GitHub Desktop.
Save highfestiva/64cd22d952f5ce73c03750e51ed1bed4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Server for remote naughts and crosses playing.
from flask import Flask, jsonify, request
map = [[' ' for _ in range(20)] for _ in range(20)]
map[10][10] = 'X'
turn = 2
webapp = Flask(__name__)
def check_winner():
return
@webapp.route('/map')
def getmap():
strmap = [''.join(row) for row in map]
return jsonify(strmap)
@webapp.route('/marker', methods=['PUT'])
def marker():
global turn
x = int(request.form['x'])
y = int(request.form['y'])
t = int(request.form['turn'])
assert 0 <= x <= len(map)
assert 0 <= y <= len(map)
if t == turn:
map[y][x] = 'X' if t==1 else 'O'
turn = 3-t
check_winner()
return jsonify('ack')
r = jsonify('nak')
r.status_code = 409
return r
webapp.run(host='0.0.0.0', port=9002, threaded=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment