Sign in to follow this  
mandar9589

Making an opening book for my game of Connect four.

Recommended Posts

I have a fairly playable connect four game and now my next step is to make an opening book for it. All I want to know is how to make an opening book for connect four. One idea I can think of is write it in a text file while playing and then retrieve it. But then when I will re-run the software for the second time, then the data will be erased and everything will be gone. How can I do this?

Share this post


Link to post
Share on other sites
Quote:
Original post by mandar9589
One idea I can think of is write it in a text file while playing and then retrieve it.

Write a text file beforehand, not while playing.

Quote:
Original post by mandar9589
But then when I will re-run the software for the second time, then the data will be erased and everything will be gone.

What's stopping you from loading it again?

Share this post


Link to post
Share on other sites
The point is first making a book using arrays was very tedious. and in this method, I am not able to make up my mind on how to read the moves from file as the basic moves are not in the integer data type.

Share this post


Link to post
Share on other sites
I will be more clear on this point.
I understood that in order to program a book, I have to fill in the data once.
then I have to read it,I fully agree that my data erase point was not valid, once I make the final entry in the file.

Now coming to structure of my program, I have utilized a modified data type and not primitive as int or any thing else. There are two separate numbers in it, one is for the row and other for the column.

Now when I try to read this modified data, then it gives me NullPointer exception. and the output is null , but still there is content in the file.

I tried to access the data by storing it in a string and then tried to convert it into required data type.
but not successful.
Can any one help me with how to store the contents from a file into a string?

Share this post


Link to post
Share on other sites
I'm not convinced you actually need an opening book for this game, but let's say you do. A given board has two players and a bunch of columns where you drop tokens alternating the player, right? One way to store this information might be as an array. Zero might mean empty, 1 for computer, 2 for enemy. If the board is 5x5 (simplified example) you might have an opening book like this (just a format I came up with on the spot):

00000:00000:00000:00000:00000 - 2
00120:00000:00000:00000:00000 - 3
02120:00010:00000:00000:00000 - 4

On the left is a compact representation of the board, from the 0th row up. The first line reads "if it's my move and board is empty, drop token on 2nd column (index starts from 0)". The second line reads "if I have token on 2nd column 0th row and enemy has token on 3rd column 0th row, drop token on third column". Etc. It's a bit of a compromise between compactness and human readability.

I would read in these strings. Then when you need to consult the book, encode the current board from your data structure into this string format and perform pattern matching. If a match is found, execute the recommended move.

I'm not a C++ programmer but I believe the function you are looking for is fgets() from stdio.h. Or in Java (since you mention NullPointerException) you would want BufferedReader.readLine()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this