Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lol - well... #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added __init__.py
Empty file.
Binary file added __init__.pyc
Binary file not shown.
6 changes: 6 additions & 0 deletions admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin
from mysite.tictactoe.models import Board, Space


admin.site.register(Space)
admin.site.register(Board)
Binary file added admin.pyc
Binary file not shown.
25 changes: 25 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<html>
<head>
<title>Tic Tac DOMINATE</title>
</head>
<body>
<h1>You cannot beat me, Hal</h1>
<p>{{message}}</p>
<div>
<form name="main" action="/tictactoe/processmove/" method="POST">
{% csrf_token %}
<table border='2'></tr>{% for space in spaces %}
<td width=75px height=75px align=center valign=center>
{% if 'empty' in space %}
<input type="checkbox" name="button{{ forloop.counter }}" value="Take"/>
{% endif %}
{% if 'X' in space %} <h1>X</h1> {% endif %}
{% if 'O' in space %} <h1>O</h1> {% endif %}
{% if 'NULL' in space %} <h1>-</h1> {% endif %}
</td>
{% if forloop.counter|divisibleby:"3" %}
</tr><tr>
{% endif %}
{% endfor %}
<input type="submit" value="Play!" />
</tr></table></form></div>
151 changes: 151 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -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
Binary file added models.pyc
Binary file not shown.
6 changes: 6 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html><head>Test Results</head>

<body><div>
{% for string in wins %}
{{string}}</br>
{% endfor %}</div></body></html>
86 changes: 86 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.conf.urls.defaults import *

urlpatterns = patterns('mysite.tictactoe.views',
('^$', 'index'),
('^processmove/$', 'processmove'),
('^BuildSquares/$', 'BuildSquares'),
('^endgame/$', 'endgame'),
)
Binary file added urls.pyc
Binary file not shown.
64 changes: 64 additions & 0 deletions views.py
Original file line number Diff line number Diff line change
@@ -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
Binary file added views.pyc
Binary file not shown.