Turn Base battle game python

Viewed 99

I created a turn based battle game in python and it just won't work, I have tried multiple ways, and yet it is still the same. The situation is this: both players (player1 = me, player 2 = pc) will choose 3 units to attack each other, if one of us killed all of the units, the player wins.

My main issue is when I kill the first PC unit, at the PC's turn, the PC still manages to kill me using his first unit and vice versa. I need something like when the unit reach hp < 0, the unit is unable to hit anymore.

Notes: battlefield is a list that holds both players' 3 units. I have two to hold both player's units

AI_battlefield = [first_ai,second_ai,third_ai]
player_battlefield=[first_unit,second_unit,third_unit]
        
if len(ai_battlefield) = 3 and len(player_..)== 3:
   ( player or AI attack as usual but when it comes to 2 unit as below:

if len(AI_battlefield) = 3 and len(player_battlefield) == 2 :   
   if playe1.hp < 0
       player2.attack if user input is 1 
       player3.attack if user input is 2 
   elif player2.hp <0
       .....
   elif player3.hp <0 
       attack....
if len(asa_jdasdS) == 2 and len(pdlsad) ==3 
    if player1hp < 0 
    elif player2hp < 0 
    elif player3hp <0   

I have tried multiple ways and yet it doesn't work. This is my example of my another solution but yet it still won't work:

while start_game:
    RANDOM_VALUE = random.randint(-5, 10)
    time.sleep(1)
    game.total_turn()
    if game.round % 2 != 0:
        if len(AI_battlefield) == 0:
            print("Game Over ")
            print("You Win")
            print('Thanks for playing')
            start_game = False

        elif len(player_battlefield) == 0:
            print("Game Over ")
            print("You Lose")
            print('Thanks for playing')
            start_game = False
        print("--- Available Units --- ")
        field_unit(first_unit, second_unit, third_unit)
        field_unit(first_AI, second_AI, third_AI)
        print("\n")
        print(f" === Round {game.round} === ")
        print("Player move:")
        player_unit = (int(input("Select unit [Q=Quit] : ")))
        choice = int(input(f"Select opponent to attack [Q=Quit]:"))
        if first_AI.hp > 0:
            if choice == 1:
                if len(player_battlefield) == 3:
                    if player_unit == 1:
                        func_dict.get("cond_c", attack)(first_unit, first_AI, RANDOM_VALUE, AI_battlefield, 0, AI_grave)

                    elif player_unit == 2:  # player 1 hit the 2nd pc
                        func_dict.get("cond_c", attack)(second_unit, first_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                        AI_grave)

                    elif player_unit == 3:
                        func_dict.get("cond_c", attack)(third_unit, first_AI, RANDOM_VALUE, AI_battlefield, 0, AI_grave)

                elif len(player_battlefield) == 2:
                    if first_unit.hp < 0:
                        if player_unit == 1:
                            func_dict.get("cond_c", attack)(second_unit, first_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                            AI_grave)
                        elif player_unit == 2:
                            func_dict.get("cond_c", attack)(third_unit, first_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                            AI_grave)
                    elif second_unit.hp < 0:
                        if player_unit == 1:
                            func_dict.get("cond_c", attack)(first_unit, first_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                            AI_grave)
                        elif player_unit == 2:
                            func_dict.get("cond_c", attack)(third_unit, first_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                            AI_grave)

                elif len(player_battlefield) == 1:
                    if first_unit.hp < 0 and second_unit.hp < 0:
                        func_dict.get("cond_c", attack)(third_unit, first_AI, RANDOM_VALUE, AI_battlefield, 0, AI_grave)
                    elif second_unit.hp < 0 and third_unit.hp < 0:
                        func_dict.get("cond_c", attack)(first_unit, first_AI, RANDOM_VALUE, AI_battlefield, 0, AI_grave)
                    elif first_unit.hp < 0 and third_unit.hp < 0:
                        func_dict.get("cond_c", attack)(second_unit, first_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                        AI_grave)

        elif second_AI.hp > 0:
            if choice == 2:
                if len(player_battlefield) == 3:
                    if player_unit == 1:
                        func_dict.get("cond_c", attack)(first_unit, second_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                        AI_grave)

                    elif player_unit == 2:  # player 1 hit the 2nd pc
                        func_dict.get("cond_c", attack)(second_unit, second_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                        AI_grave)

                    elif player_unit == 3:
                        func_dict.get("cond_c", attack)(third_unit, second_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                        AI_grave)
                elif len(player_battlefield) == 2:
                    if first_unit.hp < 0:
                        if player_unit == 1:
                            func_dict.get("cond_c", attack)(second_unit, second_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                            AI_grave)
                        elif player_unit == 2:
                            func_dict.get("cond_c", attack)(third_unit, second_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                            AI_grave)
                    elif second_unit.hp < 0:
                        if player_unit == 1:
                            func_dict.get("cond_c", attack)(first_unit, second_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                            AI_grave)
                        elif player_unit == 2:
                            func_dict.get("cond_c", attack)(third_unit, second_AI, RANDOM_VALUE, AI_battlefield, 0,
                                                            AI_grave)
0 Answers
Related