连接4 -不能程序检查如果有人赢了

Connect 4 - Cant Program Checking If Someone Won

本文关键字:如果 检查 不能 程序 连接      更新时间:2023-10-16

我已经编写了一个程序,使整个游戏完美运行,我唯一不能做的就是检查胜利。我用两个文件创造了一款游戏: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();
}

This Is The Functions File:

#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 << "nnPlayer, Please Enter Your Name. You'll Be Xn<< ";
    cin >> p1;
    cout << "2nd Player, Please Enter Your Name. You'll Be On<< ";
    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 << "nnBoard:n";
    cout << "n 0 1 2 3 4 5 6n";
    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();
    }
}

这是一个井字游戏吗?但如果是的话,那为什么要划船呢?列的大小不相同,因为如果他的连胜记录是对角线的,那么确定获胜者将会有问题。

如前所述,您可能希望在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
  }
}

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

蛮力:从每个位置开始,在八个不同的方向上遍历,只要位置在棋盘上,不空,与原始位置相同。如果你达到4,那么在原来位置的玩家就是赢家。

因为你应该在每次游戏后进行连接,所以最多只能有一个玩家连接。

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