Battleship Game Visualization
Implement and test a Python program using turtle graphics to provide a visualization for the game of Battleship discussed in Program Development problem D3
The Correct Answer and Explanation is :
Here’s a Python program using the turtle graphics library to visualize the Battleship game. This visualization will create a grid representing the game board and place ships on it. The program will also allow marking hits and misses.
Python Code for Battleship Visualization
import turtle
# Constants
GRID_SIZE = 10
CELL_SIZE = 40
SHIP_COLOR = "gray"
HIT_COLOR = "red"
MISS_COLOR = "blue"
GRID_COLOR = "black"
# Initialize Turtle
screen = turtle.Screen()
screen.setup(GRID_SIZE * CELL_SIZE + 100, GRID_SIZE * CELL_SIZE + 100)
screen.title("Battleship Game Visualization")
t = turtle.Turtle()
t.speed(0)
t.hideturtle()
def draw_grid():
"""Draws the Battleship game grid."""
t.penup()
for i in range(GRID_SIZE + 1):
# Vertical lines
t.goto(i * CELL_SIZE, 0)
t.pendown()
t.goto(i * CELL_SIZE, GRID_SIZE * CELL_SIZE)
t.penup()
# Horizontal lines
t.goto(0, i * CELL_SIZE)
t.pendown()
t.goto(GRID_SIZE * CELL_SIZE, i * CELL_SIZE)
t.penup()
def draw_ship(x, y, size, horizontal=True):
"""Draws a ship at a given coordinate."""
t.fillcolor(SHIP_COLOR)
t.penup()
t.goto(x * CELL_SIZE, y * CELL_SIZE)
t.pendown()
t.begin_fill()
if horizontal:
t.goto((x + size) * CELL_SIZE, y * CELL_SIZE)
t.goto((x + size) * CELL_SIZE, (y + 1) * CELL_SIZE)
t.goto(x * CELL_SIZE, (y + 1) * CELL_SIZE)
else:
t.goto(x * CELL_SIZE, (y + size) * CELL_SIZE)
t.goto((x + 1) * CELL_SIZE, (y + size) * CELL_SIZE)
t.goto((x + 1) * CELL_SIZE, y * CELL_SIZE)
t.goto(x * CELL_SIZE, y * CELL_SIZE)
t.end_fill()
def mark_hit(x, y):
"""Marks a hit at the given coordinate."""
t.fillcolor(HIT_COLOR)
t.penup()
t.goto(x * CELL_SIZE + CELL_SIZE // 4, y * CELL_SIZE + CELL_SIZE // 4)
t.pendown()
t.begin_fill()
t.circle(CELL_SIZE // 4)
t.end_fill()
def mark_miss(x, y):
"""Marks a miss at the given coordinate."""
t.fillcolor(MISS_COLOR)
t.penup()
t.goto(x * CELL_SIZE + CELL_SIZE // 2, y * CELL_SIZE + CELL_SIZE // 2)
t.pendown()
t.dot(CELL_SIZE // 3)
# Draw the game board
draw_grid()
# Example Ships (row, column, size, horizontal=True/False)
draw_ship(1, 2, 3, True)
draw_ship(5, 6, 4, False)
# Example Hits and Misses
mark_hit(1, 2)
mark_hit(2, 2)
mark_miss(3, 3)
mark_miss(6, 6)
# Keep the window open
turtle.done()
Explanation
This program visualizes a Battleship game board using turtle graphics in Python.
- Grid Creation:
- The
draw_grid()function creates a 10×10 grid using vertical and horizontal lines. - The board is structured using a coordinate system where each cell is 40×40 pixels.
- Ship Placement:
draw_ship(x, y, size, horizontal=True)function places ships at given coordinates.- Ships are filled with a gray color and can be drawn horizontally or vertically.
- Hit and Miss Indications:
mark_hit(x, y)places a red circle in the targeted cell to mark a hit.mark_miss(x, y)places a blue dot in the targeted cell to mark a miss.
- Example Scenario:
- A 3-cell horizontal ship is placed at (1,2).
- A 4-cell vertical ship is placed at (5,6).
- Hits are marked on (1,2) and (2,2).
- Misses are marked at (3,3) and (6,6).
This program provides an interactive way to visualize Battleship gameplay. By modifying ship placement and attack coordinates, you can simulate different game scenarios.