Reading and Saving Text Files in BWAPI

https://github.com/bwapi/bwapi
fkstarr
Posts: 10
Joined: Fri Mar 05, 2010 6:06 pm

Reading and Saving Text Files in BWAPI

Postby fkstarr » Fri Mar 05, 2010 7:16 pm

So, I've been trying a to get BWAPI to read a file on start up and save a file on closing. The idea is that the AI should be able to "remember" what happened in previous games in order to learn from its mistakes in a reinforcement learning algorithm. I ran the following simple function in void ExampleAIModule::onStart() to see what would happen:

CODE
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <map>

void ExampleAIModule::readFromFile()
{
char buffer[256];
std::ifstream myfile ("test.txt");

if (! myfile.is_open())
{
Broodwar->printf("error");
}

while (! myfile.eof() )
{
myfile.getline (buffer,100);
Broodwar->printf("%s", buffer );
}
}


The result was BW crashing, while beeping. beep beep beep ad infinitum. I have a feeling that I can't do this with a .dll but... any ideas on how to approach this problem?
Nuclear
Posts: 13
Joined: Thu Jan 28, 2010 8:32 pm

Postby Nuclear » Fri Mar 05, 2010 11:37 pm

Does the file exists? The module looks for it in the Starcraft main folder (where starcraft.exe is).
If the file doesn't exists, you need to exit from readFromFile function (if you don't want to create it and write something inside).
So you need return; after Broodwar->printf("error");
Or create a file:

Code: Select all

std::ofstream out;
out.open("test.txt");
out << "Writing something to a file.n";
out.close();

Otherwise, myfile.eof() returns false (because the file doesn't exists) and the game hangs in the infinite while loop.
krasi0
Posts: 254
Joined: Thu Dec 18, 2008 12:42 pm

Postby krasi0 » Sat Mar 06, 2010 11:35 am

sorry for the off topic but, fkstarr, have you managed to make Starcraft automatically restart matches so that your learning algo is based on let's say hundreds of iterations?
fkstarr
Posts: 10
Joined: Fri Mar 05, 2010 6:06 pm

Postby fkstarr » Sat Mar 06, 2010 8:15 pm

Thanks Nuclear, me and my brother finally figured it out. It was those two problems: 1) the file location and 2) closing the file.

And krasi0, I haven't... but it's definitely one of the next steps. Any ideas? I was gonna program the algorithm first and then figure it out, heh.
krasi0
Posts: 254
Joined: Thu Dec 18, 2008 12:42 pm

Postby krasi0 » Sun Mar 07, 2010 11:24 am

Currently I am using Autohotkey to start a new game vs a specific opponent. I don't know how to restart a game from within BWAPI though. Any ideas would be appreciated.
Nuclear
Posts: 13
Joined: Thu Jan 28, 2010 8:32 pm

Postby Nuclear » Mon Mar 15, 2010 9:48 am

In single player you can use Broodwar->restartGame()
fkstarr
Posts: 10
Joined: Fri Mar 05, 2010 6:06 pm

Postby fkstarr » Wed Jun 16, 2010 7:13 pm


Return to “BWAPI (wrappers in other languages / questions and announcements related to BWAPI itself)”

Who is online

Users browsing this forum: No registered users and 1 guest