2013-05-02 66 views
2

這裏是我已經使用Python創建了這個井字遊戲..只接受整數爲井字

import os 
os.system('cls') 

i = 0 
#Exiter 
def exithoja(): 
    import sys 
    raw_input 
    sys.exit() 

#Displays Win or Draw 
def diswin(name,grid): 
    i = checkwin(grid) 
    os.system('cls') 

    viewgrid(grid) 

    if i ==1: 
     print name, " has won the game !!" 
    elif i == -1: 
     print "This Match is a draw !!" 
    exithoja() 
#Checking for Win or Draw Function 
def checkwin(grid): 
    i = 0 
    result = 0 
    extra=0 
    for i in range (1,9):   #This part checks for full grid. 
     if (grid[i] == 'X' or grid[i]=='O'): 
      extra += 1 
     if (grid[1] == grid[2] and grid[2] == grid[3]): 
      result = 1     #This part checks for win. 

     elif(grid[4] == grid[5] and grid[5] == grid[6]): 
      result = 1 
     elif(grid[7] == grid[8] and grid[8] == grid[9]): 
      result = 1 

     elif(grid[1] == grid[4] and grid[4] == grid[7]): 
      result = 1 

     elif(grid[2] == grid[5] and grid[5] == grid[8]): 
      result = 1 

     elif(grid[3] == grid[6] and grid[6] == grid[9]): 
      result = 1 

     elif(grid[1] == grid[5] and grid[5] == grid[9]): 
      result = 1 

     elif(grid[3] == grid[5] and grid[5] == grid[7]): 
      result = 1 

     elif(extra==9):  #This part checks for draw. 
      result = -1 

    return result   #0 for continue,1 for win, -1 for draw. 


#Grid Print Function 
def viewgrid(grid): 
    print "\n\n  .  .\n", 
    print "  |  | " 
    for i in range(1,10): 
     if i%3==0: 
      print " ", 
     else: 
      print "", 
     print "",grid[i]," ", 
     if i%3 == 0: 
      if i == 9: 
       print "\n  |  |", 
       print "\n  '  '" 
      else: 
       print "\n  |  | ", 
       print "\n------+-------+-------\n", 
       print "  |  | " 

     else: 
      print "|", 

#Grid Print Function Ends 

#Marks the user choice is 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 your choice from the 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 your choice from the 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  
    for j in range(0,10): 
     grid.append(j) 
#Reset Grid Code ends here 

#This is the main program, defined as a function itself 
def playgame(): 
    print "\n\nUser 1 - Please Enter your name : ", 
    name1 = raw_input() 
    print "User 2 - Please Enter your name : ", 
    name2 = raw_input() 
    print "\n",name1,", your marking is O", 
    print "\n",name2,", your marking is X" 
    user1 = 'O' 
    user2 = 'X' 
    raw_input() 
    grid = [] 
    gridreset(grid) 
    def player1(name1): 
     i = player11(name1,grid) 
     if markgrid('O',grid,i) == 0: 
      player1(name1) 
     player12(i,grid,name1) 
     i = checkwin(grid) 
     print i 
     if i==1 or i == -1: 
      diswin(name1,grid) 
     player2(name2) 
     return grid 
    def player2(name2): 
     i = player21(name2,grid) 
     if markgrid('X',grid,i) == 0: 
      player2(name2) 
     player22(i,grid,name2) 
     i = checkwin(grid) 
     if i==1 or i == -1: 
      diswin(name2,grid) 
     player1(name1) 
     return grid 
    player1(name1) 
    player2(name2) 
    return grid 
#Main Program end 

#Main Program Execution 
grid = [] 

grid = playgame() 

最主要的左邊是隻接受整數的變量「到位」的功能Player11的和player21 ..如果它不是一個整數的函數將被再次調用,併爲「地點」的新值將被分配.. 請幫助我如何去... 在此先感謝.. 編輯:用戶/播放器不會被賦予任何錯誤代碼...

+4

您是否可以將代碼示例僅限於相關函數? (即'player11'和'player21') – Hannele 2013-05-02 17:38:06

+0

所以你需要檢查字符串是否是整數?這是你的答案 http://stackoverflow.com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not – nacholibre 2013-05-02 17:44:02

+0

如何使用try和except語句? Howerver,我真的不明白你的問題... – 2013-05-02 17:45:39

回答

2

根據 求寬恕比要求許可更好,我會做一個簡單的嘗試:除了第

while True: 
    r = raw_input('choose ') 
    try: 
     c = int(r) 
    except ValueError: 
      print 'Not valid.\n' 
    else: 
     break 
     #This will be executed only if no exception is raised 

print c 

注: 只有在不發生異常的else: break將被執行。因此,如果輸入的內容與整數不同,則會引發異常,else:break子句將不會執行,它不會中斷循環,並會再次請求再次插入一個數字。如果您插入一個數字,則不會引發異常,將執行'else:break'並退出while循環,並將您的編號保存在變量中c

+0

我沒有得到你爲什麼使用「其他:打破」的情況下,即使它不會發生.. – 2013-05-03 14:39:13

+0

解釋在答案:-) – Ant 2013-05-03 15:41:55

+0

我試過你的代碼,修改它在下面的功能.. 但它不工作,如下面的答案解釋... Thanksin advance .. – 2013-05-03 16:04:11