DEV Community

Dillon Honore
Dillon Honore

Posted on

Police Department Management Simulator

Looking for someone that can guide me through making a police department management simulator (hiring/firing, budgeting, creating divisions of the Department and whatnot) in Python. Do note that I have zero coding knowledge beyond 1+1==2.

Top comments (1)

Collapse
 
djhonore profile image
Dillon Honore

Asked an AI how and this is what I have so far:

import tkinter as tk
from tkinter import ttk, messagebox
from datetime import datetime, timedelta
import random

class DepartmentManager:
    def __init__(self, root):
        self.root = root
        self.root.title("Police Department Management Simulator")
        self.root.configure(bg='black')

        # Initialize department statistics
        self.budget = 1000000
        self.officers = 50
        self.vehicles = 25
        self.morale = 80
        self.crime_rate = 15
        self.current_date = datetime.now()
        self.satisfaction = 75

        # Enhanced divisions with specializations
        self.divisions = {
            "Patrol": {
                "officers": 30,
                "budget": 500000,
                "specializations": {
                    "Traffic": 5,
                    "Community": 5,
                    "Response": 20
                }
            },
            "Investigations": {
                "officers": 10,
                "budget": 300000,
                "specializations": {
                    "Homicide": 3,
                    "Narcotics": 4,
                    "Cyber": 3
                }
            },
            "Special Units": {
                "officers": 5,
                "budget": 100000,
                "specializations": {
                    "SWAT": 2,
                    "K9": 2,
                    "Aviation": 1
                }
            },
            "Administration": {
                "officers": 5,
                "budget": 100000,
                "specializations": {
                    "Internal Affairs": 2,
                    "Training": 2,
                    "Records": 1
                }
            }
        }

        self.setup_gui()

    def setup_gui(self):
        # Main frame
        main_frame = tk.Frame(self.root, bg='black')
        main_frame.pack(padx=10, pady=10, fill='both', expand=True)

        # Department overview section
        overview_frame = tk.LabelFrame(main_frame, text="Department Overview", fg='green', bg='black')
        overview_frame.pack(fill='both', expand=True, padx=5, pady=5)

        self.overview_text = tk.Text(overview_frame, height=8, bg='black', fg='green', wrap=tk.WORD)
        self.overview_text.pack(fill='both', expand=True)

        # Division management section
        division_frame = tk.LabelFrame(main_frame, text="Division Management", fg='green', bg='black')
        division_frame.pack(fill='both', expand=True, padx=5, pady=5)

        self.division_text = tk.Text(division_frame, height=12, bg='black', fg='green', wrap=tk.WORD)
        self.division_text.pack(fill='both', expand=True)

        # Action buttons
        button_frame = tk.Frame(main_frame, bg='black')
        button_frame.pack(fill='x', pady=5)

        actions = [
            ("Next Month", self.advance_month),
            ("Hire Officers", self.hire_officers),
            ("Budget Management", self.open_budget_window),
            ("Unit Management", self.open_unit_window)
        ]

        for text, command in actions:
            btn = tk.Button(button_frame, text=text, command=command,
                          bg='black', fg='green')
            btn.pack(side='left', padx=5)

        self.update_display()

    def open_budget_window(self):
        budget_window = tk.Toplevel(self.root)
        budget_window.title("Budget Management")
        budget_window.configure(bg='black')

        # Budget allocation frame
        allocation_frame = tk.LabelFrame(budget_window, text="Budget Allocation", fg='green', bg='black')
        allocation_frame.pack(padx=10, pady=10, fill='both', expand=True)

        self.budget_entries = {}

        for div_name, div_info in self.divisions.items():
            frame = tk.Frame(allocation_frame, bg='black')
            frame.pack(fill='x', pady=2)

            tk.Label(frame, text=f"{div_name}:", bg='black', fg='green').pack(side='left')

            entry = tk.Entry(frame, bg='black', fg='green')
            entry.insert(0, str(div_info['budget']))
            entry.pack(side='right')

            self.budget_entries[div_name] = entry

        # Total budget display
        tk.Label(budget_window, text=f"Total Budget: ${self.budget:,}", 
                bg='black', fg='green').pack(pady=5)

        # Save button
        tk.Button(budget_window, text="Save Allocations", 
                 command=lambda: self.save_budget_allocations(budget_window),
                 bg='black', fg='green').pack(pady=5)

    def open_unit_window(self):
        unit_window = tk.Toplevel(self.root)
        unit_window.title("Specialized Unit Management")
        unit_window.configure(bg='black')

        self.unit_entries = {}

        for div_name, div_info in self.divisions.items():
            # Division frame
            div_frame = tk.LabelFrame(unit_window, text=div_name, fg='green', bg='black')
            div_frame.pack(padx=10, pady=5, fill='both', expand=True)

            self.unit_entries[div_name] = {}

            for spec_name, spec_count in div_info['specializations'].items():
                frame = tk.Frame(div_frame, bg='black')
                frame.pack(fill='x', pady=2)

                tk.Label(frame, text=f"{spec_name}:", bg='black', fg='green').pack(side='left')

                entry = tk.Entry(frame, bg='black', fg='green')
                entry.insert(0, str(spec_count))
                entry.pack(side='right')

                self.unit_entries[div_name][spec_name] = entry

        # Save button
        tk.Button(unit_window, text="Save Unit Assignments", 
                 command=lambda: self.save_unit_assignments(unit_window),
                 bg='black', fg='green').pack(pady=5)

    def save_budget_allocations(self, window):
        total_allocated = 0
        new_allocations = {}

        for div_name, entry in self.budget_entries.items():
            try:
                amount = int(entry.get())
                total_allocated += amount
                new_allocations[div_name] = amount
            except ValueError:
                messagebox.showerror("Error", f"Invalid budget amount for {div_name}")
                return

        if total_allocated > self.budget:
            messagebox.showerror("Error", "Total allocation exceeds available budget!")
            return

        # Apply the new allocations
        for div_name, amount in new_allocations.items():
            self.divisions[div_name]['budget'] = amount

        self.update_display()
        window.destroy()

    def save_unit_assignments(self, window):
        for div_name, specs in self.unit_entries.items():
            div_total = 0
            new_assignments = {}

            for spec_name, entry in specs.items():
                try:
                    count = int(entry.get())
                    div_total += count
                    new_assignments[spec_name] = count
                except ValueError:
                    messagebox.showerror("Error", f"Invalid officer count for {spec_name}")
                    return

            if div_total > self.divisions[div_name]['officers']:
                messagebox.showerror("Error", 
                    f"Total assignments for {div_name} exceeds available officers!")
                return

            # Apply the new assignments
            self.divisions[div_name]['specializations'] = new_assignments

        self.update_display()
        window.destroy()

    def update_display(self):
        # Update overview display
        self.overview_text.delete(1.0, tk.END)
        self.overview_text.insert(tk.END, 
            f"Date: {self.current_date.strftime('%B %Y')}\n"
            f"Budget: ${self.budget:,}\n"
            f"Officers: {self.officers}\n"
            f"Vehicles: {self.vehicles}\n"
            f"Department Morale: {self.morale}%\n"
            f"Crime Rate: {self.crime_rate}%\n"
            f"Public Satisfaction: {self.satisfaction}%\n")

        # Update division display
        self.division_text.delete(1.0, tk.END)
        for div_name, div_info in self.divisions.items():
            self.division_text.insert(tk.END,
                f"{div_name}:\n"
                f"  Officers: {div_info['officers']}\n"
                f"  Budget: ${div_info['budget']:,}\n"
                f"  Specializations:\n")

            for spec_name, spec_count in div_info['specializations'].items():
                self.division_text.insert(tk.END,
                    f"    - {spec_name}: {spec_count} officers\n")
            self.division_text.insert(tk.END, "\n")

    def advance_month(self):
        # Simulate monthly changes
        self.budget -= self.calculate_monthly_expenses()
        self.morale += random.randint(-5, 5)
        self.crime_rate += random.randint(-2, 2)
        self.satisfaction += random.randint(-3, 3)

        # Adjust based on specialization effectiveness
        for division in self.divisions.values():
            if division['budget'] < self.calculate_division_needs(division):
                self.morale -= 2
                self.crime_rate += 1
                self.satisfaction -= 1

        # Keep values in reasonable ranges
        self.morale = max(0, min(100, self.morale))
        self.crime_rate = max(0, min(100, self.crime_rate))
        self.satisfaction = max(0, min(100, self.satisfaction))

        self.current_date += timedelta(days=30)
        self.update_display()

        if self.budget < 0:
            messagebox.showerror("Game Over", "Department has run out of funds!")

    def calculate_monthly_expenses(self):
        total = 0
        for division in self.divisions.values():
            total += self.calculate_division_needs(division)
        return total

    def calculate_division_needs(self, division):
        officer_cost = division['officers'] * 5000
        equipment_cost = sum(division['specializations'].values()) * 1000
        return officer_cost + equipment_cost

    def hire_officers(self):
        hire_window = tk.Toplevel(self.root)
        hire_window.title("Hire Officers")
        hire_window.configure(bg='black')

        # Create frame for hiring options
        hire_frame = tk.LabelFrame(hire_window, text="Hire New Officers", fg='green', bg='black')
        hire_frame.pack(padx=10, pady=10, fill='both', expand=True)

        # Division selection
        tk.Label(hire_frame, text="Select Division:", bg='black', fg='green').pack(pady=5)
        division_var = tk.StringVar(value=list(self.divisions.keys())[0])
        division_menu = ttk.Combobox(hire_frame, textvariable=division_var, values=list(self.divisions.keys()))
        division_menu.pack(pady=5)

        # Number of officers to hire
        tk.Label(hire_frame, text="Number of Officers:", bg='black', fg='green').pack(pady=5)
        number_entry = tk.Entry(hire_frame, bg='black', fg='green')
        number_entry.insert(0, "1")
        number_entry.pack(pady=5)

        # Cost display
        cost_label = tk.Label(hire_frame, text="Cost: $50,000 per officer", bg='black', fg='green')
        cost_label.pack(pady=5)

        def confirm_hire():
            try:
                num_officers = int(number_entry.get())
                total_cost = num_officers * 5000

                if total_cost > self.budget:
                    messagebox.showerror("Error", "Insufficient funds for hiring!")
                    return

                division = division_var.get()
                self.divisions[division]['officers'] += num_officers
                self.officers += num_officers
                self.budget -= total_cost

                messagebox.showinfo("Success", f"Hired {num_officers} officers for {division}")
                self.update_display()
                hire_window.destroy()

            except ValueError:
                messagebox.showerror("Error", "Please enter a valid number of officers")

        # Confirm button
        tk.Button(hire_frame, text="Confirm Hiring", command=confirm_hire,
                 bg='black', fg='green').pack(pady=10)

if __name__ == "__main__":
    root = tk.Tk()
    app = DepartmentManager(root)
    root.mainloop()
Enter fullscreen mode Exit fullscreen mode