using System;
using System.Windows.Forms;
namespace Shotgunna
{
public partial class Form1 : Form
{
int playerShots = 0;
int computerShots = 0;
Random random = new Random();
public Form1()
{
InitializeComponent();
}
private void PlayRound(int playerChoice)
{
// Slumpmässigt val för datorn (0 för load, 1 för shot, 2 för block)
int computerChoice = random.Next(3);
// Kolla om spelaren har skott innan de kan skjuta
if (playerChoice == 1 && playerShots == 0)
{
MessageBox.Show("Spelaren kan inte skjuta, de har inga skott kvar.");
return;
}
// Kolla om datorn har skott innan den kan skjuta
if (computerChoice == 1 && computerShots == 0)
{
MessageBox.Show("Datorn kan inte skjuta, den har inga skott kvar.");
return;
}
// Utför valen och uppdatera antalet laddade skott (om spelaren laddar)
if (playerChoice == 0)
{
playerShots++;
UpdateShotsCounts();
}
if (computerChoice == 0)
{
computerShots++;
UpdateShotsCounts();
}
// Skjuta mot ladda regeln (spelaren vinner om de skjuter)
if (playerChoice == 1 && computerChoice == 0)
{
MessageBox.Show("Spelaren vinner genom att skjuta mot ladda!");
ResetGame();
}
if (computerChoice == 1 && playerChoice == 0)
{
MessageBox.Show("Datorn vinner genom att skjuta mot ladda!");
ResetGame();
}
// Skjuta mot blocka (block) regeln
if (playerChoice == 1 && computerChoice == 2)
{
MessageBox.Show("Spelaren förlorar ett skott genom att skjuta mot block!");
playerShots--; // Spelaren förlorar ett skott
UpdateShotsCounts();
}
if (computerChoice == 1 && playerChoice == 2)
{
MessageBox.Show("Datorn förlorar ett skott genom att skjuta mot block!");
computerShots--; // Datorn förlorar ett skott
UpdateShotsCounts();
}
// Ladda mot blocka regeln (den som laddar får ett skott om den andra blockar)
if (playerChoice == 0 && computerChoice == 2)
{
MessageBox.Show("Spelaren får ett skott eftersom datorn blockade.");
playerShots++;
UpdateShotsCounts();
}
else if (computerChoice == 0 && playerChoice == 2)
{
MessageBox.Show("Datorn får ett skott eftersom spelaren blockade.");
computerShots++;
UpdateShotsCounts();
}
// Skjuta mot skjuta regeln (båda förlorar ett skott om de båda skjuter)
if (playerChoice == 1 && computerChoice == 1)
{
MessageBox.Show("Båda förlorar ett skott genom att skjuta mot skjuta!");
playerShots--;
computerShots--;
UpdateShotsCounts();
}
// Kolla om någon vinner med "shotgun" (spelaren vinner om de har 3 skott och skjuter)
if (playerShots >= 3 && playerChoice == 1)
{
MessageBox.Show("Spelaren vinner med shotgun!");
ResetGame();
}
else if (computerShots >= 3 && computerChoice == 1)
{
MessageBox.Show("Datorn vinner med shotgun!");
ResetGame();
}
}
private void ResetGame()
{
playerShots = 0;
computerShots = 0;
UpdateShotsCounts();
}
private void UpdateShotsCounts()
{
playerShotsLabel.Text = "Player Shots: " + playerShots;
computerShotsLabel.Text = "Computer Shots: " + computerShots;
}
private void ShootBtn_Click(object sender, EventArgs e)
{
int playerChoice = 1; // Spelaren väljer "Shoot"
PlayRound(playerChoice);
}
private void BlockBtn_Click(object sender, EventArgs e)
{
int playerChoice = 2; // Spelaren väljer "Block"
PlayRound(playerChoice);
}
private void ReloadBtn_Click(object sender, EventArgs e)
{
int playerChoice = 0; // Spelaren väljer "Reload"
PlayRound(playerChoice);
}
}
}
Prasad RaveendranPosted Oct 25, 2023, 8:39 PM
Let's analyze the code to understand why the count might jump by 2 in some cases:
Shooting: When either the player or the computer chooses to shoot, one shot is deducted from their respective shot count. This is done with the following lines of code:
If both the player and the computer choose to shoot in the same round, both of them lose a shot, which results in the count decrementing by 2.
Block vs. Reload: When one player chooses to reload (playerChoice = 0) and the other player chooses to block (playerChoice = 2), the player who reloaded gains an additional shot. This is done with the following code:
If the player chooses to reload and the computer chooses to block, the player gains an extra shot, and the computer's shot count increases by 1 as well, resulting in a net change of +2 shots.
These are the two scenarios where the shot count may jump by 2. It's part of the game's rules, where certain actions lead to an immediate change in shot count for both the player and the computer. This is intentional in the game's design and aligns with the game's mechanics. If you want to modify these rules to have a different behavior, you'll need to adjust the code accordingly.