0

I am creating a game of Tic-Tac-Toe in python and am struggling to create a module that detects if someone has won. I am passing in 2 things into the module, the Board and a set of winning combinations:
win_comb=((0,1,2),(3,4,5),(6,7,8),(6,3,0),(7,4,1),(8,5,2),(6,4,2),(8,4,0))

The module in which I am using to check if someone has won. The module is called after 4 moves are made. If someone has won then it shall return a 1, yet if someone hasn't then it shall return a -1.

def Check_Results(Board,win_comb):
    for each in win_comb:
        try:
            if (Board[each[0]] == Board[each[1]] and Board[each[1]]== Board[each[2]] and Board[each[0]]==Board[each[2]]):
                return 1
            else:
                each=each+1
        except:
            pass
        return -1
tombam95
  • 343
  • 2
  • 14
Faisal148991
  • 47
  • 2
  • 12
  • Not related to the question, but have a look at [PEP8's suggestions for naming conventions](https://www.python.org/dev/peps/pep-0008/#naming-conventions). It doesn't change how the code works at all but it makes it much easier for anyone else reading it. `Board` -> `board`, `Check_Results` -> `check_results` etc – Holloway Nov 30 '15 at 11:37
  • You only need to check each row (or column) and one diagonal. Afterwards, [transpose](http://stackoverflow.com/questions/21444338/transpose-nested-list-in-python) your board and repeat. – Frerich Raabe Jan 28 '16 at 08:25

2 Answers2

-1

In your check if, you only need two checks.
Since the third equality will be implied (if a==b and b==c then a==c is implied)

Then you don't need to do each=each+1 since the for will already loop for each winning combinations. Finally your try/except only prevent you from seeing that you cannot do each+1 since each is a tuple and cannot be incremented.

def check_Results(board, win_comb):
    for each in win_comb:
        if (board[each[0]] == board[each[1]] and board[each[1]]== board[each[2]]):
            return 1
    return -1

Edit: note on naming convention, reserve CamelCase for Classed.

Also a one line solution:

return any(
    (board[each[0]] == board[each[1]] and board[each[1]]== board[each[2]])
    for each in win_comb)
Cyrbil
  • 6,341
  • 1
  • 24
  • 40
-1

You can use python's set functionality to determine the winner pretty easily.

#Assume pre-defined BOARD_LEN (for instance 3)
Board = [[None for y in range(BOARD_LEN)] for x in range(BOARD_LEN)]

TOKEN_1 = "0"
TOKEN_2 = "X"

"""
Get a sequence of tokens and see which one would win. Examples:
[None,0,x] gives None as winner
[0,0,0] gives 0 as winner
[0,X,0] gives None as winner
"""
def get_winner_of_sequence(seq, TOKEN1, TOKEN2):
  token_set = set(seq)
  if len(token_set) > 1 or None in token_set:
    return None
  return TOKEN1 if TOKEN1 in seq else TOKEN2

"""
Assume a N x N board.
Winning sequences are:
- all rows
- all columns
- the diagonal (0,0 -> N-1,N-1)
"""
def get_possible_winning_sequences(board):
  winning_sequences = []
  #Add each row
  #Add each column
  #Add the diagonal
  return winning_sequences
Arnab Datta
  • 5,356
  • 10
  • 41
  • 67