As44 Modified
As44 Modified
NO CONTENT PAGE NO
ABSTRACT
1
2
OBJECTIVE
3
INTRODUCTION
4
EXISTING AND PROPOSED SYSTEM
6
DECOMPOSITION
7
SOURCE CODE
8
OUTPUT
9
CONCLUSION
10
APPENDIX
11
BIBLIOGRAPHY
ABSTRACT
The Battleship game project is an engaging and interactive application developed usi
Python's Tkinter library. This digital version of the classic board game involves placing
ships on a 10x10 grid and challenging players to guess their locations within a set
number of attempts. By simulating a naval battle scenario, the game enhances playe
logical thinking and problem-solving skills through an enjoyable and user-friendly
interface. The project not only demonstrates the application of basic programming
concepts but also introduces advanced topics such as graphical user interface (GUI)
development, event handling, and game logic implementation in Python.
INTRODUCTION:
Battleship is a classic naval combat game where players aim to sink their opponent's
fleet of ships by guessing their locations on a grid. This project implements a single-
player version of the game where the player competes against the computer. The
computer randomly places ships on the grid, and the player has a limited number of
attempts to guess their locations.
KEY FEATURES
Interactive Gameplay:
Players can interact with the game by clicking on the grid to guess ship positions. The
interactive nature of the game keeps players engaged and focused, providing an
immersive experience.
Real-time Feedback:
The game provides instant feedback on hits and misses, updating the score and chan
left. This feature helps players adjust their strategies dynamically, enhancing the
learning experience.
Random Ship Placement:
Each game starts with ships placed at random positions, ensuring a unique experienc
every time. This randomness adds to the challenge and replay value of the game.
User-friendly Interface:
The graphical interface is designed to be intuitive and easy to navigate, making the
game accessible to players of all ages and skill levels.
TECHNICAL DETAILS
Language:
Python 3.x, a versatile and widely-used programming language known for its simplici
and readability.
Library:
Tkinter for GUI development, a standard library in Python that allows for easy
creation of graphical interfaces.
Game Grid:
10x10 grid represented by buttons, providing a structured and organized layout for th
game.
Scoring:
Points are awarded based on the number of ships hit and remaining chances,
encouraging strategic thinking and careful planning.
Chances:
Players start with 10 chances and can earn more by hitting ships, adding an element
risk and reward to the gameplay.
EXISTING AND PROPOSED SYSTEM
EXISTING SYSTEM
The traditional Battleship game has been around for many years and is usually played
with physical boards and pieces. Here are the main features of the existing system:
HARDWARE REQUIREMENTS
A computer with at least 2GB of RAM and a 1GHz processor for smooth gameplay.
A monitor with a resolution of at least 1024x768 to clearly display the game.
SOFTWARE REQUIREMENTS
Operating System: Windows, macOS, or Linux. The game should run on any of these
platforms.
Python: Python 3.x installed. Using the latest version ensures compatibility and acces
to new features.
Tkinter: Tkinter library for making the GUI (included with Python). Tkinter makes it
easy to create and manage the graphical interface.
DECOMPOSITION
The Battleship game project consists of several key components, each responsible for
different aspects of the game. Breaking down the project into these components help
in understanding the structure and functionality of the game.
MAIN COMPONENTS
Grid Setup
Function: create_board()
Purpose: To initialize the game board and set up the grid for gameplay.
Details:
Creates a 10x10 grid of buttons using nested loops.
Initializes game variables like attempts, score, and found ships.
Configures the appearance of each button and assigns a command to
handle clicks.
Sets up labels to display the number of chances and score, ensuring player
have the information they need during the game.
Ship Placement
Function: place_ships()
Purpose: To randomly place ships on the grid at the beginning of each game.
Details:
Clears any previous ship positions to start fresh each game.
Randomly selects unique positions on the grid until the required number o
ships (usually 5) are placed.
Ensures that ship positions do not overlap or duplicate, maintaining the
randomness and fairness of the game.
Event Handling
Function: click(row, col)
Purpose: To manage user interactions and update the game state based on player
actions.
Details:
Checks if the clicked button has already been clicked to prevent duplicate
actions.
Updates the button color based on whether a ship is hit (red) or missed
(blue).
Adjusts the number of remaining attempts and the score based on hits and
misses.
Tracks found ships and remaining ships, providing immediate feedback to
the player.
Calls other functions as needed to handle end-of-game scenarios (win or
loss).
Game Logic
Integrated with: click(row, col) function
Purpose: To determine the outcome of player actions and manage the overall game
state.
Details:
Calculates the score by combining the number of found ships and
remaining attempts.
Checks if all ships have been found or if the player has run out of attempts
Provides visual feedback and prompts to the player based on the game
state (e.g., game over, win).
Manages the flow of the game, ensuring it progresses smoothly from start
to finish.
Game Control
Functions: start_game(), reset_game(), exit_game()
Purpose: To control the overall game flow, including starting, resetting, and exiting th
game.
Details:
start_game(): Initializes the game by setting up the board and placing ships. It
hides the start and exit buttons to transition into the game mode.
reset_game(): Resets the game board to its initial state, allowing for a new
round of gameplay. It resets variables and updates the grid and labels.
exit_game(): Closes the game window and terminates the application, ensurin
a clean exit from the game.
ADDITIONAL COMPONENTS
Visual Feedback
Purpose: To provide players with clear visual cues based on their actions and the gam
state.
Details:
Buttons change color to indicate hits (red), misses (blue), and remaining
ships (yellow when revealed).
Labels are updated in real-time to show the number of remaining chances
and the current score, keeping players informed.
User Interaction
Purpose: To facilitate smooth and intuitive interaction between the player and the
game.
Details:
Handles clicks on the grid buttons, providing immediate feedback and updating the
game state.
Ensures that the interface is user-friendly and accessible, making the game
enjoyable for players of all ages.
Randomization
Purpose: To ensure that each game is unique and challenging.
Details:
Uses the random module to generate random positions for ships, ensuring
no two games are the same.
Adds an element of unpredictability, making the game more interesting
and replayable.
End-of-Game Scenarios
Purpose: To handle the game's conclusion based on the player's performance.
Details:
Provides prompts and options for the player when the game ends, whethe
they win or lose.
Calls the reveal_ships() function to show the remaining ships if the player
runs out of attempts, offering closure and understanding of the game
outcome.
SOURCE CODE:
import tkinter as tk
import random
from tkinter import messagebox
def create_board():
global buttons, attempts, chances_label, score_label, score, found_ships, ships,
clicked_buttons
attempts = 10
found_ships = 0
score = 0 # Initialize score here
ships = set() # Initialize ships here
clicked_buttons = set() # Track clicked buttons
buttons = []
for row in range(10):
row_buttons = []
for col in range(10):
button = tk.Button(root, width=6, height=3, bg='lightblue', relief='raised',
bd=5,
command=lambda r=row, c=col: click(r, c))
button.grid(row=row+1, column=col+1, padx=3, pady=3)
row_buttons.append(button)
buttons.append(row_buttons)
def reveal_ships():
for row, col in ships:
buttons[row][col].configure(bg='yellow')
def start_game():
start_button.grid_forget()
exit_button.grid_forget()
create_board()
place_ships()
def reset_game():
global attempts, score, found_ships, ships, clicked_buttons
for row in buttons:
for button in row:
button.configure(bg='lightblue', state='normal')
place_ships()
attempts = 10
found_ships = 0
clicked_buttons.clear()
chances_label.config(text=f"Chances: {attempts}")
score_label.config(text=f"Score: {score}")
def exit_game():
root.destroy()
root = tk.Tk()
root.title("Battleship")
button_style = {
"width": 20,
"height": 2,
"bg": "lightblue",
"relief": "raised",
"bd": 10,
"font": ('Helvetica', 12, 'bold')
}
start_button = tk.Button(root, text="Start", command=start_game, **button_style)
start_button.grid(row=0, columnspan=12, pady=20)
root.mainloop()
OUTPUT:
CONCLUSION
The Battleship game project demonstrates how Python and Tkinter can be used to
create an interactive and educational game. By developing this game, several key
programming concepts have been explored, including:
ALGORITHM
Initialize the Game:
Set up the main window and display start and exit buttons.
Start the Game:
Hide the start and exit buttons.
Set up the game board and place ships randomly.
Create Board:
Initialize variables and create a 10x10 grid of buttons.
Display labels for chances and score.
Place Ships:
Randomly place ships on the grid, ensuring no duplicates.
Handle Button Clicks:
Check if the button was already clicked.
Update button color and game state based on hits or misses.
Update chances and score, and check for game end conditions.
End of Game:
Display win or game over messages.
Offer to play again or exit.
Reset Game:
Reset variables and board for a new game.
Exit Game:
Close the application.
BIBLIOGRAPHY
www.wikipedia
www.google.com
NCERT computer science book