Hello I have just recently finished a code of a grid. The instructions allow you to go right,left,up or down and enter a specific value to move by. The problem right now however is, which value is entered is the only location that becomes true. I'm trying to get the entire amount to become true. For example fi we move right 3, it should be 3 spots = true instead of just the last. Can anyone help me with that?
// Make a bool 2d array and save already drilled values into the array
bool[,] drill = new bool[401, 200];
drill[200, 1] = true;
drill[200, 2] = true;
drill[200, 3] = true;
drill[201, 3] = true;
drill[202, 3] = true;
drill[203, 3] = true;
drill[203, 4] = true;
drill[203, 5] = true;
drill[204, 5] = true;
drill[205, 5] = true;
drill[205, 4] = true;
drill[205, 3] = true;
drill[206, 3] = true;
drill[207, 3] = true;
drill[207, 4] = true;
drill[207, 5] = true;
drill[207, 6] = true;
drill[207, 7] = true;
drill[206, 7] = true;
drill[205, 7] = true;
drill[204, 7] = true;
drill[203, 7] = true;
drill[202, 7] = true;
drill[201, 7] = true;
drill[200, 7] = true;
drill[199, 7] = true;
drill[199, 6] = true;
drill[199, 5] = true;
// Set some values
bool okay = true;
int column = -1;
int row = -5;
int check_column = 199;
int check_row = 5;
// Run a while loop
do
{
// Ask the user to input a direction
Console.WriteLine("Which direction would you like to go?");
string direction = Console.ReadLine();
// Ask the user to input a movement
Console.WriteLine("What value would you like to move by?");
string distance = Console.ReadLine();
int new_distance = Convert.ToInt32(distance);
// Use if statements
if (direction == "l" || direction == "L")
{
column = column - new_distance;
check_column = check_column - new_distance;
}
else if (direction == "u" || direction == "U")
{
row = row + new_distance;
check_row = check_row - new_distance;
}
else if (direction == "r" || direction == "R")
{
column = column + new_distance;
check_column = check_column + new_distance;
}
else if (direction == "d" || direction == "D")
{
row = row - new_distance;
check_row = check_row + new_distance;
}
else if (direction == "q" || direction == "Q" || new_distance == 0)
{
Console.WriteLine("Program will now end.");
okay = false;
break;
}
while (new_distance > 0)
{
if (drill[check_column, check_row] == true && check_row >= 0 && check_row <=200 && check_column >=0 && check_column <=400 && drill[check_column, check_row] != true)
{
Console.WriteLine("{0},{1} safe", column, row);
break;
}
else
{
Console.WriteLine("{0},{1} danger", column, row);
Console.WriteLine("Program will now end.");
okay = false;
break;
}
}
} while (okay);
if (okay == false)
{
Console.WriteLine("Thanks for using the program!");
}
Console.ReadLine();
To answer you question, the reason you are not seeing each move as safe is because you are not checking each position, you were simply setting your check position to the position you wanted to move and not each incremental position. I have added a class with a bit more logic in this answer because each move in your game requires slightly different logic to work.
public class DrillGame
{
private const int Columns = 401;
private const int Rows = 200;
public void Play()
{
// Make a bool 2d array and save already drilled values into the array
bool[,] drill = ProvideDrill();
// Set some values
bool okay = true;
_currentGameRow = -1;
_currentGameColumn = -5;
_currentArrayRow = 5;
_currentArrayColumn = 199;
// Run a while loop
do
{
// Ask the user to input a direction
Console.WriteLine("Which direction would you like to go?");
string direction = Console.ReadLine();
// Ask the user to input a movement
Console.WriteLine("What value would you like to move by?");
string distanceInput = Console.ReadLine();
int distanceToMove = Convert.ToInt32(distanceInput);
// Use if statements
if (direction == "l" || direction == "L")
{
okay = TryMoveLeft(distanceToMove);
}
else if (direction == "u" || direction == "U")
{
okay = TryMoveUp(distanceToMove);
}
else if (direction == "r" || direction == "R")
{
okay = TryMoveRight( distanceToMove);
}
else if (direction == "d" || direction == "D")
{
okay = TryMoveDown(distanceToMove);
}
else if (direction == "q" || direction == "Q" || distanceToMove == 0)
{
Console.WriteLine("Program will now end.");
okay = false;
break;
}
} while (okay);
if (okay == false)
{
Console.WriteLine("Thanks for using the program!");
}
Console.ReadLine();
}
private bool TryMoveLeft(int distanceToMove)
{
while(distanceToMove > 0)
{
_currentArrayColumn = _currentArrayColumn - 1;
_currentGameColumn = _currentGameColumn - 1;
if (!TryMoveColumn(distanceToMove))
{
return false;
}
distanceToMove--;
}
return true;
}
private bool TryMoveRight(int distanceToMove)
{
while (distanceToMove > 0)
{
_currentArrayColumn = _currentArrayColumn + 1;
_currentGameColumn = _currentGameColumn + 1;
if (!TryMoveColumn(distanceToMove))
{
return false;
}
distanceToMove--;
}
return true;
}
private bool TryMoveUp(int distanceToMove)
{
while (distanceToMove > 0)
{
_currentArrayRow = _currentArrayRow - 1;
_currentGameRow = _currentGameRow - 1;
if (!TryMoveRow(distanceToMove))
{
return false;
}
distanceToMove--;
}
return true;
}
private bool TryMoveDown(int distanceToMove)
{
while (distanceToMove > 0)
{
_currentArrayRow = _currentArrayRow + 1;
_currentGameRow = _currentGameRow + 1;
if (!TryMoveRow(distanceToMove))
{
return false;
}
distanceToMove--;
}
return true;
}
private bool TryMoveColumn(int distanceToMove)
{
var drill = ProvideDrill();
if (_currentArrayColumn >= 0 && _currentArrayColumn < Columns && drill[_currentArrayColumn, _currentArrayRow])
{
Console.WriteLine($"{_currentGameColumn},{_currentGameRow} safe");
return true;
}
else
{
Console.WriteLine($"{_currentGameColumn},{_currentGameRow} danger");
Console.WriteLine("Program will now end.");
return false;
}
}
private bool TryMoveRow(int distanceToMove)
{
var drill = ProvideDrill();
if (_currentArrayRow >= 0 && _currentArrayRow < Rows && drill[_currentArrayColumn, _currentArrayRow])
{
Console.WriteLine($"{_currentGameColumn},{_currentGameRow} safe");
return true;
}
else
{
Console.WriteLine($"{_currentGameColumn},{_currentGameRow} danger");
Console.WriteLine("Program will now end.");
return false;
}
}
private bool[,] ProvideDrill()
{
bool[,] drill = new bool[Columns, Rows];
drill[200, 1] = true;
drill[200, 2] = true;
drill[200, 3] = true;
drill[201, 3] = true;
drill[202, 3] = true;
drill[203, 3] = true;
drill[203, 4] = true;
drill[203, 5] = true;
drill[204, 5] = true;
drill[205, 5] = true;
drill[205, 4] = true;
drill[205, 3] = true;
drill[206, 3] = true;
drill[207, 3] = true;
drill[207, 4] = true;
drill[207, 5] = true;
drill[207, 6] = true;
drill[207, 7] = true;
drill[206, 7] = true;
drill[205, 7] = true;
drill[204, 7] = true;
drill[203, 7] = true;
drill[202, 7] = true;
drill[201, 7] = true;
drill[200, 7] = true;
drill[199, 7] = true;
drill[199, 6] = true;
drill[199, 5] = true;
return drill;
}
private int _currentArrayRow;
private int _currentArrayColumn;
private int _currentGameRow;
private int _currentGameColumn;
}
Sample output
public Text TimesText;
public bool WonPanel = false;
public int muliplyer = 0;
public bool DoMulti = false;
if(JsonData.won == 3){
TimesText.text = JsonData.won + "x";
muliplyer = 3;
DoMulti = true;
}else if(JsonData.won == 2){
TimesText.text = JsonData.won + "x";
muliplyer = 2;
DoMulti = true;
}else{
string newMoney = string.Empty;
int val;
for(int x = 0; x < MoneyText.text.Length; x++){
if(Char.IsDigit(MoneyText.text[x])){
newMoney += MoneyText.text[x];
}
}
if(newMoney.Length > 0){
val = int.Parse(newMoney);
}else{
val = 0;
}
if(DoMulti){
int MultiMoneyAmount = val + (JsonData.won * muliplyer);
MoneyText.text = MultiMoneyAmount.ToString();
DoMulti = false;
TimesText.text = "0x";
}else{
int NewMoneyAmount = val + JsonData.won;
MoneyText.text = NewMoneyAmount.ToString();
DoMulti = false;
TimesText.text = "0x";
}
This is probably not the best way to do it but the JsonData.won is received from my server and it is what the client has the opportunity to win. Right now everything works fine. They only problem I am running into is that the MoneyText.Text which is how much in total the player has won, displays "15" or "20" when I want it to display "$0.15" and "$0.20". Now I can make that happen but when they earn over a dollar that is where I am confused. 100 would equal to $1.00.
Parse it as a decimal then divide by 100(or whatever fraction is appropriate). Then for display purposes you can add whatever currency symbols, thousands separators, etc. that you want.
I thought the problem of rotating a number 180 degrees clockwise ( or ccw) and getting the same number. For all digits, except 3, 4 and 7, the rotated one is a valid digit. (rotate 3 and you get ε). I'm new in C# but I managed to solve it.
public static bool MyMethod(int originalInt)
{
bool is180 = false;
if (Array.IndexOf(originalInt.ToString().ToArray(), '3') > -1 || Array.IndexOf(originalInt.ToString().ToArray(), '4') > -1 || Array.IndexOf(originalInt.ToString().ToArray(), '7') > -1)
{
return false;
}
else
{
List<int> tempList = new List<int>();
int tempInt = originalInt;
do
{
int lastDigit = tempInt % 10;
if (lastDigit == 9)
{
lastDigit = 6;
}
else if (lastDigit == 6)
{
lastDigit = 9;
}
tempInt = tempInt / 10;
tempList.Add(lastDigit);
}
while (tempInt > 0);
tempList.Reverse();
int tempInt2 = originalInt;
int lastDigit2 = 0;
foreach (int item in tempList)
{
lastDigit2 = tempInt2 % 10;
if (item == lastDigit2)
{
is180 = true;
tempInt2 = tempInt2 / 10;
}
else
{
return false;
}
}
}
return is180;
}
Can you find a way of solving this simpler? Thank you.
Pseudocode:
map['0'] = '0';
map['1'] = '1';
map['2'] = '2';
map['5'] = '5';
map['6'] = '9';
map['8'] = '8';
map['9'] = '6';
for each position i in input_string :
if map index exists for input_string[i] :
rotated_string[i] = map[input_string[i]]
else
exit for
rotated_string = reverse(rotated_string)
if input_string = rotated_string :
has_rotational_symmetry = true
else
has_rotational_symmetry = false
I'm not 100% sure what you are asking.. but the following returns true and false properly for your method...
Edit: Now with Lippertization!
public static bool MyMethod(int originalInt)
{
var s = originalInt.ToString();
return !(s.Contains('3') || s.Contains('4') || s.Contains('7'));
}
Couldn't resist an F# version:
let is180 s =
let rd = function|'0'->'0'|'1'->'1'|'2'->'2'|'5'->'5'|'6'->'9'|'8'->'8'|'9'->'6'|_->'X'
let flip x = new string(x |> Seq.map rd |> Seq.toArray |> Array.rev)
s.Equals(flip s)
I having trouble on combobox selectedindex. basically i wanted to disable the button1 when my textbox result is 1. but the problem is when the button1 get disable and i chose another option it wont enable back. so is there another way to do it? below is just showing some part of the coding.
double[,] arr;
public Form1()
{
arr = new double[3, 3];
for (int i = 0; i < 2; i++)
{
arr[0, 0] = 1;
arr[0, 1] = 0.79;
arr[0, 2] = 1.17;
arr[1, 0] = 1.26;
arr[1, 1] = 1;
arr[1, 2] = 1.08;
arr[2, 0] = 0.85;
arr[2, 1] = 0.93;
arr[2, 2] = 1;
}
void CreateArray()
{
if (comboBox1.SelectedIndex == -1 || comboBox2.SelectedIndex == -1)
return;
else if (comboBox1.SelectedIndex == 1 || comboBox2.SelectedIndex == 0)
{
button1.Enabled = false;
}
else if (comboBox1.SelectedIndex == 0 || comboBox2.SelectedIndex == 1)
{
button1.Enabled = false;
}
else if (comboBox1.SelectedIndex == 1 || comboBox2.SelectedIndex == 2)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
If the second combobox has only 3 items then you will never be able to reach the final else clause where you reset the button to the enabled state.
This happens because you use the || logical OR operator and with only three items you will always take one of the else if condition before the final else
I have another question for you very helpful people. I use a lot of if statements many of which are just repeated and I'm sure could be shortened. This is my current bit of code
if (Globals.TotalStands <= 1)
{
ScoreUpdate.StandNo2.Visible = false;
ScoreUpdate.ScoreStand2.Visible = false;
ScoreUpdate.ScoreOutOf2.Visible = false;
}
if (Globals.TotalStands <= 2)
{
ScoreUpdate.StandNo3.Visible = false;
ScoreUpdate.ScoreStand3.Visible = false;
ScoreUpdate.ScoreOutOf3.Visible = false;
}
if (Globals.TotalStands <= 3)
{
ScoreUpdate.StandNo4.Visible = false;
ScoreUpdate.ScoreStand4.Visible = false;
ScoreUpdate.ScoreOutOf4.Visible = false;
}
if (Globals.TotalStands <= 4)
{
ScoreUpdate.StandNo5.Visible = false;
ScoreUpdate.ScoreStand5.Visible = false;
ScoreUpdate.ScoreOutOf5.Visible = false;
}
if (Globals.TotalStands <= 5)
{
ScoreUpdate.StandNo6.Visible = false;
ScoreUpdate.ScoreStand6.Visible = false;
ScoreUpdate.ScoreOutOf6.Visible = false;
}
if (Globals.TotalStands <= 6)
{
ScoreUpdate.StandNo7.Visible = false;
ScoreUpdate.ScoreStand7.Visible = false;
ScoreUpdate.ScoreOutOf7.Visible = false;
}
if (Globals.TotalStands <= 7)
{
ScoreUpdate.StandNo8.Visible = false;
ScoreUpdate.ScoreStand8.Visible = false;
ScoreUpdate.ScoreOutOf8.Visible = false;
}
as you can see there is a huge amount of code to do something simple (which I do on a few other forms as well and I'm sure there must be a better way of coding this that gets the same result? I'm a code noob so please be gentle, code is C# and software is Visual studio 2008 pro.
Most of your properties should be arrays (or some other collection). For example:
ScoreUpdate.StandNo6
could be this instead:
ScoreUpdate.StandNos[5]
Then you can use a loop instead of all those if statements:
for (int i = 0; i < Globals.TotalStands; ++i)
{
ScoreUpdate.StandNos[i].Visible = true;
ScoreUpdate.ScoreStands[i].Visible = true;
ScoreUpdate.ScoreOutOfs[i].Visible = true;
}
Or this slight variation might be better where there is an array of ScoreUpdates rather than three separate arrays:
for (int i = 0; i < Globals.TotalStands; ++i)
{
var scoreUpdate = ScoreUpdates[i];
scoreUpdate.StandNo.Visible = true;
scoreUpdate.ScoreStand.Visible = true;
scoreUpdate.ScoreOutOf.Visible = true;
}
You should make three arrays or lists of controls and use a loop.