Dynamic C Builder 6中按钮的动态分配?

Dynamic C Builder 6中按钮的动态分配?,dynamic,c++builder,button,allocation,Dynamic,C++builder,Button,Allocation,我想和蒂克塔克托玩个游戏。当我调整窗口大小时,我想在界面上显示更多的按钮。从3x3到4x4等的矩阵到9x9,这取决于我调整窗口大小的程度。我该怎么做 我将免费为任何人设计一个网站,为我提供一个有效的答案(以及为玩Tictatcoe的完整程序提供额外的东西) 谢谢大家! 您可以设置一个按钮应该具有的最大大小,然后简单地划分容器大小以找到您想要的按钮数量。这部分很简单,添加按钮并不是什么问题。您可以使用标记属性跟踪tic-tac-toe数组中的按钮位置 我在想,既然你有最多想要的按钮,你可以从一开始

我想和蒂克塔克托玩个游戏。当我调整窗口大小时,我想在界面上显示更多的按钮。从3x3到4x4等的矩阵到9x9,这取决于我调整窗口大小的程度。我该怎么做

我将免费为任何人设计一个网站,为我提供一个有效的答案(以及为玩Tictatcoe的完整程序提供额外的东西)


谢谢大家!

您可以设置一个按钮应该具有的最大大小,然后简单地划分容器大小以找到您想要的按钮数量。这部分很简单,添加按钮并不是什么问题。您可以使用
标记
属性跟踪tic-tac-toe数组中的按钮位置

我在想,既然你有最多想要的按钮,你可以从一开始就创建9x9按钮,只需将它们的
Visible
属性设置为
false
即可根据需要显示它们。然后,“调整大小”例程只需调整按钮的大小

我提供了一个如何在表单构造函数中创建按钮的简单示例,请注意,虽然这非常简单,但只是用来说明我的上述观点。请注意,我使用的是一维数组,而不是二维数组,这是为了以后更容易找到赢家

// Declare constants used for our tic tac toe example
const int minButtons = 3; // The minimum number of buttons in either direction
const int maxButtons = 9; // The maximum number of buttons in either direction
const int buttonDistance = 5; // The distance between the buttons
const int buttonSize = 50; // The targeted button size.

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
 : TForm(Owner)
{
    // Create our buttons and set some tag value to identify them later.
    for (int i = 0; i < maxButtons; i++)
    {
        for (int j = 0; j < maxButtons; j++)
        {
            int idx = i * maxButtons + j;

            buttons[idx] = new TButton(this);
            buttons[idx]->Parent = this;

            // Assign the on click event (we define this later)
            buttons[idx]->OnClick = ButtonPressed;
        }
    }

    // Let the X player start
    currentPlayer = "X";
}
现在我们需要代码来处理用户输入,也就是说,每当用户按下我们的一个按钮时,我们将Sender参数转换为TButton,这样我们就可以在单个函数中进行单击处理

void __fastcall TForm1::ButtonPressed(TObject *Sender)
{
    TButton *btn = dynamic_cast<TButton*>(Sender);
    if (btn)
    {
        // Check if this button is free to be pressed
        if (btn->Enabled)
        {
            btn->Caption = currentPlayer;
            btn->Enabled = false;


            if (IsWinner())
                ShowMessage(currentPlayer + " is the winner :)");

            if (currentPlayer == "X") currentPlayer = "O";
            else currentPlayer = "X";
        }
    }
}
void\uu fastcall TForm1::按钮按下(TObject*Sender)
{
TButton*btn=动态广播(发送方);
如果(btn)
{
//检查此按钮是否可以自由按下
如果(btn->已启用)
{
btn->Caption=currentPlayer;
btn->Enabled=false;
if(IsWinner())
ShowMessage(currentPlayer+“是赢家:”);
如果(currentPlayer==“X”)currentPlayer=“O”;
else currentPlayer=“X”;
}
}
}
最后,我们添加了一个简单的函数来检查当前玩家是否是赢家,这显然可以变得更聪明、更优化,但这只是一个简单的示例,可以作为灵感的来源:

bool __fastcall TForm1::IsWinner()
{
    bool foundWinner;

    // Calculate the number of buttons to display.
    int btd = std::min(ClientWidth, ClientHeight) / buttonSize;

    // Make sure we have atleast minButtons and at most maxButtons buttons.
    btd = (btd < minButtons) ? minButtons : (btd > maxButtons) ? maxButtons : btd;

    // Check for a winner in the direction top to bottom
    for (int i = 0; i < btd; i++)
    {
        foundWinner = true;

        for (int j = 0; j < btd; j++)
        {
            if (buttons[i * maxButtons + j]->Caption != currentPlayer)
                foundWinner = false;
        }

        if (foundWinner) return true;
    }

    // Check for a winner in the direction left to right
    for (int j = 0; j < btd; j++)
    {
        foundWinner = true;

        for (int i = 0; i < btd; i++)
        {
            if (buttons[i * maxButtons + j]->Caption != currentPlayer)
                foundWinner = false;
        }

        if (foundWinner) return true;
    }

    // Check for a winner in the diagonal directions
    foundWinner = true;
    for (int i = 0; i < btd; i++)
    {
        if (buttons[i * maxButtons + i]->Caption != currentPlayer)
            foundWinner = false;
    }

    if (foundWinner) return true;

    foundWinner = true;
    for (int i = btd - 1; i >= 0; i--)
    {
        if (buttons[i * maxButtons + i]->Caption != currentPlayer)
            foundWinner = false;
    }

   return foundWinner;
}
bool\uuu快速调用TForm1::IsWinner()
{
布尔·温纳;
//计算要显示的按钮数。
int btd=std::min(ClientWidth,ClientHeight)/按钮大小;
//确保我们至少有minButtons,最多有maxButtons。
btd=(btdmaxButtons)?maxButtons:btd;
//从上到下检查是否有赢家
对于(int i=0;i标题!=currentPlayer)
foundWinner=false;
}
如果(foundWinner)返回true;
}
//从左到右检查是否有赢家
对于(int j=0;j标题!=currentPlayer)
foundWinner=false;
}
如果(foundWinner)返回true;
}
//在对角线方向上检查是否有赢家
foundWinner=true;
对于(int i=0;i标题!=currentPlayer)
foundWinner=false;
}
如果(foundWinner)返回true;
foundWinner=true;
对于(int i=btd-1;i>=0;i--)
{
if(按钮[i*maxButtons+i]->标题!=currentPlayer)
foundWinner=false;
}
返回赢家;
}
显然还有很多事情要做,整个过程中谁赢了(更新只是添加了一个简单的检查,),创建了一个界面来开始一个新游戏等等。此外,游戏不是完全防错的,在当前版本中,即使在游戏开始后,游戏也会在调整大小时添加额外的按钮,这很可能不是您想要的,但是要解决这个问题,只需在resize函数中添加一个复选框。不管怎样,我不会为你创造整个游戏,我很确定你会想自己创造

如果你觉得它有用,你可以使用它,如果没有,那么也许(而且很可能)你可以从我的方法中得到启发(这实际上是一个2分钟的解决方案)。希望对你有用。免费的网页设计是不必要的,我想你可以把它留到以后的问题上来;)

bool __fastcall TForm1::IsWinner()
{
    bool foundWinner;

    // Calculate the number of buttons to display.
    int btd = std::min(ClientWidth, ClientHeight) / buttonSize;

    // Make sure we have atleast minButtons and at most maxButtons buttons.
    btd = (btd < minButtons) ? minButtons : (btd > maxButtons) ? maxButtons : btd;

    // Check for a winner in the direction top to bottom
    for (int i = 0; i < btd; i++)
    {
        foundWinner = true;

        for (int j = 0; j < btd; j++)
        {
            if (buttons[i * maxButtons + j]->Caption != currentPlayer)
                foundWinner = false;
        }

        if (foundWinner) return true;
    }

    // Check for a winner in the direction left to right
    for (int j = 0; j < btd; j++)
    {
        foundWinner = true;

        for (int i = 0; i < btd; i++)
        {
            if (buttons[i * maxButtons + j]->Caption != currentPlayer)
                foundWinner = false;
        }

        if (foundWinner) return true;
    }

    // Check for a winner in the diagonal directions
    foundWinner = true;
    for (int i = 0; i < btd; i++)
    {
        if (buttons[i * maxButtons + i]->Caption != currentPlayer)
            foundWinner = false;
    }

    if (foundWinner) return true;

    foundWinner = true;
    for (int i = btd - 1; i >= 0; i--)
    {
        if (buttons[i * maxButtons + i]->Caption != currentPlayer)
            foundWinner = false;
    }

   return foundWinner;
}