Problem 2. tttCheckMove()
Write a function named tttCheckMove() that checks whether or not a TicTacMove is on an empty space
on a TTT board.
Input (two parameters):
· Parameter 1: A string of 9 characters denoting the state of a TTT board. Each character in the
string indicates the state of one TicTacToe square – an ‘X’ or ‘O’ if the square is occupied, a ‘-‘
if it is not. The squares are numbered left to right and from the bottom up:

|6|7|8|
|3|4|5|
|0|1|2| ---- simulates tic tac toe board

For example, this TicTacToe board
| | | |
|X| | | ---- X is in space '3'
| | | |
is represented by the string '---X-----'.
· Parameter 2: An int from 0 to 8 inclusive, representing a TicTacToe move.
Output:
Return True if the move is in an unoccupied space, otherwise return False.
Here is sample output:

from ticTacToe import *
board = '----X----'
move = 1
tttCheckMove(board, move)
False
row = 0
column = 1
tttCheckMove(board, row, column)
True

Dani AI

Generated

— don't worry, this is a small, well-scoped task. As pointed out, post any attempt if you need debugging; below is a minimal, easy-to-follow plan and a compact Python implementation you can drop into your program.

Plan: 1) Validate the board is a 9-character string. 2) Accept either a single integer move (0..8) or two integers (row, column). 3) Convert row/column to the string index with row 0 as the bottom row: index = column + row*3. 4) Return True only when that character is '-' (empty); otherwise False.

def tttCheckMove(board, a, b=None):
    """
    Return True if the chosen square is empty ('-').
    Usage:
      tttCheckMove(board, move)           # move in 0..8
      tttCheckMove(board, row, column)    # row, column in 0..2; row 0 is bottom
    """
    if not isinstance(board, str) or len(board) != 9:
        return False
    if b is None:
        move = a
        if not isinstance(move, int) or not (0 <= move <= 8):
            return False
        idx = move
    else:
        row, col = a, b
        if not (isinstance(row, int) and isinstance(col, int)):
            return False
        if not (0 <= row <= 2 and 0 <= col <= 2):
            return False
        idx = col + row * 3
    return board[idx] == '-'

Notes/troubleshooting: many students mix up whether row 0 is the top or bottom — confirm which your tests expect. Also check for off-by-one errors when converting row/col to an index and make sure the board string really uses - for empty squares. If a test still fails, post the exact board string, the inputs you used, and the output you expected (and your code) so the community can point to the specific issue.

Recommended Answers

All 2 Replies

You forgot your code and description of your problem.

I have no idea where to start

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.