连接4 - 如果某人获胜,则无法进行程序检查

时间:2015-07-28 11:06:50

标签: c++ connect

我做了一个使整个游戏完美运作的程序,而我唯一不能做的就是获胜。 我用两个文件做了游戏: 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();
    }
}

3 个答案:

答案 0 :(得分:1)

这是一个井字游戏吗? 但如果是,那么为什么行&amp;栏目的大小不同,因为如果他的对角线有他的胜利条件,确定胜利者将是一个问题。

答案 1 :(得分:0)

蛮力:从每个位置开始,只要位置在板上,就可以在八个不同的方向上移动,而不是空的并且与原始位置相同。如果你达到4,那么原始位置的玩家就是赢家。

因为你应该在每次比赛后都这样做,所以最多一个玩家可以有一个连接4。

您可以制作8个方向的数组,以便为​​每个方向使用x和y所需的增量。

答案 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
  }
}

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