1

I don't know why my screen won't clear. Instead, I get an arrow pointing to the first letter. I looked on Google and one source said that it certain circumstances it may return a 0. Perhaps that is why it is pointing to the first letter of my first string? There are other problems with my code as well but I've spent a lot of time trying to figure out why this function won't work and I can't figure it out.

import os
import random

'''
In this tic-tac-toe game, the user will be prompted to choose a mark.
The player will play against a computer.

CURRENT PROBLEMS WITH GAME:
board_tiles and tiles_available do not reset after new game
clear_screen function does not work
Error when tie occurs
'''

board_tiles = [0, 1, 2, 3, 4, 5, 6, 7, 8]
tiles_available = [0, 1, 2, 3, 4, 5, 6, 7, 8]


# create a function that clears the screen
def clear_screen():
    os.system('cls')


# create a function that draws the tic-tac-toe board
def draw_board():
    # clear screen
    clear_screen()

    print(f'{board_tiles[0]} | {board_tiles[1]} | {board_tiles[2]}')
    print('-' * 10)
    print(f'{board_tiles[3]} | {board_tiles[4]} | {board_tiles[5]}')
    print('-' * 10)
    print(f'{board_tiles[6]} | {board_tiles[7]} | {board_tiles[8]}')


# create a function that allows the user to choose a piece
def choose_mark():
    player = input("Would you like to be 'X' or 'O'? ").upper()
    if player == 'X':
        computer = 'O'
    else:
        computer = 'X'
    print(f'Player is {player}.  Computer is {computer}.')

    return player, computer


# create a function that allows the computer to make a random move on the board
def computer_move(computer):
    computers_move = random.choice(tiles_available)
    tiles_available.remove(computers_move)
    board_tiles.insert(computers_move, computer)
    board_tiles.remove(computers_move)

    return computers_move


# create a function that checks if the chosen spot is available and draws it to the board
def draw_mark(player, computer):
    while tiles_available:
        players_move = int(input('Where would you like to move? '))
        if players_move in tiles_available:
            tiles_available.remove(players_move)
            board_tiles.insert(players_move, player)
            board_tiles.remove(players_move)
            computer_move(computer)
            draw_board()
            score()
        else:
            print('{players_move} has already been taken')

    return player


# define what is a win/lose/tie
def score():
    if board_tiles[0] == board_tiles[1] == board_tiles[2] or \
        board_tiles[3] == board_tiles[4] == board_tiles[5] or \
        board_tiles[6] == board_tiles[7] == board_tiles[8] or \
        board_tiles[0] == board_tiles[3] == board_tiles[6] or \
        board_tiles[1] == board_tiles[4] == board_tiles[7] or \
        board_tiles[2] == board_tiles[5] == board_tiles[8] or \
        board_tiles[0] == board_tiles[4] == board_tiles[8] or \
        board_tiles[2] == board_tiles[4] == board_tiles[6]:
        print('YOU WIN!!!')
        play_again()

    if len(tiles_available) == 0:
        print('It is a tie!')
        play_again()


def play_again():
    replay = input("Would you like to play again? \n"
                   "Type 'y' for yes and 'n' for no: ").lower()
    if replay == 'y':
        game()
    else:
        print('Come back!')
        quit()


def game():
    playing = True
    player, computer = choose_mark()
    while playing:
        draw_board()
        draw_mark(player, computer)

game()
  • are you using windows? – Jean-François Fabre Apr 30 '18 at 15:47
  • 1
    Is there any particular reason why you don't want this program to work on Linux, where `cls` generally doesn't exist? – PM 2Ring Apr 30 '18 at 15:47
  • 1
    you may want to check https://stackoverflow.com/questions/2084508/clear-terminal-in-python – Jean-François Fabre Apr 30 '18 at 15:48
  • also, if you're using redirection of some kind, that could explain it. Do you get `♀` ? – Jean-François Fabre Apr 30 '18 at 15:48
  • 1
    Where are you running the program? Shell commands such as `cls` in windows will run in a specific terminal shell, but if you run your python script from an IDE for example, you cannot rely on terminal specific programs. You might want to use a library such as [asciimatics](https://github.com/peterbrittain/asciimatics) to create a text based ui (bonus: it's cross-platform). – Håken Lid Apr 30 '18 at 16:03
  • Ah! I'm writing this in PyCharm. I only made it compatible with Windows just because this game is only for me to practice Python. –  Apr 30 '18 at 16:30

0 Answers0