Click here to Skip to main content
15,355,133 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is my views.py

Python
@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
Posted
Updated 22-May-22 14:17pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900