题
到这里一切正常,我只需要创建检查某人是否获胜的方法。
关于如何有效解决这个问题有什么建议吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TresEnRaya
{
public partial class Form1 : Form
{
string[,] tablero;
bool jugador = true;
public Form1()
{
InitializeComponent();
AsignarTags();
tablero = new string[3, 3];
button1.Click += clickHandler;
button2.Click += clickHandler;
button3.Click += clickHandler;
button4.Click += clickHandler;
button5.Click += clickHandler;
button6.Click += clickHandler;
button7.Click += clickHandler;
button8.Click += clickHandler;
button9.Click += clickHandler;
}
private void AsignarTags()
{
button1.Tag = new Posicion() { X = 0, Y = 0 };
button2.Tag = new Posicion() { X = 0, Y = 1 };
button3.Tag = new Posicion() { X = 0, Y = 2 };
button4.Tag = new Posicion() { X = 1, Y = 0 };
button5.Tag = new Posicion() { X = 1, Y = 1 };
button6.Tag = new Posicion() { X = 1, Y = 2 };
button7.Tag = new Posicion() { X = 2, Y = 0 };
button8.Tag = new Posicion() { X = 2, Y = 1 };
button9.Tag = new Posicion() { X = 2, Y = 2 };
}
private void CambiarSimbolo(Button button)
{
Posicion objPosicion = (Posicion)button.Tag;
if (jugador == true)
{
tablero[objPosicion.X, objPosicion.Y] = "X";
button.Text = "X";
button.Enabled = false;
jugador = false;
}
else
{
tablero[objPosicion.X, objPosicion.Y] = "Y";
button.Text = "Y";
button.Enabled = false;
jugador = true;
}
VerificarGanador();
}
private void VerificarGanador()
{
//THE MAGIC GOES HERE. WINGARDIUM LEVIO-Sah
}
private void clickHandler(object sender, EventArgs e)
{
Button myButton = (Button)sender;
switch (myButton.Name)
{
case "button1":
CambiarSimbolo(myButton);
break;
case "button2":
CambiarSimbolo(myButton);
break;
case "button3":
CambiarSimbolo(myButton);
break;
case "button4":
CambiarSimbolo(myButton);
break;
case "button5":
CambiarSimbolo(myButton);
break;
case "button6":
CambiarSimbolo(myButton);
break;
case "button7":
CambiarSimbolo(myButton);
break;
case "button8":
CambiarSimbolo(myButton);
break;
case "button9":
CambiarSimbolo(myButton);
break;
}
}
}
}
谢谢您的帮助。
解决方案
在最有效的方法是知道的最后X或O的放置在棋盘上的位置,并检查仅包括该位置的方向。这样一来,你是不是使用蛮力,以确定如果一个球员赢得了。
其他提示
我曾经见过一种保留 8 个不同计数器的技术,每个获胜方向 1 个。
将计数器初始化为零。
当 X 放置后,将该行、列和对角线的计数器加 1。
当 氧 放置后,将行、列和对角线的计数器减 1。
如果任何计数器达到 3 或 -3,您就知道您获胜了。
+3 意味着 X 韩元。
-3 意味着 氧 韩元。
哪个计数器达到 +/-3 就会告诉您哪一行/哪列/哪对角线获胜。
像其他人所述,蛮力是好的。
不过,我宁愿列表中的节点,而不是循环他们,因为@Kendrick做到了。
例如:
TicTacVector winVectors[] =
{
{"Top Row", {0,0}, {0,1}, {0,2}},
{"Middle Row", {1,0}, {1,1}, {1,2}},
[...]
{"Diagonal 1", {0,2}, {1,1}, {2,0}}
};
(注:以上伪代码实际上不会编译)的
这是在手工编码位比较密集,但我认为它也更容易看到怎么回事。
穷举搜索。
好了,只有8种可能的获胜组合,你可以简单地检查它们不受一组代表在赚钱的仓位模板。
在回想起来,我将实际修改您的数据结构为数字而不是字符串MD-阵列,其中:
0 =>空白单元格,点击 1 =>播放器A(X),点击 -1 =>播放器B(O)
您然后可以只查看是否跨越任何行,列,或对角线之和等于或者3或-3。
有关的3x3,身份证蛮力它。有必然是一个更好的答案,但也有只有8获胜条件(vert1,vert2,vert3,horiz1,horiz2,horiz3,从顶部横左,从左下横)
for x = 0 to 2
If pos(x,0)==pos(x,1)==pos(X,2)
return pos(x,0)
for y = 0 to 2
If pos(0,y)==pos(1,y)==pos(2,y)
return pos(0,y)
if(pos(0,0)==pos(1,1)==Pos(2,2) || pos(0,2)==pos(1,1)==pos(2,0))
return pos(1,1)
else
return null
这个是什么?
Button[][] matrix = new[]
{
new []{ button1, button2, button3 },
new []{ button4, button5, button6 },
new []{ button7, button8, button9 },
new []{ button1, button5, button9 },
new []{ button3, button5, button7 },
new []{ button1, button4, button7 },
new []{ button2, button5, button8 },
new []{ button3, button6, button9 }
};
var result = matrix.FirstOrDefault(set =>
set.All(button => button.Text == "X") ||
set.All(button => button.Text == "Y"));
if(result != null)
{
string winner = result.First().Text;
}
else
{
// tie
}
不隶属于 StackOverflow