c# - C# 使用接口查找 Connect 4 的获胜者
问题描述
我目前正在执行一项使用 Visual Studio 表单在标准 7x6 网格上创建 Connect 4 游戏的任务。我已经在这里搜索过,但找不到使用与我应该检查获胜相同的方法的问题。我已经完成了游戏的架构;板子功能齐全,棋子正确掉落,已经放置的棋子不能被覆盖。然而,任务的关键是检查获胜者需要实现一个接口。这是所需的接口:
interface Winner
{
//find winner in the specified direction starting from (c,r)
bool straightup(int c, int r);
bool straightdown(int c, int r);
bool left(int c, int r);
bool right(int c, int r);
bool diagleftup(int c, int r);
bool diagrightdown(int c, int r);
bool diagleftdown(int c, int r);
bool diagrightup(int c, int r);
//return true if there is winner
bool winner();
}
如果需要,这是板的代码:
bool isWinner, xTurn; //Keeps track if there is a winner and whose turn it is
int xWins, oWins; //Keeps track of each players' wins
//Keeps track of which row the next piece in the column will drop
int col1Drop, col2Drop, col3Drop, col4Drop, col5Drop, col6Drop, col7Drop;
Button[,] places; //Array of buttons on the board
public Form1()
{
InitializeComponent();
isWinner = false;
xTurn = true;
xWins = 0;
oWins = 0;
col1Drop = 0; col2Drop = 0; col3Drop = 0; col4Drop = 0; col5Drop = 0; col6Drop = 0; col7Drop = 0;
currentTurn.Text = "X";
winRecords.Text = "Record:" + Environment.NewLine + Environment.NewLine + "X Wins - 0"+ Environment.NewLine + "O Wins - 0";
//Initializes the array of buttons on the board, starting from left to right, bottom to top
places = new Button[,]{ { col1row1, col1row2, col1row3, col1row4, col1row5, col1row6 }, { col2row1, col2row2, col2row3, col2row4, col2row5, col2row6 }, { col3row1, col3row2, col3row3, col3row4, col3row5, col3row6 }, { col4row1, col4row2, col4row3, col4row4, col4row5, col4row6 }, { col5row1, col5row2, col5row3, col5row4, col5row5, col5row6 }, { col6row1, col6row2, col6row3, col6row4, col6row5, col6row6 }, { col7row1, col7row2, col7row3, col7row4, col7row5, col7row6 } };
}
private void col1row6_Click(object sender, EventArgs e) //Runs when top button on column is clicked, all other buttons in column are disabled
{
if(xTurn) //Plays if its X's turn
{
if (col1Drop < 6) //Places piece as long as there's still a spot available
{
places[0, col1Drop].BackgroundImage = GUI_Connect4.Properties.Resources.X;
places[0, col1Drop].BackgroundImageLayout = ImageLayout.Stretch; //Fills the next available row in the column
xTurn = false; //Changes it to next player's turn
currentTurn.Text = "O";
col1Drop++; //Moves to next avaiable column
}
}
else //Plays if it's O's turn
{
if (col1Drop < 6)
{
places[0, col1Drop].BackgroundImage = GUI_Connect4.Properties.Resources.O;
places[0, col1Drop].BackgroundImageLayout = ImageLayout.Stretch;
xTurn = true;
currentTurn.Text = "X";
col1Drop++;
}
}
} //This code is the same for each column, with values changed accordingly
我将如何检查是否获胜,接口应该在哪里实现?我假设它应该只在一个单独的 checkWinner 方法中实现,该方法在播放一段时运行。
解决方案
推荐阅读
- osrm - 使用 osrm-backend 检测在桥梁或高速公路上方/下方的路线
- google-sheets - 具有多个条件的 Google 表格平均值
- python - 如何在 Python 中创建交互式 3D 图形
- r - 将列名的字符串(例如“c(speed,dist”)传递给R中的`across`函数
- javascript - 如何更改 mdl 按钮的背景颜色?
- html - 如何将一个网格元素的高度限制为另一个网格元素?
- android - 如何有意识地演奏一首歌
- python - 使用重复的用户输入将文本添加到现有 txt 文件
- makefile - 如何在 Makefile 中查找未使用的变量
- vb.net - 有人用过 Vincenzo Rossi 的 DataGridView-Filter-Popup-Extensions 吗?