this is my views.py
@csrf_exempt
def tictactoe_ai(request):
context = {'current_url' : '/tictactoe'}
if request.method == "GET":
my_ai = Tic_ai.objects.create()
request.session['tictai_id'] = str(my_ai.id)
return render(request,'static_page/tictactoe_ai.html',context)
if request.is_ajax():
res = {}
the_id = int(request.session['tictai_id'])
my_ai = Tic_ai.objects.get(id = the_id)
rest = request.POST
x,y = rest.get("x"),rest.get("y")
a = str(x) + ' ' + str(y) + '\n'
message = my_ai.in_stuf(a)
res["x"],res["y"] = message.split(" ")
return JsonResponse(res)
this is my models.py
class Tic_ai(models.Model):
ai = subprocess.Popen(
r"D:\duoduo2\DDQK-Caesar\ddqk-caesar\static_page\tictac_ai.exe",
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True)
def in_stuf(self,a):
self.ai.stdin.write(a.encode())
self.ai.stdin.flush()
return self.ai.stdout.readline().decode().replace('\n','').replace('\r','')
def finish(self):
self.ai.kill()
I found that although the code "my_ai = Tic_ai.objects.create()" was excecute. but the ai are all the same. I want to have different ai in different Tic_ai. But the "ai" are all the same
please. This trouble had disturbed me for a whole day
What I have tried:
What i've tried are all in my question