i know it is a lot to read..... and i have different variables to compare win - the gameboard play itself and an undeclared plr, i did that because i'm not sure what and how it should be used there (so i expected those errors), b4 i added in the checkWin() the board displayed and user entered a col - n nothing else happnd....im in a very bad state... any suggestions as to what i should use or an example of what i can use with my version, wud be appreciated
PS~ im not THAT gud @ c++ so laymans terms with any explanations plzzzzzzzzzzz
thk you
and im gettin an expected primary expression before '[' token error :confused:
/***************************************************************************
** **
** Five In A Row **
** **
** **
** **
** By Shade Marshall **
** (102485@usc.edu.tt) **
** **
** April, 2010 **
** **
****************************************************************************
***************************************************************************/
#include <iostream>
#include <fstream>
using namespace std;
//Class declaration
class fiveInArow
{
private:
string reallyLikeMyTutors;
public:
string plr1;
string plr2;
int chip1;
int chip2;
int gameBoard[10][10];
int option;
int rows;
int cols;
fiveInArow();//class constructor
~fiveInArow();//destructor
void menu();// Menu Method
void playerOpt();// User Option to play game
void drawBoard();//draw board of play
bool chkChip();//
void gamePlay();// used to play game
void checkCol();//used to check availability of play in specified column
bool diagonalRightWin();// check for right diagonal wins
bool diagonalLeftWin();// check for left diagonal wins
bool horizontalWin(); // check for horizontal wins
bool verticalWin(); // check for vertical wins
bool checkWin(); // to call all win check functions/methods
};
//implementation section
fiveInArow::fiveInArow()
{
for(int rowCount = 0; rowCount < 10; rowCount++) // for loop to initialize gameboard rows
{
for (int colsCount=0; colsCount < 10; colsCount++) // nested for loop to initialize gameboard columns
{
gameBoard[rowCount][colsCount]='_'; // puts an underscore'_'
}
}
}//default constructor
fiveInArow::~fiveInArow()
{
}// destructor
void fiveInArow::menu() // Function to display Menu
{
do
{
system("color 1f"); // function which change the background and text color.
system("title Main Menu"); // function to display the Main Menu on the system window.
system("cls"); // Function used to clear the screen.
// Main Menu Display.
cout << "********************************************"<< endl;
cout << "***********WELCOME TO 5-In-A-Row************"<< endl;
cout << "********************************************"<< endl;
cout << "********** 1. Player1 vs Player2 *********"<< endl;
cout << "********** 2. Player vs Computer *********"<< endl;
cout << "********** 3. View Player Rankings *********"<< endl;
cout << "********** 4. Exit Game *********"<< endl;
cout << "********************************************"<< endl;
cout << "********************************************"<< endl;
cout << "Enter an Option from the Menu: " ; // Display Message
cin >> option; // User input
system("cls");
switch(option) // Start of the Switch fuction
{
case 1: // Player1 vs Player2
playerOpt(); // Player option method call
drawBoard();//drawBoard method called
gamePlay();// gridPlay method call
system("pause");
break;
case 2: // Player vs Computer
cout << "Not yet implemented" << endl;
break;
case 3: // View Player Rankings
cout << "Not yet implemented" << endl;
break;
case 4: // Exit Game
cout << "Have a Nice Day!! :D" <<endl;
option = 4;
break;
default:
cout <<"Select an Option from the Main Menu: " << endl;
break;
}
}while(option != 4);
}
void fiveInArow::playerOpt()//User Option to play game
{
cout << "Player1, please enter your name: ";
cin >> plr1;
plr1 = chip1;
cout << endl;
cout << "Player2, please enter your name: ";
cin >> plr2;
plr2 = chip2;
cout << endl;
cout << "\t \t " << plr1 << " \a vs \a " << plr2 << endl;
cout << endl;
}
void fiveInArow::drawBoard()//board display
{
for(int rowCount = 0; rowCount < 10; rowCount++) //for loop used to draw gameboard using rows count
{
for (int colsCount = 0; colsCount < 10 ;colsCount++) //for loop used to draw gameboard using columns count
{
cout << '|' << gameBoard[rowCount][colsCount]; // draws '|' before value in gameboard
}
cout << '|' << endl; // draws '|' afta value in gameboard
}
cout << '|' << '0' << '|' <<'1' << '|' << '2' <<
'|'<<'3'<<'|'<<'4'<<'|'<< '5' <<'|'<<'6'<<'|' <<'7'<< '|' <<'8' << '|' <<'9' << endl;
}
void fiveInArow::gamePlay()//GameBoard Grid display.
{
rows = 10; // used to set values to the rows of the gameBoard.
cols = 10; // used to set values to the cols of the gameBoard.
cout << " Your turn To play: " << plr1 << endl;
cout << " Select column of board to play: ";
cin >> option;
switch(option)
{
case 0: drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 1: drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 2: drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 3: drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 4: drawBoard();
system ("cls");//linux - clear
break;
case 5: drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 6: drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 7: drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 8: drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 9: drawBoard();
checkCol();
system ("cls");//linux - clear
break;
default: cout << "\a Select Option from Game Board: " << endl;
exit(1);
break;
}
cout << " Your turn To play: " << plr2 << endl;
cout << " Select column of board to play: ";
cin >> option;
switch(option)
{
case 0: cout << "Under Construction: col1" << endl;
drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 1: cout << "Under Construction: col1" << endl;
drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 2: cout << "Under Construction: col1" << endl;
drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 3: cout << "Under Construction: col1" << endl;
drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 4: cout << "Under Construction: col1" << endl;
drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 5: cout << "Under Construction: col1" << endl;
drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 6: cout << "Under Construction: col1" << endl;
drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 7: cout << "Under Construction: col1" << endl;
drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 8: cout << "Under Construction: col1" << endl;
drawBoard();
checkCol();
system ("cls");//linux - clear
break;
case 9: cout << "Under Construction: col1" << endl;
drawBoard();
checkCol();
system ("cls");//linux - clear
break;
default: cout << "\a Select Option from Game Board: " << endl;
exit(1);
break;
}
}
bool fiveInArow::chkChip()
{
if (chip1)
{
return true;
}
else
{
return false;
}
}
void fiveInArow::checkCol()
{
if (chkChip() == true ) // if it is plr1
while (gameBoard[rows][cols] != 0) //while choice is not 0 (empty)
{
gameBoard[rows --][cols]; // check same column, row above until choice is 0 (empty)
gameBoard[rows][cols] = 1; //then assign to 1 when choice is 0
}
else
if (chkChip() == false) // else if plr2 and choice is not 0
while (gameBoard[rows][cols] != 0) // while choice is not 0 (empty)
{
gameBoard[rows --][cols]; //check same column, row above until choie is 0
gameBoard[rows][cols] = 2; //then assign to 2 when choice is 0
}
}
bool fiveInArow::checkWin()
{
if ((diagonalRightWin() == true) ||
(diagonalLeftWin() == true) ||
(horizontalWin() == true) ||
(verticalWin() == true))
cout << " We Have A WINNER!!! " << endl;
cout << "CONGRATULATIONS :D " << endl;
}
bool fiveInArow::diagonalRightWin()
{
if ((gameBoard[rows][cols] == [rows-1][cols+1] ) && //why 'expected primary expressio before '[' token error'?
(gameBoard[rows][cols] == [rows - 2][cols + 2]) &&
(gameBoard[rows][cols] == [rows - 3][cols + 3]) &&
(gameBoard[rows][cols] == [rows - 4][cols + 4]) == true) ||
(gameBoard[rows][cols] == [rows + 1] && [cols - 1]) &&
(gameBoard[rows][cols] == [rows - 1] && [cols + 1]) &&
(gameBoard[rows][cols] == [rows - 2] && [cols + 2]) &&
(gameBoard[rows][cols] == [rows - 3] && [cols + 3]) ||
(plr == [rows + 2] && [cols - 2]) &&
(plr == [rows + 1] && [cols - 1]) &&
(plr == [rows - 1] && [cols + 1]) &&
(plr == [rows - 2] && [cols + 2]) ||
(plr == [rows + 3] && [cols - 3]) &&
(plr == [rows + 2] && [cols - 2]) &&
(plr == [rows + 1] && [cols - 1]) &&
(plr == [rows - 1] && [cols + 1]) ||
(plr == [rows + 4] && [cols - 4]) &&
(plr == [rows + 3] && [cols - 3]) &&
(plr == [rows + 2] && [cols - 2]) &&
(plr == [rows + 1] && [cols - 1]))
return true;
}
bool fiveInArow::diagonalLeftWin()
{
if (plr == [rows - 1] && [cols - 1]) &&
(plr == [rows - 2] && [cols - 2]) &&
(plr == [rows - 3] && [cols - 3]) &&
(plr == [rows - 4] && [cols - 4]) ||
(plr == [rows + 1] && [cols + 1] &&
(plr == [rows - 1] && [cols - 1]) &&
(plr == [rows - 2] && [cols - 2]) &&
(plr == [rows - 3] && [cols - 3]) ||
(plr == [rows + 2] && [cols + 2]) &&
(plr == [rows + 1] && [cols + 1]) &&
(plr == [rows - 1] && [cols - 1]) &&
(plr == [rows - 2] && [cols - 2]) ||
(plr == [rows + 3] && [cols + 3]) &&
(plr == [rows + 2] && [cols + 2]) &&
(plr == [rows + 1] && [cols + 1]) &&
(plr == [rows - 1] && [cols - 1]) ||
(plr == [rows + 4] && [cols + 4]) &&
(plr == [rows + 3] && [cols + 3]) &&
(plr == [rows + 2] && [cols + 2]) &&
(plr == [rows + 1] && [cols + 1])
return true;
}
bool fiveInArow::horizontalWin()
{
if (plr == [rows] && [cols + 1]) &&
(plr == [rows] && [cols + 2]) &&
(plr == [rows] && [cols + 3]) &&
(plr == [rows] && [cols + 4]) ||
(plr == [rows] && [cols - 1]) &&
(plr == [rows] && [cols + 1]) &&
(plr == [rows] && [cols + 2]) &&
(plr == [rows] && [cols + 3]) ||
(plr == [rows] && [cols - 1]) &&
(plr == [rows] && [cols - 2]) &&
(plr == [rows] && [cols + 1]) &&
(plr == [rows] && [cols + 2]) ||
(plr == [rows] && [cols] - 1) &&
(plr == [rows] && [cols] - 2) &&
(plr == [rows] && [cols] - 3) &&
(plr == [rows] && [cols] + 1) ||
(plr == [rows] && [cols - 1]) &&
(plr == [rows] && [cols - 2]) &&
(plr == [rows] && [cols - 3]) &&
(plr == [rows] && [cols - 4]) ||
return true;
}
bool fiveInArow::verticalWin()
{
if (plr == [rows - 1] && [cols]) &&
(plr == [rows - 2] && [cols]) &&
(plr == [rows - 3] && [cols]) &&
(plr == [rows - 4] && [cols]) ||
(plr == [rows + 1] && [cols]) &&
(plr == [rows - 1] && [cols]) &&
(plr == [rows - 2] && [cols]) &&
(plr == [rows - 3] && [cols]) ||
(plr == [rows + 1] && [cols]) &&
(plr == [rows + 2] && [cols]) &&
(plr == [rows - 1] && [cols]) &&
(plr == [rows - 2] && [cols]) ||
(plr == [rows + 1] && [cols]) &&
(plr == [rows + 2] && [cols]) &&
(plr == [rows + 3] && [cols]) &&
(plr == [rows - 1] && [cols]) ||
(plr == [rows + 1] && [cols]) &&
(plr == [rows + 2] && [cols]) &&
(plr == [rows + 3] && [cols]) &&
(plr == [rows + 4 ]&& [cols])
return true;
}
int main()
{
fiveInArow display;// create an object of the class
display.menu();//object call of menu method
display.drawBoard();// object call of drawBoard method
display.gamePlay();// call of gridPlay method to start playing
display.checkWin();//check for player win
system("pause");
return 0;
}