migcak's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
Members
Active Posts:
45 (0.06 per day)
Joined:
15-August 12
Profile Views:
510
Last Active:
User is offline Aug 09 2014 09:33 AM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Windows
Favorite Browser:
Chrome
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Playstation
Your Car:
Dodge
Dream Kudos:
0
Icon   migcak Feeling optimistic

Posts I've Made

  1. In Topic: Error converting game into a two-player game

    Posted 8 Aug 2014

    The solutions work as expected and the game runs good.
    //Global variables
    enum GameState {menu, level};
    
    GameState CurrentState = menu;
    
    void Button::handle_input()
    {
    	int x = 0, y = 0;
    
    	if(event.type == SDL_MOUSEBUTTONDOWN)
    	{
    		if(event.button.button == SDL_BUTTON_LEFT)
    		{
    			x = event.button.x;
    			y = event.button.y;
    		}
    
    			if((x > box.x) && (x < box.x + box.w) && (y > box.y) && (y < box.y + box.h))
    			{
    				if(message != NULL)
    					SDL_FreeSurface(message);
    
    				message = NULL;
    
    				if(button != NULL)
    					SDL_FreeSurface(button);
    
    				button = NULL;
    
    				apply_surface(0, 0, background, screen);
    				apply_surface(0, 0, board, screen);
    
    				CurrentState = level;
    			}
    
    			
    	}
    }
    
    int main(int argc, char* args[])
    {
    	bool quit = false;
    
    	int x, y;
    
    	bool isFilled[9] = {false, false, false, false, false, false, false, false, false};
    
    	enum Player_t {player1, player2};
    
    	Player_t current = player1;
    
    	SDL_Rect box[9];
    
    	box[0].x = 15;
    	box[0].y = 15;
    	box[0].w = 150;
    	box[0].h = 150;
    
    	box[1].x = 178;
    	box[1].y = 15;
    	box[1].w = 156;
    	box[1].h = 150;
    
    	box[2].x = 347;
    	box[2].y = 15;
    	box[2].w = 150;
    	box[2].h = 150;
    
    	box[3].x = 15;
    	box[3].y = 180;
    	box[3].w = 150;
    	box[3].h = 150;
    
    	box[4].x = 178;
    	box[4].y = 178;
    	box[4].w = 156;
    	box[4].h = 150;
    
    	box[5].x = 347;
    	box[5].y = 178;
    	box[5].w = 150;
    	box[5].h = 150;
    
    	box[6].x = 15;
    	box[6].y = 347;
    	box[6].w = 150;
    	box[6].h = 150;
    
    	box[7].x = 178;
    	box[7].y = 347;
    	box[7].w = 156;
    	box[7].h = 150;
    
    	box[8].x = 347;
    	box[8].y = 347;
    	box[8].w = 150;
    	box[8].h = 150;
    
    	Button myButton(250, SCREEN_HEIGHT / 2, 200, 125);
    
    	if(init() == false)
    		return 1;
    
    	if(load_files() == false)
    		return 1;
    
    	while(quit == false)
    	{
    		if(SDL_PollEvent(&event))
    		{
    			if(event.type == SDL_QUIT)
    				quit = true;
    		}
    
    		switch(CurrentState)
    		{
    			case menu:
    				message = TTF_RenderText_Solid(font, "Welcome to my Tic Tac Toe Game", textColor);
    
    				if(message == NULL)
    					return 1;
    
    				apply_surface(0, 0, background, screen);
    				apply_surface(75, 150, message, screen);
    
    				myButton.show();
    				myButton.handle_input();
    				break;
    
    			case level:
    
    				if(event.type == SDL_MOUSEBUTTONDOWN)
    				{
    					x = event.button.x;
    					y = event.button.y;
    
    					if(event.button.button == SDL_BUTTON_LEFT)
    					{
    						for(int index = 0; index < 9; index++)
    						{
    							if((x > box[index].x) && (x < box[index].x + box[index].w) && (y > box[index].y) && (y < box[index].y + box[index].h))
    							{
    								if(current == player1)
    								{
    									if(isFilled[index] == false)
    									{
    										apply_surface(box[index].x, box[index].y, xPiece, screen);
    
    										isFilled[index] = true;
    
    										current = player2;
    									}
    								}
    
    								else if(current == player2)
    								{
    									if(isFilled[index] == false)
    									{
    										apply_surface(box[index].x, box[index].y, oPiece, screen);
    
    										isFilled[index] = true;
    	
    										current = player1;
    									}
    								}
    							}
    						}
    					}
    				}
    				break;
    
    			default:
    				break;
    		}
    
    		if(SDL_Flip(screen) == -1)
    			return 1;
    	}
    
    	clean_up();
    
    	return 0;
    }
    
    
  2. In Topic: Error converting game into a two-player game

    Posted 8 Aug 2014

    View Post§ingulαrity, on 07 August 2014 - 08:17 PM, said:

    Your code is missing two important aspects to ensure correct continuity. Firstly, the game doesn't keep track of whether you're on the menu or in the game, so it places x's or o's whether you're in-game or not. Secondly, it doesn't keep track of which boxes are already filled, allowing one to overwrite any box that's already filled. While you've made the menu properly disappear, what you've failed to do is make sure the game acts accordingly.

    I thought of a solution to both problems though they could be wrong. Could you let me know if there are any other solutions, I'd like to know my options and learn from this too. For the first problem a switch statement with two cases should solve the problem. The first case is called menu and the second is called level, for example. I could make a global enumator and on line 196 set the second case to be true because that's after the player clicks the play button and a board appears. The first case should be set in the beginning. For the second problem I could make a char array and in the if statement on line 303 I could add if there is a x or an o in that spot marked by the variable index. I'll post what happened later.
  3. In Topic: Error converting game into a two-player game

    Posted 7 Aug 2014

    This is what happens. When the game starts the player sees the menu. Here's an example. Game menu The player can click on the button to play the game but let's say they click somewhere on menu then the game piece image will show which is a problem or if they click the button a x or an o game piece will appear on the board before the player actually selects a square to put the game piece. but I could put the code in the handle_input() for the button class and it should work right. When the board appears, the player can click on the squares and a x or an o game piece will appear on the square. They appear with no predictable pattern and sometimes clicking on a square makes the previous game pieces go away. Here are examples of what it looks like. Game board 1 Game board 2 Game board 3 Here's all the code for the game just in case but what's important is lines 170 and below if you ask me.

    #include "SDL.h"
    #include "SDL_image.h"
    #include "SDL_ttf.h"
    #include <string>
    #include <fstream>
    using namespace std;
    
    //Screen information
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 520;
    const int SCREEN_BPP = 32;
    
    SDL_Surface *background = NULL; //Points to the background for the game
    SDL_Surface *message = NULL;    //Points the string
    SDL_Surface *button = NULL;     //Points the button image
    SDL_Surface *xPiece = NULL;     //Points the x image
    SDL_Surface *oPiece = NULL;     //Points the o image
    SDL_Surface *board = NULL;      //Points the game board
    SDL_Surface *screen = NULL;     //Points the screen
    
    SDL_Color textColor = {255, 255, 255}; //Color of the message, white in this case
    
    TTF_Font *font = NULL; //Used for the string
    
    SDL_Event event;
    
    //Holds the button coordinates and allows the player to click the button
    class Button
    {
    	private:
    		SDL_Rect box;
    
    	public:
    		Button(int x, int y, int w, int h);
    
    		void handle_input(); //Lets the player click the button
    
    		void show();//Puts the button on the screen
    };
    
    //Loads the image and formats it properly. Code by Lazy Foo
    SDL_Surface* load_image(string fileName)
    {
    	SDL_Surface * loadedImage = NULL;
    	SDL_Surface *optimizedImage = NULL;
    
    	loadedImage = IMG_Load(fileName.c_str());
    
    	if(loadedImage != NULL)
    	{
    		optimizedImage = SDL_DisplayFormat(loadedImage);
    
    		SDL_FreeSurface(loadedImage);
    
    		if(optimizedImage != NULL)
    		{
    			SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF));
    		}
    	}
    
    	return optimizedImage;
    }
    
    //Puts the image on the screen. Code by Lazy Foo
    void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clips = NULL)
    {
    	SDL_Rect offset;
    
    	offset.x = x;
    	offset.y = y;
    
    	SDL_BlitSurface(source, clips, destination, &offset);
    }
    
    //Initializes everything for SDL. Returns true if everything is good. Code by Lazy Foo 
    bool init()
    {
    	if(SDL_Init(SDL_INIT_EVERYTHING == -1))
    		return false;
    
    	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    
    	if(screen == NULL)
    		return false;
    
    	if(TTF_Init() == -1)
    		return false;
    
    	SDL_WM_SetCaption("practice game", NULL);
    
    	return true;
    }
    
    //Calls load_image and assigns SDL_Surface variables. Returns true if everything is good. Code by Lazy Foo 
    bool load_files()
    {
    	background = load_image("bluebackground.jpg");
    
    	board = load_image("tic-tac-toe-board.png");
    
    	button = load_image("Fplaybutton.bmp");
    
    	xPiece = load_image("game-piece-x.bmp");
    
    	oPiece = load_image("game-piece-o.bmp");
    
    	white = load_image("white.bmp");
    
    	orange = load_image("orange.bmp");
    
    	font = TTF_OpenFont("lazy.ttf", 30);
    
    	if(background == NULL)
    		return false;
    
    	if(board == NULL)
    		return false;
    
    	if(button == NULL)
    		return false;
    
    	if(xPiece == NULL)
    		return false;
    
    	if(oPiece == NULL)
    		return false;
    
    	if(white == NULL)
    		return false;
    
    	if(orange == NULL)
    		return false;
    
    	if(font == NULL)
    		return false;
    
    	return true;
    }
    
    //Cleans SDL_Surface variables and closes SDL and TTF. Code by Lazy Foo 
    void clean_up()
    {
    	SDL_FreeSurface(background);
    
    	SDL_FreeSurface(board);
    
    	SDL_FreeSurface(xPiece);
    
    	SDL_FreeSurface(oPiece);
    
    	SDL_FreeSurface(white);
    
    	SDL_FreeSurface(orange);
    
    	TTF_CloseFont(font);
    
    	TTF_Quit();
    
    	SDL_Quit();
    }
    
    Button::Button(const int x, const int y, int w, int h)
    {
    	box.x = x;
    	box.y = y;
    	box.w = w;
    	box.h = h;
    }
    
    void Button::handle_input()
    {
    	int x = 0, y = 0;
    
    	if(event.type == SDL_MOUSEBUTTONDOWN)
    	{
    		if(event.button.button == SDL_BUTTON_LEFT)
    		{
    			x = event.button.x;
    			y = event.button.y;
    		}
    
    			if((x > box.x) && (x < box.x + box.w) && (y > box.y) && (y < box.y + box.h))
    			{
    				if(message != NULL)
    					SDL_FreeSurface(message);
    
    				message = NULL;
    
    				if(button != NULL)
    					SDL_FreeSurface(button);
    
    				button = NULL;
    
    				apply_surface(0, 0, background, screen);
    				apply_surface(0, 0, board, screen);
    			}	
    	}
    }
    
    void Button::show()
    {
    	apply_surface(box.x, box.y, button, screen);
    }
    
    int main(int argc, char* args[])
    {
    	//Flag that determines when the game should stop
    	bool quit = false;
    
    	//Used to determine the players' turn
    	enum Player_t {player1, player2};
    
    	Player_t current = player1;
    
    	//Used to hold mouse coordinate
    	int x, y;
    
    	SDL_Rect box[9]; //Holds the position, width, and height for each square in the Tic Tac Toe board
    
    	box[0].x = 15;
    	box[0].y = 15;
    	box[0].w = 150;
    	box[0].h = 150;
    
    	box[1].x = 178;
    	box[1].y = 15;
    	box[1].w = 156;
    	box[1].h = 150;
    
    	box[2].x = 347;
    	box[2].y = 0;
    	box[2].w = 150;
    	box[2].h = 150;
    
    	box[3].x = 15;
    	box[3].y = 180;
    	box[3].w = 150;
    	box[3].h = 150;
    
    	box[4].x = 178;
    	box[4].y = 178;
    	box[4].w = 156;
    	box[4].h = 150;
    
    	box[5].x = 347;
    	box[5].y = 178;
    	box[5].w = 150;
    	box[5].h = 150;
    
    	box[6].x = 15;
    	box[6].y = 347;
    	box[6].w = 150;
    	box[6].h = 150;
    
    	box[7].x = 178;
    	box[7].y = 347;
    	box[7].w = 156;
    	box[7].h = 150;
    
    	box[8].x = 347;
    	box[8].y = 347;
    	box[8].w = 150;
    	box[8].h = 150;
    
    	Button myButton(250, SCREEN_HEIGHT / 2, 200, 125);
    
    	if(init() == false)
    		return 1;
    
    	if(load_files() == false)
    		return 1;
    
    	message = TTF_RenderText_Solid(font, "Welcome to my Tic Tac Toe Game", textColor); //Create a message 
    
    	if(message == NULL)
    		return 1;
    
    	apply_surface(0, 0, background, screen); //Applies the background on the screen
    	apply_surface(75, 150, message, screen); //Applies the message on the screen
    
    	while(quit == false)
    	{
    		if(SDL_PollEvent(&event))
    		{
    			myButton.handle_input();
    
    			if(event.type == SDL_QUIT)
    				quit = true;
    		}
    
    		myButton.show();
    		
    		//Allows the player to click on the screen. 
    		if(event.type == SDL_MOUSEBUTTONDOWN)
    		{
    			x = event.button.x;
    			y = event.button.y;
    
    			if(event.button.button == SDL_BUTTON_LEFT)
    			{
    				for(int index = 0; index < 9; index++)
    				{
    					if((x > box[index].x) && (x < box[index].x + box[index].w) && (y > box[index].y) && (y < box[index].y + box[index].h))
    					{
    						if(current == player1) //Check to see if player1 turn is true
    						{
    							apply_surface(box[index].x, box[index].y, xPiece, screen); //Puts the x image on the correct square of the game board
    
    							current = player2; //Allows the else if statement to be true 
    						}
    
    						else if(current == player2) //Check to see if player2 turn is true
    						{
    							apply_surface(box[index].x, box[index].y, oPiece, screen);//Puts the o image on the correct square of the game board
    
    							current = player1; //Allows the if statement to be true
    						}
    					}
    				}
    			}
    		}
    					
    		if(SDL_Flip(screen) == -1)
    			return 1;
    	}
    
    	clean_up();
    
    	return 0;
    }
    
    
  4. In Topic: Error converting game into a two-player game

    Posted 7 Aug 2014

    It isn't consistent. Basically, it might show 6 o's and three x's on the board. I can post a picture to show you if you want.
  5. In Topic: Error converting game into a two-player game

    Posted 6 Aug 2014

    I shorten the code a bit. The game doesn't switch between x's and o's correctly though. What happens is that it toggles between the x image and o image. For example, if I click on a space it might be x's or it might be o's.
             if(event.type == SDL_MOUSEBUTTONDOWN)
    		{
    			x = event.button.x;
    			y = event.button.y;
    
    			if(event.button.button == SDL_BUTTON_LEFT)
    			{
    				for(int index = 0; index < 9; index++)
    				{
    					if((x > box[index].x) && (x < box[index].x + box[index].w) && (y > box[index].y) && (y < box[index].y + box[index].h))
    					{
    						if(current == player1)
    						{
    							apply_surface(box[index].x, box[index].y, xPiece, screen);
    
    							current = player2;
    						}
    
    						else if(current == player2)
    						{
    							apply_surface(box[index].x, box[index].y, oPiece, screen);
    
    							current = player1;
    						}
    					}
    				}
    			}
    		}
    

My Information

Member Title:
New D.I.C Head
Age:
21 years old
Birthday:
February 23, 1993
Gender:
Location:
Tennessee
Interests:
Artificial Intelligence
C++ programing
game programming
Years Programming:
2
Programming Languages:
C++
C#
Python
LUA

Contact Information

E-mail:
Click here to e-mail me
Yahoo:
Yahoo  george_hunter86@yahoo.com

Friends

Comments

migcak has no profile comments yet. Why not say hello?