I’m trying to store more than 512 combinations of a game somewhere locally on my hard drive as a single file permanently unlike the temorary nature of arrays. If I store it in a text file like I normally do, I wouldint be able to access the n’th combination easily. Is there any database kind of thing where i can store it kind of like XL sheets, or tables. I’m a complete noob at database related things. So even basic things you suggest will be helpful. I’ll be using java for this
Note on the game:- I’m trying to store all possible combinations of a tic tac toe game. Say I store one combination as “xoooxooox”. There are 512 combinations possible for this(2^9). Having it stored line by line in a text file is not that great to access the n’th combination. So, can you guys give me a clue on how to store it locally on my hard drive permanently as i said before…!
I guess i’ll work out the algorithm and code. I just need your help with the storage part! i tried googling it and failed…
Thanks in advance!
2
There may be more combinations than you think.
Even so, with a modern computer the number is not all that big. You could easily read the entire text file into an array in memory and work with that.
A better approach is to consider that your data file is very predictable:
9 characters (x or o), plus one or two (system dependent) for the end of line.
Let’s say your system uses one character for end of line. This means each row is 10 characters long, and quite possibly 10 bytes long as well. (Assuming ASCII or UTF-8)
If you want to read the 3rd row, you’ll need to SEEK to position 30, then read 10 bytes.
I haven’t programmed Java in decades, so I don’t have the specific library calls. This sample from java2s might help:
import java.io.File;
import java.io.FileInputStream;
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("C:/String.txt");
FileInputStream fin = new FileInputStream(file);
int ch;
// skip first 30 bytes
fin.skip(30);
for (int i =0; i < 10; i++) {
ch = fin.read();
myString = myString + ch;
}
}
}
5