简单的控制台五子棋游戏(人VS人)

来源:互联网 发布:音乐 知乎 编辑:程序博客网 时间:2024/05/14 22:32

写个简单游戏,复习下C++

//Chess.h

#ifndef _CHESS_H_
#define _CHESS_H_
//棋子
class Chess{
private:
int m_colour;//颜色
int m_show;//是否显示
public:
Chess(int colour = 0, int show = 0): m_colour(colour), m_show(show){}
~Chess(){}
int getColour()const {return m_colour;}
int getShow() const { return m_show;}
void setColour(int colour) { m_colour = colour; }
void setShow(int show){m_show = show;}

};
#endif


//ChessBd.h

//棋盘

#ifndef _CHESSBD_H_
#define _CHESSBD_H_


#include <iostream>
#include "Chess.h"
//棋盘
class Chessboard{
private:
enum {LINES = 15};
int m_col;//行数
int m_row;//列数
Chess cs[LINES][LINES];//每个棋盘上固定15*15个棋子,默认不显示
public:
Chessboard(int col = LINES, int row = LINES);
virtual ~Chessboard(){}
void setChess(int x, int y, int colour, int show);
const Chess &getChess(int x, int y);
void showChessBd(void);
int getCol() const { return m_col;}
int getRow() const { return m_row;}
int setCol(int col) { m_col = col;}
int setRow(int row) { m_row = row;}
void reset();
};


#endif


//Person.h

#ifndef _PERSON_H_
#define _PERSON_H_


#include "Chess.h"
#include "ChessBd.h"


class Person{
private:
int m_colour;//白方、黑方
protected:
bool inOneLine(Chessboard &csbd, int x, int y);
bool inOneRow(Chessboard &csbd, int x, int y);
bool inOneSlope(Chessboard &csbd, int x, int y);
public:
Person(int colour = 0):m_colour(colour) {}
int getColour() const { return m_colour; }
void setColour(int colour) {m_colour = colour; }
void think() { /*TODO*/ }
bool putChess(Chessboard &csbd, int x, int y);
bool isWin(Chessboard &csbd, int x, int y);
};


#endif


//main.c

#include <iostream>
#include "Person.h"


using std::cin;
using std::cout;
using std::endl;


int main()
{
Person white(0);
Person black(1);
Chessboard csbd(15, 15);

csbd.showChessBd();
cout << "Please putchess(eg, loacate(3 ,5) input 3 5 " << endl;
while(1)
{
int x, y;
char play;

do{
cout << "---->white: ";
cin >> x >> y;
}while(!white.putChess(csbd, x, y));


if(white.isWin(csbd, x, y))
{
cout << "The Winner is white corner, congratulations!" << endl;
cout << "Do you want to play again?['Y' to play]" << endl;
cin >> play;
if(play == 'Y' || play == 'y')
{
csbd.reset();
csbd.showChessBd();
continue;
}
break;
}
do{
cout << "---->Black: ";
cin >> x >> y;
}while(!black.putChess(csbd, x, y));
if(black.isWin(csbd, x, y))
{
cout << "The Winner is Black corner, congratulations!" << endl;
cout << "Do you wantto play again?['Y' to play]" << endl;
cin >> play;
if(play == 'Y' || play == 'y')
{
csbd.reset();
continue;
}
break;
}
}

return 0;
}

//ChessBd.cpp

#include <iostream>
#include "Chess.h"
#include "ChessBd.h"


using std::cout;
using std::endl;


Chessboard::Chessboard(int col, int row)
{
m_col = col;
m_row = row;

}


void Chessboard::setChess(int x, int y, int colour, int show)
{
if(cs[y][x].getShow() == 1)
{
return;
}
cs[y][x].setColour(colour);
cs[y][x].setShow(show);
}


const Chess & Chessboard::getChess(int x, int y)
{
if(x >=0 && x < LINES && y >= 0 && y < LINES)
return cs[y][x];
}


void Chessboard::showChessBd()
{
for(int i = 0; i < m_col; i++)
{
for(int j = 0; j < m_row; j++)
{
if(cs[i][j].getShow() == 1)
{
if(cs[i][j].getColour() == 0)
{
cout << "○";
}
else
{
cout << "●";
}
}
else
{
cout << "+";
}
}
cout << endl;
//cout << i << endl;
}
/*
for(int i = 0; i < m_row; i++)
{
cout << i;
}
cout << endl;
*/
}


void Chessboard::reset()
{
for(int i= 0; i < m_col; i++)
{
for(int j = 0; j < m_row; j++)
{
cs[i][j].setShow(0);
}
}
}


//Person.cpp


#include <iostream>
#include "Person.h"


using std::cout;
using std::endl;


bool Person::putChess(Chessboard &csbd, int x, int y)
{
if(x < 0 || x >=csbd.getRow() || y < 0 || y >= csbd.getCol())
{
cout << "input error. put again please.\n";
return false;
}
if(csbd.getChess(x, y).getShow() == 1)
{
cout << "exist chess, put again please!" << endl;
return false;
}
else
{
csbd.setChess(x, y, getColour(), 1);
}
csbd.showChessBd();
return true;
}


bool Person::isWin(Chessboard &csbd, int x, int y)
{
bool win = false;


if(inOneLine(csbd, x, y) || inOneRow(csbd, x, y) || inOneSlope(csbd, x, y))
{
win = true;
}
return win;
}


bool Person::inOneLine(Chessboard &csbd, int x, int y) 
{
int leftBorder = 0;
int rightBorder= 0;
int colour = getColour();
int count = 0;
leftBorder = x - 4 > 0?(x-4):0;
rightBorder = x + 4 < csbd.getRow()?(x+4):csbd.getRow();

//cout << "[DEBUG] inOneLine leftBorder = " << leftBorder << ", rightBorder = " << rightBorder << endl;
for(int left = leftBorder; left <= rightBorder; left++)
{
if(1 == csbd.getChess(left, y).getShow() && colour == csbd.getChess(left, y).getColour())
{
if( ++count == 5)
{
return true;
}
}
else
{
count = 0;
}
// cout << "count1 = " << count << endl;
}
return false;
}


bool Person::inOneRow(Chessboard &csbd, int x, int y) 
{
int bottomBorder = 0;
int topBorder= 0;
int colour = getColour();
int count = 0;

topBorder = y - 4 > 0?(y-4):0;
bottomBorder = y + 4 < csbd.getCol()?(y+4):csbd.getCol();

// cout << "[DEBUG] inOneRow topBorder = " << topBorder << ", bottomBorder = " << bottomBorder << endl;
for(int up = topBorder; up <= bottomBorder; up++)
{
if(1 == csbd.getChess(x, up).getShow() && colour == csbd.getChess(x, up).getColour())
{
if(++count >= 5)
{
return true;
}
}
else
{
count = 0;
}
}
return false;
}


bool Person::inOneSlope(Chessboard &csbd, int x, int y) 
{
//1.寻找左上角的边界点(lefttop_x, leftop_y)
//2. 寻找右下角的边界点(rightdown_x, rightdown_y)
//3.寻找左下角的边界点(leftdown_x, leftdown_y)
//4. 寻找右上角的边界点(righttop_x, righttop_y)
int lefttop_x = 0, lefttop_y = 0;
int rightdown_x = 0, rightdown_y = 0;
int leftdown_x = 0, leftdown_y = 0;
int righttop_x = 0, righttop_y = 0;

int i, j;
int colour = getColour();
//左上角的合法坐标
for(i = x, j = y; i >= 0&& i>= x-4 &&j>= 0 && j>= y-4; i--, j--)
{
lefttop_x = i, lefttop_y = j;
}
// cout << "[DEBUG] inOneSlope lefttop (" << lefttop_x << ", " << lefttop_y <<" )" << endl;

for(i = x, j = y; i < csbd.getRow() && i <= x+4 && j < csbd.getCol() && j<= y+4; i++, j++)
{
rightdown_x = i, rightdown_y = j;
}
// cout << "[DEBUG] inOneSlope rightdown (" << rightdown_x << ", " << rightdown_y <<" )" << endl;

for(i = x, j = y; i >= 0&& i>= x-4 && j < csbd.getCol() && j<= y+4; i--, j++)
{
leftdown_x = i, leftdown_y = j;
}
// cout << "[DEBUG] inOneSlope leftdown (" << leftdown_x << ", " << leftdown_y << " )" << endl;

for(i = x, j = y; i < csbd.getRow() && i <= x+4 && j>= 0 && j>= y-4; i++, j--)
{
righttop_x = i, righttop_y = j;
}
// cout << "[DEBUG] inOneSlope righttop (" << righttop_x << ", " << righttop_y <<" )" << endl;

//判断斜率小于0的斜线
int count = 0;
for(i = lefttop_x , j = lefttop_y; i <= rightdown_x; i++, j++)
{
if(1 == csbd.getChess(i, j).getShow() && colour == csbd.getChess(i, j).getColour())
{
if(++count >= 5)
{
return true;
}
}
else
{
count = 0;
}
}
//判断斜率大于0的斜线
count = 0;
for(i = leftdown_x , j = leftdown_y; i <= righttop_x; i++, j--)
{
if(1 == csbd.getChess(i, j).getShow() && colour == csbd.getChess(i, j).getColour())
{
if(++count >= 5)
{
return true;
}
}
else
{
count = 0;
}
}
return false;
}


0 0
原创粉丝点击