-
Notifications
You must be signed in to change notification settings - Fork 0
/
concommon.java
58 lines (58 loc) · 1.63 KB
/
concommon.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.Scanner;
import java.io.*;
public class concommon
{
// pre: fn is a path to a conways save, row and col and the total number of rows
// and columns, and rulefile is the path to the rulefile to use
// post: Return a Conways with the given save loaded or a random Conways
// if loading the save failed. Both have the rulefile.
public static Conways fromFile(String fn,int row,int col,String rulefile)
{
Conways con;
Scanner input;
try
{
input = new Scanner(new File(fn));
con = new Conways(input.nextInt(),input.nextInt(),rulefile);
}
catch(Exception e)
{
con = new Conways(row,col,rulefile);
con.populate();
con.error = "File Not Found";
return con;
}
for(int r = 0; input.hasNext() && r < con.numRows(); r++)
{
for (int c = 0; input.hasNext() && c < con.numColumns(); c++)
{
char i = input.next().charAt(0);
if(i == '*')
{
con.set(r,c,new life());
}
else if (i == '-') continue;
else continue;
}
}
return con;
}
// pre: con is a running conways game, filename is a path to save the game to
// post: con is written to filename
public static void toFile(Conways con, String filename)
{
PrintWriter f;
try
{
f = new PrintWriter(filename,"UTF-8");
}
catch(Exception e)
{
con.error = "Failed to open "+filename;
return;
}
f.println(con.numRows()+" "+con.numColumns());
f.println(con);
f.close();
}
}