python - Accepting only integers for TIC TAC TOE -
here's tic tac toe game have created using python..
import os os.system('cls') = 0 #exiter def exithoja(): import sys raw_input sys.exit() #displays win or draw def diswin(name,grid): = checkwin(grid) os.system('cls') viewgrid(grid) if ==1: print name, " has won game !!" elif == -1: print "this match draw !!" exithoja() #checking win or draw function def checkwin(grid): = 0 result = 0 extra=0 in range (1,9): #this part checks full grid. if (grid[i] == 'x' or grid[i]=='o'): += 1 if (grid[1] == grid[2] , grid[2] == grid[3]): result = 1 #this part checks win. elif(grid[4] == grid[5] , grid[5] == grid[6]): result = 1 elif(grid[7] == grid[8] , grid[8] == grid[9]): result = 1 elif(grid[1] == grid[4] , grid[4] == grid[7]): result = 1 elif(grid[2] == grid[5] , grid[5] == grid[8]): result = 1 elif(grid[3] == grid[6] , grid[6] == grid[9]): result = 1 elif(grid[1] == grid[5] , grid[5] == grid[9]): result = 1 elif(grid[3] == grid[5] , grid[5] == grid[7]): result = 1 elif(extra==9): #this part checks draw. result = -1 return result #0 continue,1 win, -1 draw. #grid print function def viewgrid(grid): print "\n\n . .\n", print " | | " in range(1,10): if i%3==0: print " ", else: print "", print "",grid[i]," ", if i%3 == 0: if == 9: print "\n | |", print "\n ' '" else: print "\n | | ", print "\n------+-------+-------\n", print " | | " else: print "|", #grid print function ends #marks user choice possible ('x' or 'o' if possible, 0 if not possible) def markgrid(user,grid, place): if grid[place] != place: returnvalue = 0 else: returnvalue = user return returnvalue #end of mark grid function #player 1 marking function part 1 def player11(name1,grid): os.system('cls') viewgrid(grid) print name1, ", please enter choice grid above : ", place = raw_input() place = int(place,10) if place == '.': import sys sys.exit() elif place > 9 or place < 1: place = 0 return place #player 1 marking function part 1 ends #player 1 marking function part 2 def player12(place,grid,name1): if place == 0: while place==0: place == player11(name1,grid) grid[place] = markgrid('o',grid,place) #player 1 marking function part 2 ends #player 2 marking function part 1 def player21(name2,grid): os.system('cls') viewgrid(grid) print name2, ", please enter choice grid above : ", place = raw_input() place = int(place,10) if place == '.': import sys sys.exit() elif place > 9 or place < 1: place = 0 return place #player 2 marking function part 1 ends #player 2 marking function part 2 def player22(place,grid,name2): if place == 0: while place==0: place == player21(name2,grid) grid[place] = markgrid('x',grid,place) #player 2 marking function part 2 ends #reset grid code starts here def gridreset(grid): j = 0 j in range(0,10): grid.append(j) #reset grid code ends here #this main program, defined function def playgame(): print "\n\nuser 1 - please enter name : ", name1 = raw_input() print "user 2 - please enter name : ", name2 = raw_input() print "\n",name1,", marking o", print "\n",name2,", marking x" user1 = 'o' user2 = 'x' raw_input() grid = [] gridreset(grid) def player1(name1): = player11(name1,grid) if markgrid('o',grid,i) == 0: player1(name1) player12(i,grid,name1) = checkwin(grid) print if i==1 or == -1: diswin(name1,grid) player2(name2) return grid def player2(name2): = player21(name2,grid) if markgrid('x',grid,i) == 0: player2(name2) player22(i,grid,name2) = checkwin(grid) if i==1 or == -1: diswin(name2,grid) player1(name1) return grid player1(name1) player2(name2) return grid #main program end #main program execution grid = [] grid = playgame()
the main thing left accept integers variable "place" in functions player11 , player21.. if it's not integer function called again , new value "place" assigned.. please me on how go ... in advance.. edit: user/player not given error code...
according principle seeking forgiveness better asking permission, simple try:except clause
while true: r = raw_input('choose ') try: c = int(r) except valueerror: print 'not valid.\n' else: break #this executed if no exception raised print c
note: else: break
executed if no exception raised. if input different integer, exception raised, else:break
clause not executed, not break loop , ask insert again number again. if insert number, no exception raised, 'else:break'
executed , exit while loop, number still stored in variable c
Comments
Post a Comment