Search code examples
pythonfunctionloopstic-tac-toe

Tic Tac Toe Referee


I'm trying to solve a new task - Tic Tac Toe Referee. I have to make a judgement of the game result, but not to make an algorythm of the game. The program should return only one winner (O or X) or draw (like a string, by the way).

def checkio(game_result):
    for y in game_result:
        if y == "XXX":
            return "X"
        elif y == "OOO":
            return "O"
    for y in range(3):
        if game_result[y][0] == game_result[y][1] == game_result[y][2] != None:
            return game_result[y][0]
        if game_result[0][y] == game_result[1][y] == game_result[2][y] != None:
            return game_result[0][y]
    if game_result[0][0] == game_result[1][1] == game_result[2][2] != None:
        return game_result[1][1]
    if game_result[2][0] == game_result[1][1] == game_result[0][2] != None:
        return game_result[1][1]
    return "D"

I really don't understand where had i done a mistake and the system answers that in case of:

  • .OX
  • .XX
  • .OO

("." means an empty cell)

My result is "." but the right result is "D".

How to fix it? Thanks for attention.


Solution

  • The line:

    if game_result[y][0] == game_result[y][1] == game_result[y][2] != None:
            return game_result[y][0]
    

    is causing you trouble - all of the cells in column 0 are the same, you you are returning the value of column[0][0] i.e '.', when you should be returning 'draw'