Jump to content
  • Advertisement


  • Content count

  • Joined

  • Last visited

Community Reputation

122 Neutral

About wkiffer

  • Rank
  1. wkiffer

    Tic Tac Toe Help!

    To go about setting up AI for something like Tic-Tac-Toe think about what a real person would do. First they would see if they can win on this move, if they can they make that move. Then they see if the opponent can win on their next move, if so they block that. If nothing else they take the best square they can. To accomplish this what I did was I set up a function to check the board for a winner. It took a reference to a vector(or array in your case) of boxes that made up the board and checked it against every single winning combination (don't worry there are only 8) and returned a char: 'H' for human win, 'C' for computer win, 'T' for tie, or 'N' for nobody. The winner function looked a little like this. char winner(int &board[9]) { //Every combination of winning moves, snipped because I'm lazy int winningmoves[8][3]= {{0,1,2},{3,4,5}...{0,4,8},{2,4,6}}; //Go through all the winning move combinations //Check to see if all 3 boxes of the board are the same //and that they aren't empty //If they are somebody has won //Check to see who it is and return it for(int i=0;i<8;i++) { if((board[winningmoves[0]]==board[winningmoves[1]) && (board[winningmoves[1]]==board[winningmoves[2]]) && (board[winningmoves[0] != EMPTY)) { if(board[winningmoves[0]]=='C') return 'C'; else { return 'H'} } //Check to see if the whole board is occupied, if it is it returns Tie as //default, if any boxes are empty it returns Nobody for(int i=0;i<9;i++) { if(board==EMPTY) return 'N'; } return 'T' } Ok, now to the actual AI part, remember are 3 step plan for AI? 1. See if we can win 2. See if we can block opponent from winning 3. Take best move available First to see if we can win we loop through every box in the grid and we occupy it a Computer piece. Then we check to see if there is a winner. If we won with that move that's the move we take. for(int i=0;i<9;i++) { if(board==EMPTY) { board==COMPUTER; if(winner(board)=='C') { return i; } else {board==EMPTY;} } } It's that easy! If there hasn't been move returned yet we go through all the boxes again this time placing a human piece in there and seeing if the human wins, if so we move there to block it. for(int i=0;i<9;i++) { if(board==EMPTY) { board=HUMAN; if(winner(board)=='H') { board=EMPTY; return i; } else{ board=EMPTY; } } } It's pretty much the same thing as last time only we change which piece we put in each time. Next if the computer can't win or block a move we just pick the best available move, you could, if you wanted to here put in more in depth AI to try and "trap" the opponent by getting in one of those situations where you can win two different ways but for the sake of simplicity I'm going to just pick the best move available. Now how do you pick the best move? Simple, you make an array of the best moves and run through them. If that box is empty you take it. If not you move on. //Best moves in order are: Center, Corners, Middle boxes?(don't know what to //call them). int bestmoves[9]={4,0,2,6,8,1,3,5,7}; for(int i=0;i<9;i++) { if(board[bestmoves]==EMPTY) { return bestmoves; } } Hope that helps! Of course, I just typed this all up now so I'm not guaranteing(sp?) that the code is perfect but the idea is still there. You'll have to modify the code for your program no doubt to change how you mark boxes empty and whatnot, but whatever.
  2. wkiffer

    SDL Animation?

    Here's a pretty good tutorial. It only covers moving something left and right however moving up and down should just be a simple matter of adding a y offset and a y velocity variable. Hope it helps, Will
  3. wkiffer

    SDL_PollEvent troubles

    Personally I have a global variable such as bool quit which I assign to false. Then the main loop is while(!quit). For other loop I throw in another SDL_PollEvent and if they decide to quit I set quit to true so the main loop stops and then I break from the current loop. However I'm new to this too, so there might be a better way.
  4. My first attempt at a 2d game is a Tetris Clone (rather cliche isn't it?). Anyways, I'm not very far however I just implemented the Falling Shape function and I'm having a little problem with it. First off, however, you should probably know how I'm going about making the tetris game, I divided the playing grid into a multidimension array of an object I made representing every individual square. Each square has a bool variable telling if it is empty or not as well as a pointer to the image of the block it contains if it is occupied. The grid starts at the bottom left so [0][0] is the bottom left hand corner and [19][9] is the top right. To display the grid I just run through every block and if it is occupied I display the block. Now for the falling shape function I take an argument saying which shape it is, I then another multidimension array called coordinates, depending on which shape is passed to the function I assign where the starting coordinates on the grid would be so if is the straight shape I assign coordinates as {{19,5},{18,5},{17,5},{16,5}}. To make the shape fall each y value by 1, then check to see if there is a collision. I do this by sending the array to the checkcollision function, it loops through the values of the array and sees if the corresponding blocks in the grid are already occupied, if it is it returns true, the coordinates are reincrimented and the loop stops. However when I run it the blocks just stay in their starting positions indicating that a collision was detected the first time through the loop which shouldn't happened. I've already checked to make sure that my program doesn't automatically initialize the occupied variable of every block to true by default. I really can't see anythign wrong with the collision function and I was hoping you guys could help. void ShapeFall(int shape) { //Assign the appropriate color block for the shape in order to //display it correctly SDL_Surface *blockcolor=blocks[shape].block; int coordinates[4][2]; switch(shape) { // //Apply starting coordinates to shape, left out because its so long // } bool collision=false; Timer timer; while (!collision) { if(SDL_PollEvent(&event)) {if(event.type==SDL_QUIT) quit=true; break; } timer.start(); //Subtract one from the current y coordinate to simulate it falling for(int i=0;i<4;i++) { coordinates[0]--; } //Check to see if the shapes new position collides with something //if it does return the coordinates to their original position and stop the loop if(checkcollision(coordinates)) { for(int i=0;i<4;i++) { coordinates[0]++; } collision=true; } //If it doesn't change the blocks it just occupied to empty and then //set the new blocks to occupied and store its image to be displayed else { for(int h=0;h<4;h++) { coordinates[h][0]++; myGrid.grid[(coordinates[h][0])][(coordinates[h][1])].occupied=false; coordinates[h][0]--; } for(int i=0;i<4;i++) { myGrid.grid[(coordinates[0])][(coordinates[1])].occupied=true; myGrid.grid[(coordinates[0])][(coordinates[1])].block=blockcolor; } } //Draw the screen DrawScreen(); //Wait a second before doing it again while(timer.get_ticks()<1000) {} } } //This is where I'm having a problem //It returns true no matter what bool checkcollision(int coordinates[4][2]) { for(int i=0;i<4;i++) { //If the grid position the coordinates point to is already occupied //or it is below the bottom of the grid return true if(myGrid.grid[(coordinates[0])][(coordinates[1])].occupied||coordinates[0]<0) return true; } return false; } I know this was a long explanation, and I thank anyone who read it all the way through, hopefully you guys can help, Will
  5. wkiffer

    I STILL dont understand

    In order to program games you will need a very good understanding of C. Try making text based games such as Hangman, Tic-Tac-Toe, and World Jumble. Before you start each game make an effort to write down on paper what the game loop will look like, e.g. for Tic-Tac-Toe it might be: While User has not quit and there is no winner If its users turn Get users move Check to see if its valid If it is valid draw board Switch turns Check to see if there is a winner If its computers turn Get computers move Draw Board Switch Turns Check to see if there is a winner After you get the grasp of C, then you can start trying to learn an API such as SDL, Allegro, DirectX, or OpenGl in order to make the graphics. However, don't try to learn an API until you have a good grasp of the language because even though you might be able to learn how to implement some graphics fine with only a little knowledge you'll still need a good handle on C to make the rest of the game(inventory, game states, and whatnot). Hope that helped.
  6. I Found it!!! I should've showed you guys the 10 lines before this also, you guys probably would have caught it. I had a 4 nested if statements above it and at the end of them I accidently put 5 }'s, and the compiler took the last one as the one closing the function, putting this code out in the open which, like Zahlman, pointed out, throws the error. Thanks for the help guys.
  7. Yeah, its inside a function. Maybe this will be one of those things where if you ignore it for a while it'll go away ;)
  8. Hi, I'm using Dev-C++ and I recently ran accross this error message that I can't make heads or tails of. My code so far is: int x=220; int y=585; for (int i=0;i<20;i++) { for(int h=0;h<10;h++) { myGrid.grid[h].x=x; myGrid.grid[h].y=y; x+=20; } x=220; y+=20; } The full error message I'm getting is: expected unqualified-id before "for" expected `,' or `;' before "for" expected constructor, destructor, or type conversion before '<' token expected `,' or `;' before '<' token expected constructor, destructor, or type conversion before '++' token expected `,' or `;' before '++' token It looks like I'm missing a semicolon somewhere but I'm sure I'm not. I've been over this code for a good hour, changing it and whatnot but I haven't been able to fix it. It'll probably just end up being a stupid mistake that I missed, but it's always easier to have someone else's eyes look at it.
  9. Wow, I never even saw the MultiMedia Tab up there...such a simple solution too. Thanks for the responses guys. Will.
  10. I'm using Dev C++ to get started using SDL, however for now the only way I can get my SDL programs to work is to start a new project then go to the project options and set it to a win32 GUI and paste "-lmingw32 -lSDLmain -lSDL" in the Linker part under parameters. Is there any way to make this a default or some way to make it so I don't have to do this, so I can just make a new aource code of an SDL script instead of making a whole new project? Thanks, Will. [Edited by - wkiffer on July 24, 2006 11:16:31 AM]
  11. wkiffer

    Jump from basic c++ to direct x

    Quote: One thing I would make sure you do is don't look at the tutorials and/or code and freak out about how hard it looks Haha, yeah, the first time I looked at a tutorial to make a simple window I was like "All this code to make a window?" But of course every tutorial assures me that I can just copy and paste that every time I need a window, but I looked at SDL and it looks about right for a beginner, maybe later I'll get into directx itself. Thanks for all your help.
  12. wkiffer

    Jump from basic c++ to direct x

    Thanks for the reply, especially for the link to Visual C++. To tell the truth, after looking at a couple direct x tutorials I started to think about looking into OpenGl but SDL looks like just what I need, it even has access to audio and input features. The site even has nice tutorials aimed at beginners. Thanks again
  13. wkiffer

    Letter Jumble

    Well, thinking outside the box here, my first word jumble program was written before I learned the random_shuffle() function, so I used a function that randomly picked two letters of the word and switched them, doing that for however many letters were in the word. The code looked something like this: void jumbleword(string& word) { int letters=word.length(); //Seed the random numbers srand(time(0)); //For however many letters are in the word generate two random numbers //Then switch the letters of the word corresponding to the numbers for(int i=0;i<letters;i++) { int myint1=rand()%letters; int myint2=rand()%letters; char letter=word[myint1]; word[myint1]=word[myint2]; word[myint2]=letter; } } That being said, its probably a much much better idea just to stick with the random_shuffle() function, it's easier, cleaner, and probably more efficient. But just so you know there's always more then one way to do something(albeit a much more complicated way).
  14. I've been reading alot of tutorials on c++ and I've made some fairly simple games command prompt style such as hangman, world jumble, and tic tac toe. I understand classes, pointers, references, inheritence, and all that good stuff, so I was wondering what the next step was. Is there some middle ground between direct x and c++ that I should check out first or should I just dive into direct x? Anybody know of any tutorials that specifically focus on people coming from basic c++ to direct x? Also another quick question, the few tutorials I have read have said to use Visual C++ 2005 Express Edition. The problem is when I go to install it it can't connect to the internet to download the rest. I've tried fixing it but I haven't been able to so is there really any reason for me to try to get a copy rather then just use a different compiler such as Dev-C++? Thanks in advance.
  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!