How would I connect my HTML code as an page that leads to my python tic-tac-toe
ID: 3728859 • Letter: H
Question
How would I connect my HTML code as an page that leads to my python tic-tac-toe game? The HTML page i have asks two players to enter their name and then those names will be used in the python 3.5 to notify which user will be X and O.
Here is my HTML code:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
Player1: <input type="text" name="fullname"><br>
Player2: <input type="text" name="fullname"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This is the python code:
#!/usr/local/bin/python3.5
import cgi, cgitb
cgitb.enable()
def imageTag(string):
if string=='X':
return "<img src="x.png" />"
else:
return "<img src="O.png" />"
def threeInARow(tttBoard, p1, p2, p3):
# 0 1 2
# 3 4 5
# 6 7 8
if tttBoard[p1] == tttBoard[p2] and
tttBoard[p2] == tttBoard[p3] and
tttBoard[p1] != ' ':
return tttBoard[p1]
return None
def findWinner(tttBoard):
if threeInARow(tttBoard, 6, 7, 8) is not None:
return threeInARow(tttBoard, 6, 7, 8)
return None
def printWhoGoes(whogoes):
print ("<center><h2>")
print ("Player " + whogoes + " goes")
print ("</h2></center>")
def printRestart():
print ("<form method='POST'>")
print ("<input type='submit' value='Restart Game'>")
print ("</form>")
def printCell(cellEntry, cellNumber):
if cellEntry == ' ':
print ("<input type='submit' name='cell' value='" + str(cellNumber) + "'>")
else:
print ( imageTag(cellEntry) )
def printForm(boardList, whogoes):
print ("<table border='1' align='center'>")
print ("<form method='POST'>")
boardString = ':'
boardString = boardString.join(boardList)
print ("<input type='hidden' name='tttString' value='" + str(boardString) + "'>")
print ("<input type='hidden' name='whogoes' value='" + str(whogoes) + "'>")
# /t == tab to make the output html look better
for row in range(0,3):
print (" <tr>")
print (" <td align='center' width='20' height='20'>")
printCell(boardList[0 + row*3], 0 + row*3)
print (" </td>")
print (" <td align='center' width='20' height='20'>")
printCell(boardList[1 + row*3], 1 + row*3)
print (" </td>")
print (" <td align='center' width='20' height='20'>")
printCell(boardList[2 + row*3], 2 + row*3)
print (" </tr>")
print ("</form>")
print ("</table>")
print ("Content-type: text/html ")
print ("Version 0.00a")
tttForm = cgi.FieldStorage()
cell = tttForm.getvalue('cell')
if cell is None: # (re)start the game
tttBoard = [' ',' ', ' ',
' ',' ', ' ',
' ',' ', ' ' ]
whogoes = 'X'
else:
tttString = tttForm.getvalue('tttString')
whogoes = tttForm.getvalue('whogoes')
tttBoard = tttString.split(':')
tttBoard[ int(cell) ] = whogoes
if whogoes=='X':
whogoes='O'
else:
whogoes='X'
winner = findWinner(tttBoard)
if winner is None:
printWhoGoes(whogoes)
printForm(tttBoard, whogoes)
else:
print (winner)
print ("is the winner")
printRestart()
Explanation / Answer
Answer )
You should use django for this purpose .Django is a free and open-source web framework, written in Python, which follows the model-view-template architectural pattern.
In python should use something
and do your respective coding
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.