Tuesday, February 21, 2023

Programming a Combat Cycle

 



using System;

 

public class CombatCycle
{
    private int _playerHealth;
    private int _enemyHealth;
    private bool _isPlayerTurn;
    
    public CombatCycle(int playerHealth, int enemyHealth)
    {
        _playerHealth = playerHealth;
        _enemyHealth = enemyHealth;
        _isPlayerTurn = true;
    }
    
    public void StartCombat()
    {
        while (_playerHealth > 0 && _enemyHealth > 0)
        {
            if (_isPlayerTurn)
            {
                Console.WriteLine("Player's turn.");
                Console.WriteLine("Choose an action:");
                Console.WriteLine("1. Attack");
                Console.WriteLine("2. Defend");
                int choice = int.Parse(Console.ReadLine());
                if (choice == 1)
                {
                    int damage = RollDice(6);
                    Console.WriteLine($"Player attacks and deals {damage} damage to the enemy.");
                    _enemyHealth -= damage;
                }
                else if (choice == 2)
                {
                    Console.WriteLine("Player defends and takes no damage this turn.");
                }
                _isPlayerTurn = false;
            }
            else
            {
                Console.WriteLine("Enemy's turn.");
                int damage = RollDice(6);
                Console.WriteLine($"Enemy attacks and deals {damage} damage to the player.");
                _playerHealth -= damage;
                _isPlayerTurn = true;
            }
            Console.WriteLine($"Player health: {_playerHealth}");
            Console.WriteLine($"Enemy health: {_enemyHealth}");
        }
        if (_playerHealth <= 0)
        {
            Console.WriteLine("Player has been defeated!");
        }
        else if (_enemyHealth <= 0)
        {
            Console.WriteLine("Enemy has been defeated!");
        }
    }
    
    private int RollDice(int sides)
    {
        Random random = new Random();
        return random.Next(1, sides + 1);
    }
}

 

class Program
{
    static void Main(string[] args)
    {
        CombatCycle combat = new CombatCycle(20, 20);
        combat.StartCombat();
    }
}

No comments:

Post a Comment

3D Maya Themed Diorama

This is my themed diorama work I did a campsite kind of thing with a caravan, a tent, bushes, rocks, trees and fences. I wanted to this mode...