• Advertisement
Sign in to follow this  

W00T! I Got It! (But Now I Can't Use It)

This topic is 4170 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

Finally...after much failure, much whining to you guys, and much frustration and typing...I have completed my tic-tac-toe program. Only problem is that I want to be able to actually use it outside visual studio, and it won't let me do that. I did a release build, only problem is when I try to open it, it automatically closes myself. (Maybe SDL can't initialize?) Here's the finished code:
//////////////////////////////////////
#ifdef WIN32						// Only way this will compile for some reason
#pragma comment(lib, "SDL.lib")		//
#pragma comment(lib, "SDLmain.lib")	// Dunno what this does...
#endif								//
//////////////////////////////////////
#include <SDL.h>

enum players {one = 1, two = 2} player;
int boardarray[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
bool Won();
int GetCord(Uint16 x, Uint16 y);

int main(int argc, char * argv[])
{
	player = one;
	if(SDL_Init(SDL_INIT_VIDEO)) exit(1);
	SDL_WM_SetCaption("Tic-Tac-Toe", "Tic-Tac-Toe");
	SDL_Surface * screen, *board, *X, *O, *twowin, *onewin;
	screen = SDL_SetVideoMode(500, 600, 0, SDL_SWSURFACE|SDL_ANYFORMAT);
	board = SDL_LoadBMP("images/gameboard.bmp");
	X = SDL_LoadBMP("images/X.bmp");
	O = SDL_LoadBMP("images/O.bmp");
	twowin = SDL_LoadBMP("images/playertwowin.bmp");
	onewin = SDL_LoadBMP("images/playeronewin.bmp");
	SDL_Rect square[3][3];
	SDL_Rect win;
	win.h = 100; win.w = 400; win.x = 0; win.y = 550;
	for(int k = 0; k < 3; k++)
	{
		for(int l = 0; l < 3; l++)
		{
			square[k][l].h = 95;
			square[k][l].w = 93;
			square[k][l].x = 103 + k * 100;
			square[k][l].y = 106 + l * 100;
		}
	}
	if(board == NULL) exit(2);
	int x, y, z;
	if(SDL_BlitSurface(board, NULL, screen, NULL)) exit(3);
	SDL_UpdateRect(screen,0,0,0,0);
	SDL_Event action;
	for(;;)
	{
		if(!SDL_PollEvent(&action))
		{
			if(action.type == SDL_KEYDOWN)
			{
				if(action.key.keysym.sym == SDLK_F2)
				{
					if(SDL_BlitSurface(board, NULL, screen, NULL))exit(4);
					SDL_UpdateRect(screen,0,0,0,0);
					for(int i = 0; i < 3; i++)
					{
						for(int j = 0; j < 3; j++)
						{
							boardarray[j] = 0;
						}
					}
				}
				else if(action.key.keysym.sym = SDLK_ESCAPE) return 0;
			}
		}
		while(!Won())
		{
			if(!SDL_PollEvent(&action))
			{
				if(action.type == SDL_KEYDOWN)
				{
					if(action.key.keysym.sym == SDLK_F2)
					{
						if(SDL_BlitSurface(board, NULL, screen, NULL))exit(4);
						SDL_UpdateRect(screen,0,0,0,0);
						for(int i = 0; i < 3; i++)
						{
							for(int j = 0; j < 3; j++)
							{
								boardarray[j] = 0;
							}
						}
					}
					else if(action.key.keysym.sym = SDLK_ESCAPE) return 0;
				}
				else if(action.type == SDL_MOUSEBUTTONDOWN)
				{
					if(action.button.button == SDL_BUTTON_LEFT)
					{
						z = GetCord(action.button.x, action.button.y);
						if(z < 4) x = 0; else if(z < 7) x = 1; else x = 2;
						switch(z)
						{
						case 1:
						case 4:
						case 7:
							y = 0;
							break;
						case 2:
						case 5:
						case 8:
							y = 1;
							break;
						case 3:
						case 6:
						case 9:
							y = 2;
							break;
						default:
							y = -1;
							break;
						}
						if(z > 0 && boardarray[x][y] == 0)
						{
							boardarray[x][y] = player;
							if(player == one)
							{
								if(SDL_BlitSurface(X, NULL, screen, &square[x][y])) exit(5);
							}
							else if(player == two)
							{
								if(SDL_BlitSurface(O, NULL, screen, &square[x][y])) exit(6);
							}
							SDL_UpdateRect(screen, 0, 0, 0, 0);
							if(player == one) player = two;
							else player = one;
						}
					}
				}
			}
		}
		if(player = one)
		{
			if(SDL_BlitSurface(twowin, NULL, screen, &win)) exit(7);
		}
		else if(SDL_BlitSurface(onewin, NULL, screen, &win)) exit(8);
		SDL_UpdateRect(screen, 0, 0, 0,0);
	}
}

bool Won()
{
	for(int x = 1; x < 3; ++x)
	{
	if((boardarray[0][0] == x) && (boardarray[1][1] == x) && (boardarray[2][2] == x)) return true;
	if((boardarray[0][2] == x) && (boardarray[1][1] == x) && (boardarray[2][0] == x)) return true;
	if((boardarray[0][0] == x) && (boardarray[0][1] == x) && (boardarray[0][2] == x)) return true;
	if((boardarray[1][0] == x) && (boardarray[1][1] == x) && (boardarray[1][2] == x)) return true;
	if((boardarray[2][0] == x) && (boardarray[2][1] == x) && (boardarray[2][2] == x)) return true;
	if((boardarray[0][0] == x) && (boardarray[1][0] == x) && (boardarray[2][0] == x)) return true;
	if((boardarray[0][1] == x) && (boardarray[1][1] == x) && (boardarray[2][1] == x)) return true;
	if((boardarray[0][2] == x) && (boardarray[1][2] == x) && (boardarray[2][2] == x)) return true;
	}
	return false;
}

int GetCord(Uint16 x, Uint16 y)
{
	if(!(x >= 105 && y >= 105)) return -1;
	if(x < 205)
	{
		if(y < 198) return 1;
		else if((301 > y) && (y > 207)) return 2;
		else if((398 > y) && (y > 309)) return 3;
		else return -2;
	}
	else if(x < 305)
	{
		if(y < 198) return 4;
		else if((301 > y) && (y > 207)) return 5;
		else if((398 > y) && (y > 309)) return 6;
		else return -2;
	}
	else if(x < 405)
	{		
		if(y < 198) return 7;
		else if((301 > y) && (y > 207)) return 8;
		else if((398 > y) && (y > 309)) return 9;
		else return -3;
	}
	return -4;
}

Share this post


Link to post
Share on other sites
Advertisement
You should read up on static and dynamic link libraries. Your program may depend on SDL.dll, and while Visual Studio knows where that is, it needs to be in the working directory of the program in order for it to be found at runtime.

Share this post


Link to post
Share on other sites
Quote:
Original post by DantarionX
You should read up on static and dynamic link libraries. Your program may depend on SDL.dll, and while Visual Studio knows where that is, it needs to be in the working directory of the program in order for it to be found at runtime.


Okay...so I should copy SDL.dll into the Release directory, along with the images and the text file that the SDL guys want me to use then?

Share this post


Link to post
Share on other sites
If that is where it is putting the .exe that is the result of your compiling, then yes, put it there. If that doesn't work, try putting it in the same folder as your code.

(I haven't ever really worked with SDL, so I can't specifically help you with that. I remember when I started programming. For me, it was almost a trial and error thing, until I started to get the hang of things. Hence, my answer - if at first it doesn't succeed, try it a different way [wink]).

Share this post


Link to post
Share on other sites
Congratulations. [smile] It's a great feeling to get something working, especially when you've had trouble with it.

In regards to your comment at the top of the source, you're specifying linker options. See here.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kazgoroth
Congratulations. [smile] It's a great feeling to get something working, especially when you've had trouble with it.

In regards to your comment at the top of the source, you're specifying linker options. See here.


Well I knew it was something like that..I just didn't get the syntax. It works though...so like I said in the comment, I don't really care.

Thanks though.

Share this post


Link to post
Share on other sites
I think the point Kaz was trying to make (and this is based on reading your code in other threads as well), is that you don't always seem to know what your actually doing. He was merely trying to point you in the direction of understanding. If your serious about being a programmer then you need to know that the most important thing a programmer can learn is to understand what he is telling the computer to do.

Grats on your project as well.

Share this post


Link to post
Share on other sites
Well done!

Would you like us to review your code?

EDIT: Better still, do you want to explain what the code does? (We know what it does, this is an exercise to see how much you understand.)

Skizz

Share this post


Link to post
Share on other sites
Quote:
Original post by BringBackFuturama
Quote:
Original post by Kazgoroth
Congratulations. [smile] It's a great feeling to get something working, especially when you've had trouble with it.

In regards to your comment at the top of the source, you're specifying linker options. See here.


Well I knew it was something like that..I just didn't get the syntax. It works though...so like I said in the comment, I don't really care.

Thanks though.


It doesn't make sense to create a thread complaining that you always seem to "fail" in your programming projects, and when someone explains to you something really important as linking, you just say 'I don't really care'. If you don't care about the syntax, then certainly the syntax won't care about you, so don't expect things to get better with that attitude :) Even now, do you understand why you had to link with SDL.lib/SDLMain.lib or why you needed to copy SDL.dll into your app's directory? Or are you content to just know that 'this is the only way this will compile for some reason'?

Share this post


Link to post
Share on other sites
There are a couple of things one perhaps should point out.

if(board == NULL) exit(2);

Since your in the main function it's better to just use return 2; and not exit(2). You've already done it in the big switch.

I can't see you're calling SDL_Quit anywhere.. this is a must since it releases resources/cleans up after SDL (reading this will help alot).

Don't be afraid to use a bit longer names to express what the functions do and what the variables contain. SDL_LoadBMP returns a surface pointer so changing:

O = SDL_LoadBMP("images/O.bmp");

to eiter

surfaceO = SDL_LoadBMP("images/O.bmp");

or

pSurfaceO = SDL_LoadBMP("images/O.bmp");

(the last one to point out that it's a pointer - not all people like that solution but it's a matter of taste) might be a good idea. In this case specially since a O is quite similar to 0.

Try grouping similar things together and add an empty line to indicate that there's something new coming - for instance grouping variable declarations together. You've grouped together the creation of different surfaces but by either adding some whitespace before and after or by putting them in their own function the code might be even more readable :)

Edit:
I suddenly remembered the reason why I posted this - use SDL_GetError() to get some more info on the error and print it to screen or log it!

[Edited by - e-u-l-o-g-y on September 20, 2006 4:11:57 AM]

Share this post


Link to post
Share on other sites
Congratsulations! Way to stick with it [smile].

About distribution, I would reccommend getting one of those free web sites at freewebs, angelfire or bravehost so that you can throw a zip file with your game in it up there so people can download it.

Share this post


Link to post
Share on other sites
Quote:
Original post by e-u-l-o-g-y
There are a couple of things one perhaps should point out.

if(board == NULL) exit(2);

Since your in the main function it's better to just use return 2; and not exit(2). You've already done it in the big switch.

I can't see you're calling SDL_Quit anywhere.. this is a must since it releases resources/cleans up after SDL (reading this will help alot).

Don't be afraid to use a bit longer names to express what the functions do and what the variables contain. SDL_LoadBMP returns a surface pointer so changing:

O = SDL_LoadBMP("images/O.bmp");

to eiter

surfaceO = SDL_LoadBMP("images/O.bmp");

or

pSurfaceO = SDL_LoadBMP("images/O.bmp");

(the last one to point out that it's a pointer - not all people like that solution but it's a matter of taste) might be a good idea. In this case specially since a O is quite similar to 0.

Try grouping similar things together and add an empty line to indicate that there's something new coming - for instance grouping variable declarations together. You've grouped together the creation of different surfaces but by either adding some whitespace before and after or by putting them in their own function the code might be even more readable :)

Edit:
I suddenly remembered the reason why I posted this - use SDL_GetError() to get some more info on the error and print it to screen or log it!


Wow...that was a dumb mistake forgetting to call SDL_Quit. :| I usually put that in right after I type in SDL_Init, but I guess I missed it this time. Well thanks...gotta go back and fix some stuff now.

Share this post


Link to post
Share on other sites
Quote:
Original post by mikeman
Quote:
Original post by BringBackFuturama
Quote:
Original post by Kazgoroth
Congratulations. [smile] It's a great feeling to get something working, especially when you've had trouble with it.

In regards to your comment at the top of the source, you're specifying linker options. See here.


Well I knew it was something like that..I just didn't get the syntax. It works though...so like I said in the comment, I don't really care.

Thanks though.


It doesn't make sense to create a thread complaining that you always seem to "fail" in your programming projects, and when someone explains to you something really important as linking, you just say 'I don't really care'. If you don't care about the syntax, then certainly the syntax won't care about you, so don't expect things to get better with that attitude :) Even now, do you understand why you had to link with SDL.lib/SDLMain.lib or why you needed to copy SDL.dll into your app's directory? Or are you content to just know that 'this is the only way this will compile for some reason'?


I know that it needs to find all related libraries and files referenced to run correctly, but I don't really understand the syntax, so for now I'm just using the thing I found that works. I've kinda got a standpoint about things that are too complicated for me that if it works, why question it?

Share this post


Link to post
Share on other sites
Quote:
Original post by Skizz
Well done!

Would you like us to review your code?

EDIT: Better still, do you want to explain what the code does? (We know what it does, this is an exercise to see how much you understand.)

Skizz


Basically I initialize SDL, then load all the bitmaps, set the values of the boardarray to 0, blit the main board to the game, then run through the main game loop which checks for the key a newgame or the key to exit and either reblits the board over everything and resets the values of boardarray, or exits the game if one of of those happens, then it runs through the main loop if the game's not over (checked by Won), which checks again for F2 or ESC and does the same thing, then checks for a mouse click, checks the position, moves to the GetCord function to get a value from the mouse click which is translated to x and y and then if it's valid on the board, the current player number becomes the value of that part of boardarray, and then either an X or O is blitted toe the screen in that part of the board. Then it changes player, and either repeats, or if the loop is over goes back to just the for loop, blits either the player 1 or 2 wins message to the bottom of the screen, and waits for an exit or new game.

Share this post


Link to post
Share on other sites
Quote:
Original post by Simian Man
Congratsulations! Way to stick with it [smile].

About distribution, I would reccommend getting one of those free web sites at freewebs, angelfire or bravehost so that you can throw a zip file with your game in it up there so people can download it.


Freewebs is the only one of those I've really used (angelfire too, but that's never been a good thing with me). I guess I could use that.

Share this post


Link to post
Share on other sites
Quote:

I've kinda got a standpoint about things that are too complicated for me that if it works, why question it?


Why? Because it might only appear to work. Because it might cause something else to go wrong, elsewhere. Because it might cause something else to work right, elsewhere. Because it might have performance implicitations. Because it might be redundant. Because it means you can't trust any of your code, really, because its behavior might be based on the bit of code you don't understand. Et cetera.

:)

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Quote:

I've kinda got a standpoint about things that are too complicated for me that if it works, why question it?


Why? Because it might only appear to work. Because it might cause something else to go wrong, elsewhere. Because it might cause something else to work right, elsewhere. Because it might have performance implicitations. Because it might be redundant. Because it means you can't trust any of your code, really, because its behavior might be based on the bit of code you don't understand. Et cetera.

:)


True, but I know that all that the code in this case does is link the SDL libraries to the program. I may not really know how to use the code, but I know what it does.

Share this post


Link to post
Share on other sites
You might want to try and refactor your code. Most programs follow the general form:

initialise system - setup video, audio and input devices
initialise application - load game data
main loop - process user input and update output
deinitialise application - free game data
deinitialise system - shutdown video, audio and input devices

Create a main function along those lines:

int main (int argc, char *argv)
{
if (InitSystem ())
{
if (InitApplication ())
{
GameLoop ();
DeinitApplication ();
}
DeinitSystem ();
}
}

and then create the missing functions by moving existing code to the appropriate function. Avoid global variables, access data created by each stage via access functions. Think of the System, Application and Game as three separate entities (objects) that have a well defined way of passing information between them.

About those #pragma's - they are compiler specific preprocessor commands. The key thing about them is that if a compiler doesn't recognise the command then it is ignored as opposed to generating an error, so this program will only really work with DevStudio. You can get rid of those if you're using DevStudio by opening the project properties dialog (right click project in workspace/solution explorer and select properties), selecting the linker options and then the input sub options and add the two library names to the additional dependancy list.

Have fun.

Skizz

Share this post


Link to post
Share on other sites
Quote:
Original post by Skizz
You might want to try and refactor your code. Most programs follow the general form:

initialise system - setup video, audio and input devices
initialise application - load game data
main loop - process user input and update output
deinitialise application - free game data
deinitialise system - shutdown video, audio and input devices

Create a main function along those lines:

int main (int argc, char *argv)
{
if (InitSystem ())
{
if (InitApplication ())
{
GameLoop ();
DeinitApplication ();
}
DeinitSystem ();
}
}

and then create the missing functions by moving existing code to the appropriate function. Avoid global variables, access data created by each stage via access functions. Think of the System, Application and Game as three separate entities (objects) that have a well defined way of passing information between them.

About those #pragma's - they are compiler specific preprocessor commands. The key thing about them is that if a compiler doesn't recognise the command then it is ignored as opposed to generating an error, so this program will only really work with DevStudio. You can get rid of those if you're using DevStudio by opening the project properties dialog (right click project in workspace/solution explorer and select properties), selecting the linker options and then the input sub options and add the two library names to the additional dependancy list.

Have fun.

Skizz


Well thanks for the advice, and I'll try to follow something similar to that in future projects. This one I just wanted to see if I could even do a simple tic-tac-toe game. Apparently I can. I only used most of the stuff in the main and globals because I wanted to get it done without thining in pointers and references and such (that kind of stuff is a bit difficult for me) so that I could just focus on the "game programming" part of it.

Share this post


Link to post
Share on other sites
Quote:
Original post by CzarKirk
I spotted this in your program

if(player = one)


Thanks for that. I fixed it now. I probably wouldn't have even noticed for a while either.

Share this post


Link to post
Share on other sites
Quote:
Original post by BringBackFuturama
Quote:
Original post by Simian Man
Congratsulations! Way to stick with it [smile].

About distribution, I would reccommend getting one of those free web sites at freewebs, angelfire or bravehost so that you can throw a zip file with your game in it up there so people can download it.


Freewebs is the only one of those I've really used (angelfire too, but that's never been a good thing with me). I guess I could use that.


I tried using all of these and they don't like zip files.

Share this post


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

  • Advertisement