As 44
As 44
NO CONTENT PAGE NO
ABSTRACT
1
OBJECTIVE
2
INTRODUCTION
3
DECOMPOSITION
6
SOURCE CODE
7
OUTPUT
8
CONCLUSION
9
APPENDIX
10
BIBLIOGRAPHY
11
ABSTRACT
INTRODUCTION:
Battleship is a classic naval combat game where players aim to sink their oppon
fleet of ships by guessing their locations on a grid. This project implements a si
player version of the game where the player competes against the computer. T
computer randomly places ships on the grid, and the player has a limited numb
attempts to guess their locations.
KEY FEATURES
Interactive Gameplay:
Players can interact with the game by clicking on the grid to guess ship position
interactive nature of the game keeps players engaged and focused, providing a
immersive experience.
Real-time Feedback:
The game provides instant feedback on hits and misses, updating the score and
left. This feature helps players adjust their strategies dynamically, enhancing th
learning experience.
Random Ship Placement:
Each game starts with ships placed at random positions, ensuring a unique exp
every time. This randomness adds to the challenge and replay value of the gam
User-friendly Interface:
The graphical interface is designed to be intuitive and easy to navigate, making
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 sim
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 layou
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 ele
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
with physical boards and pieces. Here are the main features of the existing syst
HARDWARE REQUIREMENTS
A computer with at least 2GB of RAM and a 1GHz processor for smooth gamepl
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 o
platforms.
Python: Python 3.x installed. Using the latest version ensures compatibility and
to new features.
Tkinter: Tkinter library for making the GUI (included with Python). Tkinter mak
easy to create and manage the graphical interface.
DECOMPOSITION
The Battleship game project consists of several key components, each responsi
different aspects of the game. Breaking down the project into these component
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 p
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 num
ships (usually 5) are placed.
Ensures that ship positions do not overlap or duplicate, maintaining t
randomness and fairness of the game.
Event Handling
Function: click(row, col)
Purpose: To manage user interactions and update the game state based on pl
actions.
Details:
Checks if the clicked button has already been clicked to prevent dupl
actions.
Updates the button color based on whether a ship is hit (red) or misse
(blue).
Adjusts the number of remaining attempts and the score based on hi
misses.
Tracks found ships and remaining ships, providing immediate feedba
the player.
Calls other functions as needed to handle end-of-game scenarios (win
loss).
Game Logic
Integrated with: click(row, col) function
Purpose: To determine the outcome of player actions and manage the overall
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 atte
Provides visual feedback and prompts to the player based on the gam
state (e.g., game over, win).
Manages the flow of the game, ensuring it progresses smoothly from
to finish.
Game Control
Functions: start_game(), reset_game(), exit_game()
Purpose: To control the overall game flow, including starting, resetting, and ex
game.
Details:
start_game(): Initializes the game by setting up the board and placing sh
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 ne
round of gameplay. It resets variables and updates the grid and labels.
exit_game(): Closes the game window and terminates the application, en
a clean exit from the game.
ADDITIONAL COMPONENTS
Visual Feedback
Purpose: To provide players with clear visual cues based on their actions and
state.
Details:
Buttons change color to indicate hits (red), misses (blue), and remain
ships (yellow when revealed).
Labels are updated in real-time to show the number of remaining cha
and the current score, keeping players informed.
User Interaction
Purpose: To facilitate smooth and intuitive interaction between the player and
game.
Details:
Handles clicks on the grid buttons, providing immediate feedback and updating
game state.
Ensures that the interface is user-friendly and accessible, making the
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, ens
no two games are the same.
Adds an element of unpredictability, making the game more interesti
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, wh
they win or lose.
Calls the reveal_ships() function to show the remaining ships if the pl
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, ship
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='raise
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_st
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
create an interactive and educational game. By developing this game, several k
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