Connect 4 - 无法编程检查是否有人获胜

问题描述 投票:0回答:2

我已经编写了一个程序,可以使整个游戏完美运行,而我唯一不能做的就是检查胜利。 我已经将游戏制作成两个文件: 1. 主要 2. 功能。 这是“主”文件:

#include <iostream>
#include <sstream>
#include <windows.h>
#include <string>

using namespace std;


void getname();
void scoregiving();
void gamestart();
void boardmaking();
void fullgameplay();

int main()
{
    gamestart();
    getname();
    scoregiving();
    fullgameplay();
}

这是函数文件:

#include <iostream>
#include <sstream>
#include <Windows.h>
#include <string>

using namespace std;

string p1, p2; 
int tu,
    board[6][7],
    colomuns[7],
    makeboard,
    makeboard1,
    scoregive,
    scoregive1,
    input,
    colomunss,
    check,
    check1;

void line()
{
    cout << "|=|=|=|=|=|=|=|\n|";
}
void getname()
{
    cout << "\n\nPlayer, Please Enter Your Name. You'll Be X\n<< ";
    cin >> p1;
    cout << "2nd Player, Please Enter Your Name. You'll Be O\n<< ";
    cin >> p2;
    tu = 1;
}
void gamestart()
{
    cout << "                        (--OOO--OOO---OOO--OOO--)\n";
    cout << "                        |XX========XXX========XX|\n";
    cout << "                       ||CONNECT 4 BY: NETVIZHEN||\n";
    cout << "                        |XX========XXX========XX|\n";
    cout << "                        (--OOO--OOO---OOO--OOO--)";
} 
void boardmaking()
{ 
    cout << "\n\nBoard:\n";
    cout << "\n 0 1 2 3 4 5 6\n";
    line();
    for (makeboard = 0; makeboard <= 5; makeboard ++)
        for (makeboard1 = 0; makeboard1 <= 6; makeboard1++)
        {
            if (board[makeboard][makeboard1] == 0)
            {
                cout << " |";
            }
            else if (board[makeboard][makeboard1] == 1)
            {
                cout << "X|";
            }
            else if (board[makeboard][makeboard1] == 2)
            {
                cout << "O|";
            }
            if (makeboard1 == 6)
            {
                cout << "\n";
                line();
            }
        }
}
void scoregiving()
{
    for (scoregive = 0 ; scoregive < 6 ; scoregive++)
      for (scoregive1 = 0 ; scoregive1 < 7 ; scoregive1++)
              board[scoregive][scoregive1] = 0;
    for (colomunss = 0; colomunss <= 6; colomunss++)
        colomuns[colomunss] = 0;
}
void wincheck()
{
    for (check = 0; check <=5; check++)
        for (check1 = 0; check1 <= 6; check1++)
            if (board[check][check1] == tu)
                if (board[check - 1][check1] == tu && board[check - 2][check1] ==  tu && board[check - 3][check1] == tu && board[check][check1] == tu)
                    cout << "ggh";
}
void putin()
{
    cout << "\n<< ";
    cin >> input;
    if (input >= 7)
        cout << "\nThis Location Is Outside The Board. Please Retry.";
    else if (colomuns[input] > 5 )
    cout << "\nThis Column Is Full. Please Retry.";
    else
    {
        board[5-colomuns[input]][input] = tu;
        colomuns[input]++;
        wincheck();
        if (tu == 2)
            tu--;
        else if (tu == 1)
            tu++;
    }
} 
void fullgameplay()
{
    while(true)
    {
        boardmaking();
        putin();
    }
}
c++
2个回答
0
投票

如前所述,您可能希望在 8 个 for 循环中检查每个方向,或者每次都使用布尔值进行检查。

bool n = true;
bool ne = true;
bool e = true;
bool se = true;
bool s = true;
bool sw = true;
bool w = true;
bool nw = true;

for (int count = 0; count < 4; count++)
{
  if (board[x + count][y] != tu)
  {
    e = false;
  }

  if (board[x + count][y + count] != tu)
  {
    ne = false; 
  }
//continue for each direction.

  if(n == true || ne == true || e == true //and so on)
  {
    //player wins
  }
}

这是可以使用的主要概念,但显然必须进行一些更改以适应您的代码。


0
投票

蛮力:从每个位置开始,向八个不同的方向遍历,只要该位置在棋盘内,不为空且与原来的位置相同。如果达到 4,则原位置的玩家获胜。

由于您应该在每次游戏后执行此操作,因此最多一个玩家可以拥有连接 4。

您可以创建一个包含 8 个方向的数组,以使用每个方向的 x 和 y 所需的增量。

© www.soinside.com 2019 - 2024. All rights reserved.