Dear All,
I'm having trouble getting my c++ code to work (below). Any suggestions as to what could be the problem? I think it has to do with the while loop, since the program ends before command[0] is a 'Q'.
Thank you in advance.
#include <iostream>
#define MAX 8
using namespace std;
bool validCoord(char col, int row);
void processCoord(string command,char player,string board[MAX][MAX]);
int translateColumn(char letter);
void printboard(string board[MAX][MAX]);
int main(int argc, char *argv[])
{
std::cout << "************************************" << std::endl;
std::cout << "* Welcome to Reversi (Othello) *" << std::endl;
std::cout << "************************************" << std::endl;
std::cout << "" << std::endl;
std::cout << "Player 1 will be X and Player 2 will be O" << std::endl;
std::cout << "Enter ? for a list of commands" << std::endl;
std::cout << "c333333333333333333333333333333333333333333333333 33333333333cc" << std::endl;
std::string command;
std::cin >> command;
std::cout << command << std::endl;
int rows = 8;
int cols = 8;
string board[8][8] = {0};
char player = 'X';
while (command[0] != 'Q') {
if (command[0] == '?') {
std::cout << "? - display list of commands" << std::endl;
std::cout << "<Col><Row> - move to given coordinates" << std::endl;
std::cout << " where <Col> is a column letter (a-h)" << std::endl;
std::cout << " and <Row> is a row number (1-8)" << std::endl;
std::cout << "N - play new game" << std::endl;
std::cout << "P - display board" << std::endl;
std::cout << "Q - quit game" << std::endl;
}
if (command[0] == 'P')
printboard(board);
if (validCoord(command[0],command[1]))
processCoord(command, player, board);
std::cin >> command;
if (player == 'X')
player = 'O';
else
player = 'X';
std::cin >> command;
}
std::cout << "Enter ? for a list of commands" << std::endl;
std::cout << "c333333333333333333333333333333333333333333333333 33333333333cc" << std::endl;
std::string command2;
std::cin >> command2;
std::cout << command2 << std::endl;
return 0;
}
bool validCoord(char col, int row)
{
if (col == 'a' || col == 'b' || col == 'c' || col == 'd' || col == 'e' || col == 'f' || col == 'g' || col == 'h' || col == 'A' || col == 'B' || col == 'C' || col == 'D' || col == 'E' || col == 'F' || col == 'G' || col == 'H') {
if (row == '1' || row == '2' || row == '3' || row == '4' || row == '5' || row == '6' || row == '7' || row == '8')
return true;
else
cout << "Invalid Coordinates (" << col << "," << row << ")." << std::endl;
return false;
}
else
cout << "Invalid Coordinates (" << col << "," << row << ")." << std::endl;
return false;
}
void processCoord(string command,char player, string board[MAX][MAX]) {
if (board[translateColumn(command[0])][command[1]] != "0")
std::cout << "Position (" << command[0] << "," << command[1] << ") is occupied." << std::endl;
else {
if (player == 'X')
board[translateColumn(command[0])][command[1]] = 'X';
else
board[translateColumn(command[0])][command[1]] = 'O';
}
}
int translateColumn(char letter)
{
int col;
if (letter == 'a' || letter == 'A')
col = 1;
if (letter == 'b' || letter == 'B')
col = 2;
if (letter == 'c' || letter == 'C')
col = 3;
if (letter == 'd' || letter == 'D')
col = 4;
if (letter == 'e' || letter == 'E')
col = 5;
if (letter == 'f' || letter == 'F')
col = 6;
if (letter == 'g' || letter == 'G')
col = 7;
if (letter == 'h' || letter == 'H')
col = 8;
return col;
}
void printboard(string board[MAX][MAX])
{
int t;
int w;
cout << " +---+---+---+---+---+---+---+---+" << endl;
for (t = 0;t < MAX; t++)
cout << " " << 8 - t << " " << endl;
for (w = 0;w < MAX; w++)
cout << board[w][8 - t] << " | " << endl;
cout << " +---+---+---+---+---+---+---+---+" << endl;
cout << " a b c d e f g h " << endl;
}