Reputation: 611
the fallowing is a tic tak toe game code in python, can some one show me how i can make it in GUI form with a reset option and shows who wins at the end. like X win or O win?
board = " 1 | 2 | 3\n-----------\n 4 | 5 | 6\n-----------\n 7 | 8 | 9"
. checkboard=[1,2,3,4,5,6,7,8,9,1,4,7,2,5,8,3,6,9,1,5,9,3,5,7]
spaces=range(1,10)
def moveHandler(board,spaces,checkboard,player,n):
if player==1:
check="X"
else:
check="O"
while spaces.count(n)==0:
print "\nInvalid Space"
n=playerinput(player)
spaces=spaces.remove(n)
board=board.replace(str(n),check)
for c in range(len(checkboard)):
if checkboard[c]==n:
checkboard[c]=check
status = checkwinner(checkboard,check)
return board,status
def checkwinner(checkboard,check): a,b,c=0,1,2
while a<=21:
combo = [checkboard[a],checkboard[b],checkboard[c]]
if combo.count(check) == 3:
status =1
break
else:
status =0
a+=3
b+=3
c+=3
return status
def playerinput(player): try: key = int(raw_input('\n\nPlayer ' + str(player) + ': Please select a space '))
except ValueError:
print "Invalid Space"
key = playerinput(player)
return key
while True:
player = len(spaces)%2 +1
if player == 1:
player = 2
else:
player =1
print "\n\n" + board
key = playerinput(player)
board,status =moveHandler(board,spaces,checkboard,player,key)
if status == 1:
print '\n\nPlayer ' + str(player) + ' is the winner!!!'
print board
break
elif len(spaces)==0:
print "No more spaces left. Game ends in a TIE!!!"
print board
break
else:
continue
Upvotes: 0
Views: 519
Reputation: 881477
Clearly you need to choose a GUI toolkit (Python supports many of them), use it to paint the board as a 3 x 3 grid of squares, and change the playerinput
function to accept input by (e.g.) the current player double clicking on the empty square he or she wants to play in.
Then, you need to change the print
statements to show information on the GUI surface.
The game however would be much better if it didn't try to control the flow of events but rather responded to events initiated by the players -- that's how real GUI apps should be done, rather than by minimal retrofitting of some interface on top of what's intrinsically designed as a command-line interactive procedure.
Each of these tasks is a substantial one, especially the overall refactoring I recommend in the last paragraph, and entirely depends in its details on what GUI toolkit you choose -- so you might want to start with that, and then generally break the question up into the various subtasks ("one question per question", since many of the latter may arise;-).
There are several SO questions on the subject of GUI choice for Python, so I recommend you study them rather than asking a new one. My personal favorite is PyQt (though more and more often I just do a simple browser-based interface with a local-only server powering it), but other popular ones include wxPython, Tkinter, PyGtk, and others listed here -- happy hunting!
Upvotes: 5
Reputation: 64105
Check out the python wiki for info about different GUI toolkits. I would recommend looking at wxPython, and going from there
Upvotes: 1