-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameboard.java
More file actions
78 lines (67 loc) · 1.97 KB
/
gameboard.java
File metadata and controls
78 lines (67 loc) · 1.97 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//Generate the board from the text file
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class gameboard {
public static void main(String[] args) throws Exception {
int boardsize = 0;
char c;
int i;
char[][] board;
String board1 = "";
//Accept a file input from the user
while(true){
Scanner in = new Scanner(System.in);
System.out.println("Type the name of the file you would like to read: ");
String fileName = in.nextLine();
try{
FileReader fr = new FileReader(fileName);
//convert the file components into a string
while ((i=fr.read()) != -1) {
c = (char) i;
board1 += String.valueOf(c);
}
break;
} catch (FileNotFoundException e) {
System.out.println("This is not an existing file please restart and try again");
}
}
//clean the string to have it all on one line
board1 = board1.replaceAll("\r", "").replaceAll("\n", "");
//compute the boardsize
char s = board1.charAt(1);
boolean doubleDigits = isNumeric(String.valueOf(s));
if(doubleDigits){
String t = board1.substring(0, 2);
boardsize = Integer.parseInt(t);
}else{
char p = board1.charAt(0);
boardsize = Integer.parseInt(String.valueOf(p));
}
board = new char[boardsize][boardsize];
//create the board
if(doubleDigits){
for (int j=0; j < boardsize; j++) {
for (int k=0; k < boardsize; k++) {
board[j][k] = board1.charAt(k + j*boardsize + 2);
}
}
} else {
for (int j=0; j < boardsize; j++) {
for (int k=0; k < boardsize; k++) {
board[j][k] = board1.charAt(k + j*boardsize + 1);
}
}
}
robot.initializeRobotEnvironment(board);
}
//checks if a string is a number
public static boolean isNumeric(String str) {
try {
Integer.parseInt(str);
return true;
} catch(NumberFormatException e){
return false;
}
}
}