Home ยป FLAMES game in Python

FLAMES game in Python

Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course
1 Year Subscription
Oracle Java Certification
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]

The flames calculator is used to find the compatibility between you and your partner.

Flames stand for Friends, Lovers, Affectionate, Marriage, Enemies, Siblings. It is a famous game that used to be played by young girls. The game does not use any sort of algorithm or scientific method to predict whether an individual is perfect for someone or not, it's simply meant to be fun.

From a Python programming point of view it's a fun programming exercise and can show you a few techniques

We give 2 examples – a command line one and a GUI one.

Code example 1

 

def flames_meaning(result):

    switcher = {
        'F': 'Friendship',
        'L': 'Love',
        'A': 'Affection',
        'M': 'Marriage',
        'E': 'Enemy',
        'S': 'Sweetheart'
    }
    
    return switcher.get(result)


def flames(name1, name2):

    flames = 'FLAMES'

    for x in name1:
        if x in name2:
            name2 = name2.replace(x, '', 1)
            name1 = name1.replace(x, '', 1)

    flamed_name = name1 + name2

    print('\nFlame name >>> ' + flamed_name)

    flamed_name_length = len(flamed_name)

    print('\nCompute FLAMES...')

    print('\nNow ' + flames)

    while len(flames) != 1:

        if len(flames) >= flamed_name_length:
            striked_out_char = flames[flamed_name_length - 1]
            flames = flames.replace(striked_out_char, '', 1)

        else:
            striked_out_char = flames[(flamed_name_length % len(flames)) - 1]
            flames = flames.replace(striked_out_char, '', 1)

        print('Now ' + flames + ', striked out ' + striked_out_char)

    print('\nFLAMES result: ' + flames_meaning(flames))

print('*** WELCOME TO THE FLAMES GAME ***\n')
name1 = input('Enter name 1 (first name only) >>> ').upper()
name2 = input('Enter name 2 (first name only) >>> ').upper()
flames(name1, name2)

When run with 2 random names

*** WELCOME TO THE FLAMES GAME ***

Enter name 1 (first name only) >>> william
Enter name 2 (first name only) >>> barbara

Flame name >>> WILLIMBRBARA

Compute FLAMES…

Now FLAMES
Now FLAME, striked out S
Now FAME, striked out L
Now FAM, striked out E
Now FA, striked out M
Now F, striked out A

FLAMES result: Friendship

Code example 2

A slightly different way to do it, this is a GUI version

 

from tkinter import *

def clear_all():
    Player1_field.delete(0, END)
    Player2_field.delete(0, END)
    Status_field.delete(0, END)
    # set focus on the Player1_field entry box
    Player1_field.focus_set()

def tell_status():
    p1 = Player1_field.get()
    p2 = Player2_field.get()
    p1 = p1.replace(" ", "")
    p2 = p2.replace(" ", "")
    p1 = list(p1)
    p2 = list(p2)
    Status_field.insert(10, result_flame(p1, p2))

def result_flame(x, y):
    for i in x[:]:
        if i in y:
            x.remove(i)
            y.remove(i)
    count = len(x) + len(y)
    result = ["Friends", "Love", "Affection", "Marriage", "Enemy", "Siblings"]
    while len(result) > 1:
        split_index = (count % len(result) - 1)
        if (split_index >= 0):
            right = result[split_index + 1:]
            left = result[:split_index]
            result = right + left
        else:
            result = result[:len(result) - 1]
    return result

if __name__ == "__main__":
    # Create a GUI window
    root = Tk()
    # Set the background colour of GUI window
    root.configure(background='light yellow')
    # Set the configuration of GUI window
    root.geometry("350x125")
    # set the name of tkinter GUI window
    root.title("Flames Game")
    # Create Player 1 Name: label
    label1 = Label(root, text="Name 1 ", fg='black', bg='light green')
    # Create Player 2 Name: label
    label2 = Label(root, text="Name 2 ", fg='black', bg='light blue')
    # Create Relation Status: label
    label3 = Label(root, text="Relationship Status", fg='black', bg='#FFE4C4')
    # placing the widgets
    label1.grid(row=1, column=0, sticky="E")
    label2.grid(row=2, column=0, sticky="E")
    label3.grid(row=4, column=0, sticky="E")
    # Create a text entry box
    Player1_field = Entry(root)
    Player2_field = Entry(root)
    Status_field = Entry(root)
    # placing the widgets
    Player1_field.grid(row=1, column=1, ipadx="50")
    Player2_field.grid(row=2, column=1, ipadx="50")
    Status_field.grid(row=4, column=1, ipadx="50")
    # Create a Submit Button
    button1 = Button(root,
                     text="Flame",
                     bg="#FF7F50",
                     fg="black",
                     command=tell_status)
    # Create the Clear Button
    button2 = Button(root,
                     text="Clear",
                     bg="#CD5C5C",
                     fg="black",
                     command=clear_all)
    # placing the widgets
    button1.grid(row=3, column=1)
    button2.grid(row=5, column=1)
    # Start the GUI
    root.mainloop()

 

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More