连接4 -不能程序检查如果有人赢了
Connect 4 - Cant Program Checking If Someone Won
我已经编写了一个程序,使整个游戏完美运行,我唯一不能做的就是检查胜利。我用两个文件创造了一款游戏: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所需的增量。
相关文章:
- 检查数组中是否有字符串中的值,如果没有,则添加它
- 如果 constexpr语言 - 为什么完全检查丢弃的语句?
- 检查私有成员变量是否在一定范围内,如果没有调整
- 有没有办法搜索向量的元素,<String>然后检查它是否包含特定的字符,如果它确实打印了它
- 如果是数字或字符,请检查输入
- 如果 BGL 中的add_vertex则检查顶点是否存在
- 如果检查不起作用,则 C++
- 如果字符串向量包含字符'p',如何检查 c++
- 如果结果为假,再次检查如何导致更多错误的结果?
- 如果方法不进行类型检查,为什么C++模板匹配?
- 如果元素的数量已经知道,检查数组或向量中的所有元素是否相等更好?
- 如果检查和内联条件之间是否存在编译器差异
- 如果条件检查,多个向量大小相等
- 使用C++代码检查操作系统是否可用(如果可用,是否打开/关闭)
- 使用未来wait_for时无法检查成员变量,但是如果我睡在线程中,它可以工作
- c++ 检查 Unix 中是否存在目录,如果存在,则调用 void 函数
- 检查 int 的输入,如果是字符,请再次请求输入(Turbo C++)
- 这是检查 const 函数(如果是否创建某些内容)的更好方法
- 如果 constexpr 和需要表达式进行临时概念检查
- 检查单词中的字母是否在另一个数组中,如果全部匹配,则返回true