C# Increment adding more than intended - c#

I'm coding a rock paper scissors game and some reason the counter is incrementing weird. Could anyone explain to me why when I win it increments by 1, when I lose it increments by 2, and when I tie, it increments by 3? I just want it to add by 1. I appreciate all answers.
class RPSLS
{
public enum GameMode
{
Unknown,
Single_Player,
Two_Player,
}
public GameMode GameType {get; set;}
public enum Hand { Rock = 1, Paper, Scissors, Lizard, Spock, Report, Exit };
public enum Outcome { Win, Lose, Tie };
public Hand ComputerHand { get; set; }
public Hand PlayerHand { get; set; }
public char UserSelection { get; set; }
int win = 0;
int lose = 0;
int tie = 0;
public Hand getUserHand()
{
while (!validateSelection())
{
Console.Clear();
Console.WriteLine("Invalid Input");
UserSelection = Convert.ToChar(Console.ReadLine());
}
switch (Char.ToUpper(UserSelection))
{
case 'R':
PlayerHand = Hand.Rock;
break;
case 'P':
PlayerHand = Hand.Paper;
break;
case 'S':
PlayerHand = Hand.Scissors;
break;
case 'L':
PlayerHand = Hand.Lizard;
break;
case 'K':
PlayerHand = Hand.Spock;
break;
case 'Q':
PlayerHand = Hand.Exit;
break;
case 'T':
PlayerHand = Hand.Report;
break;
default:
throw new Exception("Unexpected Error");
}
return PlayerHand;
}
public Outcome DetermineWinner()
{
if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Paper)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Lizard)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Scissors)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Scissors)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Rock)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Spock)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Paper)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Spock)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Scissors)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Rock)
{
win++;
return Outcome.Win;
}
else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Rock)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Spock)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Paper)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Spock)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Scissors)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Lizard)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Rock)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Scissors)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Lizard)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Paper)
{
lose++;
return Outcome.Lose;
}
else if (PlayerHand == Hand.Exit)
{
Environment.Exit(0);
}
else if (PlayerHand == Hand.Report)
{
Report();
}
tie++;
return Outcome.Tie;
}
private void Report()
{
Console.WriteLine("Your Score is (W:L:T:) : {0}:{1}:{2}", win, lose, tie);
}
private void PlayGame()
{
bool gameOver = false;
var rand = new Random();
char response;
while (!gameOver)
{
Screen();
UserSelection = Convert.ToChar(Console.ReadLine());
getUserHand();
ComputerHand = (Hand)rand.Next(1, 6);
Console.Clear();
Console.WriteLine("Computer's Hand: {0}", ComputerHand);
Console.WriteLine("Player's Hand: {0}", PlayerHand);
if (DetermineWinner() == Outcome.Win)
Console.WriteLine("Win");
else if (DetermineWinner() == Outcome.Lose)
Console.WriteLine("Lose");
else if (DetermineWinner() == Outcome.Tie)
Console.WriteLine("Tie");
Console.WriteLine("Would you Like to play another game? (y/n)");
response = Convert.ToChar(Console.ReadLine());
while(validateResponse(response) == false)
{
Console.WriteLine("Invalid Input, Please re-enter your selection: ");
response = Convert.ToChar(Console.ReadLine());
}
if (response == 'N' || response == 'n')
gameOver = true;
Console.Clear();
}
}
public bool validateResponse (char response)
{
if (Char.ToUpper(response) != 'Y' && Char.ToUpper(response) != 'N')
return false;
return true;
}
private bool validateSelection()
{
char value = Char.ToUpper(UserSelection);
if (value != 'R' && value != 'P' && value != 'S' && value != 'L' && value != 'K' && value != 'T' && value != 'Q')
return false;
return true;
}
private void Screen()
{
Console.WriteLine("(R)ock | (P)aper | (S)cissors | (L)izard | Spoc(k) | (Q)uit | Repor(t)");
}
public GameMode getGameType()
{
bool flag = true;
string buffer;
GameMode GameType = GameMode.Unknown;
GameType = GameMode.Unknown;
Console.WriteLine("Available options are: (S)ingle Player | (T)wo Player");
Console.Write("What would you like to play?: ");
while (flag)
{
buffer = Console.ReadLine();
try
{
switch (buffer.ToUpper())
{
case "S":
case "SINGLE PLAYER":
GameType = GameMode.Single_Player;
break;
case "T":
case "TWO PLAYER":
GameType = GameMode.Two_Player;
break;
default:
Console.WriteLine("Action not understood.");
break;
}
if (GameType != GameMode.Unknown)
{
PerformWork(GameType);
}
}
catch (ArgumentException)
{
Console.WriteLine("'{0}' is not understood.", buffer);
}
}
return GameType;
}
private void PerformWork(GameMode cmd)
{
if (cmd == GameMode.Single_Player)
{
Console.Clear();
SinglePlayer(cmd);
}
else if (cmd == GameMode.Two_Player)
{
Console.WriteLine("Two Player Mode");
}
else
{
Console.WriteLine("Action not understood.");
}
}
private void SinglePlayer(GameMode cmd)
{
Console.WriteLine("Hello. You are now playing Single Player Mode");
Console.Write("Please Enter Your Name: ");
String nameInput = Console.ReadLine();
Console.Write("Please Enter A Name For Computer: ");
string computerNameInput = Console.ReadLine();
Console.Clear();
Console.WriteLine(nameInput + " vs " + computerNameInput);
PlayGame();
}
}
}

The side effects from this block of code will increment your class level variables.
if (DetermineWinner() == Outcome.Win)
Console.WriteLine("Win");
else if (DetermineWinner() == Outcome.Lose)
Console.WriteLine("Lose");
else if (DetermineWinner() == Outcome.Tie)
Console.WriteLine("Tie");
Each time DetermineWinner() is called the fields are touched.

You're incrementing when you call DetermineWinner() which you do 3 times for a tie, twice for a loss, and once for a win.
eg assuming the result is a tie, you're calling DetermineWinner() to check if it's a win which meets the tie condition in the function and increments the value. The evaluation fails, so it runs the else and calls DetermineWinner() again, whcih increments again.
Fix would be to call DetermineWinner() once and assign the result to a variable then you can evaluate that without reincrementing.

Related

Is there any way to make a switch statement out of this mess of if else if?

Is there an easier way to do this without bringing in a bunch of mathematics? Perhaps maybe a switch statement?
if (myChoice == "Rock" && compChoice == "Scissors")
{
winner = "player";
win++;
}
else if (myChoice == "Rock" && compChoice == "Lizard")
{
winner = "player";
win++;
}
else if (myChoice == "Paper" && compChoice == "Rock")
{
winner = "player";
win++;
}
else if (myChoice == "Paper" && compChoice == "Spock")
{
winner = "player";
win++;
}
else if (myChoice == "Scissors" && compChoice == "Paper")
{
winner = "player";
win++;
}
else if (myChoice == "Scissors" && compChoice == "Lizard")
{
winner = "player";
win++;
}
else if (myChoice == "Lizard" && compChoice == "Spock")
{
winner = "player";
win++;
}
else if (myChoice == "Lizard" && compChoice == "Paper")
{
winner = "player";
win++;
}
else if (myChoice == "Spock" && compChoice == "Scissors")
{
winner = "player";
win++;
}
else if (myChoice == "Spock" && compChoice == "Rock")
{
winner = "player";
win++;
}
else if (compChoice == "Rock" && myChoice == "Scissors")
{
winner = "computer";
lose++;
}
else if (compChoice == "Rock" && myChoice == "Lizard")
{
winner = "computer";
lose++;
}
else if (compChoice == "Paper" && myChoice == "Rock")
{
winner = "computer";
lose++;
}
else if (compChoice == "Paper" && myChoice == "Spock")
{
winner = "computer";
lose++;
}
else if (compChoice == "Scissors" && myChoice == "Paper")
{
winner = "computer";
lose++;
}
else if (compChoice == "Scissors" && myChoice == "Lizard")
{
winner = "computer";
lose++;
}
else if (compChoice == "Lizard" && myChoice == "Spock")
{
winner = "computer";
lose++;
}
else if (compChoice == "Lizard" && myChoice == "Paper")
{
winner = "computer";
lose++;
}
else if (compChoice == "Spock" && myChoice == "Scissors")
{
winner = "computer";
lose++;
}
else if (compChoice == "Spock" && myChoice == "Rock")
{
winner = "computer";
lose++;
}
else
{
winner = "none";
tie++;
}
I played around with this for a little while but looking for an easier way to show some friends that are learning c#. I'm quite the beginner myself so I wasn't able to offer anymore assistance. I'm hoping that someone on here can point us in the right direction. Thanks in advance for any advice you can offer.
This is a good candidate for a switch expression, if your compiler is new enough to support it:
winner = myChoice switch
{
"Rock" => compChoice switch
{
"Rock" => "none",
"Scissors" or "Lizard" => "player",
"Paper" or "Spock" => "computer"
},
"Scissors" => compChoice switch
{
"Scissors" => "none",
"Paper" or "Lizard" => "player",
"Rock" or "Spock" => "computer"
}
// and so on
}
Naturally this also works with proper enum variables.
But this is still redundant, as you have to have anti-symmetric entries. We can take advantage of that:
bool beats(string first, string second)
=> first switch
{
"Rock" => second is "Scissors" or "Lizard",
"Scissors" => second is "Paper" or "Lizard",
"Paper" => second is "Rock" or "Spock",
"Lizard" => second is "Paper" or "Spock",
"Spock" => second is "Rock" or "Scissors"
};
bool playerWins = beats(playerChoice, compChoice);
bool computerWins = beats(compChoice, playerChoice);
switch ((playerWins, computerWins)) {
case (true, false):
winner = "player";
wins++;
break;
case (false, true):
winner = "computer";
loss++;
break;
case (false, false):
winner = "none";
draw++;
break;
case (true, true):
MessageBox.Show("Problem with the rules!");
break;
}
This is competitive with a lookup table for code length and probably more readable. And it checks the consistency of the rules, you can't accidentally make player "Rock" beat computer "Paper" at the same time that player "Paper" beats computer "Rock".
I'd personally get rid of as many of those strings as possible. Here's what you could do with a couple enums and a supporting class to play a round of the game.
public enum Choice
{
Rock, Paper, Scissors, Spock, Lizard
}
public enum Result
{
Win, Lose, Draw
}
public static class Roshambo
{
// This is a lookup table of the choice against what it beats.
private static readonly Dictionary<Choice, List<Choice>> Any = new()
{
{Choice.Rock, new() {Choice.Scissors, Choice.Lizard}},
{Choice.Paper, new() {Choice.Rock, Choice.Spock}},
{Choice.Scissors, new() {Choice.Paper, Choice.Lizard}},
{Choice.Lizard, new() {Choice.Spock, Choice.Paper}},
{Choice.Spock, new() {Choice.Scissors, Choice.Rock}}
};
public static Result Play(Choice one, Choice theOther)
{
if (one == theOther) return Result.Draw;
return Any[one].Contains(theOther)
? Result.Win
: Result.Lose;
}
}
public static void Main(string[] args)
{
var result = Roshambo.Play(Choice.Scissors, Choice.Rock);
// Here you could switch over the result
Console.WriteLine(result); // Lose
}
If we have all states as
static List<string> states = new List<string> {
"Scissors", "Paper", "Rock", "Lizzard", "Spock"
};
the rules will be simple:
A wins B if and only if either indexB - indexB == 1 or indexB - indexA == 2.
E.g. Rock wins Scissors and Lizzard
Note, however, that we should compute circular differencies (-1 ~ 4, -2 ~ 3, -3 ~ -2 etc.), e.g. Spock wins Scissors; me can do it with a help of modulo arithmetics:
private static bool FirstWins(string first, string second) {
int difference = (states.IndexOf(first) - states.IndexOf(second) + states.Count)
% states.Count;
return difference == 2 || difference == states.Count - 1;
}
Finally
static string Winner(string user, string computer) =>
FirstWins(user, computer) ? "User"
: FirstWins(computer, user) ? "Computer"
: "Draw";

How to return "if" in a if loop?

Hi guys actually first I wanted to do this loop.
Process p = Process.GetProcessesByName("etcProgram.bin")[0];
foreach (System.Diagnostics.ProcessModule moz in p.Modules)
if (csh.Text == "csh" || bin.Text == "bin")
{
if (moz.FileName.IndexOf("csh") != -1)
{
csh.Text = moz.BaseAddress.ToString();
}
if (moz.FileName.IndexOf("bin") != -1)
{
bin.Text = moz.BaseAddress.ToString();
}
}
else
{
!!!!!! return to "if" until "if code" happens !!!!!!
}
But my poor code knowledge can't come through this problem. So I wrote nearly same thinh with timer. Then I wrote this code.
private void tmrActive_Tick(object sender, EventArgs e)
{
try
{
Process p = Process.GetProcessesByName("Wolfteam.bin")[0];
foreach (System.Diagnostics.ProcessModule moz in p.Modules)
if (csh.Text == "csh" || bin.Text == "bin")
{
if (moz.FileName.IndexOf("csh") != -1)
{
csh.Text = moz.BaseAddress.ToString();
}
if (moz.FileName.IndexOf("bin") != -1)
{
bin.Text = moz.BaseAddress.ToString();
}
}
else
{
tmrActive.Stop();
MessageBox.Show("It's stopped");
}
}
But I saw that MessageBox appears 5-6 times when I started this.And I dont know why. So I dont feel very safe about use this code.
1- Do you know what's the problem with that timer. Shouldn't this messagebox appear once?
2- Can you help me about the code without timer.Is there anyway to do it?
You mean something like...
foreach (System.Diagnostics.ProcessModule moz in p.Modules)
{
bool breakloop = false;
while (!breakloop)
{
if (csh.Text == "csh" || bin.Text == "bin")
{
if (moz.FileName.IndexOf("csh") != -1)
csh.Text = moz.BaseAddress.ToString();
if (moz.FileName.IndexOf("bin") != -1)
bin.Text = moz.BaseAddress.ToString();
breakloop = true;
}
}
}
You can simply use break statement in order to stop a loop.
foreach (System.Diagnostics.ProcessModule moz in p.Modules)
{
if (csh.Text == "csh" || bin.Text == "bin")
{
if (moz.FileName.IndexOf("csh") != -1)
{
csh.Text = moz.BaseAddress.ToString();
}
if (moz.FileName.IndexOf("bin") != -1)
{
bin.Text = moz.BaseAddress.ToString();
}
break;
}
}
Hope this helps.
You could use this whole code as a Recursive function with a Specific condition to stop the condition whenever you want like this
foreach (System.Diagnostics.ProcessModule moz in p.Modules) { looping () {
bool breakloop = false;
while (!breakloop)
{
if (csh.Text == "csh" || bin.Text == "bin")
{
if (moz.FileName.IndexOf("csh") != -1)
csh.Text = moz.BaseAddress.ToString();
if (moz.FileName.IndexOf("bin") != -1)
bin.Text = moz.BaseAddress.ToString();
breakloop = true;
looping();
}
}

Paper Scissors Rock using switches Visual Studio

I am relatively new to coding on c# and just wondering why I get a red underline on my "Else" line. can anyone help me??
class Program
{
static void Main()
{
string temp;
int number;
char computer = ' ', answer;
Random rand = new Random();
Console.WriteLine("Press p for paper, s for scissor or r for rock");
temp = Console.ReadLine();
answer = Convert.ToChar(temp);
number = rand.Next(3);
switch (number)
{
case 1:
computer = 'p';
Console.WriteLine("CPU chose paper");
break;
case 2:
computer = 's';
Console.WriteLine("CPU chose scissors");
break;
case 3:
computer = 'r';
Console.WriteLine("CPU chose rock");
break;
}
if (answer == computer)
{
Console.WriteLine("Draw");
}
else if (((answer == 'r')&&(computer == 's'))
||((answer == 's')&&(computer == 'p'))
||((answer == 'p')&&(computer == 'r')))
{
Console.WriteLine("You have won");
}
else (((answer == 's')&&(computer == 'r'))
||((answer == 'p')&&(computer == 's'))
||((answer == 'r')&&(computer == 'p')))
{
Console.WriteLine("You have lost");
}
Console.ReadLine();
}
}
You cant have conditions in else. I have converted your else with else if. below is corrected code
class Program
{
static void Main()
{
string temp;
int number;
char computer = ' ', answer;
Random rand = new Random();
Console.WriteLine("Press p for paper, s for scissor or r for rock");
temp = Console.ReadLine();
answer = Convert.ToChar(temp);
number = rand.Next(3);
switch (number)
{
case 1:
computer = 'p';
Console.WriteLine("CPU chose paper");
break;
case 2:
computer = 's';
Console.WriteLine("CPU chose scissors");
break;
case 3:
computer = 'r';
Console.WriteLine("CPU chose rock");
break;
}
if (answer == computer)
{
Console.WriteLine("Draw");
}
else if (((answer == 'r') && (computer == 's')) || ((answer == 's') && (computer == 'p')) || ((answer == 'p') && (computer == 'r')))
{
Console.WriteLine("You have won");
}
else if
(((answer == 's') && (computer == 'r')) || ((answer == 'p') && (computer == 's')) || ((answer == 'r') && (computer == 'p')))
{
Console.WriteLine("You have lost");
}
else
{
Console.WriteLine("In conclusive");
}
Console.ReadLine();
}
}
The else alone can't take a condition. To check a condition you need another if clause in the else branch:
if (condition) {
...
}
else if (other condition)
{
...
}

Simplifying IF statement logic - inner logic overlap

Can the below logical if conditions be simplified ?
I have wrote this code but some parts are overlapping so I thought to seek for some help to see whether it can be simplified...
I have actually three different fields but following the same patterns.
EDIT :
if (Row.ReceivableAmount_IsNull == true && Row.CustomerID == LastCustomerID)
{
if (LastReceivableAmount == null)
{
Row.PreviousReceivableAmount_IsNull = true;
}
else
{
Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
}
}
else
{
Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
LastReceivableAmount = Row.ReceivableAmount;
}
if (Row.SaleAmount_IsNull == true && Row.CustomerID == LastCustomerID)
{
if (LastSaleDate == null)
{
Row.PreviousSaleDate_IsNull = true;
}
else
{
Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
}
}
else
{
if (LastSaleDate == null)
{
Row.PreviousSaleDate_IsNull = true;
}
else
{
Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
}
LastSaleDate = Row.Date;
}
if (Row.PaymentAmount_IsNull == true && Row.CustomerID == LastCustomerID)
{
if (LastPaymentDate == null)
{
Row.PreviousPaymentDate_IsNull = true;
}
else
{
Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
}
}
else
{
Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
LastPaymentDate = Row.Date;
}
Yes, you only care about LastSaleDate in your outer if condition, so move everything else out.
Once you've moved it out, you can invert your original condition, reducing your if/else to just an if.
if (LastReceivableAmount == null)
{
Row.PreviousReceivableAmount_IsNull = true;
}
else
{
Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
}
if (!Row.ReceivableAmount_IsNull || Row.CustomerID != LastCustomerID)
{
Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
LastReceivableAmount = Row.ReceivableAmount;
}
if (LastSaleDate == null)
{
Row.PreviousSaleDate_IsNull = true;
}
else
{
Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
}
if (!Row.SaleAmount_IsNull || Row.CustomerID != LastCustomerID)
{
LastSaleDate = Row.Date;
}
if (LastPaymentDate == null)
{
Row.PreviousPaymentDate_IsNull = true;
}
else
{
Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
}
if (!Row.PaymentAmount_IsNull == true || Row.CustomerID != LastCustomerID)
{
Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
LastPaymentDate = Row.Date;
}
Since both branches of the if are similar, except one statement, you could use the following approach:
if (LastSaleDate == null)
{
Row.PreviousSaleDate_IsNull = true;
}
else
{
Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
}
if (!Row.SaleAmount_IsNull || Row.CustomerID != LastCustomerID)
{
LastSaleDate = Row.Date;
}

Ending while loop

I wrote a small Shotgun app, however, the section of code that needs to stop the game when either the AI (called Genius), the user, or both, are shot, I can't get to work. What am I doing wrong? I feel like I over-complicated my code a ton by adding lots of returns with different booleans, in which some are being passed and others aren't.
In testing it right now, the loop ends no matter what if the user move (called string move) equals "f". In any other scenario, I can not get the loop to end.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
Start("r");
}
public static string Start(string move)
{
Console.Write("Welcome to the Shotgun App\nEnter s for single player and m for multiplayer: ");
string gameType = Console.ReadLine();
if (gameType == "s")
{
Console.Write("Single Player Controls:\n r = reload\n s = shield\n f = fire\nYou start with 1 ammo\nReady to play?");
Console.ReadLine();
int ammo = 1;
int geniusAmmo = 1;
string geniusMove = "";
bool done = false;
while (!done)
{
Console.Write("\nEnter your move: ");
move = Console.ReadLine();
switch (move)
{
case "r":
Console.Write("\nYou have reloaded, press enter for Genius\n");
ammo++;
Console.Write("Your ammo is " + ammo);
Console.ReadLine();
Genius(geniusMove, move, geniusAmmo, done);
break;
case "s":
Console.Write("\nYou have shielded, press enter for Genius\n");
Console.Write("Your ammo is " + ammo);
Console.ReadLine();
Genius(geniusMove, move, geniusAmmo, done);
break;
case "f":
if (ammo != 0)
{
Console.Write("\nYou have fired, press enter for Genius\n");
ammo--;
Console.Write("Your ammo is " + ammo);
Console.ReadLine();
Genius(geniusMove, move, geniusAmmo, done);
}
else
{
Console.Write("You don't have enough ammo, try again");
done = false;
}
break;
default:
Console.Write("\nInvalid move, try again\n");
done = false;
break;
}
done = EndLoop(move, geniusMove, done);
Console.ReadLine();
}
return move;
}
else
{
return move;
}
}
public static string Genius(string geniusMove, string move, int geniusAmmo, bool done)
{
Random RandomNumber = new Random();
int x = RandomNumber.Next(0,3);
if (x == 0)
{
geniusMove = "f";
geniusAmmo--;
Console.Write("Genius had decided to fire.\nGenius ammo is " + geniusAmmo + "\n");
TestMoves(move, geniusMove);
}
else if (x == 1)
{
geniusMove = "r";
geniusAmmo++;
Console.Write("Genius had decided to reload.\nGenius ammo is " + geniusAmmo + "\n");
TestMoves(move, geniusMove);
}
else if (x == 2)
{
geniusMove = "s";
Console.Write("Genius had decided to shield.\nGenius ammo is " + geniusAmmo + "\n");
TestMoves(move, geniusMove);
}
return geniusMove;
}
public static void TestMoves(string move, string geniusMove)
{
bool done = false;
if (move == "s" && geniusMove == "f")
{
Console.Write("Nice shield, no one has died yet");
}
else if (move == "f" && geniusMove == "f")
{
Console.Write("You both died! Good game!");
}
else if (move == "r" && geniusMove == "f")
{
Console.Write("No shield!? You died! Good game!");
}
else if (move == "f" && geniusMove == "s")
{
Console.Write("Genius is too good, no one has died yet");
}
else if (move == "f" && geniusMove != "s")
{
Console.Write("Genius let his guard down! Good game!");
}
else if (move != "f" && geniusMove != "f")
{
Console.Write("Keep playing it safe.");
}
else
{
}
}
static bool EndLoop(string move, string geniusMove, bool done)
{
done = false;
if (move == "s" && geniusMove == "f")
{
return false;
}
else if (move == "f" && geniusMove == "f")
{
return true;
}
else if (move != "s" && geniusMove == "f")
{
return true;
}
else if (move == "f" && geniusMove == "s")
{
return false;
}
else if (move == "f" && geniusMove != "s")
{
return true;
}
else if (move != "f" && geniusMove != "f")
{
return false;
}
else
{
return done;
}
}
}
}
You are setting done in a few different places, both in some execution branches in the switch cases, and when calling EndLoop. The assignment from EndLoop will overwrite any previous assignment, so make that The One Place You Set done.
Setting done with EndLoop in TestMoves does not have any effect since you immediately return a hard-coded value right after you call EndLoop.
I suggest you follow through EndLoop in a debugger. If it makes it easier for you to visualize what's happening, you might consider instead printing to the console the input parameters for EndLoop, and which if condition you end up selecting.
geniusMove will always be an empty string since you are not storing the result of the call to the Genius method.
Either store the result in the geniusMove variable or pass it in by reference
public static string Genius(ref string geniusMove, string move, int geniusAmmo, bool done)
or
geniusMove = Genius(geniusMove, move, geniusAmmo, done);

Categories

Resources