I have 2 things that aren't working with this code. The first thing is that in my second do-while loop in main, when I input 'g' or 'G' and I put in coordinates and select if it is left or right it loops continuously.
My second thing that doesn't work right is that when I shoot my laser, a baffle should turn it and make it go a different direction. This works but it doesn't turn the laser until the box after the baffle is located. I am so stuck with this, any help would be great, and anything that you also see that I should fix would be greatly appreciated.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
struct velocity
{
int X = 0, Y = 0;
};
struct position
{
int X = 0, Y = 0;
void addVelocity(velocity V)
{
X += V.X;
Y += V.Y;
}
};
int gameBoard[10][10];
int guessBoard[10][10];
int random(int);
void laser(int&);
int boxNum(position, velocity);
position calcExitPos(velocity&, int&);
position moveLaser(position&, velocity V);
velocity checkPos(position, velocity&);
bool checkWall(position, velocity);
int main()
{
char level, action;
int num = 0;
int score = 0, guess = 0, shots = 0, LorR = 0;
int X = 0, Y = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
gameBoard[i][j] = 0;
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
guessBoard[i][j] = 0;
}
}
cout << "Welcome to the Baffle Game." << endl;
cout << " 10 11 12 13 14 15 16 17 18 19" << endl;
cout << "9|___|___|___|___|___|___|___|___|___|___|20" << endl;
cout << "8|___|___|___|___|___|___|___|___|___|___|21" << endl;
cout << "7|___|___|___|___|___|___|___|___|___|___|22" << endl;
cout << "6|___|___|___|___|___|___|___|___|___|___|23" << endl;
cout << "5|___|___|___|___|___|___|___|___|___|___|24" << endl;
cout << "4|___|___|___|___|___|___|___|___|___|___|25" << endl;
cout << "3|___|___|___|___|___|___|___|___|___|___|26" << endl;
cout << "2|___|___|___|___|___|___|___|___|___|___|27" << endl;
cout << "1|___|___|___|___|___|___|___|___|___|___|28" << endl;
cout << "0|___|___|___|___|___|___|___|___|___|___|29" << endl;
cout << " 39 38 37 36 35 34 33 32 31 30" << endl << endl;
cout << "This is the game board, you will shoot lasers and try to find all the baffles. " << endl << endl;
//cout << "For beginner there are 4 baffles, for intermediate there are 7 baffles and for advanced there are 10 baffles." << endl;
//cout << "Please type B for beginner, I for intermediate, and A for advanced" << endl;
do
{
cout << "For beginner there are 4 baffles, for intermediate there are 7 baffles" << endl;
cout << "and for advanced there are 10 baffles." << endl;
cout << "Please type B for beginner, I for intermediate, and A for advanced" << endl;
cin >> level;
} while (level != 'b' && level != 'B' && level != 'i' && level != 'I' && level != 'a' && level != 'A');
if (level == 'B' || level == 'b')
{
//Baffles equals 4
num = random(4); // function returns to num
}
else if(level == 'I' || level == 'i')
{
//Baffles equals 7
num = random(7);
}
else
{
//Baffles equals 10
num = random(10);
}
cout << "We will begin the game now." << endl;
cout << "Here are some commands that you will need to know to play." << endl;
cout << "L: Laser shot, then pick a number on the board to shoot the laser from." << endl;
cout << "G: Guess the location of one baffle with coordinance and L or R to choose a left or right baffle." << endl;
cout << "S: When you want me to print the score that you have." << endl;
cout << "P: Print the board showing the baffles that you have already found." << endl;
cout << "Q: Quit the game at any time" << endl;
cout << "C: This shows you the board with all the baffles and which direction they are." << endl;
do
{
cout << "Please type in your command." << endl;
cin >> action;
if (action == 'L' || action == 'l')
{
laser(shots);
}
else if (action == 'G' || action == 'g')
{
cout << "We will guess where the baffle is." << endl;
cout << "Please enter the X coordinate." << endl;
cin >> X;
cout << "Please enter the Y coordinate." << endl;
cin >> Y;
//cout << gameBoard[X][Y] << endl;
cout << "Please enter L and R for Left and Right Baffles." << endl;
cin >> LorR;
//cout << gameBoard[X][Y];
if (gameBoard[X][Y] == 1)//Right
{
//cout << "1" << endl;
if (LorR == 'R' || LorR == 'r')
{
cout << "Good job you got it correct." << endl;
guessBoard[X][Y] = 'R';
//points++
}
else
cout << "Wrong!" << endl;
}
else if (gameBoard[X][Y] == 2)//Left
{
//cout << "2" << endl;
if (LorR == 'L' || LorR == 'l')
{
cout << "Good job you got it correct." << endl;
guessBoard[X][Y] = 'L';
//points++
}
else
cout << "Wrong!" << endl;
}
else if (gameBoard[X][Y] == 0)
{
cout << "Your are wrong!" << endl;
}
}
else if (action == 'S' || action == 's')
{
cout << "Number of shots: " << shots << endl;
cout << "Number of guesses: " << guess << endl;
cout << "Current score: " << endl;//Get score
}
else if (action == 'P' || action == 'p')
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << setw(2) << guessBoard[i][j] << " ";
}
cout << endl;
}
}
else if (action == 'C' || action == 'c')
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << setw(2) << gameBoard[i][j] << " ";
}
cout << endl;
}
}
else if (action == 'q' || action == 'Q')
{
}
else if (action != 'q' && action != 'Q')
{
cout << "*** Illegal command - Try again ***" << endl;
}
cout << "loop" << endl;
}
while (action == 'q' || action == 'Q' || action == 'L' || action == 'l' || action == 'G' || action == 'g' ||
action == 'S' || action == 's' || action == 'P' || action == 'p' || action == 'C' || action == 'c');
}
//This function finds a random number for the find a random index
// in the array.
int random(int randnum)
{
unsigned seed = time(0); //get the system time.
srand(seed); //seed the random number generator.
int x, y, randomNum = 0;
int count;
for (count = 0; count < randnum; count++)
{
do
{
x = (rand() % (9 - 0 + 1)) + 0;
y = (rand() % (9 - 0 + 1)) + 0;
} while (gameBoard[x][y] != 0);
cout << "(" << x << "," << y << ")" << endl;
randomNum = rand();
gameBoard[x][y] = (randomNum % 2) + 1;
cout << gameBoard[x][y] << endl;
//2 = Left baffle
//1 = right baffle
}
return gameBoard[x][y];
}
void laser(int &shots) //Shoots a laser where user asks it to
{
int shoot;
cout << "Please input what number you want to shoot the laser from." << endl;
cin >> shoot;
shots++;
velocity V;
position P = calcExitPos(V,shoot);
//checkPos(P, V);
//cout << "1" << endl;
do
{
checkPos(P, V);
moveLaser(P, V);
//cout << "1" << endl;
} while (checkWall(P, V) == false);
cout << "Laser shot #" << shots << " exited the box at " << boxNum(P, V) << endl;
}
int boxNum(position P, velocity V) //Figures out what number the laser exits
{
if (P.X == 0 && V.X == -1)
{
return 9 - P.Y;
}
else if (P.Y == 0 && V.Y == -1)
{
return 10 + P.X;
}
else if (P.X == 9 && V.X == 1)
{
return P.Y + 20;
}
else if (P.Y == 9 && V.Y == 1)
{
return 39 - P.X;
}
}
position calcExitPos(velocity &V, int &box)//Figures out where the laser shoots from by the number the user inputs
{
position P1;
if (box >= 0 && box <= 9)
{
V.X = 1;
V.Y = 0;
P1.X = 0;
P1.Y = (9 - box);
}
else if (box >= 10 && box <= 19)
{
V.X = 0;
V.Y = 1;
P1.X = (box - 10);
P1.Y = 0;
}
else if (box >= 20 && box <= 29)
{
V.X = -1;
V.Y = 0;
P1.X = 9;
P1.Y = (box - 20);
}
else if (box >= 30 && box <= 39)
{
V.X = 0;
V.Y = -1;
P1.X = (39 - box);
P1.Y = 9;
}
return P1;
}
position moveLaser(position &P, velocity V)
{
int baffletype = gameBoard[P.X][P.Y];
if (baffletype == 1)//Right
{
if (V.X == 1)
{
V.Y = -1;
V.X = 0;
P.Y--;
}
else if (V.X == -1)
{
V.Y = 1;
V.X = 0;
P.Y++;
}
else if (V.Y == 1)
{
V.X = -1;
V.Y = 0;
P.X--;
}
else if (V.Y == -1)
{
V.X = 1;
V.Y = 0;
P.X++;
}
}
else if (baffletype == 2)//Left
{
if (V.X == 1)
{
V.Y = 1;
V.X = 0;
P.Y++;
}
else if (V.X == -1)
{
V.Y = -1;
V.X = 0;
P.Y--;
}
else if (V.Y == 1)
{
V.X = 1;
V.Y = 0;
P.X++;
}
else if (V.Y == -1)
{
V.X = -1;
V.Y = 0;
P.X--;
}
}
else if (baffletype == 0)
{
if (V.X == 1)
{
V.Y = 0;
V.X = 1;
P.X++;
}
else if (V.X == -1)
{
V.Y = 0;
V.X = -1;
P.X--;
}
else if (V.Y == 1)
{
V.X = 0;
V.Y = 1;
P.Y++;
}
else if (V.Y == -1)
{
V.X = 0;
V.Y = -1;
P.Y--;
}
}
cout << "(" << P.X << ","<< P.Y << ")" << endl;
return P;
}
velocity checkPos(position P, velocity &V) //Directs the laser when the laser hits a baffle
{
int baffletype = gameBoard[P.X][P.Y];
if (baffletype == 1)//Right
{
if (V.X == 1)
{
V.Y = -1;
V.X = 0;
}
else if (V.X == -1)
{
V.Y = 1;
V.X = 0;
}
else if (V.Y == 1)
{
V.X = -1;
V.Y = 0;
}
else if (V.Y == -1)
{
V.X = 1;
V.Y = 0;
}
}
else if (baffletype == 2)//Left
{
if (V.X == 1)
{
V.Y = 1;
V.X = 0;
}
else if (V.X == -1)
{
V.Y = -1;
V.X = 0;
}
else if (V.Y == 1)
{
V.X = 1;
V.Y = 0;
}
else if (V.Y == -1)
{
V.X = -1;
V.Y = 0;
}
}
else if (baffletype == 0)
{
if (V.X == 1)
{
V.Y = 0;
V.X = 1;
P.X++;
}
else if (V.X == -1)
{
V.Y = 0;
V.X = -1;
P.X--;
}
else if (V.Y == 1)
{
V.X = 0;
V.Y = 1;
P.Y++;
}
else if (V.Y == -1)
{
V.X = 0;
V.Y = -1;
P.Y--;
}
}
//cout << "( " << P.X << ", " << P.Y << ")" << endl;
return V;
}
bool checkWall(position P, velocity V) //Checks to see if the laser hits the wall
{
//cout << "2" << endl;
P.addVelocity(V);
return (P.X > 9 || P.X < 0 || P.Y > 9 || P.Y < 0); //Checks to see if the next position is out of bounds
}
I don't have a complete solution for you but a couple of things.
You should make your while loop test simpler.
How about this (pseudocode):
bool done = false;
while (!done) {
// Process action loop
cin >> action;
action = toupper(action); // Make everything upper case
processTheAction(action);
done = areWeDoneYet(action);
}
Break up the complex "are we done yet" task and other things into functions. (Divide and conquer)
bool areWeDoneYet(action) {
switch (action) {
case A:
case B:
... (cases where we are not done)
return false;
case E:
case F:
... (cases where we are done)
return true;
}
}
Make your "event loop" simple. Have it call a complex function
"ProcessAction" That way you can tell if you are busy processing when you have an issue, or if your having an issue with the event loop.
void processTheAction( action ) {
switch (action) {
case A:
case a: // Optional method if you don't do topper on Action
// Code for action 'A' goes here
// If a *lot* of code make a function like "ProcessActionA()"
break;
case B:
case b: // not needed if you do topper
// Code for action 'B' goes here
break;
default:
printf("I'm hosed cause I forgot to manage action [%c]\n",
action);
}
}
Don't know if this helps, but it might help you to break down the problem.
Related
I'm working on a little poker application and i've run into the first problem I just can't seem to comprehend.
while (allplayersGood != 1) { //round table till all decided
cout << "TOP OF WHILE LOOP";
for (int i = 0; i < PLAYER_COUNT; i++) { //for loop for decisions from non button or blinds
int player_decision = 1;
char choice;
if ((players[i].playerhand.card1.value != 'F') && (players[i].playerhand.card1.value != 'C')) {
if ((players[i].blind != 1 && players[i].blind != 2) && players[i].button != true) {
cout << "\n\n";
cout << " POT: " << playerTable->currentPot;
cout << "\n";
for (int i = 0; i < PLAYER_COUNT; i++) {
cout << "Player " << players[i].playernumber;
cout << " (" << players[i].chip_amount << ") ";
}
while (player_decision == 1) {
if (playerTable->currentBet > players[i].currentBet) {
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "Type F for Fold, B for Call, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'F') {
player_decision = 0;
players[i].fold();
}
if (choice == 'R') {
player_decision = 0;
players[i].bet(playerTable);
}
if (choice == 'B') {
player_decision = 0;
players[i].call(playerTable);
}
}
if ((playerTable->currentBet == players[i].currentBet) && player_decision != 0) { //big blind after round table
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "Type C for Check, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'B') {
player_decision = 0;
players[i].bet(playerTable);
}
if (choice == 'C') {
if (players[i].check(playerTable) == true) {
player_decision = 0;
}
}
}
}
}
else if (players[i].blind == 1 || players[i].blind == 2) {
if (players[i].blind == 1) {
players[i].chip_amount -= sblind;
playerTable->currentPot += sblind;
players[i].blind = 0;
players[i].currentBet = sblind;
}
if (players[i].blind == 2) {
players[i].chip_amount -= bblind;
playerTable->currentPot += bblind;
players[i].blind = 0;
players[i].currentBet = bblind;
}
}
}
}
for (int i = 0; i < PLAYER_COUNT; i++) { //seperate loop for button and blinds that were ignored in loop above
int player_decision = 1;
char choice;
if (players[i].button == true || players[i].blind == 1) { //button and small blind
cout << "\n\n";
cout << " POT: " << playerTable->currentPot;
cout << "\n";
for (int i = 0; i < PLAYER_COUNT; i++) {
cout << "Player " << players[i].playernumber;
cout << " (" << players[i].chip_amount << ") ";
}
while (player_decision == 1) {
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "Type F for Fold, B for Call, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'F') {
player_decision = 0;
players[i].fold();
}
if (choice == 'R') {
player_decision = 0;
players[i].bet(playerTable);
}
if (choice == 'B') {
player_decision = 0;
players[i].call(playerTable);
}
}
}
cout << i;
if (players[i].blind == 2) { //big blind
cout << "\n\n";
cout << " POT: " << playerTable->currentPot;
cout << "\n";
for (int i = 0; i < PLAYER_COUNT; i++) {
cout << "Player " << players[i].playernumber;
cout << " (" << players[i].chip_amount << ") ";
}
while (player_decision == 1) {
cout << "\n\nPlayer " << players[i].playernumber << " ("; players[i].playerhand.printhand(); cout << ") " << "C for Check, R for Raise: ";
cin >> choice;
players[i].choice = choice;
if (choice == 'C') {
if (players[i].check(playerTable) == true) {
player_decision = 0;
}
}
if (choice == 'R') {
player_decision = 0;
players[i].bet(playerTable);
}
}
}
}
int playersBetting = 0;
int playersGood = 0;
int playersChecked = 0;
int playersNot = 0;
for (int i = 0; i < PLAYER_COUNT; i++) {
if (players[i].playerhand.card1.value != 'F') {
playersBetting++;
if (players[i].currentBet == playerTable->currentBet) {
playersGood++;
}
}
}
for (int i = 0; i < PLAYER_COUNT; i++) {
if (players[i].playerhand.card1.value != 'F') {
if (players[i].isChecked == true) {
playersChecked++;
}
else {
playersNot++;
}
}
}
cout << playersBetting << playersGood;
if ((playersBetting == playersGood) || (playersNot == 0)) {
cout << "NEXT ROUND STARTED";
}
}
The issue is, during the second for loop with comment "seperate loop for button and blinds that were ignored in loop above" after the first if statement succeeds because players[0] has button equal to true, the player will make the terminal input as a decision, and the program will exit the for loop and go down to the end with the playersBetting and playersGood loops, then return back to the for loop at index 1 correctly.
I'm sorry if this is a little complicated to understand there is a lot of code that I probably didn't put into context very well, if you need any extra information please let me know.
Thank you.
You seem to have different loops inside one another. This is possible, but in that case, you need to use another loop variable (j instead of i), let me show you what happens:
for i ...
for j ...
This causes the following values to be taken for i and j:
i j
1 1
1 2
1 ...
1 n
2 1
2 2
2 ...
2 n
...
n 1
n 2
...
n n
... and here it stops.
If you keep using i in the inner loop, this is what you get:
i (outside loop) i (inside loop)
1 1
2 2 // indeed: changing i inside also changes i outside
... ...
n n
So you jump out of the outside loop, even after just having looped the inside loop one time.
I figured it out, it was unrelated to the actual loop and actually had to do with a value I changed upstream. Thank you for the few who tried to help with such little context haha
Have a good one
I wanted to expand on an older Tic Tac Toe game I made where two players can play versus each other. I want to give the user the option of playing against a difficult AI. The issue is that the AI won't pick the best move all the time. For instance, it will always pick spot 1 if going first. If the user picks spot 2, it will pick spot 4. After this, no matter what the user picks (besides spot 7) the AI won't pick spot 7. Victory for the AI is far from inevitable (the user can still win the game at this point), so that's not the problem.
Any help is appreciated. Thanks!
I'm positive the problem is with my minimax or bestmove functions. It may just be that I haven't properly implemented by minimax function, but I can't spot the issue.
#include <iostream>
#include <iomanip>
#include <string>
#include <array>
// This is a program to play a single game of tic-tac-toe
// between either two human (non-AI) players or an AI.
using namespace std;
void PrintBoard(array <char, 9>);
int programprogress();
int checkwin(array <char, 9>);
int minimax(array <char, 9>, int, int, bool);
int bestMove(array <char, 9>, int);
int Opposite(int);
char PlayerSymbol(int);
const int SIZE = 9;
array <char, SIZE> Pos = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int player_number = 1;
int k = -11, result;
bool AI = false, first;
// Global variables used by 2 or more functions.
// Array had to be initialized with numbers instead of blank spaces
// because the check win function wouldn't work properly.
int main()
{
string userinp;
cout << "This is tic tac toe! Here's your board!" << endl;
PrintBoard(Pos);
cout << "Would you like to play versus an AI? (Y/N)" << endl;
cin >> userinp;
if (userinp[0] == 'Y')
{
cout << "Excellent! Would you like to start first, or second? (F/S)" << endl;
cin >> userinp;
if (userinp[0] == 'F')
{
cout << "You will start first!" << endl;
first = false;
player_number = 2;
}
else
{
cout << "The AI will start first!" << endl;
first = true;
}
AI = true;
}
else
{
cout << "Excellent! Your game will start soon." << endl;
}
result = programprogress();
player_number--;
PrintBoard(Pos);
if (result == 1)
cout << endl << "Player " << player_number << " has won!!!\n";
else if (result == 10)
cout << endl << "The AI has won! Better luck next time!\n";
else if (result = -10)
cout << endl << "You beat the world's best AI! Congratulations!\n";
else
cout << endl << "The game has been drawn!" << endl;
return 0;
}
void PrintBoard(array <char, 9> Pos)
{
system("cls");
cout << setw(6) << "|" << setw(6) << "|" << endl << setw(3) << Pos[0] << setw(3) << "|" << setw(3) << Pos[1] << setw(3) << "|" << setw(3) << Pos[2] << " TIC TOE" << endl;
cout << "_____|_____|_____" << endl;
cout << setw(6) << "|" << setw(6) << "|" << endl << setw(3) << Pos[3] << setw(3) << "|" << setw(3) << Pos[4] << setw(3) << "|" << setw(3) << Pos[5] << " TAC " << endl;
cout << "_____|_____|_____" << endl;
cout << setw(6) << "|" << setw(6) << "|" << endl << setw(3) << Pos[6] << setw(3) << "|" << setw(3) << Pos[7] << setw(3) << "|" << setw(3) << Pos[8] << " TIC TOE " << endl;
cout << " | |" << endl;
}
int programprogress()
{
while (k == -11 && AI)
{
bool InvalidChoice = false;
char letter;
//player_number = (player_number % 2) ? 1 : 2;
int PlayerChoice;
if (player_number == 2)
{
cout << endl << "What is your move?" << endl;
cin >> PlayerChoice;
while ((PlayerChoice < 1) || (PlayerChoice > 9))
{
cout << "That's an invalid choice! Please choose a number that is 1-9!" << endl;
cin >> PlayerChoice;
}
PlayerChoice--;
letter = (!first) ? 'X' : 'O';
if (Pos[PlayerChoice] == '1' || Pos[PlayerChoice] == '2' || Pos[PlayerChoice] == '3' || Pos[PlayerChoice] == '4' || Pos[PlayerChoice] == '5' || Pos[PlayerChoice] == '6' || Pos[PlayerChoice] == '7' || Pos[PlayerChoice] == '8' || Pos[PlayerChoice] == '9')
{
Pos[PlayerChoice] = letter;
PrintBoard(Pos);
}
/*else
{
cout << "That space is already taken!" << endl;
player_number--;
}*/
k = checkwin(Pos);
if (k != -11)
k = k * -10;
player_number = 1;
}
else
{
cout << endl << "The computer has made its move!" << endl;
letter = (first) ? 'X' : 'O';
if (first)
PlayerChoice = bestMove(Pos, 1);
else
PlayerChoice = bestMove(Pos, 2);
Pos[PlayerChoice] = letter;
PrintBoard(Pos);
k = checkwin(Pos);
if (k != -11)
k = k * 10;
player_number = 2;
}
}
while (k == -11 && !AI)
{
bool InvalidChoice = false;
char letter;
player_number = (player_number % 2) ? 1 : 2;
int PlayerChoice;
cout << endl << "What's player " << player_number << "'s move?" << endl;
cin >> PlayerChoice;
while ((PlayerChoice < 1) || (PlayerChoice > 9))
{
cout << "That's an invalid choice! Please choose a number that is 1-9!" << endl;
cin >> PlayerChoice;
}
PlayerChoice--;
letter = (player_number == 1) ? 'X' : 'O';
if (Pos[PlayerChoice] == '1' || Pos[PlayerChoice] == '2' || Pos[PlayerChoice] == '3' || Pos[PlayerChoice] == '4' || Pos[PlayerChoice] == '5' || Pos[PlayerChoice] == '6' || Pos[PlayerChoice] == '7' || Pos[PlayerChoice] == '8' || Pos[PlayerChoice] == '9')
{
Pos[PlayerChoice] = letter;
PrintBoard(Pos);
}
else
{
cout << "That space is already taken!" << endl;
player_number--;
}
k = checkwin(Pos);
player_number++;
}
return k;
}
int checkwin(array <char, SIZE> Pos)
{
if (Pos[0] == Pos[1] && Pos[1] == Pos[2])
return 1;
else if (Pos[3] == Pos[4] && Pos[4] == Pos[5])
return 1;
else if (Pos[6] == Pos[7] && Pos[7] == Pos[8])
return 1;
else if (Pos[0] == Pos[3] && Pos[3] == Pos[6])
return 1;
else if (Pos[1] == Pos[4] && Pos[4] == Pos[7])
return 1;
else if (Pos[2] == Pos[5] && Pos[5] == Pos[8])
return 1;
else if (Pos[0] == Pos[4] && Pos[4] == Pos[8])
return 1;
else if (Pos[2] == Pos[4] && Pos[4] == Pos[6])
return 1;
else if (Pos[0] != '1' && Pos[1] != '2' && Pos[2] != '3'
&& Pos[3] != '4' && Pos[4] != '5' && Pos[5] != '6'
&& Pos[6] != '7' && Pos[7] != '8' && Pos[8] != '9')
return 0;
else
return -11;
}
int minimax(array <char, SIZE> newpos, int depth, int player, bool opp)
{
int scale = 0;
if ((player == 1 && first) || (player == 2 && !first))
scale = 10;
else
scale = -10;
//cout << scale;
int score = scale*checkwin(newpos);
if (score < 0)
score += depth;
else if (score > 0)
score -= depth;
if (score == -10 || score == 10 || score == 0)
return score;
if (opp)
{
int best = -1000;
for (int i = 0; i < SIZE; i++)
{
if (newpos[i] != 'X' && newpos[i] != 'O')
{
char temp = newpos[i];
newpos[i] = PlayerSymbol(player);
best = max(best, minimax(newpos, depth + 1, Opposite(player), !opp));
newpos[i] = temp;
}
}
return best;
}
else
{
int best = 1000;
for (int i = 0; i < SIZE; i++)
{
if (newpos[i] != 'X' && newpos[i] != 'O')
{
char temp = newpos[i];
newpos[i] = PlayerSymbol(player);
best = min(best, minimax(newpos, depth + 1, Opposite(player), !opp));
newpos[i] = temp;
}
}
return best;
}
}
int bestMove(array <char, SIZE> newpos, int player)
{
int best = -1000;
int bestpos = -1;
for (int i = 0; i < SIZE; i++)
{
if (newpos[i] != 'X' && newpos[i] != 'O')
{
char temp = newpos[i];
newpos[i] = PlayerSymbol(player);
int move = minimax(newpos, 0, player, !first);
newpos[i] = temp;
if (move > best)
{
//cout << "I like pineapple on pizza" << endl;
bestpos = i;
best = move;
}
/*if (move == best)
{
cout << "I like pineapple on pizza" << endl;
}*/
}
}
cout << bestpos;
return bestpos;
}
int Opposite(int x)
{
if (x == 1)
return 2;
else
return 1;
}
char PlayerSymbol(int x)
{
if (x == 1)
return 'X';
else
return 'O';
}
An out of bounds error due to the -1 value of bestpos. I'm not sure how to change this, though.
There are 4 issues that I could find. Solving them seems to lead to the intended behavior.
Firstly, when you call minimax(newpos, 0, player, !first); from inside the bestMove function, you pass player rather than Opposite(player), indicating that the first minimax step will be performed by the same player as the bestMove step. In other words: The AI makes two successive moves for itself. Therefore player needs to be changed to Opposite(player).
Secondly, minimax has a bool variable named opp that seems to indicate whether it is the AI or its opponent making the move. For the first minimax step, opp is set to !first, indicating that only if the AI goes first, the opponent will make a move after the AI. That is incorrect. It is always the opponent making a move after the AI. So bestMove should call minimax with true rather than !first. As an aside, opp is redundant, because you can use (player == 1 && first) || (player == 2 && !first) to check whether it's the AI or its opponent making the move.
Thirdly, the scale is set the wrong way around. With (player == 1 && first) || (player == 2 && !first) you check whether its the AI making a move. But you do that in the next minimax step, after the potentially winning move. So if the AI is making a move and the game is already won, then the opponent made the winning move, not the AI. Ergo, the scale should be
if ((player == 1 && first) || (player == 2 && !first))
scale = -10;
else
scale = 10;
instead.
Lastly, you check whether the score is 10 or -10 after adding the depth. If depth is not 0, then this check will always fail. So beyond depth 0, the AI can only see a draw, never a win. You could instead write
if (score == -10 || score == 10 || score == 0)
{
if (score < 0)
score += depth;
else if (score > 0)
score -= depth;
return score;
}
Hope this answers your question fully.
My code is below. I have tried all different options. I am still learning C++ through classes but have reached an impasse. Xcode says error are in the lines there are three straight * marks. Ex: ***code* Any advice would be great and looking for ways to understand arrays better.
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int board [7] [7];
void initBoard (int array1[7][7]) {
srand(time(0));
string position[7][7];
int moves = rand() % 8 + 5;
int r = 0;
int c = 0;
for (c = 0; c < 7; c++) {
if (c == 0 || c == 6) {
for (r = 0; r < 7; r++) {
position[r][c] = " ";
}
}
else {
for (r = 0; r < 7; r++) {
if (r == 0 || r == 6) {
position[r][c] = " ";
}
else {
position[r][c] = "_ ";
}
}
}
}
***void plantBombs (int array2[7][7]) {***
r = 0;
c = 0;
int k = (rand() % 5) + 1;
int d = ((rand() % 111) % 5) + 1;
int numBombs = rand() % 4 + 3;
int bombR[numBombs];
int bombC[numBombs];
int i = 0;
while (i < numBombs){
bombR[i] = (rand() % 71) % 5 + 1;
bombC[i] = (rand() % 123) % 5 + 1;
while (bombR[i] == k && bombC[i] == d) {
bombR[i] = (rand() % 97) % 5 + 1;
bombC[i] = (rand() % 79) % 5 + 1;
}
while ((i > 0) && (bombR[i-1] == bombR[i]) && (bombC[i-1] == bombC[i])){
bombR[i] = (rand() % 213) % 5 + 1;
bombC[i] = (rand() % 857) % 5 + 1;
}
position[bombR[i]][bombC[i]] = "* ";
i = i + 1;
}
}
***void placeYou (int array2[7][7]){***
position[k][d] = "U ";
}
***void movePlayer (int arrary3[7][7]){***
while (moves != 0) {
cout << "SURVIVE " << moves << " MORE MOVES!" << endl;
for (c = 0; c < 7; c++) {
for (r = 0; r < 7; r++) {
cout << position[r][c];
}
cout << endl;
}
cout << "Enter direction (N/S/E/W): ";
string direction;
cin >> direction;
//if (direction == "N")
if (direction == "N"){
if (position[k][d-1] == " "){
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k][d-1] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k][d-1] = "U ";
d = d - 1;
}
}
if (direction == "E"){
if (position[k+1][d] == " "){
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k+1][d] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k+1][d] = "U ";
k = k + 1;
}
}
if (direction == "S"){
if (position[k][d+1] == " ") {
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k][d+1] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k][d+1] = "U ";
d = d + 1;
}
}
if (direction == "W"){
if (position[k-1][d] == " "){
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k-1][d] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k-1][d] = "U ";
k = k - 1;
}
moves = moves - 1;
}
if (moves == 0){
cout << "***YOU WIN***";
}
else {
cout << "";
}
}
}
}
int main()
{
initBoard(board);
***plantBombs(board);
placeYou(board);
movePlayer(board);***
return 0;
}
I am working on a school project (I was allowed to do a simulation in C++ for my Science Fair project) and everything is going pretty ok (besides another bug that I had an issue with) until now. In order to fully understand what I am doing, I suggest that you take a quick glance at this page.
Okay. Below is my code. When you try to run it, you can clearly see that some bots' x and y cordinates are in the negatives, which shouldn't be happening. I have triple checked all the operations and everything looks fine to me. I used this to help me understand X and Y locations relative in arrays. Is there any clear issue of my mistake? I am still getting used to multidimensional arrays, so please take that into consideration. Also, I am aware that most of it is highly inefficient; cleaning it up will be a project itself. Thank you and Happy Holidays!
*My issues are coming from the moveBot functions, which are towards the bottom. It didn't seem right to have you read over unnecessary parts.
Updated Code:
// NANORG SIMULATION
// CREATED BY JACOB HARTMANN FOR THE SCIENCE FAIR (2013)
// CODED IN C++
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include "bot.h"
using namespace std;
signed int map [40][70]; //2D array x first, then y
int mapBot [40][70]; //If bot is there, 1. If not, 0 This array keeps track if there is a bot in a location
signed int currentTick = 0; //Signed just in case something goes wrong and goes into the -s
int maxTick = 1000000; //Should always stay at 1mil
signed int totalEnergy = 0; //For some reason, when I add this here, everything breaks.
Bot bots[50];
void setupMap();
void tick();
void updateTickOutput();
void outputMapValue(int x, int y);
void assignBotID();
void setupBot();
void moveBot();
void manualMoveBot(int botID);
void mutateBot(int botID, int level, int sT);
void ckLoc(int botIdent);
void reassignLoc(int botID);
void gatherEnergy(int botID);
int main() {
cout << " ----------------------" << endl;
cout << " NANORG Simulation V1.3.1" << endl;
cout << "Created in C++ by Jacob Hartmann" << endl;
cout << " ----------------------" << endl << endl;
srand (time(NULL));
cout << "Setting up map.. (Step 1)" <<endl;
setupMap();
cout << "Assigning botIDs.. (Step 2)" << endl;
assignBotID();
cout << "Setting up bots.. (Step 3)" << endl;
setupBot();
cout << "Starting ticks.. (Step 4)" << endl;
tick();
// outputMapValue(5,5);
// mutateBot(5, 2); //Just to test
/* cout << endl << "X before: " << bots[5].x_cord << " Y before: " << bots[5].y_cord << endl;
moveBot(5);
cout << "X after: " << bots[5].x_cord << " Y after: " << bots[5].y_cord << endl;
cout << "Energy before: " << bots[5].energy <<endl;
gatherEnergy(5);
cout << "Energy after: " << bots[5].energy << endl; */ //DEBUG
return 0;
}
void setupMap(){
// srand (time(NULL)); //Not a good idea to put it here
for(int i = 0; i < 40; i++){
for(int j = 0; j < 70; j++){ // We add one extra (70) so it fills the entire array. Same for above
map[i][j] = rand() % 2 + 1; // 1==normal 2==toxic
}
}
// outputMapValue(5,4); //Debugging purposes
// outputMapValue(7,9);
cout << "Map setup - \033[0;32mSuccessful" << "\033[0m" << endl;
}
void outputMapValue(int x, int y){
cout << "The chunk value at (" << x+1 << ", " << y+1 << ") is: ";
cout << map[x][y]; //Outputting the value of (x,y)
if(map[x][y]==1){ //If (x,y) is == 1, the chunk is fine to eat
cout << " | Chunk is not toxic." << endl;
}
if(map[x][y]==2){
cout << " | Chunk is toxic." << endl;
}
}
void updateTickOutput() {
//cout << "Map Size (x,y): " << " " << mapsizeX << "," << mapsizeY << endl; This function used to just refresh everything, including map size, which really isn't needed. cout << "Current Tick: " << currentTick << " " << "Max Tick: " << maxTick << endl; //Just outputting currentTick and maxTick
cout << "Score: " << totalEnergy << endl;
}
void tick() {
while(true){
if(currentTick <= maxTick){
currentTick += 1;
moveBot();
/* if(currentTick >= 900000){ //If currentTick is over 900,000: we will begin to output individual ticks. Anything less and we get a seg fault.
updateTickOutput(); //See above
}*/
// cout << "tick!"; This was for debugging, before I made the updateTickOutput() function to make sure that the program actually ticked
}
else if(currentTick == maxTick){
cout << endl << "Done!" << endl; //Report that we are finished with the simulation.
// assignBotID(); //Make sure every bot has the proper ID. Moved to main()
break; //Kill the loop
}
// updateTickOutput(); //No real need for this, anymore.
}
}
void setupBot(){
srand(time(NULL));
for(int botNumber=0;botNumber <= 50; botNumber++){
// cout << "debug (botNumber): " << botNumber << endl; //Debug feature
bots[botNumber].x_cord = rand() % 39 + 1;
// cout << "debug (bot x cord): " << bots[botNumber].x_cord << endl; //Debug feature
int bufferX = bots[botNumber].x_cord;
bots[botNumber].y_cord = rand() % 69 + 1;
// cout << "debug (bot y cord): " << bots[botNumber].y_cord << endl; //Debug feature
int bufferY = bots[botNumber].y_cord;
if(mapBot[bufferX][bufferY] == 1){
cout << endl <<"A bot already is here!" << endl;
reassignLoc(botNumber);
}
else{
mapBot[bufferX][bufferY] = 1; //Take the bot's cords and make sure everyone knows that a bot is there.
// cout<< "debug (map location):"<<mapBot[bufferX][bufferY] << endl ; Just a nice debug feature
}
// if(botNumber==5){
// cout << "bot 5 assigned"; //I broke this entire function a while back and I used this to test if I was assigning bots correctly.
// }
}
/* cout << endl << "X: " << bots[5].x_cord+1 << endl; //Just some debugging stuff below
cout << "Y: " << bots[5].y_cord+1 << endl;
// cout << "The value at " << mapBot[bots[5].x_cord]<<","<< mapBot[bots[5].y_cord]<<" is: " << mapBot[bots[5].x_cord][bots[5].y_cord]; //This is a very messed up debug feature, but it still works.
cout << endl << "X: " << bots[6].x_cord+1 << endl;
cout << "Y: " << bots[6].y_cord+1 << endl;
cout << mapBot[6][6];
*/
cout << "Bot setup - \033[0;32mSuccessful" << "\033[0m" << endl;
}
void assignBotID(){
for(int i = 0; i < 50; ++i){
bots[i].id = i + 1;
}
/* int botNumber = 0;
string botName = "Bot";
string finalName;
string buffer;
while(botNumber <50){
if(botNumber < 50){
botNumber = botNumber + 1;
buffer = to_string(botNumber);
finalName = botName + buffer;
//finalName.id = botNumber; A very very broken line.
bots[botNumber].id = botNumber;
// cout << finalName << ":"<< bots[botNumber].id << endl; A super awesome debugging output to make sure the bot's id is correct
}
else if((botNumber = 51)){ //Redundancy :)
break;
}
}*/
}
void mutateBot(int botID, int level, int sT){
if((sT=2)){
bots[botID].mutationLevel = bots[botID].mutationLevel + level;
}
else if((sT=1)){
bots[botID].mutationLevel = bots[botID].mutationLevel - level;
}
// cout << botID << ":" << bots[botID].mutationLevel << endl; //Just a quick debugging feature to make sure it worked
}
void ckLoc(int botIdent){
int bufferX;
int bufferY;
bufferX = bots[botIdent].x_cord; //Just set the buffers. Uses a bit more memory, but that is okay.
bufferY = bots[botIdent].y_cord;
// cout << bufferX << endl << bufferY;
if(mapBot[bufferX][bufferY] ==1){
cout << "Bot lives here!";
reassignLoc(botIdent);
}
}
void reassignLoc(int botID){
bots[botID].x_cord = rand() % 39 + 1;
bots[botID].y_cord = rand() % 69 + 1;
ckLoc(botID);
}
void moveBot(){
for(int botID=1;botID<=50;botID++){
int direction = 0;
// int bufX = bots[botID].x_cord;
// int bufY = bots[botID].y_cord;
direction = rand() % 4 + 1;
if(direction == 1){ //NORTH
if(bots[botID].y_cord==0 || mapBot[bots[botID].x_cord][bots[botID].y_cord=-1] == 1){
//cout << "error moving bot - N ";
manualMoveBot(botID);
}
else{
//cout << "BufferY: " << bufferY;
bots[botID].y_cord -= 1;
gatherEnergy(botID);
}
}
else if(direction == 2){ //EAST
if(bots[botID].x_cord == 39 || mapBot[bots[botID].x_cord+=1][bots[botID].y_cord] == 1){
//cout << "error moving bot - E";
manualMoveBot(botID);
}
else{
bots[botID].x_cord += 1;
gatherEnergy(botID);
}
}
else if(direction == 3){ //SOUTH
if(bots[botID].y_cord == 69 || mapBot[bots[botID].x_cord][bots[botID].y_cord+=1] == 1){
//cout << "error moving bot - S ";
manualMoveBot(botID);
}
else{
bots[botID].y_cord += 1;
gatherEnergy(botID);
}
}
else if(direction == 4){ //WEST
if(bots[botID].x_cord == 0 or mapBot[bots[botID].x_cord=-1][bots[botID].y_cord] == 1){
//cout << "error moving bot - W";
manualMoveBot(botID);
}
else{
bots[botID].x_cord -= 1;
gatherEnergy(botID);
}
}
// gatherEnergy(botID); //Moved this to indivdual (spelling) stuff above. Just in case :)
// cout << endl << "Direction: " << direction << endl; //Debug
}
}
void manualMoveBot(int botID){
int direction = 0;
// int bufX = bots[botID].x_cord;
// int bufY = bots[botID].y_cord;
direction = rand() % 4 + 1;
if(direction == 1){ //NORTH
if(bots[botID].y_cord==0 || mapBot[bots[botID].x_cord][bots[botID].y_cord-1] == 1){
//cout << "error moving bot - N ";
manualMoveBot(botID);
}
else{
//cout << "BufferY: " << bufferY;
bots[botID].y_cord -= 1;
gatherEnergy(botID);
}
}
else if(direction == 2){ //EAST
if(bots[botID].x_cord == 39 || mapBot[bots[botID].x_cord+1][bots[botID].y_cord] == 1){
//cout << "error moving bot - E";
manualMoveBot(botID);
}
else{
bots[botID].x_cord += 1;
gatherEnergy(botID);
}
}
else if(direction == 3){ //SOUTH
if(bots[botID].y_cord == 69 || mapBot[bots[botID].x_cord][bots[botID].y_cord+1] == 1){
//cout << "error moving bot - S ";
manualMoveBot(botID);
}
else{
bots[botID].y_cord -= 1;
gatherEnergy(botID);
}
}
else if(direction == 4){ //WEST
if(bots[botID].x_cord == 0 or mapBot[bots[botID].x_cord-1][bots[botID].y_cord] == 1){
//cout << "error moving bot - W";
manualMoveBot(botID);
}
else{
bots[botID].x_cord += 1;
gatherEnergy(botID);
}
}
}
void gatherEnergy(int botID){
// int mV = map[bufferX][bufferY]; //Eeeeh, doesn't work here
int x = bots[botID].x_cord;
int y = bots[botID].y_cord;
// cout << "id: " << botID << endl;
// cout << "x: " << x;
// cout << endl << "y: " << y << endl;
if(1==map[x][y]){ //Good chunk
bots[botID].energy += 2000;
totalEnergy += 2000;
// cout << totalEnergy << endl; //Debug
}
else if(2==map[x][y]){ //Bad chunk (toxic)
mutateBot(botID, 1, 2);
}
}
You have several problems.
The main one is that indices of array:
so for Bot bots[50]; valid indices are [0; 49].
For map, according to contest it should be int mapBot [40][70];
so you may access element starting from mapBot[0][0] to mapBot[39][69].
You confound = and == in some comparison.
= does an assignment when == do a comparison.
I see you add extra parenthesis to remove a warning.
You have to read/understand more carefully warning messages.
so else if((currentTick = maxTick)){ in tick for example, should be else if (currentTick == maxTick) {.
Same in mutateBot...
In AssigningBotID, you increment index before accessing the array
and do redundant checks. following is enough:
void assignBotID() {
for (int i = 0; i < 50; ++i) {
bots[i].id = i /* + 1 */; // uncomment if id should be [1; 50]
// A super awesome debugging output to make sure the bot's id is correct
//std::cout << "Bot" << i << ":"<< bots[i].id << endl;
}
}
in manualMoveBot your test may change the position, since you use +=:
mapBot[bots[botID].x_cord += 1][bots[botID].y_cord] == 1
should be mapBot[bots[botID].x_cord + 1][bots[botID].y_cord] == 1
Your logic forget to update mapBot: the old place should be set to 0,
the new place should be set to 1.
Note that during initialization, it is not always set neither...
EDIT:
You should only call srand (time(NULL)); once (the one in main())
setupBot() should be something like (and so reassignLoc and ckLoc may be removed):
void setupBot() {
for (int i = 0; i < 50; ++i) {
int x, y;
do {
x = rand() % 40; // [0; 39]
y = rand() % 70; // [0; 69]
while (mapBot[x][y] == 1);
bots[i].x_cord = x;
bots[i].y_cord = y;
mapBot[x][y] = 1;
}
cout << "Bot setup - \033[0;32mSuccessful" << "\033[0m" << endl;
}
mutateBot has not be fixed: replace = by == for comparison.
void moveBot() {
for (int i = 0; i < 50; ++i) {
manualMoveBot(botID);
}
}
void manualMoveBot(int botID) {
const int oldX = bots[botID].x_cord;
const int oldY = bots[botID].y_cord;
int newX = oldX;
int newY = oldY;
int direction = rand() % 4 + 1;
if (direction == 1) { // NORTH
newY = oldY - 1;
} else if (direction == 2) { // EAST
newX = oldX + 1;
} else if (direction == 3) { // SOUTH
newY = oldY + 1;
} else if (direction == 4) { // WEST
newX = oldX - 1;
}
if (newX < 0 || 40 <= newX
|| newY < 0 || 70 <= newY
|| mapBot[newX][newY] == 1) {
cout << "error moving bot";
#if 0
// if this code is active, we may have infinite loop
// when bot is completely surrounded.
manualMoveBot(int botID);
#endif
return;
}
bots[botID].x_cord = newX;
bots[botID].y_cord = newY;
mapBot[newX][newY] = 1;
mapBot[oldX][oldY] = 0;
gatherEnergy(botID);
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am getting this error when compiling:
project6.cpp: In function ‘int main()’:
project6.cpp:187: error: expected ‘}’ at end of input
However, there is clearly an end bracket to my int main () function at that line, so I am confused as to why I am getting this error. I also checked all of my other brackets and found none that were not closed. Any help would be much appreciated!
#include <iostream>
using namespace std;
void initRace(char grid[52][72])
{
for(int r = 0; r < 52; r = r + 1)
{ for(int c = 0; c < 72; c = c + 1)
{ grid[r][0] = 'X';
grid[0][c] = 'X';
grid[r][71] = 'X'; // border
grid[51][c] = 'X';
}
for(int c = 65; c <= 70; c = c + 1)
{ grid[51][c] = 'F'; // finish line
}
}
for(int r = 1; r <= 35; r = r + 1)
{ for(int c = 10; c <= 29; c = c + 1)
{ grid[r][c] = 'X'; // first barrier
}
}
for(int r = 16; r <= 50; r = r + 1)
{ for(int c = 40; c <=64; c = c + 1)
{ grid[r][c] = 'X'; //second barrier
}
}
for(int r = 1; r <= 50; r = r + 1)
{ for(int c =1; c <=9; c = c + 1)
{ grid[r][c] = ' '; //first block of spaces
}
}
for(int r = 36; r <= 50; r = r + 1)
{ for(int c =10; c <=29; c = c + 1)
{ grid[r][c] = ' '; //second block of spaces
}
}
for(int r = 1; r <= 50; r = r + 1)
{ for(int c =30; c <=39; c = c + 1)
{ grid[r][c] = ' '; //third block of spaces
}
}
for(int r = 1; r <= 15; r = r + 1)
{ for(int c =40; c <=64; c = c + 1)
{ grid[r][c] = ' '; //fourth block of spaces
}
}
for(int r = 1; r <= 50; r = r + 1)
{ for(int c =65; c <=70; c = c + 1)
{ grid[r][c] = ' '; //fifth block of spaces
}
}
grid[1][1] = 'O';
}
void printRace(char grid[52][72])
{ for (int i = 0 ; i < 52; i = i + 1)
{ for (int j = 0 ; j < 72; j = j + 1)
{ cout << grid[i][j] << " ";
}
cout << endl;
}
}
int main(void)
{ char grid[52][72];
initRace(grid);
int xAcceleration;
int yAcceleration;
int xVelocity = 0;
int yVelocity = 0;
int xPosition = 1;
int yPosition = 1;
for(int i = 1; i < 100; i = i + 1)
{ printRace(grid);
cout << "Horizontal and vertical acceleration (-1,0,1): ";
cin >> xAcceleration;
cin >> yAcceleration;
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}
if((yAcceleration != 0) && (yAcceleration != 1) && (yAcceleration != -1))
{ printRace(grid);
if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
}
i = 500;
}
xVelocity = xVelocity + xAcceleration;
yVelocity = yVelocity + yAcceleration;
xPosition = xPosition + xVelocity;
yPosition = yPosition + yVelocity;
if((xPosition >= 10) && (xPosition <=29) && (yPosition >= 1) && (yPosition<= 35))
{ grid[yPosition][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl; // crashed into first barrier
i = 500;
}
if((xPosition >= 40) && (xPosition <= 64) && (yPosition >= 16) && (yPosition <= 50))
{ grid[yPosition][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl; // crashed into second barrier
i = 500;
}
if(xPosition <= 0) //crashed into left border
{ grid[yPosition][0] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if(yPosition <= 0) //crashed into top border
{ grid[0][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if(xPosition >= 71) //crashed into right border
{ grid[yPosition][71] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if((yPosition >= 51) && (xPosition >= 1) && (xPosition <= 39)) //crashed into bottom border
{ grid[51][xPosition] = 'O';
printRace(grid);
cout << "Crashed after " << i << " seconds" << endl;
i = 500;
}
if((xPosition >= 65) && (xPosition <= 70) && (yPosition >= 51)) // crossed finish line
{ grid[51][xPosition] = 'O';
printRace(grid);
cout << "Finished after " << i << " seconds" << endl;
i = 500;
}
grid[yPosition][xPosition] = 'O';
}
return 0;
} // THIS IS LINE 187
One of your else blocks does not have a closing brace... Look below:
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
Due to this, the total number of closing braces is not equal to the opening braces, hence the error.
The first else statement in the big for-loop misses a closing brace.
The error is here:
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}
The else block has not been closed.
You are missing a closing curly bracket } in the else clause below:
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}
Should be:
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}
}
Add a closing brace in the section below(at marked line):
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{
cout << "Crashed after " << i << " second" << endl;
}
else
{
cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}//<-- This was missing
}
I think this block is missing a proper ending bracket,
if((xAcceleration != 0) && (xAcceleration != 1) && (xAcceleration != -1))
{ if(i == 1)
{ cout << "Crashed after " << i << " second" << endl;
}
else
{ cout << "Crashed after " << i << " seconds" << endl;
printRace(grid);
i = 500;
}