//////////////////////////////////////
#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>
#include <iostream>
enum players {one = 1, two = 2} player;
int boardarray[3][3];
bool Won();
int GetCord(Uint16 x, Uint16 y);
int main(int argc, char * argv[])
{
player = one;
boardarray[3][3] = 0,0,0,0,0,0,0,0,0;
if(SDL_Init(SDL_INIT_VIDEO)) exit(1);
SDL_WM_SetCaption("Tic-Tac-Toe", "Tic-Tac-Toe");
SDL_Surface * screen, *board, *X, *O;
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");
SDL_Rect square[3][3];
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;
}
else if(action.type == SDL_MOUSEBUTTONDOWN)
{
if(action.button.button == SDL_BUTTON_LEFT)
{
z = GetCord(action.button.x, action.button.y);
if(z > 0)
{
y = z%3;
x = z/3;
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(Won())
{
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
std::cout << boardarray[j] << ' ';
}
break;
}
}
}
}
}
return 1;
}
bool Won()
{
int x = 1;
if(player == two) ++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(400 > x > 105 || 400 > y > 105) return -1;
if(x < 205)
{
if(y < 198) return 1;
else if(298 > y > 203) return 2;
else if(398 > y > 303) return 3;
else return -2;
}
else if(x < 305)
{
if(y < 198) return 4;
else if(298 > y > 203) return 5;
else if(398 > y > 303) return 6;
else return -2;
}
else
{
if(y < 198) return 7;
else if(298 > y > 203) return 8;
else if(398 > y > 303) return 9;
else return -3;
}
return -4;
}
I AM SO SICK OF FAILING!
I guess...I'm just not cut out to be a programmer. This is my 5th failure at creating a game now. All of these times I've had to ask you guys to help with one thing or another, and the damn thing still wouldn't work. I can't even get a simple game of tic tac toe to work. And it's always something stupid like "you used = instead of ==" or "you set this up completely wrong." Why does nothing work for me?! I don't just quit when one thing goes wrong though, I'll spend like a month working a project that seems to take the rest of you about 2 days (and that's the complete beginners, I've taken programming classes and got As too). Is there some sort of magic trick that you're pulling here...or have I just completely lost it?
Alright...I want to try again, honestly. This is what I want to do with my life. But why does it have to be so hard to do such simple things?
Look at this code for my tic-tac-toe game, and some one tell me how you would approach debugging it..because I can't see anything. I've got it to draw what I want initially, the array gets set up, I can click, but it will only draw Xs when it feels like it. My take on this was either in my detection for clicking in the squares, or a syntax issue somewhere.
Here is my code in it's glitchy, ugly completedness:
You may want to try Python/PyGame. It takes out a lot of the crap that is easy to screwup and not really relevant to what you're trying to do. When you get a handle on that, you can move back into C++ if you're feeling frisky. Even if you don't know Python yet, you'll be more effective with Python/PyGame in a week than you are now with C++/SDL, I promise you.
Quote:Original post by CarlSagansMyBoy
You may want to try Python/PyGame. It takes out a lot of the crap that is easy to screwup and not really relevant to what you're trying to do. When you get a handle on that, you can move back into C++ if you're feeling frisky. Even if you don't know Python yet, you'll be more effective with Python/PyGame in a week than you are now with C++/SDL, I promise you.
I took python, but even the teacher of the class couldn't get the language to work have the time. So I guess you could say I've had a bad experience with it, and that's why I don't use it. I do have PyGame and Python though.
Hey, my first suggestion is not to take failure soo hard. It isn't really even failure, unless you give up and even then it can hardly be called failure. You still learn something from each "failure", and if that is your objective at the end of the day then you are succeeding. Even if it is not exactly the way you hoped to start with. Everybody started somewhere, and I certainly remember when I started all the silly mistakes I made (not that I am coder superman now)!
I only had a quick glance at your code. What IDE are you using?
If you are using visual studio, it has an excellent debugger built right in. It will let you make single steps over code, examine the value of variables and all sorts of things like that.
I suggest that you spend a little bit of time working over your ide to find the debugger features (like breakpoints). I would go over this now but I don't know what IDE you are using.
Best of luck, you'll make it through!
I only had a quick glance at your code. What IDE are you using?
If you are using visual studio, it has an excellent debugger built right in. It will let you make single steps over code, examine the value of variables and all sorts of things like that.
I suggest that you spend a little bit of time working over your ide to find the debugger features (like breakpoints). I would go over this now but I don't know what IDE you are using.
Best of luck, you'll make it through!
Quote:Original post by maxpenguin
Hey, my first suggestion is not to take failure soo hard. It isn't really even failure, unless you give up and even then it can hardly be called failure. You still learn something from each "failure", and if that is your objective at the end of the day then you are succeeding. Even if it is not exactly the way you hoped to start with. Everybody started somewhere, and I certainly remember when I started all the silly mistakes I made (not that I am coder superman now)!
I only had a quick glance at your code. What IDE are you using?
If you are using visual studio, it has an excellent debugger built right in. It will let you make single steps over code, examine the value of variables and all sorts of things like that.
I suggest that you spend a little bit of time working over your ide to find the debugger features (like breakpoints). I would go over this now but I don't know what IDE you are using.
Best of luck, you'll make it through!
Well thus far, none of my programs have done what I want them to as much effort as I'd put it them. My biggest accomplishment has been getting SDL to work with Visual Studio. Which was hard! I've tried using a couple of the features on my debugger, and using some printlining. Yeah, this solved some basic stuff, but never my biggest issues. I don't really get most of the stuff in Visual Studio, but it's a pretty big program, and to be honest, I only like it for writing code, compiling and running programs in it is just a mess.
Visual Studio can be very daunting, all of the great stuff it does for you though is well worth the effort.
You have to help yourself here, I cannot magically pull the answer out of somewhere. I have never used SDL, but from "else if(action.type == SDL_MOUSEBUTTONDOWN)" it looks fishy to me. What is the Z for?
Put a break point there. Run the program. Click the button. See what it does (use step into/over to move through the program one step at a time), what did you expect it to do? Figure out what is different between what it IS doing and what you EXPECT. There is your bug.
People always make mistakes, the most brilliant programmer ever doesn't write completely bug free code (especially in projects 100 or 1000 times more complex than your project). This is why learning how to use the debugger is such a massively important skill (well, aside from actually learning c/c++). I spend probably 20-40% of my day in the debugger stepping through code, tracing down bugs in my 100,000+ line project.
Good luck, HTH.
BTW - I can't wait until they bring back Futurama [grin].
You have to help yourself here, I cannot magically pull the answer out of somewhere. I have never used SDL, but from "else if(action.type == SDL_MOUSEBUTTONDOWN)" it looks fishy to me. What is the Z for?
Put a break point there. Run the program. Click the button. See what it does (use step into/over to move through the program one step at a time), what did you expect it to do? Figure out what is different between what it IS doing and what you EXPECT. There is your bug.
People always make mistakes, the most brilliant programmer ever doesn't write completely bug free code (especially in projects 100 or 1000 times more complex than your project). This is why learning how to use the debugger is such a massively important skill (well, aside from actually learning c/c++). I spend probably 20-40% of my day in the debugger stepping through code, tracing down bugs in my 100,000+ line project.
Good luck, HTH.
BTW - I can't wait until they bring back Futurama [grin].
well, I'd e willing to glance over it and figure out what may be wrong...if you can tell me how to get SDL working with VS. do you use 2005 express? if not, don't worry about it, I'll still try my best to figure out whats wrong.
maxpenguin's right. Debugging is a skill you have to have... and it doesn't come very easy. I think you're just being way too hard on yourself. Programming takes a lot of patience and even if you are VERY patient, it can still be excrutiatingly frustrating. Don't look at your failures as failures. Just look at it as the same thing we all go through and find your way through it.
If you're really serious about this (and you really seem to be) get The Pragmatic Programmer. It explains a lot about programming practice and may open your eyes to what the everyday pro has to go through. Hehe, it's not just you. =b
If you're really serious about this (and you really seem to be) get The Pragmatic Programmer. It explains a lot about programming practice and may open your eyes to what the everyday pro has to go through. Hehe, it's not just you. =b
Quote:Original post by maxpenguin
Visual Studio can be very daunting, all of the great stuff it does for you though is well worth the effort.
You have to help yourself here, I cannot magically pull the answer out of somewhere. I have never used SDL, but from "else if(action.type == SDL_MOUSEBUTTONDOWN)" it looks fishy to me. What is the Z for?
Put a break point there. Run the program. Click the button. See what it does (use step into/over to move through the program one step at a time), what did you expect it to do? Figure out what is different between what it IS doing and what you EXPECT. There is your bug.
People always make mistakes, the most brilliant programmer ever doesn't write completely bug free code (especially in projects 100 or 1000 times more complex than your project). This is why learning how to use the debugger is such a massively important skill (well, aside from actually learning c/c++). I spend probably 20-40% of my day in the debugger stepping through code, tracing down bugs in my 100,000+ line project.
Good luck, HTH.
BTW - I can't wait until they bring back Futurama [grin].
It might have something to do with the SDL_MOUSEBUTTONDOWN. That's basically supposed to check if a mouse button is clicked down and then from that run through, get the location, check for a legitimate move, etc. The z in the program is returned from the GetCord function which gets a code related to the part of the board and then x = z%3 and y = z/3 translates that into parts of the boardarray array so that the one piece of the board is changed.
It's not just you. It's programming. That's what it is: Writing some code, then figuring out why the code doesn't do what you think it should. :P
In my experience, at least. Maybe there are brilliant (or just very experienced) programmers who just write the code and have it work the first time. But I think for most people there's a lot of trial and error involved.
Edit: That said, you may not be cut out to be a programmer... which is to say, in light of the frustration and stress it entails, it may not be what you really want to do with your life. Personally, I know I couldn't be a programmer if I had to do it eight hours a day, with deadlines involved.
YMMV, of course.
[Edited by - Logodae on September 19, 2006 12:28:31 AM]
In my experience, at least. Maybe there are brilliant (or just very experienced) programmers who just write the code and have it work the first time. But I think for most people there's a lot of trial and error involved.
Edit: That said, you may not be cut out to be a programmer... which is to say, in light of the frustration and stress it entails, it may not be what you really want to do with your life. Personally, I know I couldn't be a programmer if I had to do it eight hours a day, with deadlines involved.
YMMV, of course.
[Edited by - Logodae on September 19, 2006 12:28:31 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement