Guest User

Untitled

a guest
Jun 19th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. def checkio(game_result):
  2.     win_posX=[['XXX']
  3.               ['XXX']
  4.               ['XXX']]
  5.     win_posO=[['OOO']
  6.               ['OOO']
  7.               ['OOO']]
  8.     res, pr=[],[]
  9.     for i in win_posX:
  10.         if game_result in win_pos:
  11.             res.append('X')
  12.         else:
  13.             for k in range(len(game_result)):
  14.                     for z in game_result:
  15.                         if z[k][0] == 'X':
  16.                             pr.append('X')
  17.                             if pr == ['X','X','X']: res.append('X')
  18.                         if  z[k][k] == 'X':
  19.                             pr.append('X')
  20.                             if pr == ['X','X','X']: res.append('X')
  21.                         if z[k][2] and z[k][1] and z[k][0]: res.append('X')
  22.                    
  23.     for i in win_posO:
  24.         if game_result in win_pos:
  25.             res.append('O')
  26.         else:
  27.             for k in range(len(game_result)):
  28.                 for z in game_result:
  29.                     if z[k][0] == 'O':
  30.                         pr.append('O')
  31.                         if pr == ['O','O','O']: res.append('O')
  32.                     if  z[k][k] == 'O':
  33.                         pr.append('O')
  34.                         if pr == ['O','O','O']: res.append('X')
  35.                     if z[k][2] and z[k][1] and z[k][0]: res.append('O')
  36.                    
  37.     if res == ['X','O']:
  38.         return 'D'
  39.     elif res==['X']:
  40.         return 'X'
  41.     elif res==['O']:
  42.         return 'O'
  43.     else: return -1
  44.  
  45. if __name__ == '__main__':
  46.     #These "asserts" using only for self-checking and not necessary for auto-testing
  47.     assert checkio([
  48.         "X.O",
  49.         "XX.",
  50.         "XOO"]) == "X", "Xs wins"
  51.     assert checkio([
  52.         "OO.",
  53.         "XOX",
  54.         "XOX"]) == "O", "Os wins"
  55.     assert checkio([
  56.         "OOX",
  57.         "XXO",
  58.         "OXX"]) == "D", "Draw"
  59.     assert checkio([
  60.         "O.X",
  61.         "XX.",
  62.         "XOO"]) == "X", "Xs wins again"
Advertisement
Add Comment
Please, Sign In to add comment