C++/SDL, Tic Tac Toe turn taking - c++

Alright so I have this tic tac toe game I'm making with SDL and C++. I'm trying to implement AI into the game. I don't have a problem setting up the AI, but I have a problem making it where you can take turns. My problem is that when I make my move, I can just move as many times as I want before the AI moves. I want it so I can make my move, and I can't make my move again until the AI makes a move. No matter what I do it seems the turn taking doesn't work properly.
This is the class header
class Buttons
{
private:
SDL_Rect squares[8];
public:
Buttons();
void handle_input();
void load_Squares(SDL_Rect sqRects, SDL_Texture* squarTexs);
void show_Squares();
void AI_move();
int grid[9];
bool moveMade = true;
};
Here I check for mouse input, and depending on the location during the left button press, it sets the according grid value to equal 1, meaning it becomes displayed as a circle on the screen. I also make sure that the AI has made a move before it allows me to click.
void Buttons::handle_input()
{
double mouseX = 0, mouseY = 0;
if((event.type == SDL_MOUSEBUTTONDOWN))
{
//If left mouse button was clicked and AI has made a move
if(event.button.button == SDL_BUTTON_LEFT && moveMade == true)
{
//Get mouse location
mouseX = event.button.x;
mouseY = event.button.y;
//If mouse location is in particular square, set according grid value to 1
if((mouseX >= 0) && (mouseX < SCREEN_WIDTH / 3) && (mouseY >= 0) && (mouseY < SCREEN_HEIGHT / 3) && (grid[0] == 0))
{
grid[0] = 1;
moveMade = false;
}
//Basically does this for all other 9 grids
Here is my AI function, where I check to make sure the moveMade variable = false. Every time I make a move in the input function above, it sets moveMade to false, which means it should access this function, and only until it finishes this AI_move function should I be able to make a move again, because moveMade is set back equal to true.
void Buttons::AI_move()
{
if(moveMade == false)
{
AI_block(&moveMade);
AI_complete(&moveMade);
AI_rand(&moveMade);
moveMade = true;
}
}
Last is my show function, where I show a Circle(player) if the grid array value = 1, and I show the X(AI) if the grid value = 2.
void Buttons::show_Squares()
{
switch(grid[0])
{
case 1:
load_Squares(squares[0], circleTexture); break;
case 2:
load_Squares(squares[0], xTexture); break;
}
switch(grid[1])
{
//Does this all the way to grid[8]
}
Alright so my problem doesn't have to do with the AI dealing accordingly, as I haven't even set up my defense and offense functions. My problem is that I can make another move before the AI moves. Sorry if this is way too long, but if I could get any feedback on this that would be great.

Have you tried putting breakpoints at various points such as if(event.button.button == SDL_BUTTON_LEFT && moveMade == true) and then following the program through the see if moveMade ever actually gets changed to false?
You should also look at changing show_Squares() into a loop as there is a lot of repeated code using incremented indexes. Something like this:
void Buttons::show_Squares()
{
size_t array_size = sizeof(squares) / sizeof(int); //gets the number of elements in the array
for(size_t i = 0; i < array_size; i++)
{
switch(grid[i])
{
case 1:
load_Squares(squares[i], circleTexture); break;
case 2:
load_Squares(squares[i], xTexture); break;
}
}
}

Related

C++ and SDL2 Grid movement is too fast

My grid movement is too fast. This problem is due to speed, but I can't change it. Because then the grid doesn't work.
if (Game::event.type == SDL_KEYDOWN) {
if (Game::event.key.keysym.sym == SDLK_w || Game::event.key.keysym.sym == SDLK_UP) {
transform->velocity.y = -1;
}
else if (Game::event.key.keysym.sym == SDLK_s || Game::event.key.keysym.sym == SDLK_DOWN) {
transform->velocity.y = 1;
}
else if (Game::event.key.keysym.sym == SDLK_d || Game::event.key.keysym.sym == SDLK_RIGHT) {
transform->velocity.x = 1;
}
else if (Game::event.key.keysym.sym == SDLK_a || Game::event.key.keysym.sym == SDLK_LEFT) {
transform->velocity.x = -1;
}
} else if (Game::event.type == SDL_KEYUP) {
transform->velocity.x = 0;
transform->velocity.y = 0;
}
And update for player position:
void update() override {
position.x += round(velocity.x * 32);
position.y += round(velocity.y * 32);
}
Problem is in update player position, but if there weren't *32 player gets out from grid. (32 is grid size)
Any idea how to solve it?
And yes, I use SDL_Delay.
If you want only one step per press then in your update() function you need to zero your velocity vector after you're done using it, that will make it step only once, but you also have to check event.key.repeat and only accept events when it's 0 to avoid repeating the event ~30 times/second as long as the key is held down. If you want the stepping to repeat after a given delay then you'll need to store the return value (a uint32_t or Uint32 if you prefer) of SDL_GetTicks(); taken the moment a SDL_KEYUP event was received (with repeats ignored) then check if enough milliseconds have passed to repeat the stepping then add the time threshold to the stored time value to properly wait for the next repeating.
Also you're zeroing your velocity vector every time any key is released, which in some cases you don't want. It might be better to add to the vector for each SDL_KEYDOWN event and subtract to under for each SDL_KEYUP, for matching keys of course. Also if you're gonna use WASD you should use scancodes instead of keysym, or your control scheme will be quite awkward on non-QWERTY layouts.
Also there's probably no need for SDL_Delay(), just use Vsync which is set when you initialise your renderer with something like renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);.

in C++, SFML, How do I draw an animation from another class within a different class?

I am making a street fighter type game for my University project using c++ and SFML, I am new to this so I don't really know what I'm doing.
I wanted to have a teleport button that lets the player teleport 700 pixels away from their current position and have an animation (smoke effect) play when they move.
I have made a separate class for this animation which has an update function that plays each frame. But I don't know how to use the animation from the "Teleport" class in the "Player" class.
Any help would be greatly appreciated...
Code: (the 'isSliding' is a confusing variable name but it's meant to mean teleporting)
if ((input->isKeyDown(sf::Keyboard::D)) && (!isSliding) && (hDir != 0)) //slide: if slide is pressed AND cooldown is finished.
{
cout << "\nSlide pressed!";
cout << "\nElapsed slide time: " + to_string(slideElapsedTime);
if (playerStamina >= slideCost)
{
//isAttacking = true;
isSliding = true;
slideElapsedTime = 0;
input->setKeyUp(sf::Keyboard::D);
if (walk.getFlipped()) //if walk is flipped then flip the slide animation.
{
duck.setFlipped(true);
}
else duck.setFlipped(false);
currentAnimation = &duck; //using duck animation for teleport animation.
currentAnimation->animate(dt);
currentAnimation->reset();
setTextureRect(currentAnimation->getCurrentFrame());
if (isSliding)
{
target = sf::Vector2f(getPosition().x + 700 * hDir, getPosition().y); //set target location (current location + offset)
direction = target - getPosition(); //set direction to travel in (depends on current position)
if (getPosition() != target) //if not at same location...
{
move(direction*dt*scale); //...then move to target location.
}
}
playerStamina -= slideCost; //reduce the stamina
damage = 2; //set damage to be dealt to enemy.
//check collision and apply damage
}
}

I don't get the expected feedback of a basic rectangle collision implementation in C++

Hi I am building a basic game in wich I have to detect collisions between an array of sprites. For now the sprites just move forward and change direction randomly or when reached a screen edge. The game is made in C++ with my college graphich library.
I implemented a basic algorithm for detecting collisions into this function:
bool DetectarColisionTanques(strTanque tanque1, strTanque tanque2) {
if(
(tanque1.px + tanque1.width >= tanque2.px) &&
(tanque1.px <= tanque2.px + tanque2.width) &&
(tanque1.py + tanque1.height >= tanque2.py) &&
(tanque1.py <= tanque2.py + tanque2.height)
) {
return true;
} else {
return false;
}
}
Sorry that code is in spanish, tanque1 and tanque2 are 2 structs containing each sprite coordinates wich I want to see if collide, px and py are the sprite origin at the upper left edge of the image.
But when I try to run the function i don't get the expected feedback:
void MoverEnemigos(){
for (int i=0;i<nEnemigos;i++) {
switch (enemigos[i].dir) {
case 0: enemigos[i].py-=enemigos[i].vel;break;
case 1: enemigos[i].px+=enemigos[i].vel;break;
case 2: enemigos[i].py+=enemigos[i].vel;break;
case 3: enemigos[i].px-=enemigos[i].vel;break;
for (int k= i+1; k<nEnemigos; k++) {
if(DetectarColisionTanques(enemigos[i],enemigos[k])) {
printf("Collision detected");
printf("\n");
}
}
}
enemigos[i].width= esat::SpriteWidth(spriteTanque[enemigos[i].dir]);
enemigos[i].height= esat::SpriteHeight(spriteTanque[enemigos[i].dir]);
}
}
In this function called MoverEnemigos() I move all the sprites px and py depending on their direction and then I try to call the function DetectarColisionTanques() to see if I get the printf at the same time I see in my graphic window that the sprites are obviously colliding. But I never ever get the printf.
PD: All the functions are running in a while at 60fps.

Slowdown when loading the same SpriteFrame used in an animation in Cocos2dx

I am currently experiencing some heavy slowdowns with my game. I have narrowed it down to something related with texture animations.
In my game there are characters that walk in 1 of 4 possible directions, they will walk up to a point, then change direction and continue walking (sort of like a tower defense game).
First i am loading the sprite frame cache like this
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("characters.plist");
This code is only run once during the life time of my application.
When the characters get loaded to the screen their animation is being set using the following code:
int direction = 0;
int number = 0;
if (this->to_x < 0) // Left
{
direction = 1;
number = 1;
}
else if(this->to_x > 0) // Right
{
direction = 2;
number = 1;
}
if (this->to_y < 0) // Down
{
direction = 0;
number = 0;
}
else if(this->to_y > 0) // Up
{
direction = 3;
number = 2;
}
int s = 0; //skin
// Set the animation
Animation *animation = Animation::create();
for (int i = 0; i < INT16_MAX; i++)
{
string frame_sprite_name = StringUtils::format("%s_%d_%d_%d.png",parameters[name].image_name.c_str(),s,number,i);
auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frame_sprite_name);
if (frame) {
animation->addSpriteFrame(frame);
} else {
break;
}
}
// Invert the sprite when they go right
if (direction == 2) {
setFlippedX(true);
}else{
setFlippedX(false);
}
// Set the pace of the animation based on the type
if (name=="runner") {
animation->setDelayPerUnit(0.15f);
} else{
animation->setDelayPerUnit(0.3f);
}
Animate *animate = Animate::create(animation);
this->stopAllActions();
this->runAction(RepeatForever::create(animate));
What this code does is:
Check the direction
Get the sprite frame from the cache based on the direction
Run the action with repeat forever.
However this code is ran every time they change direction to set the new animation of the active characters. Also, at one time I can have around 40-50 of these characters going around.
I've noticed that after a few minutes in the game the slowdown starts to happen as soon as a new "character" is created, (since they are created in rapid succession in waves). And the slowdown also happens when the characters change in direction. So this makes me believe I am using the textures wrong.
If anyone knows how to fix this please let me know.
PD: I was thinking about the possibility of pre-loading all the animations and then just having each of the sprites that represent the characters run the corresponding animation.
You should definitely cache the animation in the AnimationCache with addAnimation and getAnimation methods.

creating combinations of obstacles in a 2D side scroller game

My first game in ALLEGRO 5 with c++.
It has a player that keeps moving continuously in right direction. From the right edge of the screen player faces obstacles like triangles and squares. These obstacles come alive at right edge of screen and die at left edge of screen.
Suppose X is triangle and O is square.
i want to create them in few combinations like
..{x} {xo} {xoox} {oxxo} {oxx}... And some variations(random maybe)
And after that I will randomize the occurence of these pattern.
SO I found a method to implement what i wanted. I used a switch() to randomly select various cases of combination. It works pretty well. But once in a while it overlaps even after I introduced a minimum gap xoff
Here is the code:
`//I have used srand(time(NULL)); once in main function too.
if(state == PLAYING)
{
ball->Moveball();
ball->Jumpball();
//Camera-----------
ball->Cameraupdate();
al_identity_transform(&camera);
al_translate_transform(&camera,-Cameraposition[0],-Cameraposition[1]);
al_use_transform(&camera);
if(rand() % 200==0)
{
xoff+=200;// to introduce a minimum gap between them but this also fail once in a while.
int ch;
ch=rand()%4;
switch(ch)
{
case 0:
//T
triangle = new Triangle(850 + Cameraposition[0]+xoff,319);
objects.push_back(triangle);
triangle->SetAlive(true);
break;
case 1:
//TT
triangle = new Triangle(850 + Cameraposition[0]+xoff,319);
objects.push_back(triangle);
triangle->SetAlive(true);
triangle = new Triangle(850 +30+ Cameraposition[0]+xoff,319);
objects.push_back(triangle);
triangle->SetAlive(true);
break;
case 2:
//S
square = new Square(850 + Cameraposition[0]+xoff,310);
objects.push_back(square);
square->SetAlive(true);
break;
case 3:
//SS
square = new Square(850 + Cameraposition[0]+xoff,310);
objects.push_back(square);
square->SetAlive(true);
square = new Square(850 +82+ Cameraposition[0]+xoff,310);
objects.push_back(square);
square->SetAlive(true);
break;
}
}
`
Problem solved :(For those who experienced similar doubt)The above code solves the problem of creating random combination of obstacles and generating those combinations randomly. But do check for collisions if you don't want to get overlapping combinations.
Just add another for loop:
void createObstacles(int numObstacles)
{
for(int i = 0; i < numObstacles; i++)
{
if(rand() % 2 == 0)
{
// Create a triangle.
}
else
{
// Create a square.
}
}
}
Call this every time you want to create obstacles, controlling the number of objects released at one time by changing the parameter numObstacles.
You can now do:
if(rand() % 200 == 0)
{
// Create anywhere from 1 to maxToCreate (which is 4) obstacles at a time.
const int maxToCreate = 4;
int numToCreate = (rand() % maxToCreate) + 1;
// Call the function to make some obstacles.
createObstacles(numToCreate);
}
Since your shapes are overlapping, either fix your initialization code to initialize them the way