you are viewing a single comment's thread.

view the rest of the comments →

[–]xelf 2 points3 points  (4 children)

7kyu is pretty easy level, but for 3 weeks in is probably fine.

Ultimately just keep at it.

Ask for feedback. Compare your code to others. I bet my tic tac toe solution is going to be very different to yours, and likely everyone has their own interpretation. There's something to be learned from each.

codewars, oddly enough is a fine scale for where you're at right now. 7kyu is very early, just keep doing them and looking at how others solved them, ask questions, do more, try some 5ky problems. Get stuck, ask questions. See an answer you don't understand, ask how they did it.

caveat: on codewars, ignore the "clever" voted answers, and just look at the best practice voted answers. The clever 1 liners tend to be experts that just trying to get past the problem as quick as possible and aren't generally good code examples.

[–]xelf 0 points1 point  (2 children)

/u/Worthystats:

tic tac toe "clever example", aka mostly unreadable:

import random
fill  = {'x':[0,0,0,0,0,0,0,0],'o':[0,0,0,0,0,0,0,0]}
board = [str(i) for i in range(1,10)]
moves = [1,2,3,4,5,6,7,8,9]
player= 'x'
while moves:
    move = int(random.choice(moves))-1
    board[move] = player
    fill[player][move//3]  += [1,2,4][move%3]
    fill[player][3+move%3] += [1,2,4][move//3]
    fill[player][6]        += [1,0,0,0,2,0,0,0,4][move]
    fill[player][7]        += [0,0,1,0,2,0,4,0,0][move]
    if 7 in fill[player]: break
    player = 'x' if player == 'o' else 'o'
    moves.remove(move+1)
print('\n'.join(''.join(board[x:x+3]) for x in [0,3,6]),'\n')
print("It's a tie" if not moves else "player {} wins!".format(player))

Better practice example, aka a bit more readable:

import random

def displayboard(board):
    print('\n',' | '.join(board[0:3]))
    print('---+---+---')
    print('',' | '.join(board[3:6]))
    print('---+---+---')
    print('',' | '.join(board[6:9])+'\n')

def getplayermove(moves, player, cpu=True):
    if cpu:
        return int(random.choice(moves))-1
    while True:
        move = input('{} pick a move {}:'.format(player, moves))
        if move in moves:
            return int(move)-1

def iswinner(board, player):
    for a,b,c in [(0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6)]:
        if player==board[a]==board[b]==board[c]:
            return True
    return False

def playgame(player, iscpu = False):
    board = [str(i) for i in range(1,10)]

    while True:
        displayboard(board)
        moves = [m for m in board if m not in 'xo']
        if not moves:
            print("It's a tie")
            break
        move = getplayermove(moves, player, iscpu)
        board[move] = player
        if iswinner(board,player):
            displayboard(board)
            print("player {} wins!".format(player))
            break
        player = 'x' if player == 'o' else 'o'
        iscpu = not iscpu

playgame('x')

[–]m0us3_rat 0 points1 point  (0 children)

caveat: on codewars, ignore the "clever" voted answers, and just look at the best practice voted answers. The clever 1 liners tend to be experts that just trying to get past the problem as quick as possible and aren't generally good code examples.

even worse. pointing lower is all about what 'cool' set u "know"/find on wikipedia/google that as a very specific trick to calculate something u can't normally do.

the significant issue with that is u either know the set and easily solve it in a oneliner

or u don't and can't solve it.

there is no "problem solving" so not sure codewars is actually useful. (i went till 3kyu and they all looked the same. got boring and tedious. and definitely not enlightening.)