Skip to content

Instantly share code, notes, and snippets.

@sethprog26
Created May 6, 2021 21:27
Show Gist options
  • Save sethprog26/ffc7749032aee96b165348b609d37944 to your computer and use it in GitHub Desktop.
Save sethprog26/ffc7749032aee96b165348b609d37944 to your computer and use it in GitHub Desktop.
Project From Ademitan Akinlade-Fajemirokun
#include <iostream>
#include <string>
using namespace std;
void printRules() {
cout << "Rules:\n"
<< " * The player who has three horizontally adjacent pieces, loses\n"
<< " * The player who has three vertically adjacent pieces, loses\n"
<< " * The player who has three diagonally adjacent pieces, loses\n"
<< " * If all the cells have been filled, it's a tie" << endl;
}
class Board
{
public:
static const int playersnum = 2;
char players[playersnum] =
{
'o', 'x'
};
int currentPlayer= 0;
static const int rows = 5;
static const int columns = 5;
char cell[rows*columns];
Board()
{
for (int i=0; i < rows*columns; i++)
cell[i] = ' ';
}
void print ()
{
for (int i=0; i < columns; i++)
{
cout << "--";
}
cout << endl;
for (int i=0; i < rows; i++)
{
for (int j=0; j < columns; j++)
{
cout << cell[j+i*columns];
cout << '|';
}
cout << endl;
for (int j=0; j < columns; j++) {
cout << "--";
}
cout << endl;
}
}
void cellalloc (int row, int column)
{
cell[column+row*columns] = players[currentPlayer];
}
void movePlayer ()
{
currentPlayer = (currentPlayer+1)%playersnum;
cout << "Moved on to player " << players[currentPlayer] << endl;
}
bool checkCells ()
{
for (int i=0; i < rows; i++)
{
for (int j=0; j < columns; j++)
{
if (cell[j+i*columns] == ' ')
{
return false;
}
}
}
return true;
}
char getCell (int row, int column)
{
return cell[column+row*columns];
}
bool checklostPlayer ()
{
char currentChar = players[currentPlayer];
cout << "Checking for player character " << currentChar << endl;
for (int i=0; i < rows; i++)
{
for (int j = 1; j < columns - 1; j++)
{
if (getCell(i, j) == currentChar && getCell(i, j-1) == currentChar && getCell(i, j+1) == currentChar)
{
return true;
}
}
}
for (int i = 1; i < rows-1; i++) {
for (int j = 0; j < columns; j++) {
if (getCell(i, j) == currentChar && getCell(i-1, j) == currentChar && getCell(i+1, j) == currentChar)
{
return true;
}
}
}
for (int i=1; i < rows-1; i++)
{
for (int j=1; j < columns-1; j++)
{
if (getCell(i, j) == currentChar && getCell(i-1, j-1) == currentChar && getCell(i+1, j+1) == currentChar)
{
return true;
}
if (getCell(i, j) == currentChar && getCell(i-1, j+1) == currentChar && getCell(i+1, j-1) == currentChar)
{
return true;
}
}
}
}
};
int main()
{
Board BoardBuild;
printRules();
BoardBuild.print();
while (true)
{
cout << "Your move now" << endl;
cout << "[place] [restart] [rules] [quit]" << endl;
string command;
cin >> command;
if (command == "restart") {
BoardBuild = Board();
continue;
} else if (command == "rules") {
printRules();
continue;
} else if (command == "quit") {
break;
} else if (command == "place") {
int row;
int column;
cin >> row;
if (row < 0 || row >= BoardBuild.rows || cin.fail()) {
cout << "Invalid row" << endl;
cin.clear();
continue;
}
cin >> column;
if (column < 0 || column >= BoardBuild.columns || cin.fail()) {
cout << "Invalid column" << endl;
cin.clear();
continue;
}
BoardBuild.cellalloc(row, column);
BoardBuild.print();
if (BoardBuild.checklostPlayer())
{
cout << "You've lost, go home" << endl << BoardBuild.currentPlayer;
break;
}
if (BoardBuild.checkCells())
{
cout << "Tie Game" << endl;
break;
}
BoardBuild.movePlayer();
} else {
cout << "Unknown command" << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment