更好的输入函数。

This commit is contained in:
unlockable
2023-03-01 18:58:38 +08:00
parent 6070fbfd57
commit ea34250e91
3 changed files with 62 additions and 27 deletions

View File

@@ -36,6 +36,11 @@ ChessBoard::ChessBoard() {
this->size = 15;
}
ChessBoard::ChessBoard(int boardSize) {
this->chessCount = 0;
this->size = boardSize;
}
ChessBoard::~ChessBoard() {
}

View File

@@ -37,6 +37,7 @@ private:
public:
ChessBoard();
ChessBoard(int boardSize);
~ChessBoard();
void show();
@@ -45,6 +46,7 @@ public:
void reset();
bool isFull();
bool checkWinner(int row, int column, bool color);
int getSize();
};
class Player {

View File

@@ -1,75 +1,103 @@
#include "fiveInARow.h"
#include <iostream>
#include <string>
#include "fiveInARow.h"
int main() {
ChessBoard aBoard;
aBoard.show();
Player* players[2];
ChessBoard *aBoard;
std::cout << std::numeric_limits<std::streamsize>::max() << std::endl;
{
int boardSize = 0;
while (true) {
std::cout << "Please enter the size of the board (5-15): ";
std::cin >> boardSize;
if (std::cin.fail() || boardSize < 5 || boardSize > 15) {
std::cout << "Invalid input, try again." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
}
else {
break;
}
}
aBoard = new ChessBoard(boardSize);
}
Player *players[2];
{
std::string temp = "";
std::cout << "Player 1 name: ";
std::cin >> temp;
players[0] = new Player(temp, BLACK, &aBoard);
players[0] = new Player(temp, BLACK, aBoard);
std::cout << "Player 2 name: ";
std::cin >> temp;
players[1] = new Player(temp, WHITE, &aBoard);
players[1] = new Player(temp, WHITE, aBoard);
}
aBoard->show();
putChessPieceResult result;
bool nowMovePlayerIndex = false;
do {
std::cout << players[nowMovePlayerIndex]->getName() << " ["
<< (players[nowMovePlayerIndex]->getColor() ? "◯White"
: "⬤Black")
: "⬤Black")
<< "]: "
<< "Please enter the coordinate of your new chess piece."
<< std::endl;
char row, column;
std::cout << "Row No. ";
std::cin >> row;
fflush(stdin);
while (!(('1' <= row && row <= '9') || ('A' <= row && row <= 'F') ||
('a' <= row && row <= 'f'))) {
std::cout << "Not a valid row number. Try again." << std::endl;
while (true) {
std::cout << "Row No. ";
std::cin >> row;
fflush(stdin);
if (std::cin.fail() ||
(!(('1' <= row && row <= '9') || ('A' <= row && row <= 'F') ||
('a' <= row && row <= 'f')))) {
std::cout << "Invalid input, try again." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
}
else {
break;
}
}
std::cout << "Column No. ";
std::cin >> column;
fflush(stdin);
while (!(('1' <= row && row <= '9') || ('A' <= row) && (row <= 'F') ||
('a' <= row && row <= 'f'))) {
std::cout << "Not a valid column number. Try again." << std::endl;
while (true) {
std::cout << "Column No. ";
std::cin >> column;
fflush(stdin);
if (std::cin.fail() ||
(!(('1' <= row && row <= '9') || ('A' <= row && row <= 'F') ||
('a' <= row && row <= 'f')))) {
std::cout << "Invalid input, try again." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
}
else {
break;
}
}
result = players[nowMovePlayerIndex]->setChess(row, column);
if (result == failed) {
continue;
}
else if (result == success) {
nowMovePlayerIndex ^= 1;
aBoard.show();
aBoard->show();
continue;
}
else {
aBoard.show();
aBoard->show();
char input;
do {
std::cout << "Reset for another round? (y/n) ";
input = getchar();
fflush(stdin);
} while (input != 'y' && input != 'Y' && input != 'n' &&
input != 'N');
if (input == 'y' || input == 'Y') {
aBoard.reset();
aBoard.show();
aBoard->reset();
aBoard->show();
continue;
}
else {