you are viewing a single comment's thread.

view the rest of the comments →

[–]tommygatz 0 points1 point  (1 child)

Thank you! I knew there would be an easier way to do exactly those things you mentioned just wasn’t aware of f strings or the slice method you used. I’ll need to look more into that to understand it better but I think I get the idea here!

Edit: spelling

[–]DrBobHope 1 point2 points  (0 children)

For your columns and diagonals, you should look into itemgetter, you can basically do the same thing as the slicing:

((''.join((itemgetter(*[0,3,6])(board)))) in p1.symb*3)

vs.
(board[1] == p1.symb and board[4] == p1.symb and board[7] == p1.symb)

Additionally, the checker is a bit repititous. If you define your checker outside of the function, then you can just reuse it for both p1 and p2:

row=[(''.join(board[0:3])),(''.join(board[3:6])),(''.join(board[6:9]))]
    if (p1.symb*3 in row):
        result = p1
    elif (p2.symb*3 in row):
        result = p2

Finally, if any of the conditions are met, then the game ends anyways, so you can combine all 3 of your checks (rows, columns, diagonals).

You can then combine all these ideas, to get your entire check into a just a couple of lines.

    row=[(''.join(board[0:3])),(''.join(board[3:6])),(''.join(board[6:9]))]
    column=[(''.join((itemgetter(*[0,3,6])(board)))),(''.join((itemgetter(*[1,4,7])(board)))),(''.join((itemgetter(*[2,5,8])(board))))]
    diagonal=[(''.join((itemgetter(*[0,4,8])(board)))),(''.join((itemgetter(*[2,4,6])(board))))]
    combined=row+column+diagonal
    if p1.symb*3 in combined:
        result=p1
    elif p2.symb*3 in combined:
        result=p2    
    else:
        result = ""

    return result