I have a game that consists of
- Four countries each has four soldiers
board that has 24 places to step on.
Each turn a random dice lands a solider from each team on a place(number) on the board according to the output of the dice.
After the turn ends the games display the current position of each soldier from each team.
I wonder if there is another easier way to get the output as shown in the linked image. The current routine is 2680 lines long.
If a soldier is on a space on the board, it prints the soldier. Otherwise it prints the space number.
bool PlayBo::ShowBo()
{
cout << " France";
cout << endl;
if (g1 == french && g2 != french && g3 != french && g4 != french)
{
cout << " g1";
cout << endl;
}
if (g1 == french && g2 == french && g3 != french && g4 != french)
{
cout << " g2g1";
cout << endl;
}
if (g1 == french && g2 != french && g3 == french && g4 != french)
{
cout << " g3g1";
cout << endl;
}
if (g1 == french && g2 != french && g3 != french && g4 == french)
{
cout << " g4g1";
cout << endl;
}
if (g1 == french && g2 == french && g3 == french && g4 != french)
{
cout << " g3g2g1";
cout << endl;
}
if (g1 == french && g2 != french && g3 == french && g4 == french)
{
cout << " g4g3g1";
cout << endl;
}
if (g1 == french && g2 == french && g3 != french && g4 == french)
{
cout << " g4g2g1";
cout << endl;
}
if (g1 == french && g2 == french && g3 == french && g4 == french)
{
cout << " g4g3g2g1";
cout << endl;
}
if (g1 != french && g2 == french && g3 != french && g4 != french)
{
cout << " g2";
cout << endl;
}
if (g1 != french && g2 == french && g3 == french && g4 != french)
{
cout << " g3g2";
cout << endl;
}
if (g1 != french && g2 == french && g3 != french && g4 == french)
{
cout << " g4g2";
cout << endl;
}
if (g1 != french && g2 == french && g3 == french && g4 == french)
{
cout << " g4g3g2";
cout << endl;
}
if (g1 != french && g2 != french && g3 == french && g4 != french)
{
cout << " g3";
cout << endl;
}
if (g1 != french && g2 != french && g3 == french && g4 == french)
{
cout << " g4g3";
cout << endl;
}
if (g1 != french && g2 != french && g3 != french && g4 == french)
{
cout << " g4";
cout << endl;
}
if (g1 != french && g2 != french && g3 != french && g4 != french)
{
cout << " ";
cout << endl;
}
//space 200
if (b1->value == 200)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 200)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 200)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 200)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 200)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 200)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 200)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 200)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 200)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 200)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 200)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 200)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 200)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 200)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 200)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 200)
{
cout << " r4";
cout << endl;
}
else
{
cout << " GS";
cout << endl;
}
//space 6
if (b1->value == 6)
{
cout << " b1";
}
else if (b2->value == 6)
{
cout << " b2";
}
else if (b3->value == 6)
{
cout << " b3";
}
else if (b4->value == 6)
{
cout << " b4";
}
else if (g1->value == 6)
{
cout << " g1";
}
else if (g2->value == 6)
{
cout << " g2";
}
else if (g3->value == 6)
{
cout << " g3";
}
else if (g4->value == 6)
{
cout << " g4";
}
else if (y1->value == 6)
{
cout << " y1";
}
else if (y2->value == 6)
{
cout << " y2";
}
else if (y3->value == 6)
{
cout << " y3";
}
else if (y4->value == 6)
{
cout << " y4";
}
else if (r1->value == 6)
{
cout << " r1";
}
else if (r2->value == 6)
{
cout << " r2";
}
else if (r3->value == 6)
{
cout << " r3";
}
else if (r4->value == 6)
{
cout << " r4";
}
else
{
cout << " 06";
}
//space 7
if (b1->value == 7)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 7)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 7)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 7)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 7)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 7)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 7)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 7)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 7)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 7)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 7)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 7)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 7)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 7)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 7)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 7)
{
cout << " r4";
cout << endl;
}
else
{
cout << " 07";
cout << endl;
}
//space 5
if (b1->value == 5)
{
cout << " b1";
}
else if (b2->value == 5)
{
cout << " b2";
}
else if (b3->value == 5)
{
cout << " b3";
}
else if (b4->value == 5)
{
cout << " b4";
}
else if (g1->value == 5)
{
cout << " g1";
}
else if (g2->value == 5)
{
cout << " g2";
}
else if (g3->value == 5)
{
cout << " g3";
}
else if (g4->value == 5)
{
cout << " g4";
}
else if (y1->value == 5)
{
cout << " y1";
}
else if (y2->value == 5)
{
cout << " y2";
}
else if (y3->value == 5)
{
cout << " y3";
}
else if (y4->value == 5)
{
cout << " y4";
}
else if (r1->value == 5)
{
cout << " r1";
}
else if (r2->value == 5)
{
cout << " r2";
}
else if (r3->value == 5)
{
cout << " r3";
}
else if (r4->value == 5)
{
cout << " r4";
}
else
{
cout << " 05";
}
//space 201
if (g1->value == 211)
{
cout << " g1";
}
else if (g2->value == 211)
{
cout << " g2";
}
else if (g3->value == 211)
{
cout << " g3";
}
else if (g4->value == 211)
{
cout << " g4";
}
else
{
cout << " G1";
}
//space 22
if (b1->value == 8)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 8)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 8)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 8)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 8)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 8)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 8)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 8)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 8)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 8)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 8)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 8)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 8)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 8)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 8)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 8)
{
cout << " r4";
cout << endl;
}
else
{
cout << " 08";
cout << endl;
}
//space 4
if (b1->value == 4)
{
cout << " b1";
}
else if (b2->value == 4)
{
cout << " b2";
}
else if (b3->value == 4)
{
cout << " b3";
}
else if (b4->value == 4)
{
cout << " b4";
}
else if (g1->value == 4)
{
cout << " g1";
}
else if (g2->value == 4)
{
cout << " g2";
}
else if (g3->value == 4)
{
cout << " g3";
}
else if (g4->value == 4)
{
cout << " g4";
}
else if (y1->value == 4)
{
cout << " y1";
}
else if (y2->value == 4)
{
cout << " y2";
}
else if (y3->value == 4)
{
cout << " y3";
}
else if (y4->value == 4)
{
cout << " y4";
}
else if (r1->value == 4)
{
cout << " r1";
}
else if (r2->value == 4)
{
cout << " r2";
}
else if (r3->value == 4)
{
cout << " r3";
}
else if (r4->value == 4)
{
cout << " r4";
}
else
{
cout << " 04";
}
//space 202
if (g1->value == 212)
{
cout << " g1";
}
else if (g2->value == 212)
{
cout << " g2";
}
else if (g3->value == 212)
{
cout << " g3";
}
else if (g4->value == 212)
{
cout << " g4";
}
else
{
cout << " G2";
}
//space 9
if (b1->value == 9)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 9)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 9)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 9)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 9)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 9)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 9)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 9)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 9)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 9)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 9)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 9)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 9)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 9)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 9)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 9)
{
cout << " r4";
cout << endl;
}
else
{
cout << " 09";
cout << endl;
}
//space 3
if (b1->value == 3)
{
cout << " b1";
}
else if (b2->value == 3)
{
cout << " b2";
}
else if (b3->value == 3)
{
cout << " b3";
}
else if (b4->value == 3)
{
cout << " b4";
}
else if (g1->value == 3)
{
cout << " g1";
}
else if (g2->value == 3)
{
cout << " g2";
}
else if (g3->value == 3)
{
cout << " g3";
}
else if (g4->value == 3)
{
cout << " g4";
}
else if (y1->value == 3)
{
cout << " y1";
}
else if (y2->value == 3)
{
cout << " y2";
}
else if (y3->value == 3)
{
cout << " y3";
}
else if (y4->value == 3)
{
cout << " y4";
}
else if (r1->value == 3)
{
cout << " r1";
}
else if (r2->value == 3)
{
cout << " r2";
}
else if (r3->value == 3)
{
cout << " r3";
}
else if (r4->value == 3)
{
cout << " r4";
}
else
{
cout << " 03";
}
//space 203
if (g1->value == 213)
{
cout << " g1";
}
else if (g2->value == 213)
{
cout << " g2";
}
else if (g3->value == 213)
{
cout << " g3";
}
else if (g4->value == 213)
{
cout << " g4";
}
else
{
cout << " G3";
}
//space 10
if (b1->value == 10)
{
cout << " b1";
}
else if (b2->value == 10)
{
cout << " b2";
}
else if (b3->value == 10)
{
cout << " b3";
}
else if (b4->value == 10)
{
cout << " b4";
}
else if (g1->value == 10)
{
cout << " g1";
}
else if (g2->value == 10)
{
cout << " g2";
}
else if (g3->value == 10)
{
cout << " g3";
}
else if (g4->value == 10)
{
cout << " g4";
}
else if (y1->value == 10)
{
cout << " y1";
}
else if (y2->value == 10)
{
cout << " y2";
}
else if (y3->value == 10)
{
cout << " y3";
}
else if (y4->value == 10)
{
cout << " y4";
}
else if (r1->value == 10)
{
cout << " r1";
}
else if (r2->value == 10)
{
cout << " r2";
}
else if (r3->value == 10)
{
cout << " r3";
}
else if (r4->value == 10)
{
cout << " r4";
}
else
{
cout << " 10";
}
cout << " ";
if (y3 == british)
{
cout << "y3";
}
else
{
cout << " ";
}
cout << endl;
//space 2
if (b1->value == 2)
{
cout << " b1";
}
else if (b2->value == 2)
{
cout << " b2";
}
else if (b3->value == 2)
{
cout << " b3";
}
else if (b4->value == 2)
{
cout << " b4";
}
else if (g1->value == 2)
{
cout << " g1";
}
else if (g2->value == 2)
{
cout << " g2";
}
else if (g3->value == 2)
{
cout << " g3";
}
else if (g4->value == 2)
{
cout << " g4";
}
else if (y1->value == 2)
{
cout << " y1";
}
else if (y2->value == 2)
{
cout << " y2";
}
else if (y3->value == 2)
{
cout << " y3";
}
else if (y4->value == 2)
{
cout << " y4";
}
else if (r1->value == 2)
{
cout << " r1";
}
else if (r2->value == 2)
{
cout << " r2";
}
else if (r3->value == 2)
{
cout << " r3";
}
else if (r4->value == 2)
{
cout << " r4";
}
else
{
cout << " 02";
}
//space 204
if (g1->value == 214)
{
cout << " g1";
}
else if (g2->value == 214)
{
cout << " g2";
}
else if (g3->value == 214)
{
cout << " g3";
}
else if (g4->value == 214)
{
cout << " g4";
}
else
{
cout << " G4";
}
//space 11
if (b1->value == 11)
{
cout << " b1";
}
else if (b2->value == 11)
{
cout << " b2";
}
else if (b3->value == 11)
{
cout << " b3";
}
else if (b4->value == 11)
{
cout << " b4";
}
else if (g1->value == 11)
{
cout << " g1";
}
else if (g2->value == 11)
{
cout << " g2";
}
else if (g3->value == 11)
{
cout << " g3";
}
else if (g4->value == 11)
{
cout << " g4";
}
else if (y1->value == 11)
{
cout << " y1";
}
else if (y2->value == 11)
{
cout << " y2";
}
else if (y3->value == 11)
{
cout << " y3";
}
else if (y4->value == 11)
{
cout << " y4";
}
else if (r1->value == 11)
{
cout << " r1";
}
else if (r2->value == 11)
{
cout << " r2";
}
else if (r3->value == 11)
{
cout << " r3";
}
else if (r4->value == 11)
{
cout << " r4";
}
else
{
cout << " 11";
}
cout << " ";
if (y3 == british)
{
cout << "y3";
}
else
{
cout << " ";
}
cout << endl;
cout << "UK";
//space 1
if (b1->value == 1)
{
cout << " b1";
}
else if (b2->value == 1)
{
cout << " b2";
}
else if (b3->value == 1)
{
cout << " b3";
}
else if (b4->value == 1)
{
cout << " b4";
}
else if (g1->value == 1)
{
cout << " g1";
}
else if (g2->value == 1)
{
cout << " g2";
}
else if (g3->value == 1)
{
cout << " g3";
}
else if (g4->value == 1)
{
cout << " g4";
}
else if (y1->value == 1)
{
cout << " y1";
}
else if (y2->value == 1)
{
cout << " y2";
}
else if (y3->value == 1)
{
cout << " y3";
}
else if (y4->value == 1)
{
cout << " y4";
}
else if (r1->value == 1)
{
cout << " r1";
}
else if (r2->value == 1)
{
cout << " r2";
}
else if (r3->value == 1)
{
cout << " r3";
}
else if (r4->value == 1)
{
cout << " r4";
}
else
{
cout << " 01";
}
//space 12
cout << " ";
if (b1->value == 12)
{
cout << " b1";
}
else if (b2->value == 12)
{
cout << " b2";
}
else if (b3->value == 12)
{
cout << " b3";
}
else if (b4->value == 12)
{
cout << " b4";
}
else if (g1->value == 12)
{
cout << " g1";
}
else if (g2->value == 12)
{
cout << " g2";
}
else if (g3->value == 12)
{
cout << " g3";
}
else if (g4->value == 12)
{
cout << " g4";
}
else if (y1->value == 12)
{
cout << " y1";
}
else if (y2->value == 12)
{
cout << " y2";
}
else if (y3->value == 12)
{
cout << " y3";
}
else if (y4->value == 12)
{
cout << " y4";
}
else if (r1->value == 12)
{
cout << " r1";
}
else if (r2->value == 12)
{
cout << " r2";
}
else if (r3->value == 12)
{
cout << " r3";
}
else if (r4->value == 12)
{
cout << " r4";
}
else
{
cout << " 12";
}
cout << " ";
if (y2 == british)
{
cout << "y2";
}
else
{
cout << " ";
}
cout << endl;
//space 100
if (b1 == italian)
{
cout << "b1";
}
else
{
cout << " ";
}
if (b1->value == 100)
{
cout << "b1";
}
else if (b2->value == 100)
{
cout << "b2";
}
else if (b3->value == 100)
{
cout << "b3";
}
else if (b4->value == 100)
{
cout << "b4";
}
else if (g1->value == 100)
{
cout << "g1";
}
else if (g2->value == 100)
{
cout << "g2";
}
else if (g3->value == 100)
{
cout << "g3";
}
else if (g4->value == 100)
{
cout << "g4";
}
else if (y1->value == 100)
{
cout << "y1";
}
else if (y2->value == 100)
{
cout << "y2";
}
else if (y3->value == 100)
{
cout << "y3";
}
else if (y4->value == 100)
{
cout << "y4";
}
else if (r1->value == 100)
{
cout << "r1";
}
else if (r2->value == 100)
{
cout << "r2";
}
else if (r3->value == 100)
{
cout << "r3";
}
else if (r4->value == 100)
{
cout << "r4";
}
else
{
cout << "BS";
}
//space 101
if (b1->value == 111)
{
cout << " b1";
}
else if (b2->value == 111)
{
cout << " b2";
}
else if (b3->value == 111)
{
cout << " b3";
}
else if (b4->value == 111)
{
cout << " b4";
}
else
{
cout << " B1";
}
//space 102
if (b1->value == 112)
{
cout << "b1";
}
else if (b2->value == 112)
{
cout << "b2";
}
else if (b3->value == 112)
{
cout << "b3";
}
else if (b4->value == 112)
{
cout << "b4";
}
else
{
cout << "B2";
}
//space 103
if (b1->value == 113)
{
cout << "b1";
}
else if (b2->value == 113)
{
cout << "b2";
}
else if (b3->value == 113)
{
cout << "b3";
}
else if (b4->value == 113)
{
cout << "b4";
}
else
{
cout << "B3";
}
//space 104
if (b1->value == 114)
{
cout << "b1";
}
else if (b2->value == 114)
{
cout << "b2";
}
else if (b3->value == 114)
{
cout << "b3";
}
else if (b4->value == 114)
{
cout << "b4";
}
else
{
cout << "B4";
}
cout << " ";
//space 304
if (y1->value == 314)
{
cout << "y1";
}
else if (y2->value == 314)
{
cout << "y2";
}
else if (y3->value == 314)
{
cout << "y3";
}
else if (y4->value == 314)
{
cout << "y4";
}
else
{
cout << "Y4";
}
//space 303
if (y1->value == 313)
{
cout << "y1";
}
else if (y2->value == 313)
{
cout << "y2";
}
else if (y3->value == 313)
{
cout << "y3";
}
else if (y4->value == 313)
{
cout << "y4";
}
else
{
cout << "Y3";
}
//space 302
if (y1->value == 312)
{
cout << "y1";
}
else if (y2->value == 312)
{
cout << "y2";
}
else if (y3->value == 312)
{
cout << "y3";
}
else if (y4->value == 312)
{
cout << "y4";
}
else
{
cout << "Y2";
}
//space 301
if (y1->value == 311)
{
cout << "y1";
}
else if (y2->value == 311)
{
cout << "y2";
}
else if (y3->value == 311)
{
cout << "y3";
}
else if (y4->value == 311)
{
cout << "y4";
}
else
{
cout << "Y1";
}
//space 300
if (b1->value == 300)
{
cout << " b1";
}
else if (b2->value == 300)
{
cout << " b2";
}
else if (b3->value == 300)
{
cout << " b3";
}
else if (b4->value == 300)
{
cout << " b4";
}
else if (g1->value == 300)
{
cout << " g1";
}
else if (g2->value == 300)
{
cout << " g2";
}
else if (g3->value == 300)
{
cout << " g3";
}
else if (g4->value == 300)
{
cout << " g4";
}
else if (y1->value == 300)
{
cout << " y1";
}
else if (y2->value == 300)
{
cout << " y2";
}
else if (y3->value == 300)
{
cout << " y3";
}
else if (y4->value == 300)
{
cout << " y4";
}
else if (r1->value == 300)
{
cout << " r1";
}
else if (r2->value == 300)
{
cout << " r2";
}
else if (r3->value == 300)
{
cout << " r3";
}
else if (r4->value == 300)
{
cout << " r4";
}
else
{
cout << " YS";
}
if (y1 == british)
{
cout << "y1";
}
else
{
cout << " ";
}
cout << endl;
if (b2 == italian)
{
cout << "b2";
}
else
{
cout << " ";
}
//space 24
if (b1->value == 24)
{
cout << " b1";
}
else if (b2->value == 24)
{
cout << " b2";
}
else if (b3->value == 24)
{
cout << " b3";
}
else if (b4->value == 24)
{
cout << " b4";
}
else if (g1->value == 24)
{
cout << " g1";
}
else if (g2->value == 24)
{
cout << " g2";
}
else if (g3->value == 24)
{
cout << " g3";
}
else if (g4->value == 24)
{
cout << " g4";
}
else if (y1->value == 24)
{
cout << " y1";
}
else if (y2->value == 24)
{
cout << " y2";
}
else if (y3->value == 24)
{
cout << " y3";
}
else if (y4->value == 24)
{
cout << " y4";
}
else if (r1->value == 24)
{
cout << " r1";
}
else if (r2->value == 24)
{
cout << " r2";
}
else if (r3->value == 24)
{
cout << " r3";
}
else if (r4->value == 24)
{
cout << " r4";
}
else
{
cout << " 24";
}
//space 13
cout << " ";
if (b1->value == 13)
{
cout << " b1";
}
else if (b2->value == 13)
{
cout << " b2";
}
else if (b3->value == 13)
{
cout << " b3";
}
else if (b4->value == 13)
{
cout << " b4";
}
else if (g1->value == 13)
{
cout << " g1";
}
else if (g2->value == 13)
{
cout << " g2";
}
else if (g3->value == 13)
{
cout << " g3";
}
else if (g4->value == 13)
{
cout << " g4";
}
else if (y1->value == 13)
{
cout << " y1";
}
else if (y2->value == 13)
{
cout << " y2";
}
else if (y3->value == 13)
{
cout << " y3";
}
else if (y4->value == 13)
{
cout << " y4";
}
else if (r1->value == 13)
{
cout << " r1";
}
else if (r2->value == 13)
{
cout << " r2";
}
else if (r3->value == 13)
{
cout << " r3";
}
else if (r4->value == 13)
{
cout << " r4";
}
else
{
cout << " 13";
}
cout << " YE" << endl;
if (b3 == italian)
{
cout << "b3";
}
else
{
cout << " ";
}
//space 23
if (b1->value == 23)
{
cout << " b1";
}
else if (b2->value == 23)
{
cout << " b2";
}
else if (b3->value == 23)
{
cout << " b3";
}
else if (b4->value == 23)
{
cout << " b4";
}
else if (g1->value == 23)
{
cout << " g1";
}
else if (g2->value == 23)
{
cout << " g2";
}
else if (g3->value == 23)
{
cout << " g3";
}
else if (g4->value == 23)
{
cout << " g4";
}
else if (y1->value == 23)
{
cout << " y1";
}
else if (y2->value == 23)
{
cout << " y2";
}
else if (y3->value == 23)
{
cout << " y3";
}
else if (y4->value == 23)
{
cout << " y4";
}
else if (r1->value == 23)
{
cout << " r1";
}
else if (r2->value == 23)
{
cout << " r2";
}
else if (r3->value == 23)
{
cout << " r3";
}
else if (r4->value == 23)
{
cout << " r4";
}
else
{
cout << " 23";
}
//space 404
if (r1->value == 414)
{
cout << " r1";
}
else if (r2->value == 414)
{
cout << " r2";
}
else if (r3->value == 414)
{
cout << " r3";
}
else if (r4->value == 414)
{
cout << " r4";
}
else
{
cout << " R4";
}
//space 14
if (b1->value == 14)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 14)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 14)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 14)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 14)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 14)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 14)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 14)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 14)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 14)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 14)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 14)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 14)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 14)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 14)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 14)
{
cout << " r4";
cout << endl;
}
else
{
cout << " 14";
cout << endl;
}
if (b4 == italian)
{
cout << "b4";
}
else
{
cout << " ";
}
//space 22
if (b1->value == 22)
{
cout << " b1";
}
else if (b2->value == 22)
{
cout << " b2";
}
else if (b3->value == 22)
{
cout << " b3";
}
else if (b4->value == 22)
{
cout << " b4";
}
else if (g1->value == 22)
{
cout << " g1";
}
else if (g2->value == 22)
{
cout << " g2";
}
else if (g3->value == 22)
{
cout << " g3";
}
else if (g4->value == 22)
{
cout << " g4";
}
else if (y1->value == 22)
{
cout << " y1";
}
else if (y2->value == 22)
{
cout << " y2";
}
else if (y3->value == 22)
{
cout << " y3";
}
else if (y4->value == 22)
{
cout << " y4";
}
else if (r1->value == 22)
{
cout << " r1";
}
else if (r2->value == 22)
{
cout << " r2";
}
else if (r3->value == 22)
{
cout << " r3";
}
else if (r4->value == 22)
{
cout << " r4";
}
else
{
cout << " 22";
}
//space 403
if (r1->value == 413)
{
cout << " r1";
}
else if (r2->value == 413)
{
cout << " r2";
}
else if (r3->value == 413)
{
cout << " r3";
}
else if (r4->value == 413)
{
cout << " r4";
}
else
{
cout << " R3";
}
//space 15
if (b1->value == 15)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 15)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 15)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 15)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 15)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 15)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 15)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 15)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 15)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 15)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 15)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 15)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 15)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 15)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 15)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 15)
{
cout << " r4";
cout << endl;
}
else
{
cout << " 15";
cout << endl;
}
//space 21
if (b1->value == 21)
{
cout << " b1";
}
else if (b2->value == 21)
{
cout << " b2";
}
else if (b3->value == 21)
{
cout << " b3";
}
else if (b4->value == 21)
{
cout << " b4";
}
else if (g1->value == 21)
{
cout << " g1";
}
else if (g2->value == 21)
{
cout << " g2";
}
else if (g3->value == 21)
{
cout << " g3";
}
else if (g4->value == 21)
{
cout << " g4";
}
else if (y1->value == 21)
{
cout << " y1";
}
else if (y2->value == 21)
{
cout << " y2";
}
else if (y3->value == 21)
{
cout << " y3";
}
else if (y4->value == 21)
{
cout << " y4";
}
else if (r1->value == 21)
{
cout << " r1";
}
else if (r2->value == 21)
{
cout << " r2";
}
else if (r3->value == 21)
{
cout << " r3";
}
else if (r4->value == 21)
{
cout << " r4";
}
else
{
cout << " 21";
}
//space 402
if (r1->value == 412)
{
cout << " g1";
}
else if (r2->value == 412)
{
cout << " g2";
}
else if (r3->value == 412)
{
cout << " g3";
}
else if (r4->value == 412)
{
cout << " g4";
}
else
{
cout << " R2";
}
//space 16
if (b1->value == 16)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 16)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 16)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 16)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 16)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 16)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 16)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 16)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 16)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 16)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 16)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 16)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 16)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 16)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 16)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 16)
{
cout << " r4";
cout << endl;
}
else
{
cout << " 16";
cout << endl;
}
//space 20
if (b1->value == 20)
{
cout << " b1";
}
else if (b2->value == 20)
{
cout << " b2";
}
else if (b3->value == 20)
{
cout << " b3";
}
else if (b4->value == 20)
{
cout << " b4";
}
else if (g1->value == 20)
{
cout << " g1";
}
else if (g2->value == 20)
{
cout << " g2";
}
else if (g3->value == 20)
{
cout << " g3";
}
else if (g4->value == 20)
{
cout << " g4";
}
else if (y1->value == 20)
{
cout << " y1";
}
else if (y2->value == 20)
{
cout << " y2";
}
else if (y3->value == 20)
{
cout << " y3";
}
else if (y4->value == 20)
{
cout << " y4";
}
else if (r1->value == 20)
{
cout << " r1";
}
else if (r2->value == 20)
{
cout << " r2";
}
else if (r3->value == 20)
{
cout << " r3";
}
else if (r4->value == 20)
{
cout << " r4";
}
else
{
cout << " 20";
}
//space 401
if (r1->value == 411)
{
cout << " r1";
}
else if (g2->value == 411)
{
cout << " r2";
}
else if (g3->value == 411)
{
cout << " r3";
}
else if (g4->value == 411)
{
cout << " r4";
}
else
{
cout << " R1";
}
//space 17
if (b1->value == 17)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 17)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 17)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 17)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 17)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 17)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 17)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 17)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 17)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 17)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 17)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 17)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 17)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 17)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 17)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 17)
{
cout << " r4";
cout << endl;
}
else
{
cout << " 17";
cout << endl;
}
//space 19
if (b1->value == 19)
{
cout << " b1";
}
else if (b2->value == 19)
{
cout << " b2";
}
else if (b3->value == 19)
{
cout << " b3";
}
else if (b4->value == 19)
{
cout << " b4";
}
else if (g1->value == 19)
{
cout << " g1";
}
else if (g2->value == 19)
{
cout << " g2";
}
else if (g3->value == 19)
{
cout << " g3";
}
else if (g4->value == 19)
{
cout << " g4";
}
else if (y1->value == 19)
{
cout << " y1";
}
else if (y2->value == 19)
{
cout << " y2";
}
else if (y3->value == 19)
{
cout << " y3";
}
else if (y4->value == 19)
{
cout << " y4";
}
else if (r1->value == 19)
{
cout << " r1";
}
else if (r2->value == 19)
{
cout << " r2";
}
else if (r3->value == 19)
{
cout << " r3";
}
else if (r4->value == 19)
{
cout << " r4";
}
else
{
cout << " 19";
}
//space 18
if (b1->value == 18)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 18)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 18)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 18)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 18)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 18)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 18)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 18)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 18)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 18)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 18)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 18)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 18)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 18)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 18)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 18)
{
cout << " r4";
cout << endl;
}
else
{
cout << " 18";
cout << endl;
}
//space 400
if (b1->value == 400)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 400)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 400)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 400)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 400)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 400)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 400)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 400)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 400)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 400)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 400)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 400)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 400)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 400)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 400)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 400)
{
cout << " r4";
cout << endl;
}
else
{
cout << " Italy";
cout << endl;
}
if (r1 == spanish && r2 != spanish && r3 != spanish && g4 != spanish)
{
cout << " r1";
cout << endl;
}
if (r1 == spanish && r2 == spanish && r3 != spanish && r4 != spanish)
{
cout << " r1r2";
cout << endl;
}
if (r1 == spanish && r2 != spanish && r3 == spanish && r4 != spanish)
{
cout << " r1r3";
cout << endl;
}
if (r1 == spanish && r2 != spanish && r3 != spanish && r4 == spanish)
{
cout << " r1r4";
cout << endl;
}
if (r1 == spanish && r2 == spanish && r3 == spanish && r4 != spanish)
{
cout << " r1r2r3";
cout << endl;
}
if (r1 == spanish && r2 != spanish && r3 == spanish && r4 == spanish)
{
cout << " r1r3r4";
cout << endl;
}
if (r1 == spanish && r2 == spanish && r3 != spanish && r4 == spanish)
{
cout << " r1r2r4";
cout << endl;
}
if (r1 == spanish && r2 == spanish && r3 == spanish && r4 == spanish)
{
cout << " r1r2r3r4";
cout << endl;
}
if (r1 != spanish && r2 == spanish && r3 != spanish && r4 != spanish)
{
cout << " r2";
cout << endl;
}
if (r1 != spanish && r2 == spanish && r3 == spanish && r4 != spanish)
{
cout << " r2r3";
cout << endl;
}
if (r1 != spanish && r2 == spanish && r3 != spanish && r4 == spanish)
{
cout << " r2r4";
cout << endl;
}
if (r1 != spanish && r2 == spanish && r3 == spanish && r4 == spanish)
{
cout << " r2r3r4";
cout << endl;
}
if (r1 != spanish && r2 != spanish && r3 == spanish && r4 != spanish)
{
cout << " r3";
cout << endl;
}
if (r1 != spanish && r2 != spanish && r3 == spanish && r4 == spanish)
{
cout << " r3r4";
cout << endl;
}
if (r1 != spanish && r2 != spanish && r3 != spanish && r4 == spanish)
{
cout << " r4";
cout << endl;
}
if (r1 != spanish && r2 != spanish && r3 != spanish && r4 != spanish)
{
cout << " ";
cout << endl;
}
-
1\$\begingroup\$ Also, what's up with the quote ("The C++ language has two main components...")? \$\endgroup\$Zeta– Zeta2017年07月19日 20:31:40 +00:00Commented Jul 19, 2017 at 20:31
3 Answers 3
There are a great number of things you could do to improve this program.
Don't abuse using namespace std
Putting using namespace std
at the top of every program is a bad habit that you'd do well to avoid. I don't know that you've actually done that, but it's an alarmingly common thing for new C++ programmers to do.
Use const
where practical
It seems to me that printing the board shouldn't (and apparently doesn't) alter the underlying object. If that's the case, it should be declared const
to relay and enforce that fact.
Eliminate "magic numbers"
The current code contains these two lines:
//space 201
if (g1->value == 211)
It's not at all obvious whether 201 (mentioned in the comment) or 211 (actually used in the code) is the correct value, or exactly what it represents. Better would be to use a named const
value to avoid such confusion.
Separate concerns
There are essentially two different things involved in printing the board. The first is each individual data item and the second is the location on the screen. Also, the board is symmetric and is, more or less, four different quadrants. Each quadrant has a single country name. If we consider each country's "home" positions as four independent positions, the middle-of-board positions (e.g. the ones you've got labelled "G1" through "G4") and the positions on one side of the diamond, there are 15 possible different positions in which a particular soldier might be located within each quadrant. Since computers like base-16 numbers, let's say there are 16 and simply skip one. If we number each position with a 2-digit hex number and omit That layout looks like this:
State0
03020100
08
3e 09
3d 04 0a
3c 05 0b 13
3b 06 0c 12
3a 07 0d 11
State3 39 0e 10
38 34353637 17161514 18
30 2e 19 State1
31 2d 27 1a
32 2c 26 1b
33 2b 25 1c
2a 24 1d
29 1e
28
20212223
State2
Using this we can write a nice, neat print routine, but first let's look at objects.
Use objects
You have a board and four countries/players, each with four soldiers. It seems that you have an object for the board, but it's not clear that you have implemented objects for the other things. Doing so would greatly simplify your code. Here's one way to do that, using C++11 constructs and libraries:
class PlayBo {
public:
friend std::ostream &operator<<(std::ostream &out, const PlayBo &pb);
std::string pos(int location) const;
private:
static constexpr int SoldierCount{4};
static constexpr int CountryCount{4};
struct Country {
Country(std::string name, char letter, int offset) :
name{name},
prefix{letter},
offset{offset},
soldier{}
{
std::iota(soldier.begin(), soldier.end(), offset);
}
std::string name;
char prefix;
int offset;
std::array<int, SoldierCount> soldier;
};
std::array<Country, CountryCount> players{{
{"France", 'g', 0x00},
{"Italy ", 'y', 0x10},
{" UK ", 'b', 0x20},
{"Spain ", 'r', 0x30},
}};
};
Don't use std::endl
if you don't really need it
The difference betweeen std::endl
and '\n'
is that '\n'
just emits a newline character, while std::endl
actually flushes the stream. This can be time-consuming in a program with a lot of I/O and is rarely actually needed. It's best to only use std::endl
when you have some good reason to flush the stream and it's not very often needed for simple programs such as this one. Avoiding the habit of using std::endl
when '\n'
will do will pay dividends in the future as you write more complex programs with more I/O and where performance needs to be maximized.
Use an ostream operator<<
to print a class
Instead of using a named member function to print the class, I usually find it makes the object easier to use if instead we define an output stream operator. In this case, a friend class declaration within the PlayBo
class might look like this:
friend std::ostream &operator<<(std::ostream &out, const PlayBo &pb);
Here's an implementation using the classes shown above:
std::ostream &operator<<(std::ostream &out, const PlayBo &b)
{
return out
<< " " << b.players[0].name << "\n"
<< " " << b.pos(0x03) << b.pos(0x02) << b.pos(0x01) << b.pos(0x00) << "\n"
<< "\n"
<< " " << b.pos(0x08) << "\n"
<< " " << b.pos(0x3e) << " " << b.pos(0x09) << "\n"
<< " " << b.pos(0x3d) << " " << b.pos(0x04) << " " << b.pos(0x0a) << "\n"
<< " " << b.pos(0x3c) << " " << b.pos(0x05) << " " << b.pos(0x0b) << " " << b.pos(0x13) << "\n"
<< " " << b.pos(0x3b) << " " << b.pos(0x06) << " " << b.pos(0x0c) << " " << b.pos(0x12) << "\n"
<< " " << b.pos(0x3a) << " " << b.pos(0x07) << " " << b.pos(0x0d) << " " << b.pos(0x11) << "\n"
<< b.players[3].name << " " << b.pos(0x39) << " " << b.pos(0x0e) << " " << b.pos(0x10) << "\n"
<< " " << b.pos(0x38) << " " << b.pos(0x34) << b.pos(0x35) << b.pos(0x36) << b.pos(0x37) << " " << b.pos(0x17) << b.pos(0x16) << b.pos(0x15) << b.pos(0x14) << " " << b.pos(0x18) << "\n"
<< " " << b.pos(0x30) << " " << b.pos(0x2e) << " " << b.pos(0x19) << " " << b.players[1].name << "\n"
<< " " << b.pos(0x31) << " " << b.pos(0x2d) << " " << b.pos(0x27) << " " << b.pos(0x1a) << "\n"
<< " " << b.pos(0x32) << " " << b.pos(0x2c) << " " << b.pos(0x26) << " " << b.pos(0x1b) << "\n"
<< " " << b.pos(0x33) << " " << b.pos(0x2b) << " " << b.pos(0x25) << " " << b.pos(0x1c) << "\n"
<< " " << b.pos(0x2a) << " " << b.pos(0x24) << " " << b.pos(0x1d) << "\n"
<< " " << b.pos(0x29) << " " << b.pos(0x1e) << "\n"
<< " " << b.pos(0x28) << "\n"
<< "\n"
<< " " << b.pos(0x20) << b.pos(0x21) << b.pos(0x22) << b.pos(0x23) << "\n"
<< " " << b.players[2].name << "\n";
}
Each line in the function represents a line in the output to make it slightly easier to see what's going on. This is not the neatest possible method to use, but it's considerably easier to see what's happening than in the original and it's much, much shorter.
Now all that remains is to implement the helper function which returns the string to be printed, given the location. This is one way to do that:
std::string PlayBo::pos(int location) const
{
std::stringstream ss;
bool found = false;
for (const auto &state: players) {
for (int i = 0; i < SoldierCount; ++i) {
if (state.soldier[i] == location) {
ss << state.prefix << i;
found = true;
break;
}
}
}
if (!found) {
if ((location & 0xf) < 8) {
ss << " ";
} else {
ss << std::hex << std::setfill('0') << std::setw(2) << location;
}
}
return ss.str();
}
That function does a rather simple-minded linear search for each soldier
location and prints either that soldier's designation or the appropriate string if the location is empty. By numbering things cleverly, we can make the code easier to write. In this case, only the empty places along the diamond are printed, while the starting places and the inside-the-diamond locations should be represented with spaces. Note that all we need to do is look at the low four bits to determine aht classification. That is, anything the is in the range of 0 through 7 should be spaces if the location is empty. This keeps the code small and simple.
Make sure to #include
all required headers
This program apparently uses std::cout
and a number of other things from <iostream>
but doesn't show that #include
. To make it easier for reviewers to review, it's useful to explicitly put all required headers in the code to be reviewed. For the rewritten version shown above, you'd need these:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <array>
Testing
The code as presented doesn't appear to have been tested and contained some bugs. To avoid that, testing is very useful. Here's some extremely simple test code to exercise the printing function shown above.
int main() {
PlayBo board;
std::cout << board << "\n";
}
Here's the output that produces:
France
g3g2g1g0
08
3e 09
3d 0a
3c 0b y3
3b 0c y2
3a 0d y1
Spain 39 0e y0
38 18
r0 2e 19 Italy
r1 2d 1a
r2 2c 1b
r3 2b 1c
2a 1d
29 1e
28
b0b1b2b3
UK
I don't know if those positions or soldier designations are what you want, but with the revised code, those are trivial to change.
Use a better random number generator
The code currently includes this line:
int roll = rand()%6 + 1;
Unless RAND_MAX
is evenly divisible by 6 (and it's virtually certain not to be), then rather than an equal probability for all numbers 1 through 6, this will skew to the lower numbers. In C++11, we can do much better:
#include <random>
class Die {
public:
int operator()() {
std::uniform_int_distribution<> dist(1,6);
return dist(eng);
}
private:
static std::mt19937 eng;
};
std::mt19937 Die::eng{std::random_device{}()};
Now when you need a die roll, use the object instead of that.
int main()
{
for (unsigned limit=30000000; limit; --limit) {
Die{}();
}
}
This generate 3 million die rolls and on my machine, it takes about 500ms.
-
\$\begingroup\$ I only reviewed what you posted and can't help you with parts of the program I haven't seen. \$\endgroup\$Edward– Edward2017年07月23日 11:13:26 +00:00Commented Jul 23, 2017 at 11:13
Your first section:
if (g1 == french && g2 != french && g3 != french && g4 != french)
{
cout << " g1";
cout << endl;
}
if (g1 == french && g2 == french && g3 != french && g4 != french)
{
cout << " g2g1";
cout << endl;
}
if (g1 == french && g2 != french && g3 == french && g4 != french)
{
cout << " g3g1";
cout << endl;
}
if (g1 == french && g2 != french && g3 != french && g4 == french)
{
cout << " g4g1";
cout << endl;
}
if (g1 == french && g2 == french && g3 == french && g4 != french)
{
cout << " g3g2g1";
cout << endl;
}
if (g1 == french && g2 != french && g3 == french && g4 == french)
{
cout << " g4g3g1";
cout << endl;
}
if (g1 == french && g2 == french && g3 != french && g4 == french)
{
cout << " g4g2g1";
cout << endl;
}
if (g1 == french && g2 == french && g3 == french && g4 == french)
{
cout << " g4g3g2g1";
cout << endl;
}
if (g1 != french && g2 == french && g3 != french && g4 != french)
{
cout << " g2";
cout << endl;
}
if (g1 != french && g2 == french && g3 == french && g4 != french)
{
cout << " g3g2";
cout << endl;
}
if (g1 != french && g2 == french && g3 != french && g4 == french)
{
cout << " g4g2";
cout << endl;
}
if (g1 != french && g2 == french && g3 == french && g4 == french)
{
cout << " g4g3g2";
cout << endl;
}
if (g1 != french && g2 != french && g3 == french && g4 != french)
{
cout << " g3";
cout << endl;
}
if (g1 != french && g2 != french && g3 == french && g4 == french)
{
cout << " g4g3";
cout << endl;
}
if (g1 != french && g2 != french && g3 != french && g4 == french)
{
cout << " g4";
cout << endl;
}
if (g1 != french && g2 != french && g3 != french && g4 != french)
{
cout << " ";
cout << endl;
}
Can be greatly simplified if you take out the common bits.
std::cout << " "; // This amount of space is always printed.
std::cout << ((g4 == french) ? "g4" : " ");
std::cout << ((g3 == french) ? "g3" : " ");
std::cout << ((g2 == french) ? "g2" : " ");
std::cout << ((g1 == french) ? "g1" : " ");
std::cout << "\n"; // prefer to use '\n' rather than std::endl
I hope that inspires you to simplify the rest of the code.
The next section is a classic example of when to use an array:
if (b1->value == 200)
{
cout << " b1";
cout << endl;
}
else if (b2->value == 200)
{
cout << " b2";
cout << endl;
}
else if (b3->value == 200)
{
cout << " b3";
cout << endl;
}
else if (b4->value == 200)
{
cout << " b4";
cout << endl;
}
else if (g1->value == 200)
{
cout << " g1";
cout << endl;
}
else if (g2->value == 200)
{
cout << " g2";
cout << endl;
}
else if (g3->value == 200)
{
cout << " g3";
cout << endl;
}
else if (g4->value == 200)
{
cout << " g4";
cout << endl;
}
else if (y1->value == 200)
{
cout << " y1";
cout << endl;
}
else if (y2->value == 200)
{
cout << " y2";
cout << endl;
}
else if (y3->value == 200)
{
cout << " y3";
cout << endl;
}
else if (y4->value == 200)
{
cout << " y4";
cout << endl;
}
else if (r1->value == 200)
{
cout << " r1";
cout << endl;
}
else if (r2->value == 200)
{
cout << " r2";
cout << endl;
}
else if (r3->value == 200)
{
cout << " r3";
cout << endl;
}
else if (r4->value == 200)
{
cout << " r4";
cout << endl;
}
else
{
cout << " GS";
cout << endl;
}
You have 16 soldiers. Four soldiers from each country.
b1, b2, b3, b4
g1, g2, g3, g4
y1, y2, y3, y4
r1, r2, r3, r4
If you put this in an array. Then use the country number as the first index and the soldier number as the second index. Then you can use a simple loop to check each of the soldiers.
TypeForSoldier data[4][4]; // instead of b1,b2, ..... r3,r4
char countryLetter[] = {'b', 'g', 'y', 'r'};
for(int country = 0; country < 4; ++country) {
for(int soldier = 0; soldier < 4; ++soldier) {
if (data[country][soldier]->value == 200) {
std::cout << " "
<< countryLetter[country]
<< soldier
<< "\n";
break;
}
}
}
First, read "Why is "using namespace std" considered bad practice?".
Next, only flush your stream manually when you have to. Not only is it more verbose, but it also kills performance. So, no throwing around
std::endl
.Consider changing your data-structure:
Make it a single array of length N = #board_fields + #home_fields = 4 * 6 + 4 * 4
Each field stores whether it's empty (0 for empty home-fields, 1-24 for board-fields) or which of the 4 * 4 (25-40) pieces are there.Next, have an array mapping from that id to a human-readable abbreviation.
And as a final part, use
sprintf
or some other printing function to create your whole map in one go.
const static char names[][4] = {
" ",
"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12",
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"g1", "g2", "g3", "g4", "y1", "y2", "y3", "y4",
"r1", "r2", "r3", "r4", "b1", "b2", "b3", "b4"
};
char field[4*6+4*4];
void init_board() {
std::itoa(std::begin(field), std::end(field), 1);
}
void print_board() {
const char format[] =
" France\n"
" %s%s%s%s\n"
...;
char out[sizeof(format)];
#define X(n) names[field[n]]
sprintf(out, format, X(24), X(25), X(26), X(27), ...);
#undef X
std::cout << out;
}