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);
Related
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)
{
...
}
I am writing a Rock(Sten), Paper(Påse), Scissor(Sax) game, that plays against the computer. It works and all but I want to break the game when one off the two wins three times. But it keeps looping...
Im really new to programming so excuse if the code is messy... :(
And im Swedish so the code is in Swedish to... Hope you understand, if not ask me..
This is the Main:
static void Main(string[] args)
{
Game ssp = new Game();
Interaction.MsgBox("Welcome!");
string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
ssp.Start();
ssp.Seewicharethevinner(Choice);
}
This is the class with the methods that handels the game:
string CompusterChoice;
//Starts the game
public void Start()
{
//Computers hand
Random rnd = new Random();
int x = rnd.Next(0, 3);
if (x == 0)
{ DatornsVal = "Rock"; }
else if (x == 1)
{ DatornsVal = "Paper"; }
else if (x == 2)
{ DatornsVal = "Scissor"; }
}
//Look who will win
public void Seewicharethewinner(string _Choice)
{
string PlayerChoice = _Choice;
string _PlayerChoice = _Choice.ToUpper();
string _ComputerChoice = ComputerChoice.ToUpper();
if (_PlayerChoice == _ComputerChoice)
{
Interaction.MsgBox("Tie!\nYour choice was: " + _Choice + "\n" + "Computer choice was: " + _ComputerChoice);
string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
ssp.Start();
ssp.Seewicharethevinner(Choice);
}
else if (_ComputerChoice == "ROCK" && _PlayerChoice == "SCISSOR" || _ComputerChoice == "SICSSOR" && _PlayerChoice == "PAPER" || _ComputerChoice == "PAPER"
&& _PlayerChoice == "ROCK")
{
Interaction.MsgBox("You Lose!\nYour choice was: " + _Choice + "\n" + "Computer choice was: " + _ComputerChoice);
int player = 0;
int computer = 1;
Points(computer, player);
string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
ssp.Start();
ssp.Seewicharethevinner(Choice);
}
else if (_ComputerChoice == "ROCK" && _PlayerChoice == "PAPER" || _ComputerChoice == "SICSSOR" && _PlayerChoice == "ROCK" || _ComputerChoice == "PAPER"
&& _PlayerChoice == "SICSSOR")
{
Interaction.MsgBox("You won!\nYour choice was: " + _Choice + "\n" + "Computer choice was: " + _ComputerChoice);
int player = 1;
int computer = 0;
Points(computer, player);
string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
ssp.Start();
ssp.Seewicharethevinner(Choice);
}
}
public void Points(int _computer, int _player)
{
int computerpoints = 0;
int playerpoints = 0;
if (_computer > _player)
{
computerpoints++;
}
else
{
playerpoints++;
}
if (computerpoints == 3)
{
Interaction.MsgBox("Computer won three times!");
}
if (playerpoints == 3)
{
Interaction.MsgBox("You won three times!");
}
}
So it looks like the problem is that in your Poang method you check to see if someone has won 3 times and if so display a message, but after you check that you don't terminate the program. It just keeps going on as if nothing happened. Also, your win count variables are locally scoped, so they lose their value every time the function ends.
There are a lot of things that could be done to make this program better, however I am just going to provide the simplest fix here:
public void UtseVinnare(string _Val)
{
string SpelareVal = _Val;
string _SpelarVal = _Val.ToUpper();
string _DatornsVal = DatornsVal.ToUpper();
if (_DatornsVal == _SpelarVal)
{
Interaction.MsgBox("Oavgjort!\nDitt val var: " + SpelareVal + "\n" + "Datorns val var: " + DatornsVal);
string Val = Interaction.InputBox("Välj Sten, Sax eller Påse:");
Starta();
UtseVinnare(Val);
}
else if (_DatornsVal == "STEN" && _SpelarVal == "SAX" || _DatornsVal == "SAX" && _SpelarVal == "PÅSE" || _DatornsVal == "PÅSE"
&& _SpelarVal == "STEN")
{
Interaction.MsgBox("Du förlorade!\nDitt val var: " + SpelareVal + "\n" + "Datorns val var: " + DatornsVal);
int spelare = 0;
int dator = 1;
if (Poang(dator, spelare))
{
return;
}
string Val = Interaction.InputBox("Välj Sten, Sax eller Påse:");
Starta();
UtseVinnare(Val);
}
else if (_DatornsVal == "STEN" && _SpelarVal == "PÅSE" || _DatornsVal == "SAX" && _SpelarVal == "STEN" || _DatornsVal == "PÅSE"
&& _SpelarVal == "SAX")
{
Interaction.MsgBox("Du vann!\nDitt val var: " + SpelareVal + "\n" + "Datorns val var: " + DatornsVal);
int spelare = 1;
int dator = 0;
if (Poang(dator, spelare))
{
return;
}
string Val = Interaction.InputBox("Välj Sten, Sax eller Påse:");
Starta();
UtseVinnare(Val);
}
}
int datorpoangraknare = 0;
int spelarpoangraknare = 0;
public bool Poang(int _dator, int _spelare)
{
if (_dator > _spelare)
{
datorpoangraknare++;
}
else
{
spelarpoangraknare++;
}
if (datorpoangraknare == 3)
{
Interaction.MsgBox("Datorn vann tre gånger!");
return true;
}
if (spelarpoangraknare == 3)
{
Interaction.MsgBox("Du vann tre gåger!");
return true;
}
return false;
}
Instead of trying to fix your code with the current methods I would suggest adding the following to make your code easier to follow:
1: Use enums to give a clear meaning to numbers.
public enum Choice
{
Rock,
Paper,
Scissor
}
public enum WinResult
{
Won,
Tie,
Lost
}
2: Add a method to ask input from user and return the result.
private Choice GiveChoice()
{
// This is a label where we can jump to if the input was invalid.
start:
// Ask the question.
Console.Clear();
Console.WriteLine("Choose (0:Rock, 1:Paper, 2:Scissor):");
string answer = Console.ReadLine();
int result = -1;
// Validate and re-ask if invalid.
if (!int.TryParse(answer, out result) || (result < 0 && result > 2))
goto start;
return (Choice) result;
}
3: Add a method to compare 2 results from eachother.
// Returns if v1 has won, tied or lost from v2. (Left to right)
private WinResult CompareForWinner(Choice v1, Choice v2)
{
if (v1 == Choice.Paper)
{
if (v2 == Choice.Paper)
return WinResult.Tie;
if (v2 == Choice.Rock)
return WinResult.Lost;
return WinResult.Won;
}
if (v1 == Choice.Rock)
{
if (v2 == Choice.Paper)
return WinResult.Lost;
if (v2 == Choice.Rock)
return WinResult.Tie;
return WinResult.Won;
}
// v1 = Scissor.
if (v2 == Choice.Paper)
return WinResult.Won;
if (v2 == Choice.Rock)
return WinResult.Lost;
return WinResult.Tie;
}
It's not a direct answer to your question. But I think it will help you solve it yourself.
Surely the answer to this question should be go and read a book on programming c#?
I noticed that this is marked as a Duplicate above. When you go to that Duplicate that too is marked as a duplicate and only 11 hours ago.
I really don't think people should be just posting up their homework....
I'm a beginner with programming so any hints would be greatly appreciated!
I'm trying to create a program where the user guesses whether a 'coin toss' will produce heads (0) or tails (1) using a random number generator and a loop as the 'coin'. The program must output the percent of correct guesses out of the total games played.
I've tried many different ways and it's far from right! It's v discouraging.
Thanks in advance for any tips!!
-C
This is basically the closest I've come:
for (numPlays = 0; ; numPlays++)
{
Console.Write("\nWrite H/h to guess Heads, T/t to guess Tails, or Q/q to quit => ");
userChoice = Convert.ToChar(Console.ReadLine());
if (userChoice == 'H' || userChoice == 'h')
{
if (compChoice == 0)
{
Console.WriteLine("\nYOU WON");
numWins++;
}
else
Console.WriteLine("\nYOU LOSE");
if (userChoice == 'Q' || userChoice == 'q')
if (compChoice == 1)
{
Console.WriteLine("\nYOU WON");
numWins++;
}
else
Console.WriteLine("\nYOU LOSE");
if (userChoice == 'q' || userChoice == 'Q')
{
percentWin = (double)(numWins / numPlays);
Console.WriteLine("\nYou won {0} out of {1} game(s) or {2:P} of the games played.", numWins, numPlays, percentWin);
}
}
}
Console.ReadKey();
}
}
I optimized your code a little bit,I hope this helps:
int numPlays = 0, numWins = 0;
int compChoice = 0;
char userChoice;
double percentWin;
Random rnd = new Random();
while (true)
{
Console.Write("\nWrite H/h to guess Heads, T/t to guess Tails, or Q/q to quit => ");
userChoice = Console.ReadKey().KeyChar;
compChoice = rnd.Next(0, 2);
if (char.ToLowerInvariant(userChoice) != 'q')
{
if (char.ToLowerInvariant(userChoice) == 'h' && compChoice == 0)
{
Console.WriteLine("\nYOU WON");
numWins++;
}
else if (char.ToLowerInvariant(userChoice) == 't' && compChoice == 1)
{
Console.WriteLine("\nYOU WON");
numWins++;
}
else
{
Console.WriteLine("\nYOU LOSE");
}
numPlays++;
}
else
{
percentWin = (double) numWins/numPlays;
Console.WriteLine("\nYou won {0} out of {1} game(s) or {2:P} of the games played.", numWins, numPlays, percentWin);
break;
}
}
I think you don't need an explanation, all of the code is self-explanatory.But you have an obvious mistake when you calculating the percentage:
percentWin = (double)(numWins / numPlays);
Here you are performing an integer division,then casting that result to double,it's pointless because you already lose your decimal points.Instead you should cast one of the operands to double so it will perform a double division and you will get the correct result.
Just invoke this method and play your game!
public static void SomeMethod()
{
Console.Write("\nWrite H/h to guess Heads, T/t to guess Tails, or Q/q to quit => ");
char userChoice;
int numWins = 0;
int numPlays = 0;
double percentWin = 0d;
while ((userChoice = Convert.ToChar(Console.ReadLine())).ToString().ToLower() != "q")
{
numPlays++;
int compChoice = new Random().Next(2);
if (userChoice == 'H' || userChoice == 'h')
{
if (compChoice == 0)
{
Console.WriteLine("\nYOU WON");
numWins++;
}
else
{
Console.WriteLine("\nYOU LOSE");
}
continue;
}
if (userChoice == 'T' || userChoice == 't')
{
if (compChoice == 1)
{
Console.WriteLine("\nYOU WON");
numWins++;
}
else
{
Console.WriteLine("\nYOU LOSE");
}
continue;
}
}
if (numPlays != 0)
{
percentWin = (double)numWins / numPlays;
}
Console.WriteLine("\nYou won {0} out of {1} game(s) or {2:P} of the games played.", numWins, numPlays, percentWin);
Console.ReadKey();
}
This should not be any difficult, you just need to make a clear algorithm
Let me suggest one
Char playOrExit= 'y'
Int32 gameCount = 0;
Int32 Wins = 0;
While (playOrExit.ToUpper.Equals('Y'))
{
Console.Write("Press Y to continue playing and N to exit");
playOrExit = Convert.ToChar(Console.ReadLine());
//Computer choice will be math.random from 0 and 1
//now take user choice
//compare and show if he won or lost
}
if(gameCount >0)
{
Console.WriteLine("\nYou played {0} times, out of which {1} game(s) were won.", gameCount,Wins);
}
This is contain from method, which writes hidden letters. When I hit a backspace, I need somehow to delete char * from console.
private static string Password()
{
bool enter=true;
string pass="";
do
{
char letter = Console.ReadKey(true).KeyChar;
if (letter == (char)13)
{ enter = false; }
else if (letter == (char)8 && pass.Length >= 1)
{
pass = pass.Remove(pass.Length - 1);
Console.Write("\b");
}
else
{
pass += letter;
Console.Write("*");
}
} while (enter);
Console.WriteLine();
return pass;
}
I searched for an answer but the command Console.Write("\b"); but it doesn't seem to work correctly.
You can set the cursor's position back one, write a null character, then move it back again. Like this:
private static string Password()
{
bool enter = true;
string pass = "";
do
{
char letter = Console.ReadKey(true).KeyChar;
if (letter == (char)13)
{ enter = false; }
else if (letter == (char)8 && pass.Length >= 1)
{
pass = pass.Remove(pass.Length - 1);
Console.CursorLeft--;
Console.Write('\0');
Console.CursorLeft--;
}
//Additionally, don't print backspaces if it's the first character.
else if (letter != (char)8)
{
pass += letter;
Console.Write("*");
}
} while (enter);
Console.WriteLine();
return pass;
}
Try this:
Console.Write("\b \b");
I have a while loop that loops until bool done = true;
In the method TestMoves(), depending on user input, the method returns the bool done as either true or false. However, I do not know how to "send" this value back to the while loop in my Start() method to stop the loop. Here is my code:
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\n ***you start with ammo\n Ready to play?");
Console.ReadLine();
int ammo = 1;
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();
string geniusMove = "";
Genius(geniusMove, move, done);
Console.ReadLine();
break;
case "s":
Console.Write("\nYou have shielded, press enter for Genius\n");
Console.Write("Your ammo is " + ammo);
Console.ReadLine();
geniusMove = "";
Genius(geniusMove, move, done);
Console.ReadLine();
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();
geniusMove = "";
Genius(geniusMove, move, done);
Console.ReadLine();
}
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;
}
}
return move;
}
else
{
return move;
}
}
static string Genius(string geniusMove, string move, bool done)
{
int geniusAmmo = 1;
geniusMove = "r";
if (geniusMove == "f")
{
geniusAmmo--;
Console.Write("Genius had decided to fire.\nGenius ammo is " + geniusAmmo + "\n");
}
else if (geniusMove == "r")
{
geniusAmmo++;
Console.Write("Genius had decided to reload.\nGenius ammo is " + geniusAmmo + "\n");
}
else if (geniusMove == "s")
{
Console.Write("Genius had decided to shield.\nGenius ammo is " + geniusAmmo + "\n");
}
TestMoves(move, geniusMove, done);
return geniusMove;
}
static bool TestMoves(string move, string geniusMove, bool done)
{
if (move == "s" && geniusMove == "f")
{
Console.Write("No one has died yet");
done = false;
return done;
}
else if (move == "f" && geniusMove == "f")
{
Console.Write("You both died! Good game!");
done = true;
return done;
}
else if (move != "s" && geniusMove == "f")
{
Console.Write("You died! Good game!");
done = true;
return done;
}
else if (move == "f" && geniusMove == "s")
{
Console.Write("No one has died yet");
done = false;
return done;
}
else if (move == "f" && geniusMove != "s")
{
Console.Write("Genius died! Good game!");
done = true;
return done;
}
else if (move != "f" && geniusMove != "f")
{
Console.Write("No one has died yet");
done = false;
return done;
}
else
{
return done;
}
}
}
}
Is there a reason you need to call TestMoves from inside of Genius instead of from your loop? It seems to me that your code could be rewritten like this:
//Every instance of:
string geniusMove = "";
Genius(geniusMove, move, done);
Console.ReadLine();
//seems like it could be rewritten as:
string geniusMove = "";
Genius(geniusMove, move, done);
done = TestMoves(geniusMove, move, done);
Console.ReadLine();
//and then you can remove the call to TestMoves from Genius
The overall flow of all the code is a bit confusing to me. You have each function returning a value, but don't appear to be doing anything with the return value. I have a feeling that with a bit of refactoring, you could make this code much shorter and more logical.
After looking at your code a bit more it looks like you could place the call to TestMoves at the very end of your loop:
default:
Console.Write("\nInvalid move, try again\n");
done = false;
break;
}
//add it here:
done = TestMoves(geniusMove, move, done);
}
return move;
You can pass the value by ref:
static string Genius(string geniusMove, string move, ref bool done) ...
And return it from TestMoves:
static bool TestMoves(string move, string geniusMove) ...
To call it:
Genius(geniusMove, move, ref done);
Since bool is a value type, not a reference type, you're not able to pass the bool through like that. Use the out keyword to explicitly specify that you want to pass it as a reference:
http://msdn.microsoft.com/en-us/library/ee332485.aspx
You could use the ref or the out keyword on the done parameter