Technique of all board-like games?

This is a discussion on Technique of all board-like games? within the Game Programming forums, part of the General Programming Boards category; Techwins, i know ALL the basic stuff about C. So i plead you to consider me as an intermediate C ...

  1. #16
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Techwins, i know ALL the basic stuff about C. So i plead you to consider me as an intermediate C programmer.
    Sorry I had no intentions of demeaning you or offending you. I figured that since you didn't know what functions are you weren't all that familiar with programming. Maybe you disunderstand my terminology of 'functions'? Many times I know an aspect of programming, but when people ask about things and use precise terminology of the aspect I have no idea what they are talking about. However, once that person shows me that aspect I go "Oh yeah I know that". By no means should I be considered even remotely more experienced than intermediate.

    The reason i'm asking all these questions is that i never programmed a game in C, and there are game techniques i havne 't encountered before.
    [That's understandable but many of the items mentioned are not spefically for game programming.

    Also, my books didn't mention much about game programming.
    That's too bad! My book has a few examples of games, which helped me out a lot.

    And techwins, your example of gotoxy doesn't work.
    Well, I'm not sure why that doesn't, but go ahead and try this because I know for sure that this works.

    Code:
    #include <windows.h>  //this is for gotoxy()
    #include <iostream.h>  //this is for cout
    #include <conio.h> //this is for getch()
    
    
    void Gotoxy(); //this is to show that a function is below 
    
    
    void main()  //this is the main function and must always be done
    {
    Gotoxy();
    }
    
    
    void Gotoxy() //name of the function
    {
       int x = 10; //this is to determine what location you want on the x-axis
       int y = 0;  //this is to determine what location you want on the y-axis
       COORD coord;  //this is for declaring a coordinate
       coord.X = x;  //this is declaring the x-axis as the the value of x
       coord.Y = y;  //this is declaring the y-axis as the the value of y
       SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);  //this is positioning the cursor
       cout << "Hi";
       getch();  //waits for user to enter a key
    }
    If this doesn't work then it must be because of inserting here on this post. When copying and pasting the other code it didn't work for me. BUT I AM 100% POSITIVE THAT THIS CODE WORKS!

  2. #17
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    If i do connect four, do you know of any popular, standard, sensible way or algorithm to check if anyone wins? There are soo many combinations...

    thnx

  3. #18
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Um no sorry I don't. Maybe as you go farther into coding the game you will think of a way to do so. One thing is for sure, you will be one hell of a connect four player after you are done making this game if you include AI with the game lol.

    Good luck and most of all enjoy.

  4. #19
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Nutshell, try including conio.c instead of conio.h

  5. #20
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Nutshell, I have an algorithm... I'll post it here..
    Actually, I'll just post the code, and let you figure it out

    (I'm not really sure what I was thinking anyway)

    Code:
    int checkWin(char player)
    {
    
    	store = 0;
    	for (i = 0; i<4; i++)
    	{
    										//cout << "i" << i //Debugging;
    		 if ((board[store][i] == board[store][i+1]) &&
    			(board[store][i+1] == board[store][i+2]) &&
    			(board[store][i+2] == board[store][i+3]) && 
    			(board[store][i+3] != blankchar))
    			{
    				return 1;
    			} else {
    				if (i == 3)				// Is this the last loop through?;			
    				{						// if not, i is never reset, the loop ends, and 0 returns;
    					if (store != 5)		//Is this the last column?;
    					{
    						store++;
    						i = -1;			//-1 instead of 0 because it's incremented at the end of the loop;
    					}
    				}
    
    			}
    	}
    
    	store = 0;
    	for (i = 0; i<3; i++)
    	{
    										//cout << "i" << i; //Debugging;
    		 if ((board[i][store] == board[i+1][store]) &&
    			(board[i+1][store] == board[i+2][store]) &&
    			(board[i+2][store] == board[i+3][store]) && 
    			(board[i+3][store] != blankchar))
    			{
    				return 1;
    			} else {
    				if (i == 2)				// Is this the last loop through?;			
    				{						// if not, i is never reset, the loop ends, and 0 returns;
    					if (store != 6)		//Is this the last column?;
    					{
    						store++;
    						i = -1;			//-1 instead of 0 because it's incremented at the end of the loop;
    					}
    				}
    			}
    	
    	}
    
    
    	for (i=0; i<5; i++)
    	{
    		for (ii=0; ii<4; ii++)
    		{
    			if ((board[i][ii] == board[i+1][ii+1]) 
    			&& (board[i][ii] == board[i+2][ii+2])
    			&& (board[i][ii] == board[i+3][ii+3])
    			&& (board[i+3][ii+3] != blankchar))
    			{						
    				return 1;
    			}
    			
    		}
    
    	}
    
    
    
    	for (i=6; i>3; i--)
    	{
    		for (ii=0; ii<4; ii++)
    		{
    			if ((board[i][ii] == board[i-1][ii+1]) 
    			&& (board[i][ii] == board[i-2][ii+2])
    			&& (board[i][ii] == board[i-3][ii+3])
    			&& (board[i-3][ii+3] != blankchar))
    			{
    				return 1;
    			}
    		}
    	}
    	return 0;
    
    
    }
    Last edited by Dual-Catfish; 04-22-2002 at 08:32 PM.

  6. #21
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Hi,

    Thats a pretty good algorithm. SO i'll hav to store pieces in a 2d array.

    Ok then. For AI, in general, should i just check for every possible move again? It'll be quite long, since i'll have to do the same coding for blocking the opponent's move.

  7. #22
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    I have no idea My connect four game didn't include AI
    Although, I think theres an example with AI on www.planetsourcecode.com

  8. #23
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Ok then. For AI, in general, should i just check for every possible move again? It'll be quite long, since i'll have to do the same coding for blocking the opponent's move.
    This is what I was originally doing when I started my Tic-Tac-Toe game, but then I realized there has to be a simpler solution! It didn't make sense having to spend hours upon hours finding all of the possible moves the AI should make...and guess what I was right. Listing all of the possible moves is nearly impossible to do unless you have a ton of time on your hands and are willing to waste that time doing a tedious task. Instead you have to come up with an algorithim on how the AI will respond to a situation. Granted you will still have to list a lot of responses for the AI depending on how responsive (good) you want the AI to be.

    The first priority the AI should have in it's alrogrithim is to connect 4 (win the game). The next priority should be for the AI to stop the human connecting 4(prevent losing). The next priority should be for the AI to connect 3 or for the AI to stop the human from connecting 3. After that it might be the priority for the AI to get to a certain spot on the grid. Really you make the AI respond how you want it to. The AI can only be as good and diverse as you program it. Start off with the basics for the AI, then once you get done go back and improve your AI algorithim a bit. Typically an AI algorithim consists of if, else if, else, if, else if, etc... statements. I don't have any code for Connect Four to show you but you can always look at my Tic-Tac-Toe algorithim if you want to. The link to download it is here. Just unzip it, then go into the tic-tac-toe folder, next go into v1.1e folder. After that open up the .cpp file. Look over the variables and such so you can get an understanding of what each variable means. Then scroll down until you see the function AI_Move(). There you will be able to see how I programmed the AI for my TTT game. You might even want to play the game a few times going against the AI to give you a feel of how the AI responds.

    Here is the first and second priority of the AI in my game.

    Code:
    if((grid[0][0] == 'O') && (grid[0][1] == 'O') && (grid[0][2] == ' '))
       {
       row = 1;
       row -= 1;
       col = 3;
       col -= 1;
       cout << "\n\t\tThe AI is choosing row 1 column 3\n\t\t";
       system("PAUSE");
       turn = 1;
       var_1 = 0;
       break;
       }
       else if((grid[0][0] == 'O') && (grid[0][2] == 'O') && (grid[0][1] == ' '))
       {
       row = 1;
       row -= 1;
       col = 2;
       col -= 1;
       cout << "\n\t\tThe AI is choosing row 1 column 2\n\t\t";
       system("PAUSE");
       turn = 1;
       var_1 = 0;
       break;
       }
    Hope that was able to help you.

  9. #24
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Techwins.........................I've done a tic tac toe game before using vb, so i know what it's like. I had a look at your function AI_MOVE. It does check for every possible move. I was asking does anyone know of a better way of moving the computer player in a game of connect four, or in general, how to avoid checking every possible move.

    thnx again

  10. #25
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    It does check for every possible move.
    No, there's a big difference between checking for every single move and listing responses to situations. Listing responses to situations is showing the order of importance in which move the AI will make. Listing every single move is listing all of the possiblities for where the human could be, AI is, and where the AI should go. Listing every single move is near impossible because of the excessive amount of situations. Listing responses is shownig where the AI should move depending on the situation.

    For example I could just use...

    Code:
    if(grid[0][0] == ' ')
    
    Response()...
    
    else if(grid[0][1] == ' ')
    
    Response()...
    
    else if(grid[0][2] == ' ')
    
    Response()...
    
    
    else if(grid[1][0] == ' ')
    
    Response()...
    
    else if(grid[1][1] == ' ')
    
    Response()...
    
    else if(grid[1][2] == ' ')
    
    Response()...
    
    else if(grid[2][0] == ' ')
    
    Response()...
    
    else if(grid[2][1] == ' ')
    
    Response()...
    
    else if(grid[2][2] == ' ')
    
    Response()...
    ...as the complete AI algorithim and it would work perfectly fine. The only problem with it would be that the AI would be incredibly easy to defeat. That is why I added other parts to the algorithim such as...

    Code:
    if(grid[2][0] == 'X' && grid[0][2] == ' ')
    
    Response()...
    
    if((grid[0][0] == 'X') && (grid[0][1] == 'X') && (grid[0][2] == ' ')
    //above is two human squares in a row
    
    Response()...
    
    if((grid[0][0] == 'O') && (grid[0][1] == 'O') && (grid[0][2] == ' '))
    //above is two AI squares in a row
    
    Response()...
    This makes the AI more adapt to different situations, which makes the AI harder to beat. As far as I know there is only one way (actually two but it's just the same way but on the opposite side) to defeat my AI. Do you see the difference between listing situations for the AI to adapt to and listing all the possiblities? Never will anybody list all the possiblities (at least I hope not for their sake lol). The same type of algorithim for my TTT game can be used with your Connect Four, and that is why I was showing you the algorithim I used for my AI.

  11. #26
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Your algorithm DOES check for every possible move, except that it terminates when it finds an exit, thats when someone wins or a move has been decided. What you did was exactly the same as what i did for my ttt game, you can hava look at PSC.

    I was asking for a more clever way to check, like instead of comparing each square to the n squares after that square, it adds up numbers between several squares and determines who wins, that sorta stuff. Does anyone hav any suggestions???

    Thnx in advance
    Last edited by Nutshell; 04-23-2002 at 05:20 AM.

  12. #27
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    All i need is an AI algorithm. I don't mean the following, as i already knew:

    Win if possible
    Block if possible
    Take good spots if possible
    Make random move

    I want, if any, an algorithm that doesn't have to check for every possible move. Or at least check it more efficiently.

    Thank you.

  13. #28
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    sorta like this?

    ok, since in connect 4 there are 4 possible ways to get 4 in a row
    (left and right, up and down, diagonal left, diagonal right), use a loop that chages values to seach for a certain type of win

    Code:
    checkforleftrightwin()
    {
    y = 0;
    while(y != 3)
    {
    if(grid[x][y] == 'x' && grid[x+1][y] == 'x' && grid[x+2][y] == 'x'  && grid[x+3][y] == 'x')
    {
    cout << "X won" << endl;
    }
    y++;
    }
    
    }
    ok ummmmm that will check right and left if you have a 4x4 grid, but the syntax is bad, replace the 'x's with the ascii x's

  14. #29
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Hi, thnx for your response.

    But in fact, i was not asking how to check if anyone wins. I've already done that. And the program has been posted at another thread. I was asking of how to do the AI without checkin every possible moves if that is in fact possible.

    Thnx anyway

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Network Programming Board
    By Eibro in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 07-07-2003, 01:04 PM
  2. c++ games comparable to psx games?
    By anarchyhollow in forum Game Programming
    Replies: 17
    Last Post: 01-08-2003, 08:49 PM
  3. Video Games Industry. 5 years left.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 12-10-2002, 10:52 PM
  4. Starting games - books? api?
    By SeanMSimonsen in forum Game Programming
    Replies: 22
    Last Post: 11-27-2002, 09:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21