拆分多个文件。
This commit is contained in:
274
OOP/FiveInARow/chessBoard.cpp
Normal file
274
OOP/FiveInARow/chessBoard.cpp
Normal file
@@ -0,0 +1,274 @@
|
||||
#include "fiveInARow.h"
|
||||
|
||||
// clang-format off
|
||||
char ChessBoard::emptyBoardString[3][29][4] = {
|
||||
{
|
||||
"┏", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┓"
|
||||
},
|
||||
{
|
||||
"┠", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┨"
|
||||
},
|
||||
{
|
||||
"┗", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┛",
|
||||
}
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
char ChessBoard::toHex(int num) {
|
||||
if (num < 10) {
|
||||
return num + '0';
|
||||
}
|
||||
else {
|
||||
return num - 10 + 'A';
|
||||
}
|
||||
}
|
||||
|
||||
void ChessBoard::makeTitle() {
|
||||
std::cout << " ";
|
||||
for (int i = 1; i <= this->size; i++) {
|
||||
std::cout << " " << this->toHex(i);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
ChessBoard::ChessBoard() {
|
||||
this->chessCount = 0;
|
||||
this->size = 15;
|
||||
}
|
||||
|
||||
ChessBoard::~ChessBoard() {
|
||||
}
|
||||
|
||||
void ChessBoard::show() {
|
||||
this->makeTitle();
|
||||
for (int i = 0; i < 15; i++) {
|
||||
std::cout << this->toHex(i + 1) << ' ';
|
||||
char(*emptyLineRef)[29][4] = NULL;
|
||||
if (i == 0) {
|
||||
emptyLineRef = &emptyBoardString[0];
|
||||
}
|
||||
else if (i == 14) {
|
||||
emptyLineRef = &emptyBoardString[2];
|
||||
}
|
||||
else {
|
||||
emptyLineRef = &emptyBoardString[1];
|
||||
}
|
||||
int j = 0;
|
||||
for (; j < 15; j++) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
std::cout << (*emptyLineRef)[j * 2];
|
||||
}
|
||||
else {
|
||||
std::cout << this->ChessPieces[i][j].show();
|
||||
}
|
||||
if (j < 14) {
|
||||
std::cout << (*emptyLineRef)[j * 2 + 1];
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
bool ChessBoard::setChess(bool color, int row, int column) {
|
||||
if (row > 14 || row < 0 || column > 14 || column < 0) {
|
||||
std::cout << "Out of border!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!this->ChessPieces[row][column].isEmpty()) {
|
||||
std::cout << "There already is a chess piece!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
this->ChessPieces[row][column].setColor(color);
|
||||
this->chessCount++;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ChessBoard::reset() {
|
||||
for (int i = 0; i < 15; i++) {
|
||||
for (int j = 0; j < 15; j++) {
|
||||
this->ChessPieces[i][j].reset();
|
||||
}
|
||||
}
|
||||
this->chessCount = 0;
|
||||
}
|
||||
|
||||
bool ChessBoard::isFull() {
|
||||
return this->chessCount == 15 * 15;
|
||||
}
|
||||
|
||||
bool ChessBoard::checkWinner(int row, int column, bool color) {
|
||||
int connetCount = 0;
|
||||
int i = row, j = column;
|
||||
int possiblePieces[9][2] = {0};
|
||||
|
||||
// vertical
|
||||
for (; i < 15; i++) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i = row - 1; i >= 0; i--) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// horizontal
|
||||
i = row;
|
||||
j = column;
|
||||
connetCount = 0;
|
||||
for (; j < 15; j++) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (j = column - 1; j >= 0; j--) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// left-up
|
||||
i = row;
|
||||
j = column;
|
||||
connetCount = 0;
|
||||
while (i < 15 && j < 15) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
i = row - 1;
|
||||
j = column - 1;
|
||||
while (i >= 0 && j >= 0) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
i--;
|
||||
j--;
|
||||
}
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// right-up
|
||||
i = row;
|
||||
j = column;
|
||||
connetCount = 0;
|
||||
while (i >= 0 && j < 15) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
i--;
|
||||
j++;
|
||||
}
|
||||
i = row + 1;
|
||||
j = column - 1;
|
||||
while (i < 15 && j >= 0) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
45
OOP/FiveInARow/chessPiece.cpp
Normal file
45
OOP/FiveInARow/chessPiece.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "fiveInARow.h"
|
||||
|
||||
ChessPiece::ChessPiece() {
|
||||
this->isEmptyStatus = true;
|
||||
this->isWinStatus = false;
|
||||
this->color = BLACK;
|
||||
}
|
||||
|
||||
ChessPiece::~ChessPiece() {
|
||||
}
|
||||
|
||||
void ChessPiece::setColor(bool newColor) {
|
||||
this->isEmptyStatus = false;
|
||||
this->color = newColor;
|
||||
}
|
||||
|
||||
void ChessPiece::setWin() {
|
||||
this->isWinStatus = true;
|
||||
}
|
||||
|
||||
void ChessPiece::reset() {
|
||||
this->isEmptyStatus = true;
|
||||
this->isWinStatus = false;
|
||||
this->color = BLACK;
|
||||
}
|
||||
|
||||
bool ChessPiece::isEmpty() {
|
||||
return this->isEmptyStatus;
|
||||
}
|
||||
|
||||
bool ChessPiece::getColor() {
|
||||
return this->color;
|
||||
}
|
||||
|
||||
std::string ChessPiece::show() {
|
||||
if (this->isWinStatus) {
|
||||
return "X";
|
||||
}
|
||||
if (this->color == BLACK) {
|
||||
return "⬤";
|
||||
}
|
||||
else {
|
||||
return "◯";
|
||||
}
|
||||
}
|
||||
64
OOP/FiveInARow/fiveInARow.h
Normal file
64
OOP/FiveInARow/fiveInARow.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <iostream>
|
||||
#define BLACK false
|
||||
#define WHITE true
|
||||
enum putChessPieceResult { failed, success, win, fullBoard };
|
||||
|
||||
class ChessPiece {
|
||||
private:
|
||||
bool color;
|
||||
bool isEmptyStatus;
|
||||
bool isWinStatus;
|
||||
|
||||
public:
|
||||
ChessPiece();
|
||||
~ChessPiece();
|
||||
|
||||
void setColor(bool newColor);
|
||||
void setWin();
|
||||
void reset();
|
||||
bool isEmpty();
|
||||
bool getColor();
|
||||
std::string show();
|
||||
};
|
||||
|
||||
class ChessBoard {
|
||||
private:
|
||||
ChessPiece ChessPieces[15][15];
|
||||
|
||||
int size;
|
||||
|
||||
int chessCount;
|
||||
|
||||
static char emptyBoardString[3][29][4];
|
||||
|
||||
char toHex(int);
|
||||
|
||||
void makeTitle();
|
||||
|
||||
public:
|
||||
ChessBoard();
|
||||
~ChessBoard();
|
||||
void show();
|
||||
|
||||
// Returns true if success, returns false if not
|
||||
bool setChess(bool color, int row, int column);
|
||||
void reset();
|
||||
bool isFull();
|
||||
bool checkWinner(int row, int column, bool color);
|
||||
};
|
||||
|
||||
class Player {
|
||||
private:
|
||||
ChessBoard *board;
|
||||
std::string playerName;
|
||||
bool chessType;
|
||||
|
||||
int toInt(char input);
|
||||
|
||||
public:
|
||||
Player(std::string name, bool type, ChessBoard *chessBoard);
|
||||
~Player();
|
||||
std::string getName();
|
||||
bool getColor();
|
||||
putChessPieceResult setChess(char row, char column);
|
||||
};
|
||||
@@ -1,400 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#define BLACK false
|
||||
#define WHITE true
|
||||
|
||||
// 还没有实现判定输赢
|
||||
// todo: 棋盘满时判定和棋
|
||||
|
||||
// clang-format off
|
||||
std::string boardTitle = " 1 2 3 4 5 6 7 8 9 A B C D E F";
|
||||
char emptyBoardString[3][29][4] = {
|
||||
{
|
||||
"┏", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┓"
|
||||
},
|
||||
{
|
||||
"┠", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┨"
|
||||
},
|
||||
{
|
||||
"┗", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┛",
|
||||
}
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
enum putChessPieceResult { failed, success, win, fullBoard };
|
||||
|
||||
class ChessPiece {
|
||||
private:
|
||||
bool color;
|
||||
bool isEmptyStatus;
|
||||
bool isWinStatus;
|
||||
|
||||
public:
|
||||
ChessPiece() {
|
||||
this->isEmptyStatus = true;
|
||||
this->isWinStatus = false;
|
||||
this->color = BLACK;
|
||||
}
|
||||
|
||||
void setColor(bool newColor) {
|
||||
this->isEmptyStatus = false;
|
||||
this->color = newColor;
|
||||
}
|
||||
|
||||
void setWin() {
|
||||
this->isWinStatus = true;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
this->isEmptyStatus = true;
|
||||
this->isWinStatus = false;
|
||||
this->color = BLACK;
|
||||
}
|
||||
|
||||
bool isEmpty() {
|
||||
return this->isEmptyStatus;
|
||||
}
|
||||
|
||||
bool getColor() {
|
||||
return this->color;
|
||||
}
|
||||
|
||||
std::string show() {
|
||||
if (this->isWinStatus) {
|
||||
return "X";
|
||||
}
|
||||
if (this->color == BLACK) {
|
||||
return "⬤";
|
||||
}
|
||||
else {
|
||||
return "◯";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class ChessBoard {
|
||||
private:
|
||||
ChessPiece ChessPieces[15][15];
|
||||
|
||||
int chessCount;
|
||||
|
||||
char toHex(int num) {
|
||||
if (num < 10) {
|
||||
return num + '0';
|
||||
}
|
||||
else {
|
||||
return num - 10 + 'A';
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
ChessBoard() {
|
||||
this->chessCount = 0;
|
||||
}
|
||||
|
||||
// void init() {
|
||||
// // for (int i = 0; i < 15; i++) {
|
||||
// // for (int j = 0; j < 15; j++) {
|
||||
// // this->ChessPieces[i][j].init();
|
||||
// // }
|
||||
// // }
|
||||
// this->chessCount = 0;
|
||||
// }
|
||||
|
||||
void show() {
|
||||
std::cout << boardTitle << std::endl;
|
||||
for (int i = 0; i < 15; i++) {
|
||||
std::cout << this->toHex(i + 1) << ' ';
|
||||
char(*emptyLineRef)[29][4] = NULL;
|
||||
if (i == 0) {
|
||||
emptyLineRef = &emptyBoardString[0];
|
||||
}
|
||||
else if (i == 14) {
|
||||
emptyLineRef = &emptyBoardString[2];
|
||||
}
|
||||
else {
|
||||
emptyLineRef = &emptyBoardString[1];
|
||||
}
|
||||
int j = 0;
|
||||
for (; j < 15; j++) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
std::cout << (*emptyLineRef)[j * 2];
|
||||
}
|
||||
else {
|
||||
std::cout << this->ChessPieces[i][j].show();
|
||||
}
|
||||
if (j < 14) {
|
||||
std::cout << (*emptyLineRef)[j * 2 + 1];
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if success, returns false if not
|
||||
bool setChess(bool color, int row, int column) {
|
||||
if (row > 14 || row < 0 || column > 14 || column < 0) {
|
||||
std::cout << "Out of border!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!this->ChessPieces[row][column].isEmpty()) {
|
||||
std::cout << "There already is a chess piece!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
this->ChessPieces[row][column].setColor(color);
|
||||
this->chessCount++;
|
||||
return true;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
for (int i = 0; i < 15; i++) {
|
||||
for (int j = 0; j < 15; j++) {
|
||||
this->ChessPieces[i][j].reset();
|
||||
}
|
||||
}
|
||||
this->chessCount = 0;
|
||||
}
|
||||
|
||||
bool isFull() {
|
||||
return this->chessCount == 15 * 15;
|
||||
}
|
||||
|
||||
bool checkWinner(int row, int column, bool color) {
|
||||
int connetCount = 0;
|
||||
int i = row, j = column;
|
||||
int possiblePieces[9][2] = {0};
|
||||
|
||||
// vertical
|
||||
for (; i < 15; i++) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i = row - 1; i >= 0; i--) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// horizontal
|
||||
i = row;
|
||||
j = column;
|
||||
connetCount = 0;
|
||||
for (; j < 15; j++) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (j = column - 1; j >= 0; j--) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// left-up
|
||||
i = row;
|
||||
j = column;
|
||||
connetCount = 0;
|
||||
while (i < 15 && j < 15) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
i = row - 1;
|
||||
j = column - 1;
|
||||
while (i >= 0 && j >= 0) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
i--;
|
||||
j--;
|
||||
}
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// right-up
|
||||
i = row;
|
||||
j = column;
|
||||
connetCount = 0;
|
||||
while (i >= 0 && j < 15) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
i--;
|
||||
j++;
|
||||
}
|
||||
i = row + 1;
|
||||
j = column - 1;
|
||||
while (i < 15 && j >= 0) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[connetCount][0] = i;
|
||||
possiblePieces[connetCount][1] = j;
|
||||
connetCount++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class Player {
|
||||
private:
|
||||
ChessBoard *board;
|
||||
std::string playerName;
|
||||
bool chessType;
|
||||
|
||||
int toInt(char input) {
|
||||
if ('1' <= input && input <= '9') {
|
||||
return input - '1';
|
||||
}
|
||||
else if ('A' <= input && input <= 'F') {
|
||||
return input - 'A' + 9;
|
||||
}
|
||||
else if ('a' <= input && input <= 'f') {
|
||||
return input - 'a' + 9;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
Player(std::string name, bool type, ChessBoard *chessBoard) {
|
||||
this->playerName = name;
|
||||
this->chessType = type;
|
||||
this->board = chessBoard;
|
||||
}
|
||||
|
||||
std::string getName() {
|
||||
return this->playerName;
|
||||
}
|
||||
|
||||
bool getColor() {
|
||||
return this->chessType;
|
||||
}
|
||||
|
||||
putChessPieceResult setChess(char row, char column) {
|
||||
int rowCount, columnCount;
|
||||
rowCount = toInt(row);
|
||||
columnCount = toInt(column);
|
||||
if (!this->board->setChess(chessType, rowCount, columnCount)) {
|
||||
return failed;
|
||||
}
|
||||
if (this->board->checkWinner(rowCount, columnCount, this->chessType)) {
|
||||
std::cout << "\n####################\n"
|
||||
<< this->playerName << " wins!" << std::endl
|
||||
<< "####################\n";
|
||||
return win;
|
||||
}
|
||||
if (this->board->isFull()) {
|
||||
std::cout << "Board is full. Tie!" << std::endl;
|
||||
return fullBoard;
|
||||
}
|
||||
else {
|
||||
return success;
|
||||
}
|
||||
}
|
||||
};
|
||||
#include "fiveInARow.h"
|
||||
|
||||
int main() {
|
||||
ChessBoard aBoard;
|
||||
|
||||
55
OOP/FiveInARow/player.cpp
Normal file
55
OOP/FiveInARow/player.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "fiveInARow.h"
|
||||
|
||||
int Player::toInt(char input) {
|
||||
if ('1' <= input && input <= '9') {
|
||||
return input - '1';
|
||||
}
|
||||
else if ('A' <= input && input <= 'F') {
|
||||
return input - 'A' + 9;
|
||||
}
|
||||
else if ('a' <= input && input <= 'f') {
|
||||
return input - 'a' + 9;
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
Player::Player(std::string name, bool type, ChessBoard *chessBoard) {
|
||||
this->playerName = name;
|
||||
this->chessType = type;
|
||||
this->board = chessBoard;
|
||||
}
|
||||
|
||||
Player::~Player() {
|
||||
}
|
||||
|
||||
std::string Player::getName() {
|
||||
return this->playerName;
|
||||
}
|
||||
|
||||
bool Player::getColor() {
|
||||
return this->chessType;
|
||||
}
|
||||
|
||||
putChessPieceResult Player::setChess(char row, char column) {
|
||||
int rowCount, columnCount;
|
||||
rowCount = toInt(row);
|
||||
columnCount = toInt(column);
|
||||
if (!this->board->setChess(chessType, rowCount, columnCount)) {
|
||||
return failed;
|
||||
}
|
||||
if (this->board->checkWinner(rowCount, columnCount, this->chessType)) {
|
||||
std::cout << "\n####################\n"
|
||||
<< this->playerName << " wins!" << std::endl
|
||||
<< "####################\n";
|
||||
return win;
|
||||
}
|
||||
if (this->board->isFull()) {
|
||||
std::cout << "Board is full. Tie!" << std::endl;
|
||||
return fullBoard;
|
||||
}
|
||||
else {
|
||||
return success;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user