• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
LAURENT*

Oh god! please end my suffering!

5 posts in this topic

I've tried to put AI into this code for so long it's breaking me emotionally.   Please fix my void Game::play(SDL_Surface *screen) function. Please just show me anything. Help me make this program work!.

 

In the code X or the ai takes it turn whenever the mouse moves. I can click to create circles but an x mark will cover it as I move the cursor. The problem lies with in the Game.cpp file. I have a function called void Game::play(SDL_Surface *screen) that calls other void functions during a do while loop. Inside my check_victory function at the bottom it return 1 if nobody won the game which keeps the loop running because continuer=1.

 

 

Header file Game.h

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

enum {EMPTY, CIRCLE, CROSS};
class Game
{
    public:
        Game();
        void play(SDL_Surface* screen);
        //Starts of your voids
		void get_computer_move(SDL_Surface* screen);
		void get_player_move(SDL_Surface* screen);
		void init_grid(SDL_Surface* screen);

		int check_victory(SDL_Surface* screen);
        ~Game();
		//0x004119d9

    private:
        SDL_Surface *board, *cmark, *xmark, *horizontale, *verticale, *diagonale1, *diagonale2;
        SDL_Event event;
        int grid[3][3], continuer, i, j, turn, end, nowinner, ai, ai2;
        SDL_Rect position, positionforme, positionwon, positionforai;
};


#endif // GAME_H_INCLUDED

Cpp file Game.cpp

#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <time.h>
#include "Game.h"


Game::Game()
{
    cmark= IMG_Load("cmark.jpg");
    xmark= IMG_Load("xmark.jpg");
    board= IMG_Load("board.jpg");
    horizontale=IMG_Load("horizontale.jpg");
    verticale=IMG_Load("verticale.jpg");
    diagonale1=IMG_Load("diagonale1.png");
    diagonale2=IMG_Load("diagonale2.png");
}


void Game::init_grid(SDL_Surface* screen)
{
	for(int i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            grid[i][j]=EMPTY;
        }
    }
}
void Game::get_player_move(SDL_Surface* screen)
{	
	if (event.type == SDL_MOUSEBUTTONDOWN )
	{				
		if (grid[event.button.y/70][event.button.x/70] == EMPTY)
					
			positionforme.x=((event.button.x/70)*70)+15;
			positionforme.y=((event.button.y/70)*70)+15;
			//Afficher un rond, si cest au tour du rond
			grid[event.button.y/70][event.button.x/70]=CIRCLE;
			SDL_BlitSurface(cmark, NULL, screen, &positionforme);
			SDL_Flip(screen);
			
	}	
}

void Game::get_computer_move(SDL_Surface* screen)
{
	int ai = rand()% 211;
	int ai2 = rand()% 211;	
	positionforai.x = ((ai/70)*70)+15;
	positionforai.y = ((ai2/70)*70)+15;

		if (grid [ai/70][ai2/70] == EMPTY)
		{							
			grid[ai2/70][ai/70]=CROSS;
			SDL_BlitSurface(xmark, NULL, screen, &positionforai);
			SDL_Flip(screen);
	}
}



void Game::play(SDL_Surface *screen)
{
    int continuer=1, turn=0, end=0;
	
    position.x=0;
    position.y=0;
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
    SDL_BlitSurface(board, NULL, screen, &position);
    SDL_Flip(screen);


	
	while (continuer)
	{
		continuer = EMPTY;
		init_grid(screen);


		SDL_WaitEvent(&event);
		if (event.type == SDL_QUIT) break;
		
		do	
		{		
				
				
				get_player_move(screen);
				continuer= check_victory(screen);
				if (continuer== EMPTY)break;
				get_computer_move(screen);
				continuer =check_victory(screen);
				if (continuer== EMPTY)break;
				

				
			//continuer=check_victory(screen); 
			}while (continuer == EMPTY);	
		
		}
	 }		



		
	


int Game::check_victory(SDL_Surface* screen)
{
    int nowinner=0;
     //It verifies if there is a winner
        for(i=0; i<3; i++)
        {    //horizontal Win Line
            if((grid[i][0]==CROSS && grid[i][1]==CROSS && grid[i][2]==CROSS) || (grid[i][0]==CIRCLE && grid[i][1]==CIRCLE && grid[i][2]==CIRCLE))
            {
                positionwon.x=20;
                positionwon.y=i*70+34;
                SDL_BlitSurface(horizontale, NULL, screen, &positionwon);
                SDL_Flip(screen);
                end=1;
                return 0;
            }
            //Vertical Win Line
            if((grid[0][i]==CROSS && grid[1][i]==CROSS && grid[2][i]==CROSS) || (grid[0][i]==CIRCLE && grid[1][i]==CIRCLE && grid[2][i]==CIRCLE))
            {
                positionwon.x=i*70+34;
                positionwon.y=20;
                SDL_BlitSurface(verticale, NULL, screen, &positionwon);
                SDL_Flip(screen);
                end=1;
                return 0;
            }
        }
        //Diagonal Win Line
        if((grid[0][0]==CROSS && grid[1][1]==CROSS && grid[2][2]==CROSS) || (grid[0][0]==CIRCLE && grid[1][1]==CIRCLE && grid[2][2]==CIRCLE))
        {
            positionwon.x=20;
            positionwon.y=20;
            SDL_BlitSurface(diagonale1, NULL, screen, &positionwon);
            SDL_Flip(screen);
            end=1;
            return 0;
        }
        if((grid[0][2]==CROSS && grid[1][1]==CROSS && grid[2][0]==CROSS) || (grid[0][2]==CIRCLE && grid[1][1]==CIRCLE && grid[2][0]==CIRCLE))
        {
            positionwon.x=22;
            positionwon.y=20;
            SDL_BlitSurface(diagonale2, NULL, screen, &positionwon);
            SDL_Flip(screen);
            end=1; 
            return 0;
        }
        for(i=0; i<3; i++)
        {
            for(j=0; j<3; j++)
            {
                if(grid[i][j]!=EMPTY)
                {
                    nowinner++;
                }
            }
        }
        if(nowinner==9)
        {
            end=1;
            return 0;
        }
        return 1;
}

Game::~Game()
{
    SDL_FreeSurface(horizontale);
    SDL_FreeSurface(verticale);
    SDL_FreeSurface(diagonale1);
    SDL_FreeSurface(diagonale2);
    SDL_FreeSurface(xmark);
    SDL_FreeSurface(cmark);
}
Edited by LAURENT*
0

Share this post


Link to post
Share on other sites

In addition to what Servant of the Lord stated, it seems to me that on the player's turn you don't wait for the player's action.

 

I don't know about SDL and know very little C++, but with my knowledge I'd fix it this way:

void Game::play(SDL_Surface *screen)
{
    int continuer=1, turn=0, end=0;
	
    position.x=0;
    position.y=0;
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
    SDL_BlitSurface(board, NULL, screen, &position);
    SDL_Flip(screen);
	
    while (continuer)
    {
	continuer = EMPTY;
	init_grid(screen);
		
	do	
	{	
                // Wait for the player's action
	        SDL_WaitEvent(&event);
	        if (event.type == SDL_QUIT)
                {
                    // Exit from game
                    continuer = EMPTY;
                    break;
                }

		get_player_move(screen);
		continuer= check_victory(screen);
		if (continuer== EMPTY)  break;

                // Draw stuff

		get_computer_move(screen);
		continuer =check_victory(screen);
		if (continuer== EMPTY)  break;

                // Draw stuff
	}
        while (continuer == EMPTY);	
		
    }
}	
2

Share this post


Link to post
Share on other sites
//You are here, drawing only once.
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
    SDL_BlitSurface(board, NULL, screen, &position);
    SDL_Flip(screen);

 

 

Nah SDL_Fillrect only make a fast fill of rectangle with the same colors. I think 255, 255, 255 is white. SDL_Blitsurface only display my jped image of the board.  

Edited by LAURENT*
0

Share this post


Link to post
Share on other sites

1. Split up your draw calls from your game logic.

2. You're flipping buffers every time you draw something

2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0