Reputation:
I'm currently having a hard time swapping two variables. I'd like to be able swap values once the user enters a value which is next to a blank cell Apologies for my extremely messy code, I'm just picking up C#.
static void SwapNums(string[,] theBoard)
{
int col, row;
string swap;
string number = ReadNumber();
for (col = 0; col if (theBoard[col, row] == "")
{
theBoard[col, row] = number;
}
}
}
}
}
}
}
} < 6; col++)
{
for (row = 0; row < 6; row++)
{
if (theBoard[col,row] == number)
{
if (theBoard[col + 1, row] == "-" || theBoard[col - 1, row] == "-" || theBoard[col, row + 1] == "" || theBoard[col, row - 1] == "-")
{
swap = theBoard[col, row];
theBoard[col, row] = "";
for (col = 0; col < 6; col++)
{
for (row = 0; row < 6; row++)
{
if (theBoard[col, row] == "")
{
theBoard[col, row] = number;
}
}
}
}
} if (theBoard[col, row] == "")
{
theBoard[col, row] = number;
}
}
}
}
}
}
}
}
}
}
}
At the moment, this code is replacing the blank cellwith what the user entered, but is not replacing the cell that contains the number to p.
Upvotes: 0
Views: 321
Reputation: 4280
Make a function that gets the "position" of an element. Something like this?
const int ROWS = 6;
const int COLUMNS = 6;
static Tuple<int, int> GetPosition(string[,] theBoard, string value)
{
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
if (theBoard[i, j] == value)
return new Tuple<int, int>(i, j);
return new Tuple<int, int>(-1, -1);
}
Then, just swap the elements, something like this:
var numberPosition = GetPosition(theBoard, number);
var minusPosition = GetPosition(theBoard, "-");
theBoard[numberPosition.Item1, numberPosition.Item2] = "-";
theBoard[minusPosition.Item1, minusPosition.Item2] = number;
Make sure to check if the element was found! (Item1
and Item2
will be -1
if not)
Here you go, complete code that illustrates the concept: http://pastebin.com/5kjDPeX8
Oh yeah, it should be only swapped if the element is next to it, so then, just do a check on the returned positions. Here is a replacement for the SwapNums
method: (I didn't update the pastebin code above)
static void SwapNums(string[,] theBoard, string number)
{
var numberPosition = GetPosition(theBoard, number);
var minusPosition = GetPosition(theBoard, "-");
if (numberPosition.Item1 == -1 || minusPosition.Item1 == -1)
throw new Exception("Element " + number + " or - was not found in theBoard!");
if (numberPosition.Item1 == minusPosition.Item1) //they are in the same row
{
if (numberPosition.Item2 + 1 == minusPosition.Item2 ||
numberPosition.Item2 - 1 == minusPosition.Item2) // if they are next to eachother
{
theBoard[numberPosition.Item1, numberPosition.Item2] = "-";
theBoard[minusPosition.Item1, minusPosition.Item2] = number;
}
}
else if (numberPosition.Item2 == minusPosition.Item2) // same column
{
if (numberPosition.Item1 + 1 == minusPosition.Item1 ||
numberPosition.Item1 - 1 == minusPosition.Item1) //if they are above or below
{
theBoard[numberPosition.Item1, numberPosition.Item2] = "-";
theBoard[minusPosition.Item1, minusPosition.Item2] = number;
}
}
}
That Tuple<int, int>
thing is just a class that contains two elements (namely int Item1
and int Item2
), which is really convenient to use when your function needs to return two things (in our case, the row and column position of the element).
The <int, int>
part means that the types of Item1
and Item2
will be int
. The <something something etc.>
things on classes generally are a part of something called generics, which is an advanced programming concept.
In short (about generics), it allows you to create a 'general' kind of an object, which could manipulate different types of object. Tuple
here can contain pairs of any type of object; Tuple<string, int>
would have string Item1
and int Item2
.
But this isn't something you should worry about right now. When you've made a few classes of your own, you'll understand why this is convenient. For now, the Tuple
class is the thing when you need to return 2 somethings from a function quick and easy.
Upvotes: 1