diff --git a/__init__.py b/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/__init__.pyc b/__init__.pyc new file mode 100644 index 000000000..a1ad1dd89 Binary files /dev/null and b/__init__.pyc differ diff --git a/admin.py b/admin.py new file mode 100644 index 000000000..16f5e1cc8 --- /dev/null +++ b/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from mysite.tictactoe.models import Board, Space + + +admin.site.register(Space) +admin.site.register(Board) diff --git a/admin.pyc b/admin.pyc new file mode 100644 index 000000000..64d0efbef Binary files /dev/null and b/admin.pyc differ diff --git a/index.html b/index.html new file mode 100644 index 000000000..514ccd8b3 --- /dev/null +++ b/index.html @@ -0,0 +1,25 @@ + + + Tic Tac DOMINATE + + +

You cannot beat me, Hal

+

{{message}}

+
+
+ {% csrf_token %} + {% for space in spaces %} + + {% if forloop.counter|divisibleby:"3" %} + + {% endif %} + {% endfor %} + +
+ {% if 'empty' in space %} + + {% endif %} + {% if 'X' in space %}

X

{% endif %} + {% if 'O' in space %}

O

{% endif %} + {% if 'NULL' in space %}

-

{% endif %} +
diff --git a/models.py b/models.py new file mode 100644 index 000000000..2d34dcf17 --- /dev/null +++ b/models.py @@ -0,0 +1,151 @@ +from django.db import models +from django.http import HttpResponse, HttpResponseRedirect + + +class Board(models.Model): + + compwin=models.BooleanField(default=False) + gamestarted=models.BooleanField(default=True) + + def Turn(self): + oneone=Space.objects.get(posx=1, posy=1) + onetwo=Space.objects.get(posx=1, posy=2) + onethree=Space.objects.get(posx=1, posy=3) + twoone=Space.objects.get(posx=2, posy=1) + twotwo=Space.objects.get(posx=2, posy=2) + twothree=Space.objects.get(posx=2, posy=3) + threeone=Space.objects.get(posx=3, posy=1) + threetwo=Space.objects.get(posx=3, posy=2) + threethree=Space.objects.get(posx=3, posy=3) + + #first try to win + + #first correction - the outer layer needed to each be if not elif + + if oneone.iso: + if onetwo.iso and onethree.isempty: onethree.CompClicked() + elif onethree.iso and onetwo.isempty: onetwo.CompClicked() + elif twotwo.iso and threethree.isempty: threethree.CompClicked() + elif threethree.iso and twotwo.isempty: twotwo.CompClicked() + elif twoone.iso and threeone.isempty: threeone.CompClicked() + elif threeone.iso and twoone.isempty: twoone.CompClicked() + if onetwo.iso: + if onethree.iso and oneone.isempty: oneone.CompClicked + elif twotwo.iso and threetwo.isempty: threetwo.CompClicked() + elif threetwo.iso and twotwo.isempty: twotwo.CompClicked() + if onethree.iso: + if twotwo.iso and threeone.isempty: threeone.CompClicked() + elif threeone.iso and twotwo.isempty: twotwo.CompClicked() + elif twothree.iso and threethree.isempty: threethree.CompClicked() + elif threethree.iso and twothree.isempty: twothree.CompClicked() + if twoone.iso: + if twotwo.iso and twothree.isempty: twothree.CompClicked() + elif twothree.iso and twotwo.isempty: twotwo.CompClicked() + elif threeone.iso and oneone.isempty: oneone.CompClicked() + if twothree.iso: + if twotwo.iso and twoone.isempty: twoone.CompClicked() + elif threethree.iso and onethree.isempty: onethree.CompClicked() + if threeone.iso: + if threetwo.iso and threethree.isempty: threethree.CompClicked() + elif threethree.iso and threetwo.isempty: threetwo.CompClicked() + elif twotwo.iso and onethree.isempty: onethree.CompClicked() + if threetwo.iso: + if twotwo.iso and onetwo.isempty: onetwo.CompClicked() + elif threethree.iso and threeone.isempty: threeone.CompClicked() + if threethree.iso: + if twotwo.iso and oneone.isempty: oneone.CompClicked() + + # have we won? + + if oneone.iso and ((onetwo.iso and onethree.iso) or (twotwo.iso and threethree.iso) or (twoone.iso and threeone.iso)): self.Win() + if onethree.iso and ((twotwo.iso and threeone.iso) or (twothree.iso and threethree.iso)): self.Win() + if twotwo.iso and ((twoone.iso and twothree.iso) or (onetwo.iso and threetwo.iso)): self.Win() + if threeone.iso and threetwo.iso and threethree.iso: self.Win() + + if self.compwin: return True + # then react to player + + #outer check + + # if player holds center + + if twotwo.isx: + if oneone.isx and threethree.isempty: threethree.CompClicked() + elif onetwo.isx and threetwo.isempty: threetwo.CompClicked() + elif onethree.isx and threeone.isempty: threeone.CompClicked() + elif twoone.isx and twothree.isempty: twothree.CompClicked() + elif twothree.isx and twoone.isempty: twoone.CompClicked() + elif threeone.isx and onethree.isempty: onethree.CompClicked() + elif threetwo.isx and onetwo.isempty: onetwo.CompClicked() + elif threethree.isx and oneone.isempty: oneone.CompClicked() + elif twoone.isx and twothree.isempty: twothree.CompClicked() + elif oneone.isempty: oneone.CompClicked() + elif onethree.isempty: onethree.CompClicked() + elif threeone.isempty: threeone.CompClicked() + elif threethree.isempty: threethree.CompClicked() + elif onetwo.isempty: onetwo.CompClicked() + elif twoone.isempty: twoone.CompClicked() + elif twothree.isempty: twothree.CompClicked() + elif threetwo.isempty: threetwo.CompClicked() + return True + + # if computer holds center + + # second correction - needed to add extra set of brackets around some of the conditionals + + elif twotwo.iso: + if ((oneone.isx and onetwo.isx) or (twothree.isx and threethree.isx)) and onethree.isempty: onethree.CompClicked() + elif oneone.isx and onethree.isx and onetwo.isempty: onetwo.CompClicked() + elif ((oneone.isx and twoone.isx) or (threetwo.isx and threethree.isx)) and threeone.isempty: threeone.CompClicked() + elif oneone.isx and threeone.isx and twoone.isempty: twoone.CompClicked() + elif onethree.isx and threethree.isx and twothree.isempty: twothree.CompClicked() + + #third correction - this check was missing + elif onethree.isx and twothree.isx and twothree.isempty: threethree.CompClicked() + + + elif ((onetwo.isx and onethree.isx) or (twoone.isx and threeone.isx)) and oneone.isempty: oneone.CompClicked() + elif threeone.isx and threethree.isx and threetwo.isempty: threetwo.CompClicked() + elif ((threeone.isx and threetwo.isx) or (onethree.isx and twothree.isx)) and threethree.isempty: threethree.CompClicked() + elif oneone.isx and threethree.isx and onetwo.isempty: onetwo.CompClicked() + elif oneone.isempty: oneone.CompClicked() + elif onethree.isempty: onethree.CompClicked() + elif threeone.isempty: threeone.CompClicked() + elif threethree.isempty: threethree.CompClicked() + elif onetwo.isempty: onetwo.CompClicked() + elif twoone.isempty: twoone.CompClicked() + elif twothree.isempty: twothree.CompClicked() + elif threetwo.isempty: threetwo.CompClicked() + + elif twotwo.isempty: twotwo.CompClicked() + + + + def Win(self): + self.compwin=True + self.save() + return HttpResponseRedirect('/tttoe/') + + + +class Space(models.Model): + isempty=models.BooleanField(default=True) + isx=models.BooleanField(default=False) + iso=models.BooleanField(default=False) + posx=models.IntegerField() + posy=models.IntegerField() + game=models.ForeignKey(Board) + + def PlayerClicked(self): + self.isempty=False + self.isx=True + self.iso=False + self.save() + return True + + def CompClicked(self): + self.isempty=False + self.isx=False + self.iso=True + self.save() + return True \ No newline at end of file diff --git a/models.pyc b/models.pyc new file mode 100644 index 000000000..08e349010 Binary files /dev/null and b/models.pyc differ diff --git a/test.html b/test.html new file mode 100644 index 000000000..11cd43c4b --- /dev/null +++ b/test.html @@ -0,0 +1,6 @@ +Test Results + +
+ {% for string in wins %} + {{string}}
+ {% endfor %}
\ No newline at end of file diff --git a/tests.py b/tests.py new file mode 100644 index 000000000..9dacca079 --- /dev/null +++ b/tests.py @@ -0,0 +1,86 @@ + + + +from django.http import HttpResponse, HttpResponseRedirect +from django.shortcuts import render_to_response +from mysite.tttoe.models import Space, Board +from django.template import RequestContext +import random + +def BuildSquares(): + squares=[] + for each in Space.objects.all(): + if each.iso: squares.append('O') + elif each.isx: squares.append('X') + else: squares.append('empty') + return squares + + +def index(request): + results=[] + for n in xrange(0,100): + moves='' + squares=BuildSquares() + new=Board() + new.save() + for x in xrange(1,4): + for y in xrange(1,4): + newspace=Space(game=new, posx=x, posy=y) + newspace.save() + while True: + value=False + for each in Board.objects.all(): + if each.compwin==True: + value=True + endgame() + if value: break + counter=0 + for each in Space.objects.all(): + if each.isempty: counter+=1 + if counter==0: + if checkwin(): + if moves not in results: results.append(moves) + endgame() + break + while True: + ran1=random.randint(1,3) + ran2=random.randint(1,3) + check=Space.objects.get(posx=ran1, posy=ran2) + if check.isempty: + check.PlayerClicked() + moves+='P'+' '+str(ran1)+str(ran2)+' ' + break + for each in Board.objects.all(): + each.Turn() + return render_to_response('tttoe/test.html', {'wins':results}) + + +def endgame(): + #will completely clear both database tables + for each in Space.objects.all(): each.delete() + for each in Board.objects.all(): each.delete() + return True + +def checkwin(): + + #finished board + + oneone=Space.objects.get(posx=1, posy=1) + onetwo=Space.objects.get(posx=1, posy=2) + onethree=Space.objects.get(posx=1, posy=3) + twoone=Space.objects.get(posx=2, posy=1) + twotwo=Space.objects.get(posx=2, posy=2) + twothree=Space.objects.get(posx=2, posy=3) + threeone=Space.objects.get(posx=3, posy=1) + threetwo=Space.objects.get(posx=3, posy=2) + threethree=Space.objects.get(posx=3, posy=3) + + #did player beat computer + + if oneone.isx and ((onetwo.isx and onethree.isx) or (twotwo.isx and threethree.isx) or (twoone.isx and threeone.isx)): return True + if onethree.isx and ((twotwo.isx and threeone.isx) or (twothree.isx and threethree.isx)): return True + if twotwo.isx and twoone.isx and twothree.isx: return True + if onetwo.isx and twotwo.isx and threetwo.isx: return True + if threeone.isx and threetwo.isx and threethree.isx: return True + + return False \ No newline at end of file diff --git a/urls.py b/urls.py new file mode 100644 index 000000000..2f793d649 --- /dev/null +++ b/urls.py @@ -0,0 +1,8 @@ +from django.conf.urls.defaults import * + +urlpatterns = patterns('mysite.tictactoe.views', + ('^$', 'index'), + ('^processmove/$', 'processmove'), + ('^BuildSquares/$', 'BuildSquares'), + ('^endgame/$', 'endgame'), +) diff --git a/urls.pyc b/urls.pyc new file mode 100644 index 000000000..b281c6aa9 Binary files /dev/null and b/urls.pyc differ diff --git a/views.py b/views.py new file mode 100644 index 000000000..55aeb94ef --- /dev/null +++ b/views.py @@ -0,0 +1,64 @@ +from django.http import HttpResponse, HttpResponseRedirect +from django.shortcuts import render_to_response +from mysite.tictactoe.models import Space, Board +from django.template import RequestContext + +def BuildSquares(): + squares=[] + for each in Space.objects.all(): + if each.iso: squares.append('O') + elif each.isx: squares.append('X') + else: squares.append('empty') + return squares + + +def index(request): + squares=BuildSquares() + message="Click Play to begin. Click a checkbox to take Square. You can try to click more checkboxes, you big cheater, but it won't work!!@! Then click Play to Continue!@!" + count=int(Board.objects.count()) + if count==0: + new=Board() + new.save() + for x in xrange(1,4): + for y in xrange(1,4): + newspace=Space(game=new, posx=x, posy=y) + newspace.save() + else: + for each in Board.objects.all(): + if each.compwin: + endgame() + message="You Lose!!! How could you lose at Tic Tac Toe? Would you like to play Global Thermonuclear War instead? (Or click Play to try again)" + for x in xrange(0,9): + if squares[x]=='empty': squares[x]='NULL' + return render_to_response('tictactoe/index.html', {'spaces':squares, 'message':message},context_instance=RequestContext(request)) + counter=0 + for each in Space.objects.all(): + if each.isempty: counter+=1 + if counter==0: + endgame() + message="Another Tie. Would you like to play Global Thermonuclear War instead? (Or click Play to try again)" + return render_to_response('tictactoe/index.html', {'spaces':squares, 'message':message},context_instance=RequestContext(request)) + +def processmove(request): + x=1 + y=1 + for z in xrange(1,10): + string='button'+str(z) + if request.POST.get(string)=='Take': + check=Space.objects.get(posx=x, posy=y) + check.PlayerClicked() + for each in Board.objects.all(): + each.Turn() + return HttpResponseRedirect('/tictactoe') + if y==3: + y=1 + x+=1 + else: y+=1 + return HttpResponseRedirect('/tictactoe/') + + +def endgame(): + #will completely clear both database tables + for each in Space.objects.all(): each.delete() + for each in Board.objects.all(): each.delete() + return True \ No newline at end of file diff --git a/views.pyc b/views.pyc new file mode 100644 index 000000000..30187642c Binary files /dev/null and b/views.pyc differ