This blog post is more like a log than a tutorial. Here I'm logging the steps I followed while creating the AI player for the TankGame which provided as a 2nd year project. I will provide every single detail with this post and the future posts, so if anyone interested in following this as a tutorial he/she will get a chance.
First of all I should explain whats required to do in this project. We are provided with a Server application which hosts the TankGame (something like this). Our task is to program an AI player program in order it can win over the other players(4 other).
Downloads:
Introduction to game : download
Server program(for practicing purposes) : download
Information about above Sever program : download
Get server Program running : Unzip the Downloaded server program, a c# project folder. You have to open MS Visual Studio and open this project. (Or double click on the C# project file located in ...\Server_v3.2.0.1\lk.ac.mrt.cse.pc11 named lk.ac.mrt.cse.pc11). Then by pressing F5 key you can run the server.
Now we have the Server program, so we should create the client program. The client and the server programs should be able to communicate with each other. So we need to create the socket connections. In server program configuration file it is configured as the server port as 6000 and the client port as 7000 and the address as 127.0.0.1 .
So according to the introduction slides in order to join the game, the client should send the msg "JOIN#" to the server. Here's the simple Java code for sending msgs to server.
private BufferedWriter write;
Socket serversoc = new
Socket("127.0.0.1", 6000);
write = new BufferedWriter(new
OutputStreamWriter(serversoc.getOutputStream()));
write.write("JOIN#");
write.flush();
However, for this server program "flush()" method didn't work, So I had to use the "write.close()" instead
.
Because of that we have to reopen the bufferedreader as well as the socket each time we want to send a msg, (Hope this server bug will repair soon and then I'll update the post ASAP).
Important : For above code to work properly you should first run the Server program. Then using the GUI select(put a tick) the four dummies and then click start. The game will wait for the other player(your client) and will start as soon as you send the "JOIN#" msg.
OK. Now our target is to read the server reply, For do this we should open the socket with the port number 7000. And then we can listen to that socket and receive the server replies. Following code will show a simple example but in your client program you should use a listener and do the job according to the msg arrival.
private BufferedReader read;
ServerSocket clientserversoc = new ServerSocket(7000);
Socket clientsoc = clientserversoc.accept();
read = new BufferedReader(new InputStreamReader(clientsoc.getInputStream()));
System.out.println(read.readLine()); // do the job, I just print out the msg
read.close();
Again here I use "read.close()" because without that line it won't work. :-/. Ok. thats it for today. Soon I'll post how to work with the game engine and the AI algorithms we're using for this program. Thank you.!