Advertisement

Chess AI

Started by January 22, 2007 08:25 AM
10 comments, last by GameDev.net 17 years, 10 months ago
how shall i build up this easiest way ? .. i am trying to use a minimax search tree.. but dont know how to implement it easiest. maybe a function in each class named eg. CalcMoves(); ? this is a rules as you maybe can see thek ing can move freely, tottaly freely over whole board as it is atm.
 

#include "chess.hpp"
#include "defines.h"
#include "cGameEngine.h"

namespace Game
{
	indBool Chess::KingLogic()
	{
		POINT mouse;
		GetCursorPos(&mouse);
		ScreenToClient(GAMEENGINE->GetMainWnd(), &mouse);


		for(int i = 0; i < 2; i++)
		{
			if( GAMEENGINE->m_input.mouseState.rgbButtons[0] && 0x80 &&
				mouse.x > m_king->m_screenX && 
				mouse.x < (m_king->m_screenX+97) &&
				GAMEENGINE->m_input.mouseState.rgbButtons[0] && 0x80 &&
				mouse.y > m_king->m_screenY && 
				mouse.y < (m_king->m_screenY+97))
			{
				m_king->m_placeBeforeX = m_king->m_screenX;
				m_king->m_placeBeforeY = m_king->m_screenY;
				m_pboard[ m_king->m_boardY ][ m_king->m_boardX ] = 0; 
				m_king->clicked = true;
			}


			m_king->m_boardX = (mouse.x / 97);
			m_king->m_boardY = (mouse.y / 97);


			if( m_king->clicked && GAMEENGINE->m_input.mouseState.rgbButtons[1] && 0x80 
				&& m_pboard[ m_king->m_boardY ][ m_king->m_boardX ] == 0)
			{
				m_king->m_boardX = (mouse.x / 97);
				m_king->m_boardY = (mouse.y / 97);

				m_king->clicked = false;
				m_king->m_screenX = (mouse.x / 97) * 97;
				m_king->m_screenY = (mouse.y / 97) * 97;
				m_pboard[ m_king->m_boardY ][ m_king->m_boardX ] = m_king->m_id;
			}
			else if(m_king->clicked && GAMEENGINE->m_input.mouseState.rgbButtons[1] && 0x80 &&
				m_pboard[ m_king->m_boardY ][ m_king->m_boardX ] != 0)
			{
				::MessageBox(0,"illegal move","Warning!",MB_OK);
			}
		}

		return true;
	}

} // end namespace







"Try not. Do, or do not. There is no try" - Yoda- Software Engineer, - Computer Game Developer
The code you posted has nothing to do with minimax. Screen coordinates and mouse positions should be nowhere near your search code. Keep representation and AI as separated as possible. In fact, I would start with a text-based program.

You can find very good information on chess programming here: http://www.brucemo.com/compchess/programming/

Advertisement
Quote: Original post by alvaro
The code you posted has nothing to do with minimax. Screen coordinates and mouse positions should be nowhere near your search code. Keep representation and AI as separated as possible. In fact, I would start with a text-based program.

You can find very good information on chess programming here: http://www.brucemo.com/compchess/programming/




well i know that the code doesn´t involve minimax, my question was how to implement it easiest ? , shall i make a function SearchThree(); och shall i put it in the logic rules function... and no, a console program isnt on the schedule and out assignment is to do in D3D.
Chess AI, if it is to play reasonably well, is not a simple thing. Chess itself is a very complicated thing.

Can you solve Tic-Tac-Toe, using minmax? You might also try Connect 4. I think some of the concepts might be useful, and it isn't that hard to program.

And it's wise to start text-based because otherwise you would just be wasting hours or days just on making an interface.

Once you get the computer actually playing the game, it should be easy to plug the logic part into any kind of interface - 2D or 3D.

In your code it looks as if GAMEENGINE is tied too closely to the interface. It shouldn't know anything about the mouse or the screen (although it should let you input a move). What colour or material the pieces and the board are is absolutely irrelevant to a human player too.
first of all there's a great series on chess programming in the AI section of the gamedev articles.
and second of all, as mentioned above i found that connect4 is a great game to start using minmax with for the following reasons:
1) there're only 7 possible moves (max) per turn so you don't have to waste time trying to write an algorithm that generates possible moves (which is hard to do in chess)
2) it's very easy to make a good static evaluation.
3) because of 1) it has a low branching factor so more levels can be searched
4) only two pieces and very simple rules (unlike chess)

also, connect4 is a hard enough game (unlike tictacto) that it's actually easy to make the computer look smart by setting up double traps and stuff. I could never beat the C4 that i made which searched 8 moves ahead (AB pruning).
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Quote: Original post by daniel_i_l
[...]
2) it's very easy to make a good static evaluation.

I agree with the rest of the post, but a good static evaluation for connect4 is not easy at all. Well, it's easier than chess, but it requires very careful analysis of ending situations, so you can concentrate from the beginning of the game in creating threats that will lead to winning endgames.

I can probably beat your 8-ply searcher if its static evaluation function doesn't know the difference between an even threat and an odd threat.

Advertisement
Thank you very much for your answers and tips :), means alot.
I tried to write a pseudo code yesterday of what to do in a minimax searhc tree in chess.

I see it much clearly now, so now the only thing is to write it.
i dont need to write a very smart and clever AI , i just have to write it so it moves reasonably.

Thank you guys for listening and helping.



"Try not. Do, or do not. There is no try" - Yoda- Software Engineer, - Computer Game Developer
Original post by alvaro
Quote: Original post by daniel_i_l

I agree with the rest of the post, but a good static evaluation for connect4 is not easy at all. Well, it's easier than chess, but it requires very careful analysis of ending situations, so you can concentrate from the beginning of the game in creating threats that will lead to winning endgames.

I can probably beat your 8-ply searcher if its static evaluation function doesn't know the difference between an even threat and an odd threat.


For my static evaluation i just checked how many 2 and 3 in-a-row pieces each side had that had potential to make a 4-in-a-row set and gave 5 points for 2 and 10 for 3. (for example, i gave 5 points to the red player if there were 2 red pieces next to 2 emty spots).

but if you want, you can get my Connect4 program here:
Connect4
The graphical interface isn't much, you just move the position with the left/right arrows and put down a piece with the down arrow. press 'r' to restart the game. the only bug that i've noticed (but haven't had the time to fix) is that sometimes at the end of the game you get a "free" move if you go on the first coloum to the left.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Quote: Original post by daniel_i_l
For my static evaluation i just checked how many 2 and 3 in-a-row pieces each side had that had potential to make a 4-in-a-row set and gave 5 points for 2 and 10 for 3. (for example, i gave 5 points to the red player if there were 2 red pieces next to 2 emty spots).

but if you want, you can get my Connect4 program here:
Connect4
The graphical interface isn't much, you just move the position with the left/right arrows and put down a piece with the down arrow. press 'r' to restart the game. the only bug that i've noticed (but haven't had the time to fix) is that sometimes at the end of the game you get a "free" move if you go on the first coloum to the left.


The program is relatively easy to beat. I also saw the bug at the end of a game after moving on column "4".

33-33-32-22-25-32-25-45-55-50-00-00-06-66-66-64 and now your program "passes".

Is there any way I can start the game, or does the program always go first? How about changing the search depth? What is the current setting?
You must be good, how did you beat it? (could you post the steps?)
It would be relativly easy to let the player start and the other things you mentioned but i really dont have time to mess around with it now. maybe during the next break i'll improve the interface and fix that bug.
the current search depth is 8 but it usually searches much less due to AB and other pruning algorithms.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky

This topic is closed to new replies.

Advertisement