Here's a Python script that implements a simplified version of Blockudoku in the console. The grid is displayed using "░" for empty cells, "▓" for filled cells, and "|" and "-" to form the table. Users can input three figures (represented by integers) separated by spaces, and the script will handle placing the pieces on the grid according to the game's logic.
# Blockudoku Console Game
# Define the grid
grid = [['░' for _ in range(9)] for _ in range(9)]
# Define the pieces dictionary
pieces = {
1: [['▓']],
2: [['▓', '▓']],
3: [['▓'],
['▓']],
4: [['▓', '▓'],
['▓', '▓']],
5: [['░', '▓', '░'],
['▓', '▓', '▓'],
['░', '▓', '░']],
6: [['▓', '▓', '▓']],
7: [['▓'],
['▓'],
['▓']],
8: [['▓', '▓', '▓'],
['▓', '░', '░'],
['▓', '░', '░']],
9: [['▓', '▓', '▓'],
['░', '░', '▓'],
['░', '░', '▓']],
}
# Function to display the grid with '░', '▓', '|' and '-' to make the table
def display_grid(grid):
print(" " + " ".join([str(i+1) for i in range(9)]))
print(" +" + "---+"*9)
for i, row in enumerate(grid):
print(f"{i+1:2}|", end="")
for j, cell in enumerate(row):
print(f" {cell}", end="")
if (j + 1) % 3 == 0:
print(" |", end="")
print()
if (i + 1) % 3 == 0:
print(" +" + "---+"*9)
# Function to place a piece on the grid
def place_piece(grid, piece, x, y):
# Check if the piece can be placed at position (x, y)
for i in range(len(piece)):
for j in range(len(piece[0])):
if piece[i][j] == '▓':
if x + i >= 9 or y + j >= 9 or grid[x + i][y + j] != '░':
return False # Cannot place piece here
# Place the piece
for i in range(len(piece)):
for j in range(len(piece[0])):
if piece[i][j] == '▓':
grid[x + i][y + j] = '▓'
return True
# Function to check and clear full rows, columns, and 3x3 blocks
def clear_lines_and_blocks(grid):
rows_to_clear = set()
cols_to_clear = set()
blocks_to_clear = set()
# Check rows
for i in range(9):
if all(cell == '▓' for cell in grid[i]):
rows_to_clear.add(i)
# Check columns
for j in range(9):
if all(grid[i][j] == '▓' for i in range(9)):
cols_to_clear.add(j)
# Check 3x3 blocks
for block_x in range(0, 9, 3):
for block_y in range(0, 9, 3):
if all(grid[i][j] == '▓' for i in range(block_x, block_x+3) for j in range(block_y, block_y+3)):
blocks_to_clear.add((block_x, block_y))
# Clear rows
for i in rows_to_clear:
for j in range(9):
grid[i][j] = '░'
# Clear columns
for j in cols_to_clear:
for i in range(9):
grid[i][j] = '░'
# Clear blocks
for block in blocks_to_clear:
block_x, block_y = block
for i in range(block_x, block_x+3):
for j in range(block_y, block_y+3):
grid[i][j] = '░'
# Main game loop
def main():
while True:
display_grid(grid)
# Get user input: Enter 3 figure numbers separated by spaces
input_pieces = input("Enter 3 figure numbers separated by spaces (e.g., '1 2 3'): ")
try:
piece_nums = [int(num) for num in input_pieces.strip().split()]
if len(piece_nums) != 3:
print("Please enter exactly 3 figures.")
continue
if not all(num in pieces for num in piece_nums):
print("Invalid piece numbers entered.")
continue
selected_pieces = [pieces[num] for num in piece_nums]
except ValueError:
print("Invalid input. Please enter integers.")
continue
# For each piece, ask user where to place it
for idx, piece in enumerate(selected_pieces):
display_grid(grid)
print(f"Current Piece ({piece_nums[idx]}):")
for row in piece:
print(''.join(row))
while True:
position = input("Enter position to place the piece in the format 'row col' (e.g., '1 1'): ")
try:
x, y = [int(num)-1 for num in position.strip().split()]
if x < 0 or x >= 9 or y < 0 or y >=9:
print("Position out of bounds. Try again.")
continue
if place_piece(grid, piece, x, y):
break
else:
print("Cannot place piece at that position. Try again.")
except ValueError:
print("Invalid input. Please enter two integers.")
# After placing the pieces, clear any full lines or blocks
clear_lines_and_blocks(grid)
# Check for game over condition (no empty cells or no possible moves)
empty_cells = sum(row.count('░') for row in grid)
if empty_cells == 0:
print("Game Over! No more empty cells.")
break
# For this example, the game will continue infinitely
# You can implement additional logic to end the game appropriately
if __name__ == "__main__":
main()
Explanation:
- Grid Initialization: We create a 9x9 grid initialized with "░" to represent empty cells.
- Pieces Dictionary: Pieces are stored in a dictionary
pieces, where each key is an integer representing the piece number, and the value is a 2D list representing the piece's shape using "░" and "▓".
- Displaying the Grid: The
display_grid function prints the grid with row and column numbers, using "|" and "-" to form the table lines.
- Placing Pieces: The
place_piece function checks if a piece can be placed at a given position without overlapping existing pieces or going out of bounds. If placement is possible, it updates the grid.
- Clearing Lines and Blocks: The
clear_lines_and_blocks function checks for and clears any full rows, columns, or 3x3 blocks by setting their cells back to "░".
- User Input: In the
main function, the user is prompted to enter three piece numbers. For each piece, the user specifies where to place it on the grid.
- Game Loop: The game continues in a loop, updating the grid after each turn and checking for the game-over condition.
Notes:
- The game does not currently implement logic to check for no possible moves, so it will continue indefinitely unless the grid is completely filled.
- You can expand the
pieces dictionary with more pieces to add variety to the game.
- Error handling is included to guide the user with valid inputs.
Sample Output:
1 2 3 4 5 6 7 8 9
+---+---+---+---+---+---+---+---+---+
1| ░ ░ ░ | ░ ░ ░ | ░ ░ ░ |
2| ░ ░ ░ | ░ ░ ░ | ░ ░ ░ |
3| ░ ░ ░ | ░ ░ ░ | ░ ░ ░ |
+---+---+---+---+---+---+---+---+---+
4| ░ ░ ░ | ░ ░ ░ | ░ ░ ░ |
5| ░ ░ ░ | ░ ░ ░ | ░ ░ ░ |
6| ░ ░ ░ | ░ ░ ░ | ░ ░ ░ |
+---+---+---+---+---+---+---+---+---+
7| ░ ░ ░ | ░ ░ ░ | ░ ░ ░ |
8| ░ ░ ░ | ░ ░ ░ | ░ ░ ░ |
9| ░ ░ ░ | ░ ░ ░ | ░ ░ ░ |
+---+---+---+---+---+---+---+---+---+
Enter 3 figure numbers separated by spaces (e.g., '1 2 3'):
Important: Ensure you have Python 3 installed to run this script. Save the code in a file with a .py extension and run it using the command python filename.py in your console.