Jump to content
  • Advertisement
Sign in to follow this  
soitsthateasy

Bug In My Code? Help M Out

This topic is 3175 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey!!! would someone please look over my code??!?!?! If you enter a char, it just puts an x and an O in all bottom row spots??!?!? HELP ME!!!
#include<iostream>
using namespace std;
void display();
bool check(int a, int b);
int drop(int b, char player);
char place[6][7];//available for whole program

int main(){
    for(int a =0;a <= 5; a++){        //fill place with whitespace
        for(int b = 0; b<=6; b++)    //
            place[a] = ' ';        //
    }                                //
    display();//Displays for first time so players can see the board
    int hold;//Will house user row choice
    int hold2 = 0;//will hold drop value
    int charsPlaced = 0;//Number of peices dropped so can end game if a draw
    bool gamewon = false;//Will be changed to true when game is won and will exit while loop
    char player = 15;//start as player 2 will change back 2 player 1
    while(!gamewon){//will stop when game is won, ! means NOT makes the oppisite be checked
        if(hold2 != -1){//check if there was a error in the last drop
            if(player == 15){//if player 2 lasted dropped a piece so its player 1s turn
                cout<<"player 1 drop where?";
                player = 254;//char of players piece
            }
            else{
                cout<<"player 2 drop where?";
                player = 15;//char of player piece
            }
        }
        while(true){//will run untill 'break;'
            if(charsPlaced == 42) break;//if draw
            cin>>hold;//get user input
            hold--;//take off 1 to account for arrays starting at 0 not 1
            if(hold <=6 && hold>= 0) break;//if within valid range stop loop
            else cout<< "\nplease enter a value between 1 and 7 :";//ask for input and loop again
            if (cin.fail())    //catch a non number
            {                        //
                cin.clear();        //Stops cin trying to put its value in to hold
                char c;            //Try entering a non number without this, 2 see what this does
                cin>>c;            //
            }                        //Catch a non number

        }
        if(charsPlaced == 42) break;//if draw
        hold2 = drop(hold,player);//drop the player store the row in hold2
        if(hold2 == -1)    cout<<"Colom is full\nPlease enter anothor number between 1 and 7:";//if error -1 row is full
        else{
            gamewon = check(hold2,hold);//check if game is run
            charsPlaced ++;//another character has been succesfully placed
            system("cls");//This clears the screen works with windows, not nesscery to run game
            display();//displayed updated board
        }
    }
    system("cls");//this clears the screen
    if(charsPlaced == 42){//if draw
        cout<<"No winner, Game was draw\n";
        system("pause");
        return 0;
    }
    if(player == 15)//if won by player 2
        cout<<"gamewon by : player 2\n";
    else cout<<"gamewon by : player 1\n";//Else won by player 1
    system("pause");//pauses before exit so players can see who won, works with windows
    return 0;//Exit application
}
void display(){
    cout<<" 1   2   3   4   5   6   7\n";
    for(int a = 0; a<= 5; a++)
    {
        for(int b =0; b <= 6; b++) cout<<char(218)<<char(196)<<char(191)<<" ";
        cout<<'\n';
        for(int b =0; b <= 6; b++) cout<<char(179)<<place[a]<<char(179)<<" ";
        cout<<'\n';
        for(int b =0; b <= 6; b++) cout<<char(192)<<char(196)<<char(217)<<" ";
        cout<<'\n';
    }
}
bool check(int a, int b){
    int vertical = 1;//(|)
    int horizontal = 1;//(-)
    int diagonal1 = 1;//(\)
    int diagonal2 = 1;//(/)
    char player = place[a];
    int i;//vertical
    int ii;//horizontal
    //check for vertical(|)
    for(i = a +1;place == player && i <= 5;i++,vertical++);//Check down
    for(i = a -1;place == player && i >= 0;i--,vertical++);//Check up
    if(vertical >= 4)return true;
    //check for horizontal(-)
    for(ii = b -1;place[a][ii] == player && ii >= 0;ii--,horizontal++);//Check left
    for(ii = b +1;place[a][ii] == player && ii <= 6;ii++,horizontal++);//Check right
    if(horizontal >= 4) return true;
    //check for diagonal 1 (\)
    for(i = a -1, ii= b -1;place[ii] == player && i>=0 && ii >=0; diagonal1 ++, i --, ii --);//up and left
    for(i = a +1, ii = b+1;place[ii] == player && i<=5 && ii <=6;diagonal1 ++, i ++, ii ++);//down and right
    if(diagonal1 >= 4) return true;
    //check for diagonal 2(/)
    for(i = a -1, ii= b +1;place[ii] == player && i>=0 && ii <= 6; diagonal2 ++, i --, ii ++);//up and right
    for(i = a +1, ii= b -1;place[ii] == player && i<=5 && ii >=0; diagonal2 ++, i ++, ii --);//up and left
    if(diagonal2 >= 4) return true;
    return false;
}
int drop(int b, char player){
    if(b >=0 && b<= 6)
    {
        if(place[0] == ' '){
            int i;
            for(i = 0;place == ' ';i++)
                if(i == 5){place = player;
            return i;}
            i--;
            place =player;
            return i;

        }
        else{
            return -1;
        }

    }
    else{
        return -1;
    }

}



Share this post


Link to post
Share on other sites
Advertisement
After making your code cleaner to look at, the game worked. Try to pickup some better styling as your original code looked horrible which makes it hard to debug. The only thing I believe I changed is using std::endl instead of "\n" in your display function. This leads me to believe that the code was just being compiled wrong due to ugly code, though I may be wrong.


#include <iostream>

using namespace std;

void display();
bool check(int a, int b);
int drop(int b, char player);

char place[6][7]; //available for whole program

int main()
{
//fill place with whitespace
for(int a = 0; a <= 5; a++)
for(int b = 0; b <= 6; b++)
place[a] = ' ';

display(); //Displays for first time so players can see the board

int hold; //Will house user row choice
int hold2 = 0; //will hold drop value
int charsPlaced = 0; //Number of peices dropped so can end game if a draw
bool gamewon = false; //Will be changed to true when game is won and will exit while loop
char player = 15; //start as player 2 will change back 2 player 1

//will stop when game is won, ! means NOT makes the oppisite be checked
while(!gamewon)
{
//check if there was a error in the last drop
if(hold2 != -1)
{
//if player 2 lasted dropped a piece so its player 1s turn
if(player == 15)
{
cout << "player 1 drop where?";
player = 254; //char of players piece
}
else
{
cout << "player 2 drop where?";
player = 15; //char of player piece
}
}

//will run untill 'break;'
while(true)
{
if(charsPlaced == 42)
break; //if draw

cin >> hold; //get user input
hold--; //take off 1 to account for arrays starting at 0 not 1

if(hold <= 6 && hold >= 0)
break; //if within valid range stop loop
else
cout<< "\nplease enter a value between 1 and 7 :"; //ask for input and loop again

if (cin.fail()) //catch a non number
{
cin.clear(); //Stops cin trying to put its value in to hold
char c; //Try entering a non number without this, 2 see what this does
cin >> c;
} //Catch a non number

}

if(charsPlaced == 42)
break; //if draw

hold2 = drop(hold, player); //drop the player store the row in hold2

if(hold2 == -1)
cout << "Colom is full\nPlease enter anothor number between 1 and 7:"; //if error -1 row is full

else
{
gamewon = check(hold2, hold); //check if game is run
charsPlaced++; //another character has been succesfully placed
system("cls"); //This clears the screen works with windows, not nesscery to run game
display(); //displayed updated board
}
}

system("cls"); //this clears the screen

//if draw
if(charsPlaced == 42)
{
cout << "No winner, Game was draw\n";
system("pause");
return 0;
}

if(player == 15) //if won by player 2
cout << "gamewon by : player 2\n";

else
cout << "gamewon by : player 1\n"; //Else won by player 1

system("pause"); //pauses before exit so players can see who won, works with windows
return 0; //Exit application
}

void display()
{
cout << " 1 2 3 4 5 6 7\n";
for(int a = 0; a <= 5; a++)
{
for(int b = 0; b <= 6; b++)
cout << char(218) << char(196) << char(191) << " ";
cout << endl;
for(int b = 0; b <= 6; b++)
cout << char(179) << place[a] << char(179) << " ";
cout << endl;
for(int b = 0; b <= 6; b++)
cout << char(192) << char(196) << char(217) << " ";
cout << endl;
}
}

bool check(int a, int b)
{
int vertical = 1; //(|)
int horizontal = 1; //(-)
int diagonal1 = 1; //(\)
int diagonal2 = 1; //(/)
char player = place[a];
int i; //vertical
int ii; //horizontal

//check for vertical(|)
for(i = a + 1; place == player && i <= 5; i++, vertical++); //Check down
for(i = a - 1; place == player && i >= 0; i--, vertical++); //Check up

if(vertical >= 4)
return true;

//check for horizontal(-)
for(ii = b - 1; place[a][ii] == player && ii >= 0; ii--, horizontal++); //Check left
for(ii = b + 1; place[a][ii] == player && ii <= 6; ii++, horizontal++); //Check right

if(horizontal >= 4)
return true;

//check for diagonal 1 (\)
for(i = a - 1, ii = b - 1; place[ii] == player && i >= 0 && ii >= 0; diagonal1++, i--, ii--); //up and left
for(i = a + 1, ii = b + 1; place[ii] == player && i <= 5 && ii <= 6; diagonal1++, i++, ii++); //down and right

if(diagonal1 >= 4)
return true;

//check for diagonal 2(/)
for(i = a - 1, ii = b + 1; place[ii] == player && i >=0 && ii <= 6; diagonal2++, i--, ii++); //up and right
for(i = a + 1, ii = b - 1; place[ii] == player && i <=5 && ii >= 0; diagonal2++, i++, ii--); //up and left

if(diagonal2 >= 4)
return true;

return false;
}

int drop(int b, char player)
{
if(b >= 0 && b <= 6)
{
if(place[0] == ' ')
{
int i;
for(i = 0; place == ' '; i++)
{
if(i == 5)
{
place = player;
return i;
}
}

i--;
place = player;
return i;
}
else
return -1;
}
else
return -1;
}




Share this post


Link to post
Share on other sites
So the problem was with this line

cin>>hold;

When you get enter a character other than a number it will return -somebignumber;

So by replacing that line with this


char GET;
cin>>GET;//get user input
hold = atoi(&GET);
hold--;//take off 1 to account for arrays starting at 0 not 1





That'll fix the problem

Jake

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!