How to update gameboard and player turns...

Ok I really need help on this. Im a beginner in C++. I have been assigned the task of creating a gameboard that has 3 columns and 5 rows. Each row consist of 1 2 3. We were told not to use arrays cause that may make it harder. Ive already begun but now im stuck. I need help on keeping player choices on the gameboard and alternating player turns. Any advice would help!!!!


[code=cpp]
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

//function prototypes
void displayRow();


string numSpace = " ";
string num1 = "1";
string num2 = "2";
string num3 = "3";

int main ()
{


//declare variables

string player1 = "";
string player2 = "";


int x = 0;
int y = 0;

//Enter player names

cout << "Enter the first player's name: ";
cin >> player1;

cout << "Enter the second player's name: ";
cin >> player2;

// clear the Command Prompt Screen
system("cls");

//display gameboard
for (int i = 1; i<=5; i++)
displayRow();
//end for



//Player 1 selects column

cout << player1 << ", enter the column to take from: ";
cin >> y;


while ( y <=0 || y >=4)
{
cout << y << " is not a valid choice, try again" << endl;
cin >> y;
}
//end while


// Player 1 selects number to take

cout << "Enter the number to take: ";
cin >> x;

while ( x <=0 || x >=6)
{
cout << x << " is not a valid choice, try again" << endl;
cin >> x;
}
//end while



system ("cls");

// The Game Board is updated based on the players selections

if (y == 1)
{
switch (x)
{
case 1:
cout <<" "<<numSpace<<num2<<numSpace<<num3<<endl;
for (int i = 1; i<=4; i++)
displayRow();
break;

case 2:
for (int i = 1; i<=2; i++)
cout <<" "<<numSpace<<num2<<numSpace<<num3<<endl;
for (int i = 1; i<=3; i++)
displayRow();
break;

case 3:
for (int i = 1; i<=3; i++)
cout <<" "<<numSpace<<num2<<numSpace<<num3<<endl;
for (int i = 1; i<=2; i++)
displayRow();
break;

case 4:
for (int i = 1; i<=4; i++)
cout <<" "<<numSpace<<num2<<numSpace<<num3<<endl;
for (int i = 1; i<=1; i++)
displayRow();
break;

case 5:
for (int i = 1; i<=5; i++)
cout <<" "<<numSpace<<num2<<numSpace<<num3<<endl;
break;

} // end switch
}//end if

if (y == 2)
{
switch (x)
{
case 1:
cout <<num1<<numSpace<<" "<<numSpace<<num3<<endl;
for (int i = 1; i<=4; i++)
displayRow();
break;

case 2:
for (int i = 1; i<=2; i++)
cout <<num1<<numSpace<<" "<<numSpace<<num3<<endl;
for (int i = 1; i<=3; i++)
displayRow();
break;

case 3:
for (int i = 1; i<=3; i++)
cout <<num1<<numSpace<<" "<<numSpace<<num3<<endl;
for (int i = 1; i<=2; i++)
displayRow();
break;

case 4:
for (int i = 1; i<=4; i++)
cout <<num1<<numSpace<<" "<<numSpace<<num3<<endl;
for (int i = 1; i<=1; i++)
displayRow();
break;

case 5:
for (int i = 1; i<=3; i++)
displayRow();
cout <<num1<<numSpace<<" "<<numSpace<<num3<<endl;
break;
} // end switch
}//end if

if (y == 3)
{
switch (x)
{
case 1:
cout <<num1<<numSpace<<num2<<numSpace<<" "<<endl;
for (int i = 1; i<=4; i++)
displayRow();
break;

case 2:
for (int i = 1; i<=2; i++)
cout <<num1<<numSpace<<num2<<numSpace<<" "<<endl;
for (int i = 1; i<=3; i++)
displayRow();
break;

case 3:
for (int i = 1; i<=3; i++)
cout <<num1<<numSpace<<num2<<numSpace<<" "<<endl;
for (int i = 1; i<=2; i++)
displayRow();
break;

case 4:
for (int i = 1; i<=4; i++)
cout <<num1<<numSpace<<num2<<numSpace<<" "<<endl;
displayRow();
break;

case 5:
for (int i = 1; i<=5; i++)
cout <<num1<<numSpace<<num2<<numSpace<<" "<<endl;
break;
} // end switch
}//end if



//Player 2 selects column

cout << player2 << ", enter the column to take from: ";
cin >> y;


while ( y <=0 || y >=4)
{
cout << y << " is not a valid choice, try again" << endl;
cin >> y;
}
//end while


// Player 2 selects number to take

cout << "Enter the number to take: ";
cin >> x;

while ( x <=0 || x >=6)
{
cout << x << " is not a valid choice, try again" << endl;
cin >> x;
}
//end while


system("pause");
}// end of main function

void displayRow()
{
cout <<num1<<numSpace<<num2<<numSpace<<num3<<endl;

}//end of displayRow function

[/code]
Last edited on
I'm not sure why using arrays would make it harder for you, but with your problem it seems that to keep the choices on the board you will need each block to be a variable e.g.
1
2
3
int columnOne_RowOne;
int columnTwo_RowOne;
etc.


But that would be much longer than a simple array or group of arrays if you want to avoid a 2D array.
1
2
3
4
int rowOne[3];
int rowTwo[3];
int rowThree[3];
etc.


For your switching players issue you could use a loop to bring them all under one segment or put players turns into their own separate methods:
1
2
3
4
5
6
7
8
9
int main() {

   // Variables and all the rest

   while (gameNotEnded()) {
      PlayerOne();
      PlayerTwo();
   }
}


This is just the way I would start off approaching the problem, not the perfect way.
Topic archived. No new replies allowed.