#include <iostream>
#include <string>
#include "BoardGame.h"
using namespace std;
// Main function.
int main()
{
// Define instance of BoardGame class.
BoardGame chess;
string board[8][8];
int row, col, move=1;
// Accept user input for initial position.
cout << "Enter your current position on the board (ROW#, COLUMN#): ";
cin >> row >> col;
chess.setcurrentRow(row);
chess.setcurrentCol(col);
// Loop user's move.
while(move > 0 && move < 9)
{
chess.getcurrentRow();
chess.getcurrentCol();
chess.legalMoves(board);
//Accept user's next move.
cout <<endl<<"Make your next Move!"<<endl;
cout<<endl<<"Enter a legal move from above: ";
cin>>move;
cout<<endl;
chess.playersChoice(move);
}
// Restart the game.
while (move == 9)
{
cout << "Enter your current position on the board (ROW#, COLUMN#): ";
cin >> row >> col;
chess.setcurrentRow(row);
chess.setcurrentCol(col);
move=1;
while(move > 0 && move < 9)
{
chess.getcurrentRow();
chess.getcurrentCol();
chess.legalMoves(board);
cout <<endl<<"Make your next Move!"<<endl;
cout<<endl<<"Enter a legal move from above: ";
cin>>move;
cout<<endl;
chess.playersChoice(move);
}
// Exit the game.
if (move == 0)
exit(0);
}
return 0;
}
#ifndef BOARDGAME_H
#define BOARDGAME_H
class BoardGame
{
private:
static const int ROWS=8;
static const int COLS=8;
int ROW;
int COL;
public:
void legalMoves(std::string [][8]);
void setcurrentRow(int);
void setcurrentCol(int);
void playersChoice(int);
int getcurrentRow()
{return ROW;}
int getcurrentCol()
{return COL;}
};
#endif
#include <iostream>
#include <string>
#include "BoardGame.h"
using namespace std;
void BoardGame::legalMoves(string arr[][8])
{
//Initialize array with empty spaces
for (int r = 0; r < ROWS; r++)
{
for (int c = 0; c < COLS; c++)
{
arr[r][c] = "[ ]";
}}
// Initialize current position with an 'X'.
cout << "Your current position on the board is [" << getcurrentRow() <<
"][" << getcurrentCol() << "] \n"<<endl;
arr[getcurrentRow()][getcurrentCol()] = "[X]";
// Show the current position on the board.
cout << " 0 1 2 3 4 5 6 7\n";
for (int r = 0; r < ROWS; r++)
{
cout << r;
for (int c = 0; c < COLS; c++)
{
cout << arr[r][c];
}
cout << endl;
}
cout<<endl;
int row, col;
// Move to legal move 1.
row = getcurrentRow() - 2; col = getcurrentCol() + 1;
if (row >=0 && row < 8 && col >=0 && col < 8)
{
cout << "Enter 1 for [" << row << "][" << col << "] or \n";
arr[row][col] = "[1]";
}
// Move to legal move 2.
row = getcurrentRow() - 1; col = getcurrentCol() + 2;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "Enter 2 for [" << row << "][" << col << "] or \n";
arr[row][col] = "[2]";
}
// Move to legal move 3.
row = getcurrentRow() + 1; col = getcurrentCol() + 2;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "Enter 3 for [" << row << "][" << col << "] or \n";
arr[row][col] = "[3]";
}
// Move to legal move 4.
row = getcurrentRow() + 2; col = getcurrentCol() + 1;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "Enter 4 for [" << row << "][" << col << "] or \n";
arr[row][col] = "[4]";
}
// Move to legal move 5.
row = getcurrentRow() + 2; col = getcurrentCol() - 1;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "Enter 5 for [" << row << "][" << col << "] or \n";
arr[row][col] = "[5]";
}
// Move to legal move 6.
row = getcurrentRow() + 1; col = getcurrentCol() - 2;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "Enter 6 for [" << row << "][" << col << "] or \n";
arr[row][col] = "[6]";
}
// Move to legal move 7.
row = getcurrentRow() - 1; col = getcurrentCol() - 2;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "Enter 7 for [" << row << "][" << col << "] or \n";
arr[row][col] = "[7]";
}
// Move to legal move 8.
row = getcurrentRow() - 2; col = getcurrentCol() - 1;
if (row >= 0 && row < 8 && col >= 0 && col < 8)
{
cout << "Enter 8 for [" << row << "][" << col << "] or \n";
arr[row][col] = "[8]";
}
// Option to restart or exit game.
cout<<"Enter 9 to return to start or"<<endl;
cout<<"Enter 0 to exit the game\n"<<endl;
// Show the board with all legal moves from the current position.
cout << " 0 1 2 3 4 5 6 7\n";
for (int r = 0; r < ROWS; r++)
{
cout << r;
for (int c = 0; c < COLS; c++)
{
cout << arr[r][c];
}
cout << endl;
}
}
// Member function to set current row.
void BoardGame::setcurrentRow(int R)
{
ROW=R;
}
// Member function to set current column.
void BoardGame::setcurrentCol(int C)
{
COL=C;
}
// Member function to accept user's next move.
void BoardGame::playersChoice(int M)
{
// Switch user's move input to position selected.
switch (M)
{
case 1: ROW = getcurrentRow() - 2;
COL = getcurrentCol() + 1;
break;
case 2: ROW = getcurrentRow() - 1;
COL = getcurrentCol() + 2;
break;
case 3: ROW = getcurrentRow() + 1;
COL = getcurrentCol() + 2;
break;
case 4: ROW = getcurrentRow() + 2;
COL = getcurrentCol() + 1;
break;
case 5: ROW = getcurrentRow() + 2;
COL = getcurrentCol() - 1;
break;
case 6: ROW = getcurrentRow() + 1;
COL = getcurrentCol() - 2;
break;
case 7: ROW = getcurrentRow() - 1;
COL = getcurrentCol() - 2;
break;
case 8: ROW = getcurrentRow() - 2;
COL = getcurrentCol() - 1;
break;
}
// Set NEW row and column.
setcurrentRow(ROW);
setcurrentCol(COL);
}