Commits

Anonymous committed 738b259

combine some cases to clean up a bit

  • Participants
  • Parent commits b8e738a

Comments (0)

Files changed (1)

File tic-tac-toe/server/tttserv.py

         new_board = json.loads(self.response.body)
         self.failIf(new_board == body)
 
-#given a row # return all of the index into elements in the row
-def indexes_for_row(i):
-    return ((i,0),(i,1),(i,2))
-
-#given a col # return all of the index into elements in the col
-def indexes_for_col(i):
-    return ((0,i),(1,i),(2,i))
-
+def rows():
+    rows = []
+    for i in range(3):
+        rows.append([(i,j) for j in range(3)])
+    return rows
+def columns():
+    cols = []
+    for i in range(3):
+        cols.append([(j,i) for j in range(3)])
+    return cols
 def diagnols():
-    return (((0,0),(1,1),(2,2)),((0,2),(1,1),(2,0)))
-
-def other_player(player):
-    if player == -1:
-        return 1
-    elif player == 1:
-        return -1
-    else:
-        raise Exception("Unknown Player")
+    return [[(0,0),(1,1),(2,2)],[(0,2),(1,1),(2,0)]]
 
 def winning_move(board, player):
-    #two in a row
-    for i in range(len(board)):
-        row = [board[r][c] for r,c in indexes_for_row(i)]
-        for r,c in indexes_for_row(i):
-            if board[r][c] == 0 and row.count(player) == len(board)-1:
-                return (r,c)
-    #two in a column
-    for i in range(len(board[0])):
-        col = [board[r][c] for r,c in indexes_for_col(i)]
-        for r,c in indexes_for_col(i):
-            if board[r][c] == 0 and col.count(player) == len(board[0])-1:
-                return (r,c)
-    #two in a diagnol
-    for d in diagnols():
-        diag = [board[r][c] for r,c in d]
-        for r,c in d:
-            if board[r][c] == 0 and diag.count(player) == len(diag)-1:
+    wins = rows() + columns() + diagnols()
+    for i in wins:
+        squares = [board[r][c] for r,c in i]
+        for r,c in i:
+            if board[r][c] == 0 and squares.count(player) == 2:
                 return (r,c)
     return None