Reputation:
So I am making a X and O's game as a little project for myself while I learn C# and was wondering how I can check certain values in an array are equal to one string.
So I store all the values on my grid in an array called gridValues[]
and I am creating a method to check if someone has one after a go. So how do I check if lets say gridValues[0]
, gridValues[1]
and gridValues[2]
(the top row) are all equal to 'X'.
Also is there any simpler way to check all the combinations?
Upvotes: 0
Views: 5373
Reputation: 38727
I recommend something like this. First, we define the different winning combinations for the array. Next we go through each of the index sets (the winning combinations), getting the elements they represent as an array. Next, we check that the first item isn't null or an empty string (i.e. it's a played item), and then we check that they all match. If there's a match, it's a winning play and we return. Otherwise, it's a losing play so we try the next set. If that fails, we return null.
This will be more efficient than checking X and O individually, since we're simply looking for 3 in a row. The key part to excluding unplayed tiles is !string.IsNullOrEmpty(elements[0])
.
private static string GetWinner(string[] grid)
{
var indexSets = new []
{
// Horizontal
new [] { 0, 1, 2 },
new [] { 3, 4, 5 },
new [] { 6, 7, 8 },
// Vertical
new [] { 0, 3, 6 },
new [] { 1, 4, 7 },
new [] { 2, 5, 8 },
// Diagonal
new [] { 0, 4, 8 },
new [] { 6, 4, 2 }
};
foreach (var indices in indexSets)
{
var elements = indices.Select(i => grid[i]).ToArray();
if (!string.IsNullOrEmpty(elements[0]) && elements[0] == elements[1] && elements[0] == elements[2])
{
return elements[0];
}
}
return null;
}
Usage example:
public static void Main(string[] args)
{
var testSet = new string[]
{
"X", "O", "O",
"O", "X", "O",
"O", "X", "X"
};
var winner = GetWinner(testSet);
if (winner != null)
{
Console.WriteLine($"{winner} wins!");
}
}
Upvotes: 0
Reputation: 6154
To compare three array elements to a given value, it can be done a number of ways, but probably the most straightforward method would be to simply build binary truth statement and put that in an if
statement:
if (gridValues[0] == 'X' && gridValues[1] == gridValues[0] && gridValues[2] == gridValues[0])
{
/* do something */
}
else
{
/* do something else */
}
That said, I don't think I would use this method to solve your overall issue of trying to find three 'X's in a row in a 3 x 3 grid.
Upvotes: 0
Reputation: 1171
I prefer this one
{
if(
IsEqual(gridValues, 0,1,2) ||
IsEqual(gridValues, 3,4,5) ||
IsEqual(gridValues, 6,7,8) ||
IsEqual(gridValues, 0,4,8) ||
IsEqual(gridValues, 6,4,2) ||
IsEqual(gridValues, 0,3,6) ||
IsEqual(gridValues, 1,4,7) ||
IsEqual(gridValues, 2,5,8) )
{
/* is done */
}
else
{
/* not equal */
}
}
public static bool IsEqual(string[] A,params int[] index)
{
if(index.Length==0)
return false;
for(int i=1;i<index.Length;i++)
if(A[index[i]]!=A[0])
return false;
return true;
}
And this maybe exact code you are looking for
public static bool IsDone(string[] gridValues, string O_X)
{
if (
IsEqual(gridValues, O_X, 0, 1, 2) ||
IsEqual(gridValues, O_X, 3, 4, 5) ||
IsEqual(gridValues, O_X, 6, 7, 8) ||
IsEqual(gridValues, O_X, 0, 4, 8) ||
IsEqual(gridValues, O_X, 6, 4, 2) ||
IsEqual(gridValues, O_X, 0, 3, 6) ||
IsEqual(gridValues, O_X, 1, 4, 7) ||
IsEqual(gridValues, O_X, 2, 5, 8))
return true;
return false;
}
public static bool IsEqual(string[] A, string a, params int[] index)
{
for (int i = 0; i < index.Length; i++)
if (A[index[i]] != a)
return false;
return true;
}
You can use it like this:IsDone(gridValues, 'X')
Upvotes: 2
Reputation: 814
If they are the only values in you array ( I mean if the array contains just those values to compare )
You can do it in linq in one line !
Here is an example :
string[] arr = { "X", "X", "X" };
var test = arr.All(x => x == "X");
Console.WriteLine(test); // Will return true if all of them is X
Else you can loop for gettings the first values :
for(int i=0;i<3;i++){
if(gridValues[i] != "X")
{
Console.WriteLine("Not equls");
break;
}
}
Upvotes: -2
Reputation: 25
Storing your grid values in a 3x3 2D array might be an easier way to go to if you are trying to check if there are three of one type in a row. The 2D array will let you iterate through your grid a little easier.
Upvotes: 2