尽量将面向对象的思想融入进程序中
ChessBoard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//ChessBoard.h
#pragma once
#define ROW 15
#define COL 15
#include<iostream>
using namespace std;
class ChessBoard //棋盘类
{
public :
char m_cSquare[ROW][COL];
public :
ChessBoard();
void show();
};
|
ChessBoard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//ChessBoard.cpp
#include"ChessBoard.h"
ChessBoard::ChessBoard()
{
for ( int i=1;i<ROW-1;i++)
for ( int j=1;j<COL-1;j++)
m_cSquare[i][j]= ' ' ;
for ( int j=0;j<COL;j++)
m_cSquare[0][j]=m_cSquare[ROW-1][j]= '-' ;
for ( int i=1;i<ROW;i++)
m_cSquare[i][0]=m_cSquare[i][COL-1]= '|' ;
}
void ChessBoard::show()
{
system ( "cls" );
for ( int i=0;i<ROW;i++)
{ for ( int j=0;j<COL;j++)
cout<<m_cSquare[i][j]<< ' ' ; //这里的“ <<' ' ”很重要,这样才能使屏幕上ROW*COL输出为方形
cout<<endl;
}
}
|
Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//Player.h
#pragma once
//宏定义四种检测五子是否连成线的方向
#define HORIZON 0
#define VERTICAL 1
#define LEFTBOTTOMTORIGHTTOP 2
#define LEFTTOPTORIGHTBOTTOM 3
#include"ChessBoard.h"
#include<iostream>
using namespace std;
#include<string>
class Player
{
private :
string m_name;
char m_chessType;
int m_x;
int m_y;
ChessBoard* m_ptBoard;
public :
Player(string name, char chessType):m_name(name),m_chessType(chessType),m_ptBoard(NULL){}
void attachToBoard(ChessBoard* ptBoard){m_ptBoard=ptBoard;}
bool isInChessBoard( int x, int y);
bool isLine( int x, int y);
bool isWin();
void setChess();
};
|
Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
//Player.cpp
#include"Player.h"
bool Player::isInChessBoard( int x, int y)
{
if (x<ROW-1 && x>0 && y<COL-1 && y>0)
return true ;
else
return false ;
}
/*下面是核心代码:如何判断五子是否连成线。
这里采用的是以玩家此刻放下的棋子为中心,从四种方向逐个判断是否在此方向上连成了线
这里将四个方向上的判断都放在一起,避免了四次调用不同方向上的判断,
但在for里面放一个switch有些别扭,可读性上似乎不好*/
bool Player::isLine( int x, int y)
{
for ( int direc=HORIZON;direc<=LEFTTOPTORIGHTBOTTOM;direc++) //四个方向,权宜之计
{
int tempX,tempY,cnt=1; //cnt:连续排列的同种类的棋子的个数,达到五个则该方赢
for ( int i=-4;i<=4;i++)
{
if (i==0) continue ; //此时循环一遍相当于什么都没做
switch (direc)
{
case HORIZON:
tempX=x; tempY=y+i; break ;
case VERTICAL:
tempX=x+i; tempY=y; break ;
case LEFTBOTTOMTORIGHTTOP:
tempX=x-i; tempY=y+i; break ;
case LEFTTOPTORIGHTBOTTOM:
tempX=x+i; tempY=y+i; break ;
}
if (isInChessBoard(tempX,tempY) && m_ptBoard->m_cSquare[tempX][tempY]==m_chessType)
cnt++;
else
cnt=0;
if (cnt==5) //五子成线
return true ;
}
} return false ;
}
void Player::setChess()
{
cout<< "请输入玩家" <<m_name<< "的x坐标和y坐标:" <<endl;
cin>>m_x>>m_y;
while (cin.fail() || m_ptBoard->m_cSquare[m_x][m_y]!= ' ' ) //输入不是int型变量或者此位置上已有棋子
{
cout<< "输入有误,请再次输入玩家" <<m_name<< "的x坐标和y坐标:" <<endl;
cin.clear(); //清除fail状态
cin.sync(); //清除缓冲区
cin>>m_x>>m_y;
}
if (isInChessBoard(m_x,m_y))
m_ptBoard->m_cSquare[m_x][m_y]=m_chessType;
}
bool Player::isWin()
{
return isLine(m_x,m_y)? true : false ;
}
|
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//main.cpp
#include"ChessBoard.h"
#include"Player.h"
int main()
{
ChessBoard board;
Player playA( "aaa" , '*' ); //玩家aaa的棋子形状是'*'
playA.attachToBoard(&board);
Player playB( "bbb" , '#' ); //玩家bbb的棋子形状是'#'
playB.attachToBoard(&board);
board.show();
while (1)
{
playA.setChess(); //玩家A放下一个棋子
if (playA.isWin())
{ cout<< "Winer!" ; break ;}
board.show();
playB.setChess(); //玩家B放下一个棋子
if (playB.isWin())
{ cout<< "Winer!" ; break ;}
board.show();
}
return 1;
}
|
以上所述就是本文的全部内容了,希望能够对大家熟练掌握C++有所帮助。