本文主要是介绍linux五子棋功能,五子棋(linux的shell坏境),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
闲着无聊搞个在shell环境下的五子棋玩玩
1.[代码]fivechess.cc
#include"fivechess.h"
#include
#include
#include
using namespace std;
Chess::Chess()
{
}
Chess::~Chess()
{
}
void Chess::init()//initial the chess board
{
for(int i=0;i
{
for(int j=0;j
{
chessBoard[i][j]='+';
}
puts("");
}
pointer=chessBoard;
system("clear");
printf("The game begin:\n");
}
int Chess::play(int player)
{
printf("Please put the position on the chess board,example(3 3):");
int row,col;
bool win;
cin>>row>>col;
while(!cin.good())
{
printf("The put is wrong,Please enter again:");
cin.clear();
while(cin.get()!='\n')continue;
cin>>row>>col;
}
if((row<=7&&row>=0)&&(col<=7&&row>=0))
{
if(1==player)
{
if(chessBoard[row][col]=='#'||chessBoard[row][col]=='@')
{
printf("Not legal,there is a pawm!");
play(player);
}
else
{
chessBoard[row][col]='#';
}
win=test(row,col,player);
if(win)
{
printf("Player %d,you win!Congratulations!!!\n",player);
dis();
return 1;
}
}
if(2==player)
{
if(chessBoard[row][col]=='@'||chessBoard[row][col]=='#')
{
printf("Not legal,there is a pawm!");
play(player);
}
else
{
chessBoard[row][col]='@';
}
win=test(row,col,player);
if(win)
{
printf("\nPlayer %d,you win!Congratulations!!!\n",player);
dis();
return 2;
}
}
}
else
{
printf("The position is not legal,Please put it again!");
play(player);
}
return 0;
}
void Chess::run()
{
while(true)
{
printf("The player 1 to put,");
if(1==play(1))return;
system("clear");
dis();
printf("The player 2 to put,");
if(2==play(2))return;
system("clear");
dis();
}
return;
}
void Chess::dis()//show the chess board
{
for(int i=0;i
{
for(int j=0;j
{
printf("%2c",pointer[i][j]);
}
puts("");
}
}
bool Chess::test(int row,int col ,int player)
{
char tmp;
if(1==player)tmp='#';
if(2==player)tmp='@';
//----------------------------------
int cnt=0;
for(int i=row-1;i>=0;i--)
{
if(chessBoard[i][col]!=tmp)break;
if(chessBoard[i][col]==tmp)cnt++;
if(4==cnt)return true;
}
for(int i=row+1;i
{
if(chessBoard[i][col]!=tmp)break;
if(chessBoard[i][col]==tmp)cnt++;
if(4==cnt)return true;
}
//----------------------------------
cnt=0;
for(int j=col-1;j>=0;j--)
{
if(chessBoard[row][j]!=tmp)break;
if(chessBoard[row][j]==tmp)cnt++;
if(4==cnt)return true;
}
for(int j=col+1;j
{
if(chessBoard[row][j]!=tmp)break;
if(chessBoard[row][j]==tmp)cnt++;
if(4==cnt)return true;
}
//----------------------------------
cnt=0;
for(int i=row-1,j=col-1;i>=0&&j>=0;i--,j--)
{
if(chessBoard[i][j]!=tmp)break;
if(chessBoard[i][j]==tmp)cnt++;
if(4==cnt)return true;
}
for(int i=row+1,j=col+1;i
{
if(chessBoard[i][j]!=tmp)break;
if(chessBoard[i][j]==tmp)cnt++;
if(4==cnt)return true;
}
cnt=0;
for(int i=row-1,j=col+1;i>=0&&j
{
if(chessBoard[i][j]!=tmp)break;
if(chessBoard[i][j]==tmp)cnt++;
if(4==cnt)return true;
}
for(int i=row+1,j=col-1;i=0;i++,j--)
{
if(chessBoard[i][j]!=tmp)break;
if(chessBoard[i][j]==tmp)cnt++;
if(4==cnt)return true;
}
return false;
}
2.[代码]fivechess.h
#ifndef _FIVECHESS_
#define _FIVECHESS_
#define N 8
typedef char chessP[N];
class Chess
{
private:
char chessBoard[N][N];
chessP* pointer;
public:
void init();
void dis();
int play(int player);
bool test(int row,int col,int player);
void run();
public:
Chess();
~Chess();
};
#endif
3.[代码]main.cc
#include
#include"fivechess.h"
int main()
{
Chess* c=new Chess;
c->init();
c->dis();
c->run();
return 0;
}
4.[图片] 2015-10-05 12:57:37屏幕截图.png
这篇关于linux五子棋功能,五子棋(linux的shell坏境)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!