连接4 C#检查获胜问题 [英] Connect 4 C# checking wins problem
本文介绍了连接4 C#检查获胜问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我目前正在用c#进行连接4游戏。我只是想知道是否有人可以帮助我检查7x6板的水平,垂直和对角线胜利。以下是董事会的代码。如果您需要更多代码,请告诉我我真的需要帮助,因为我真的陷入困境,需要在2周内完成这项工作
我尝试过的事情:
分裂赢得水平,垂直和对角线方法
Hi everyone I am currently making a connect 4 game in c#. I was just wanting to know if anyone could help me with checking horizontal, vertical and diagonal wins in a 7x6 board. Below is the code of the board. If you need more code on anything else please let me know I really really need help as I am really stuck and need to finish this in 2 weeks
What I have tried:
splitting wins up into horizontal,vertical and diagonal methods
推荐答案
你没告诉我们你的方法CheckHorizontal(..)
哪个方法不起作用,我觉得它读起来不是很直观,所以我只会告诉你我该怎么做:
声明一个X / Y元组数组,描述当前方法的位置转换看到下一个位置,0°,45°,90°和135°。对于元组,我将使用自定义类来避免内置元组<,>的Item1,Item2属性名称。类。我在左下角设置坐标系的原点。
You didn't tell us in which way your methodCheckHorizontal(..)
does not work and I find it not very intuitive to read, so I'll just show you how I would do it:
Declare an array of X/Y-tuples which describe the translation from the position at which the method currently "looks at" to the next position, for 0°, 45°, 90° and 135°. For the tuples I'd use a custom class to avoid the "Item1", "Item2" property-names of the inbuilt Tuple<,> class. I'm setting the origin of the coordinate system in the lower left corner.
// pseudocode: N NE E SE
array directionSteps = { {0, -1}, {1, -1}, {1, 0}, {1, 1} }
其余部分在评论中说明:
The remainder explained in comments:
// semi-pseudocode:
int xCenter = ... // x-coordinate of tile placed last
int yCenter = ... // y-coordinate of tile placed last
int tileColor = ... // value of tile placed last (probably 1, resp. 2)
// outer loop loops over those direction-steps N, NE, E, SE:
foreach (directionStep in directionSteps)
{
int matchingTiles = 1; // tile placed last is the first match
// to check each direction also into the opposite direction
// have a multiplier of -1 / 1 for each of them:
for (int directionUpDown = -1; directionUpDown <= 1; directionUpDown += 2)
{
int xStep = directionStep.X * directionUpDown;
int yStep = directionStep.Y * directionUpDown;
// "walk" into the current direction, starting 1 tile away;
// distance will be 5 at maximum
for (int distance = 1; distance <= 5; distance++)
{
// now "looking" at these coordinates:
int x = xCenter + xStep * distance;
int y = yCenter + yStep * distance;
if (IsOutsideOfGrid(x, y))
break;
if (Grid[x, y] == tileColor)
matchingTiles++;
else
break;
}
}
if (matchingTiles >= 4)
// player with that tileColor has won
}
这篇关于连接4 C#检查获胜问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文