#!/usr/bin/env python3
"""Tic-tac-toe for two human players in the terminal."""

def print_board(board):
    """Print the current board with cell numbers 1-9."""
    print("\nCurrent board:")
    for i in range(3):
        row = board[i*3:(i+1)*3]
        print(" " + " | ".join(cell if cell != " " else str(i*3 + j + 1) for j, cell in enumerate(row)))
        if i < 2:
            print("---+---+---")
    print()

def check_win(board, player):
    """Return True if the given player has three in a row."""
    win_patterns = [
        [0,1,2], [3,4,5], [6,7,8],  # rows
        [0,3,6], [1,4,7], [2,5,8],  # columns
        [0,4,8], [2,4,6]            # diagonals
    ]
    for pattern in win_patterns:
        if all(board[i] == player for i in pattern):
            return True
    return False

def check_draw(board):
    """Return True if the board is full with no winner."""
    return " " not in board

def main():
    board = [" "] * 9
    current_player = "X"
    print("Welcome to Tic-Tac-Toe!")
    print("Cells are numbered 1 (top-left) to 9 (bottom-right).")
    print_board(board)

    while True:
        print(f"Player {current_player}'s turn.")
        move = input("Enter a number (1-9): ").strip()

        # Validate input
        if not move.isdigit():
            print("Invalid input: please enter a number between 1 and 9.")
            continue
        cell = int(move)
        if cell < 1 or cell > 9:
            print("Invalid input: number must be between 1 and 9.")
            continue
        index = cell - 1
        if board[index] != " ":
            print("That cell is already taken. Choose another.")
            continue

        # Make the move
        board[index] = current_player
        print_board(board)

        # Check for win or draw
        if check_win(board, current_player):
            print(f"Player {current_player} wins! Congratulations!")
            break
        if check_draw(board):
            print("It's a draw! The board is full.")
            break

        # Switch player
        current_player = "O" if current_player == "X" else "X"

if __name__ == "__main__":
    main()
