Jump to content
  • Advertisement
Sign in to follow this  
BringBackFuturama

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

This topic is 4267 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
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!