Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Chess  (Read 10823 times)

0 Members and 1 Guest are viewing this topic.

Voroz

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Chess
« on: August 08, 2016, 03:12:37 pm »
Hi!
Thought i'd share my most recent project that i made during 2 days. I started making it because i thought it would be interesting to create a chess AI. I managed to make one, it's not that great but for a horrible opponent like myself it's pretty hard :).



Tell me what you think, and if you can manage to beat it. (there is no end screen right now, you'll just have to know that you won"

for windows: https://dl.dropboxusercontent.com/u/150244750/Chess.rar

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Chess
« Reply #1 on: August 09, 2016, 07:33:39 pm »
Hi Voroz

I played your game and it is very nice and interesting ... i can't figure how you obtained so artificial intelligence in 2? days ... I really enjoyed playing this chess ...

Though, there are a few issues i would ask you to add (cos i want to continue playing, it's a nice challenge):
* to animate the opponent (the computer) movements, in order to see well what it is, and which of your pieces is taken, if so (you can do it by adding the PC's piece's coords small steps and redrawing it, up to it reaches its destiny)
* to announce when you are put in check, and then do not allow any movement that doesn´t make your king reach safety, if there is any possible (to show them anyhow would be great), otherwise announce check mate
* if a pawn reaches the 8th box, it should be converted to a queen. (i did, and it didn't happen, then i lost)
* when you are in check mate, the game should announce it and it should stop, and give the option to play again
* there should be the possibility to castle (a special movement that moves the king 2 spaces sideway and the rook to the box the king skipped) if the king and the rook didn't move before. This is done in order to protect the king and open the rook movility.
* you could program and offer in the game different difficulty leves (really the AI is good)

Thanks for your chess!
I hope you can finish the details

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Chess
« Reply #2 on: August 09, 2016, 07:54:39 pm »
Hi,

I have just played again ... not so far from the game started, i don't understand why, the black king suddenly "went out for a walk" and then after 5 moves i put it check mate ...  8)

Well, i know it's quite sophisticated that logic and the AI ... I could be this game's QA ... it's very interesting  ;D

Voroz

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Re: Chess
« Reply #3 on: August 10, 2016, 12:48:22 am »
Hi, thanks for trying the game, I was beginning to think noone would reply.

All your suggestions are good, and i will try to implement most of those things. I already thought about most of these things, but decided to save them for last (except for the castle move, didn't know about that).

Adding the AI was pretty hard, but not because of the AI itself, but more because i was having issues saving everything and then restoring everything after the algorithm did its thing.

It's a quite simple algorithm though. I just put values to the chess pieces: 1-3-3-5-9-1000, and then i check every possible move that can be done by the computer, then every possible counter move the player can make, and then i check every move the computer can make again to respond.

It then checks which moves that makes the computer have the biggest point advantage compared to the player, and to add some unpredictabilty, it picks one of these moves at random, if there are several moves with the same point gain (this is why the king just moved out in the start for you, all moves would give 0 point differance between computer and you, you weren't close enough for him to see what would happen yet so he picked a random move).

Doing it this brute force way is not optimal because i can't check very far ahead in the game until it takes too long for the computer to respond. There are ways to make it more efficient though. Currently the computer has to loop about 70 000 times to calculate a response. If i wanted to check 5 moves ahead instead of checking 3 moves ahead, it would have to loop 100 million times (if we are in a situation where each player can make 40 different moves, but if it's higher, like 50 moves, then it would be 300 million and so on).

I will look into making the algorithm more efficient, and i will implement some of your suggestions, thanks again for taking some time to trying it out and giving me feedback :).
« Last Edit: August 10, 2016, 12:50:37 am by Voroz »

roccio

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: Chess
« Reply #4 on: August 10, 2016, 02:13:35 pm »
I think that for the first part of the game you could use a "opening library" of moves, then go to full algorithm playing.

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Chess
« Reply #5 on: August 23, 2016, 07:02:49 pm »
Hello, Voroz

Has there been any improvement in the chess game? As I had told you, I would like to continue playing, I like chess very much ... I play since I was a kid  ;D

If you can implement the issues that were missing it would be very good  :)

Voroz

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Re: Chess
« Reply #6 on: August 26, 2016, 07:52:34 am »
Hello, Voroz

Has there been any improvement in the chess game? As I had told you, I would like to continue playing, I like chess very much ... I play since I was a kid  ;D

If you can implement the issues that were missing it would be very good  :)
I realized that my algorithm that finds the best move wasn't working like it should, so I need to make that better first. The hard part is to realize which move the player will make. He will make a move that benefits him, but how many moves ahead is the player thinking? I need to know this so the computer makes the move that benefits it the most if the player also makes his best move.

Anyway it's a bit confusing to implement this correctly, so I don't know when i feel like attempting it again :P.

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Chess
« Reply #7 on: August 26, 2016, 10:54:39 pm »
Look up the "min max algorithm" or sometimes also called "minimax".

It's basically what you've described above.

For the AI player it will always find the best move, while expecting the player to do the worst possible move.

The number of rounds going forward determines the final difficulty. For example, only looking forward once  would be easiest to beat, where the AI just picks some random move.

BhoxzJ1

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: Chess
« Reply #8 on: March 12, 2018, 04:32:10 am »
hello guys.. how add check mate in chess using dev c ??

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Chess
« Reply #9 on: April 22, 2018, 03:27:47 am »
Hi

The 1st one is the logical part; 2nd is up to your IDE, libraries, and programming language.

As I have not the foggiest idea of the 2nd, I'll reffer to the 1st part.

You are in check mate, if and only if, you can't avoid your king being taken by any of the opponent's pieces, whatever it is the piece you move (not only your king), each other, to any of all its allowed lockers.

So, you would have to evaluate, for each one of your (alive) pieces (that you may have in a List<> or vector in C++), for each one of their possible moves, if you remain in check. If moving ANY of your pieces, WHEREVER they can move to, does not annul the check (*), then you are in check mate. I would recommend start the search by finding if any of your pieces can take the opponent piece that is threatening your king (and then verify if you are still in check - it may happen that you can take the piece which points to your king, but moving that piece of yours, unblocks it from an aligned opponent's rook - SO, for EACH possible move of ALL your pieces, it's necesary to do a complete test). If this is not posible, then I would continue verifying if a move of your king might annul the check. If there isn't, then you would have to check all of your pieces' allowed moves in order to find out is there is any way to block the threat. If you find any way to annul the check, then stop there the function process returning false (you were not in check mate).

By the way, for this, previously, you would need a function to determine if you are in check. This is not as simple as anyone might think quickly. You are in check when one opponent piece is at one move of taking your king. But this does not occur only if your opponent aligns a rook, or a bishop pointing to your king. It may also happen if he moves a piece, that was blocking another one of his, and then you are in check by a piece that was not moved.

Maybe all this is a bit complicated. I have to say that I play chess since I was a kid.
Feel free to ask if you have any more doubt

Pablo