os.system函数不会清除屏幕

时间:2018-04-30 15:44:27

标签: python tic-tac-toe os.system

我不知道为什么我的屏幕不清楚。相反,我得到一个指向第一个字母的箭头。我看了谷歌,一位消息人士说,在某些情况下,它可能会返回0.也许这就是为什么它指向我第一个字符串的第一个字母?我的代码还存在其他问题,但我花了很多时间试图弄清楚为什么这个功能无法正常工作,我无法弄清楚。

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()

0 个答案:

没有答案