Skip to main content
added 17 characters in body
Source Link

Again, just looking for advice on things I can improve. There is no classes because I have not really learned a great deal about OOP yet. I wanted to make sure I could use functions proficiently before I continued. I'm sure there is a way to loop the win_checkif statements in check_win(), but I have not figured it out yet.

Again, just looking for advice on things I can improve. There is no classes because I have not really learned a great deal about OOP yet. I wanted to make sure I could use functions proficiently before I continued. I'm sure there is a way to loop the win_check(), but I have not figured it out yet.

Again, just looking for advice on things I can improve. There is no classes because I have not really learned a great deal about OOP yet. I wanted to make sure I could use functions proficiently before I continued. I'm sure there is a way to loop the if statements in check_win(), but I have not figured it out yet.

Source Link

My first Python project Tic Tac Toe v2

Hey guys this is my second attempt at a Tic Tac Toe game using the advice given in my first post: My first Python project: Tic Tac Toe

Again, just looking for advice on things I can improve. There is no classes because I have not really learned a great deal about OOP yet. I wanted to make sure I could use functions proficiently before I continued. I'm sure there is a way to loop the win_check(), but I have not figured it out yet.

def num_to_coord(num):
# 0-index num
    num -= 1
    coord = []
    while True:
        curr_coord = num % 3
        coord.append(curr_coord)
        if len(coord) >= 2:
            break
        num -= curr_coord
        num //= 3
    return coord


def change_symbol(the_symbol):
    if the_symbol == 'x':
        the_symbol = 'o'
        return the_symbol
    if the_symbol == 'o':
        the_symbol = 'x'
        return the_symbol


def get_position():
    position = int(input("Enter a position: "))
    return position


def check_position(the_position, the_taken_positions):
    if the_position in the_taken_positions:
        return True
    if the_position not in the_taken_positions:
        return False


def draw_position(location, the_symbol, the_game_board):
    coord = num_to_coord(location)
    the_game_board[coord[0]][coord[1]] = the_symbol
    for y in range(3):
        print(add_sep('|', [the_game_board[x][y] for x in range(3)]))


def add_sep(sep, lst):
    """Adding | to board"""
    output = sep
    for i in lst:
        output += i + sep
    return output


def check_win(the_game_board):
    if the_game_board[0][0] == 'x' and the_game_board[1][0] == 'x' and the_game_board[2][0] == 'x' or \
        (the_game_board[0][0] == 'o' and the_game_board[1][0] == 'o' and the_game_board[2][0] == 'o'):
        return True
    if the_game_board[0][1] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][1] == 'x' or \
        (the_game_board[0][1] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][1] == 'o'):
        return True
    if the_game_board[0][2] == 'x' and the_game_board[1][2] == 'x' and the_game_board[2][2] == 'x' or \
        (the_game_board[0][2] == 'o' and the_game_board[1][2] == 'o' and the_game_board[2][2] == 'o'):
        return True
    if the_game_board[0][0] == 'x' and the_game_board[0][1] == 'x' and the_game_board[0][2] == 'x' or \
        (the_game_board[0][0] == 'o' and the_game_board[0][1] == 'o' and the_game_board[0][2] == 'o'):
        return True
    if the_game_board[1][0] == 'x' and the_game_board[1][1] == 'x' and the_game_board[1][2] == 'x' or \
        (the_game_board[1][0] == 'o' and the_game_board[1][1] == 'o' and the_game_board[1][2] == 'o'):
        return True
    if the_game_board[2][0] == 'x' and the_game_board[2][1] == 'x' and the_game_board[2][2] == 'x' or \
        (the_game_board[2][0] == 'o' and the_game_board[2][1] == 'o' and the_game_board[2][2] == 'o'):
        return True
    if the_game_board[0][0] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][2] == 'x' or \
        (the_game_board[0][0] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][2] == 'o'):
        return True
    if the_game_board[0][2] == 'x' and the_game_board[1][1] == 'x' and the_game_board[2][0] == 'x' or \
        (the_game_board[0][2] == 'o' and the_game_board[1][1] == 'o' and the_game_board[2][0] == 'o'):
        return True
    else:
        return False


def intro():
    intro_board = [['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
    print("Welcome to Tic Tac Toe")
    print("You can pick location by identifying the position on the board. (There are 9 positions)")
    print("The player who plays first will be using 'x' and the second player will be using 'o'.")
    for y in range(3):
        print(add_sep('|', [intro_board[x][y] for x in range(3)]))


def main():
    game_board = [[' '] * 3 for _ in range(3)]
    taken_positions = []
    symbol = 'x'
    num_moves = 0
    win = False
    intro()
    while num_moves < 9 and not win:
        position = get_position()
        taken = check_position(position, taken_positions)
        if taken:
            print("Position taken! Try again.")
        else:
            draw_position(position, symbol, game_board)
            taken_positions.append(position)
            symbol = change_symbol(symbol)
            num_moves = num_moves + 1

        win = check_win(game_board)
        if win:
            print("We have a winner!")
            break

    if num_moves == 9 and not win:
        print("WOW! You guys are good! DRAW!!!")


# MAIN
main()
print("Thanks for playing! Exiting")