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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include "game_model.h"
#include <iostream>
#define MAX_NAME_LENGTH 18
#define FOUR_IN_A_ROW 4
using std::getline;
using std::cin;
GameModel::GameModel()
: table_
{ {{ ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ' },
{ ' ', ' ', ' ', ' ', ' ', ' ' }} }
{
}
array<array<char, COLS>, ROWS> const & GameModel::getStoneTable()
{
return table_;
}
std::string const & GameModel::getCurrentPlayerName()
{
return player_[player_turn_];
}
int GameModel::addStone(int col, char stone)
{
for (int row{ ROWS - 1 }; row >= 0; --row)
if (table_[row][col] == ' ') {
table_[row][col] = stone;
return row;
}
return -1;
}
bool GameModel::setPlayerName(int player_num)
{
std::string name;
getline(cin, name);
if (name.size() > MAX_NAME_LENGTH || name == "")
return false;
else if (player_[0] == name)
return false;
player_[player_num] = name;
return true;
}
bool GameModel::hasWon(int row, char stone)
{
int same_stones{};
for (int col{}; col < COLS; ++col) {
if (table_[row][col] == stone) {
if (++same_stones == FOUR_IN_A_ROW)
return true;
}
else
same_stones = 0;
}
return false;
}
bool GameModel::getCurrentPlayerTurn()
{
return player_turn_;
}
void GameModel::nextPlayerTurn()
{
player_turn_ ? player_turn_ = false : player_turn_ = true;
}
bool GameModel::checkForTie()
{
for (int row{}; row < ROWS; ++row)
for (int col{}; col < COLS; ++col)
if (table_[row][col] == ' ')
return false;
return true;
}
| |