1. 程式人生 > >C++實現簡單的五子棋程式

C++實現簡單的五子棋程式


//全部程式碼12 .26.2017

#include<iostream>
#include<stdlib.h>
#include<iomanip>     //格式化輸出
using namespace std;
const int N = 9;       //修飾N為常量
static int A=0, B=0;
char command;


class jiemian
{
public:


void Jiemainbuzhi();
void initBoard();
void Qijubuzhi();
void Chesspainter(int x,int y);
int check(int x, int y);
intcheckcommand(int x, int y);
void  chessicont(int x);
int Checkzuobiao(int i, int j, char flag);
int x, y;    //設定路障
protected:

bool flag = 0;      //下棋次數計數
char  chessBord[N + 1][N + 1];
};


void jiemian::chessicont(int x)
{
if (x == 1)
A++;
if (x == 2)
B++;
cout << "A : B —" << A << ":" << B << '\t';
}


void jiemian::Jiemainbuzhi()
{
cout << "————五子棋遊戲————ZXL" << endl;
cout << "################################" << endl;
cout << "#     1.進入遊戲   E           # " << endl;
cout << "#     2.退出遊戲   0 0         # " << endl;
cout << "#     3.選擇關卡   C           # " << endl;        
cout << "################################" << endl;
}


void jiemian::Qijubuzhi()
{
cout << "plaese entry — x — y " << endl;
cout << setw(3) << setfill(' ') << left << 0;
for (int i = 1; i < N + 1; i++) {
cout << setw(6) << setfill(' ') << right << i;
}
cout << endl;
cout << "   |";
for (int i = 1; i < N + 1; i++) {
cout << "-----|";
}
cout << endl;
for (int i = 1; i < N + 1; i++) {
for (int j = 0; j < N + 1; j++) {
if (j == 0) {
cout << setw(3) << setfill(' ') << left << i << '|';
}
else {
cout << "  " << chessBord[i][j] << "  |";
}
}
cout << endl;
cout << "   |";
for (int i = 1; i < N + 1; i++) {
cout << "-----|";
}
cout << endl;
}
}


void jiemian::initBoard()
{
for (int i = 0; i < N + 1; i++)
for (int j = 0; j < N + 1; j++)
{
chessBord[i][j] = ' ';     //初始化
}
}


void  jiemian::Chesspainter(int x, int y)    //下棋
{


if (-1 == check(x, y))
{
cout << "座標請輸入棋盤之間的數"<<endl;
}
else if (0 == check(x, y))
{
cout << "座標已經輸入過,請不要重複輸入" << endl;
}
else
{
system("CLS");
if (flag == 0)
{
chessBord[x][y] = 'x';
cout << "Palyer " << "A with 'x' IS " << "X ," << x << "Y ," << y << endl;
flag = 1;
}
else if (flag == 1)
{
chessBord[x][y] = 'o';
cout << "Palyer " << "B with 'o' IS " << "X ," << x << "Y ," << y << endl;
flag = 0;
}
Qijubuzhi();
}
}


int  jiemian::checkcommand(int x,int y)    //缺少容錯機制
{
if(0==x&&0==y)
{
return 1;
}
else
return 0;
}


int  jiemian::check(int x, int y)
{
if (x > N + 1 || y > N + 1 || x < 1 || y < 1)
{
return -1;
}
if (chessBord[x][y] != ' ')
{
return 0;
}
return 1;
}


int  jiemian::Checkzuobiao(int i,int j ,char flag)  //入參座標和選手的旗子
{
int x = i, y = j;
int begin = 0;  
int end = 0;

    int begin1 = 0;
int end1 = 0;

//判斷列是否滿足條件  hao
(i - 4) > 0 ? begin = i - 4 : begin = 1; //已經將列減4 或者從邊界算
(i + 4) < N ? end = N : end = (i + 4);


for (j, i = begin; i + 4 <= end; i++)
{
if (chessBord[i][j] == flag&&chessBord[i + 1][j] == flag&&chessBord[i + 2][j] == flag&&chessBord[i + 3][j] == flag&&chessBord[i + 4][j] == flag)
{
return 1;
}
}

//判斷行是否滿足條件 hao
i = x, j = y;
(j - 4) > 0 ? begin = j - 4 : begin = 1;  //已經將橫座標減4
(j + 4) < N ? end = N : end = (j + 4); //如果末尾大於N則,從N算最後一個


for (i, j = begin; j + 4 <= end; j++)
{
if (chessBord[i][j] == flag&&chessBord[i][j + 1] == flag&&chessBord[i][j + 2] == flag&&chessBord[i][j + 3] == flag&&chessBord[i][j + 4] == flag)
{
return 1;
}
}




//判斷正對角線是否滿足 HAO
int len = 0;
i = x, j = y;
i >j ? len = j - 1 : len = i - 1;   //判斷i>j的作用,按照離邊距小的座標減做初始值
if (len > 4)
len = 4;
begin = i - len;       //橫座標的起始位置
begin1 = j - len;      //縱座標,,因為是正斜邊所以兩邊要減相同數


i > j ? len = (N - i) : len = (N - j);   //結束座標也要按照距離邊界近的點去減
if (len>4)
len = 4;
end= i + len;       //橫座標的結束位置
end1 = j + len;      //縱座標的結束位置


for (int i = begin, j = begin1; (i + 4 < end) && (j + 4 < end1); i++, j++)
{
if (chessBord[i][j] == flag&&chessBord[i + 1][j + 1] == flag&&chessBord[i + 2][j + 2] == flag&&chessBord[i + 3][j + 3] == flag&&chessBord[i + 4][j + 4] == flag)
{
return 1;
}
}


//判斷負對角線是否滿足 HAO
i = x, j = y;
(i - 1) >(N - j) ? len = (N - j) : len = i - 1;  //判斷負對角線座標的位置是否在上下減小的距離
if (len > 4)
len = 4;
begin = i - len;       //橫座標的起始位置
begin1 = j + len;      //縱座標的起始位置


(N - i) > (j - 1) ? len = (j - 1) : len = (N - i);
if (len>4)
len = 4;
end = i + len;       //橫座標的結束位置
end1 = j - len;      //縱座標的結束位置


for (int i = begin, j = begin1; (i + 4 < end) && (j - 4 > end1); i++, j--)
{
if (chessBord[i][j] == flag&&chessBord[i + 1][j - 1] == flag&&chessBord[i + 2][j - 2] == flag&&chessBord[i + 3][j - 3] == flag&&chessBord[i + 4][j - 4] == flag)
{
return 1;
}
}

//判斷棋盤是否下滿 .ZUIHOU
for (int z = 1; z < N + 1; z++)           //棋盤有沒有下滿
{
for (int l = 1; l < N + 1; l++)
{
if (chessBord[z][l] == ' ')
return -1;      //說明棋盤中還有空格


}
}


return 0;
}






int main()
{
jiemian obj;
obj.Jiemainbuzhi();
while(1)
{
int x=0, y=0;
char flager;
cout << "請輸入命令 —";
cin >> command;
if ('E' == command)
{
system("CLS");
obj.initBoard();
obj.Qijubuzhi();
while ('E' == command)
{
cout << "請輸入座標";
cin >> x >> y;
if (1 == obj.checkcommand(x, y))
{
break;
}
obj.Chesspainter(x, y);

if (0 == obj.Checkzuobiao(x, y, ' '))     //判斷棋盤是否下滿HAO
{
cout << "DRAW: againt ?  Y  OR  N  " << endl;
cin >> flager;
if (flager == 'N')
{
break;
}
else
{
system("CLS");
obj.initBoard();
}
}
if (1== obj.Checkzuobiao(x, y,'x')|| 1== obj.Checkzuobiao(x, y, 'o'))
{
if (1 == obj.Checkzuobiao(x, y, 'x'))
{
obj.chessicont(1);     //顯示比分
cout << "   A IS WIN" << endl;
}
    if (1 == obj.Checkzuobiao(x, y, 'o'))
{
obj.chessicont(2);
cout << "   B IS WIN" << endl;
}
cout << "againt ?  Y  OR  N  " << endl;
cin >> flager;
if (flager == 'N')
{
break;
}
else
{
obj.initBoard();
}
}
}
}
else if ('C' == command)
{
system("CLS");
cout << "please waitting___" << endl;
}
else
{
system("CLS");
cout << "請輸入正確的命令" << endl;
obj.Jiemainbuzhi();
}


}
}