so I've been looking but I simply just don't know how to state my problem.
So I'm just going to break an egg, and if you can link to the correct answer anyhow then please don't be afraid to, this is a long shot and I know this exists many places, I am just unable to find it.
I am looking at making a 2D map, based off on PLUS signs (+) and ONE (C), the C is the characters current location.
It would look like this
C+++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
When printed.
Notice C is based off of integers, namely currentX and currentY (1 & 1).
This is my current code in bp_Map.class
public class bp_Map {
// Map
public static String mapP = "+";
public static String mapC = "C";
public static int sizeY = 19;
public static int sizeX = 19;
public static void drawMap(int currX, int currY) {
int currentY = 0;
while (currentY <= sizeY) {
drawX();
System.out.print("\n");
currentY ++;
}
}
public static void drawX() {
int currentX = 0;
while (currentX <= sizeX) {
System.out.print(mapP);
currentX++;
}
}
I could use an array, instead of mapP and mapC and just do
public static final String mapChar[] = {"+", "C"}
But I don't feel the need to do this atm.
My current problem is I don't want 20 if statements (or 1 if and 19 if else statements) to check the location of X, and then print correspondingly Y.
I am new to java and still learning, I have used while, but should I use for? I'm a bit lost, hope you guys can help me. This is for a text-based rpg, and I'm working on it alongside my studies.
You don't need if-else cases - this is a perfect usage example for loops.
First of all, define things which will never change as final fields in your class:
private static final String EMPTY = "+";
private static final String CHARACTER = "C";
private static final int SIZE_X = 20;
private static final int SIZE_Y = 5;
For this example, I'll be using fields for the current X and Y coordinates too, but you may want to change this since I assume they come from elsewhere in your program:
private static int currentX = 7;
private static int currentY = 3;
Now, think of how a TV draws pixels on a screen: from the top to the bottom and from left to right, pixel by pixel, at least 30 times a second. Let's try and do the same, and draw one row at a time:
public static void main(String[] args) {
if(currentX > SIZE_X - 1 || currentY > SIZE_Y - 1) {
throw new IllegalStateException("Out of bounds");
}
for (int y = 0; y < SIZE_Y; y++) {
drawRow(y);
}
}
What would the drawRow() function look like? One possible implementation is below:
private static void drawRow(int i) {
// Use a StringBuilder, ~30 times faster than String concatenation!
StringBuilder row = new StringBuilder();
if(i == currentY) {
// Create this row differently, as it contains the character.
for (int x = 0; x < SIZE_X; x++) {
if(x == currentX) {
row.append(CHARACTER);
} else {
row.append(EMPTY);
}
}
} else {
// Create an empty row.
for (int x = 0; x < SIZE_X; x++) {
row.append(EMPTY);
}
}
// "Draw" the row by printing it to the console.
System.out.println(row.toString());
}
This produces:
++++++++++++++++++++
++++++++++++++++++++
++++++++++++++++++++
+++++++C++++++++++++
++++++++++++++++++++
Try playing around with the coordinates and run main again. This is just one of many possible solutions - the neat thing about the above code is that no Map or even array is needed, but it may be that you do need them eventually and the code would have to change to accommodate this (e.g. keep a bit matrix, make a nested for loop over it and draw the set bit as the character). Let us know if you would like an example of this.
An approach I would use in this case is the following in pseudo-code:
Create a character matrix with the dimensions of sizeX by sizeY
Use the java.util.Arrays.fill builtin to fill the entire matrix with the character '+'
Replace the character at position {currX, currY} (1-indexed) with character 'C'
Pretty-print the matrix
Here a possible implementation of what I described above:
/*
* Prints a block of sizeX by sizeY of the filler character,
* where the character at position {posX, posY} (1-indexed) is replaced with the replacement character
*
* TODO: Validation checks. Currently assumes posX and posY are always within range of the matrix
*/
public void drawMap(int sizeX, int sizeY, char fillerChar, char replacementChar, int posX, int posY){
// Create a char-matrix of dimensions sizeX by sizeY
char[][] matrix = new char[sizeX][sizeY];
// Fill this matrix initially with the filler-character
for(char[] row : matrix)
java.util.Arrays.fill(row, fillerChar);
// Replace the character at position {currX, currY} (1-indexed) with the replacement-character
matrix[posX-1][posY-1] = replacementChar;
// Print the matrix
prettyPrintMatrix(matrix);
}
private void prettyPrintMatrix(char[][] matrix){
for(char[] row : matrix){
for(char ch : row)
System.out.print(ch);
System.out.println();
}
}
This could then be called with:
drawMap(10, 10, '+', 'C', 4, 2);
Which will output:
++++++++++
++++++++++
++++++++++
+C++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
Try it online.
Some things to note:
I've added the size and characters as parameter to the method. In my TIO-link above you can see a call with different sizes or characters also works (i.e. m.drawMap(5, 5, 'a', 'B', 5, 5);).
I've added a TODO for validation checks. If the given posX or poxY are larger than the sizeX or sizeY respectively, it will of course give an ArrayOutOfBoundsException. So perhaps a check at the top of the method to see if the given pos-parameters are valid is in order depending on how you want to use it.
Related
The nature of this problem has changed since submission, but the question isn't fit for deletion. I've answered the problem below and marked it as a community post.
I'm writing a recursive path-navigating function and the final piece I need involves knowing which cell you came from, and determining where to go next.
The Stage
You are given a 2d array where 0's denote an invalid path and 1's denote a valid path. As far as I know, you are allowed to manipulate the data of the array you're navigating, so I mark a traveled path with 2's.
The Goal
You need to recursively find and print all paths from origin to exit. There are four mazes, some with multiple paths, dead ends, or loops.
I've written code that can correctly handle all three cases, except the method for finding the next path is flawed in that it starts at a fixed location relative to your current index, and checks for a travelled path; If you encounter it, it's supposed to retreat.
While this works in most cases, it fails in a case when the first place it checks happens to be the place you came from. At this point, it returns out and ends prematurely.
Because of this, I need to find a way to intelligently start scanning (clockwise or anti-clockwise) based on where you came from, so that that place is always the last place checked.
Here is some code describing the process (note: edge cases are handled prior to this, so we don't need to worry about that):
private static void main()
{
int StartX = ;//Any arbitrary X
int StartY = ;//Any arbitrary Y
String Path = ""; //Recursive calls will tack on their location to this and print only when an exit path is found.
int[][] myArray = ;//We are given this array, I just edit it as I go
Navigator(StartX, StartY, Path, myArray);
}
private static void Navigator(int locX, int locY, String Path, int[][] myArray)
{
int newX = 0; int newY = 0;
Path = Path.concat("["+locX+","+locY+"]");
//Case 1: You're on the edge of the maze
boolean bIsOnEdge = (locX == 0 || locX == myArray.length-1 || locY == 0 || locY == myArray[0].length-1);
if (bIsOnEdge)
{
System.out.println(Path);
return;
}
int[][] Surroundings = surroundingsFinder(locX, locY, myArray);
for (int i = 0; i <= 7; i++)
{
//Case 2: Path encountered
if (Surroundings[0][i] == 1)
{
myArray[locX][locY] = 2;
newX = Surroundings[1][i];
newY = Surroundings[2][i];
Navigator(newX, newY, myArray, Path);
}
//Case 3: Breadcrumb encountered
if (Surroundings[0][i] == 2)
{
myArray[locX][locY] = 1;
return;
}
}
}
//generates 2D array of your surroundings clockwise from N to NW
//THIS IS THE PART THAT NEEDS TO BE IMPROVED, It always starts at A.
//
// H A B
// G - C
// F E D
//
static int[][] surroundingsFinder(int locX, int locY, int[][] myArray)
{
int[][] Surroundings = new int[3][8];
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
}
}
//Can be done simpler, is done this way for clarity
int xA = locX-1; int yA = locY; int valA = myArray[xA][yA];
int xB = locX-1; int yB = locY+1; int valB = myArray[xB][yB];
int xC = locX; int yC = locY+1; int valC = myArray[xC][yC];
int xD = locX+1; int yD = locY+1; int valD = myArray[xD][yD];
int xE = locX+1; int yE = locY; int valE = myArray[xE][yE];
int xF = locX+1; int yF = locY-1; int valF = myArray[xF][yF];
int xG = locX; int yG = locY-1; int valG = myArray[xG][yG];
int xH = locX-1; int yH = locY-1; int valH = myArray[xH][yH];
int[][] Surroundings = new int[3][8];
Surroundings[0][0] = valA; Surroundings[1][0] = xA; Surroundings[2][0] = yA;
Surroundings[0][1] = valB; Surroundings[1][1] = xB; Surroundings[2][1] = yB;
Surroundings[0][2] = valC; Surroundings[1][2] = xC; Surroundings[2][2] = yC;
Surroundings[0][3] = valD; Surroundings[1][3] = xD; Surroundings[2][3] = yD;
Surroundings[0][4] = valE; Surroundings[1][4] = xE; Surroundings[2][4] = yE;
Surroundings[0][5] = valF; Surroundings[1][5] = xF; Surroundings[2][5] = yF;
Surroundings[0][6] = valG; Surroundings[1][6] = xG; Surroundings[2][6] = yG;
Surroundings[0][7] = valH; Surroundings[1][7] = xH; Surroundings[2][7] = yH;
return Surroundings;
}
Can anyone help me with this? As you can see, surroundingsFinder always finds A first, then B all the way to H. This is fine if and only if you entered from H. But if fails on cases where you entered from A, so I need to make a way to intelligently determine where to start finding. Once I know this, I can probably adapt the logic so I no longer use a 2D array of values, as well. But so far I can't come up with the logic for the smart searcher!
NOTE: I am aware that Java does not optimize middle-recursion. It seems impossible to get tail recursion working for a problem like this.
The Solution
The initial goal was to print, from start to end, all of the paths that exit the array.
An earlier rendition of the script wrote "0" on treaded locations rather than "2", but for some reason I imagined that I needed the "2" and I needed to differentiate between "treaded path" and "invalid path".
In fact, due to the recursive nature of the problem, I discovered that you can in fact solve the problem writing only 0's as you go. Also, I no longer needed to keep track of where I came from and instead of checking clockwise over a matrix, I was iterating from left to right down the 3x3 matrix surrounding me, skipping my own cell.
Here is the completed code for such a solution. It prints to console upon finding an exit (edge) and otherwise traces itself around the maze, complete with recursion. To start the function, you are given a square 2D array of 0's and 1's where 1 is a valid path and 0 is invalid. You are also given a set of coordinates where you are "dropped in" (locX, locY) and an empty string that accumulates coordinates, forming a path that is later printed out (String Path = "")
Here is the code:
static void Navigator(int locX, int locY, int[][] myArray, String Path)
{
int newX = 0;
int newY = 0;
Path = Path.concat("["+locX+","+locY+"]");
if ((locX == 0 || locX == myArray.length-1 || locY == 0 || locY == myArray[0].length-1))
{//Edge Found
System.out.println(Path);
pathCnt++;
myArray[locX][locY] = 1;
return;
}
for (int row = -1; row <= 1; row++)
{
for (int col = -1; col <= 1; col++)
{
if (!(col == 0 && row == 0) && (myArray[locX+row][locY+col] == 1))
{ //Valid Path Found
myArray[locX][locY] = 0;
Navigator(locX+row, locY+col, myArray, Path);
}
}
}
//Dead End Found
myArray[locX][locY] = 1;
return;
} System.out.println(Path);
pathCnt++;
swamp[locX][locY] = 1;
return;
}
for (int row = -1; row <= 1; row++)
{
for (int col = -1; col <= 1; col++)
{
if (!(col == 0 && row == 0) && (swamp[locX+row][locY+col] == 1))
{ //Valid Path Found
swamp[locX][locY] = 0;
Navigator(locX+row, locY+col, swamp, Path);
}
}
}
//Dead End Found
swamp[locX][locY] = 1;
return;
}
As you may determine yourself, every time we "enter" a cell, we have 8 neighbors to check for validity. First, to save on run time and to avoid going out of the array during our for loop (it can't find myArray[i][j] if i or j point it outside, and it will error out), we check for edges. Since we're given the area of our swamp we use a truth comparison statement that essentially says ("(am I on the top or left edge?) or (am I on the bottom or right edge?)"). If we ARE on an edge, we print out the Path we're holding (thanks to deep copy, we have a unique copy of the original Path that only prints if we're on an edge, and includes our full set of coordinates).
If we aren't on an edge, then we start looking around us. We start at top left and move horizontally to bottom right, with a special check to make sure we're not checking where we're standing.:
A B C
D . E
F G H
This loop checks only for 1's and only calls the function up again should that happen. Why? Because it is the second-to-last case. There is only one extra situation that will occur, and if we reach the end of the function it means we hit that case. Why write extra code (checking for 0's to specifically recognize it?
So, as I just mentioned, if we exit the for loop, it means we didn't encounter any 1's at all. It means we're surrounded by zeros! It means we've hit a dead end, and that means that all we have to do is error our away out of that instance of the function, ergo the final return;.
All in all, the final function is simple. But coming from no background and having to realize the patterns and meanings of these cases, and after several failed attempts at this, it can take quite a bit of work. I was several days at work on perfecting this.
Happy coding, Everyone!
Your issue seems to be with:
if (Surroundings[0][i] == 2)
{
myArray[locX][locY] = 1;
return;
}
Perhaps this should be changed to:
if (Surroundings[0][i] == 2)
{
// not sure why you need this if it's already 1
myArray[locX][locY] = 1;
// go to next iteration of the "i" loop
// and keep looking for next available path
continue;
}
Your recursive method will automatically return when none of the surrounding cells satisfy the condition if (Surroundings[0][i] == 1).
PS: It's conventional to name your variables using small letter as the first character. For example: surroundings, path, startX or myVar
So I need to take a 2D array do calculations to each elements and transfer that into another 2D array while using the values to the "left" "right" "up" and "down" of the current element. If the current element is on the edge (x = 0, y = 0, x = array.length , y = array.length) I will get an array out of bounds error. I want to create a for loop that deals with each of those cases but I don't know how to do it. A sample of my code is
private void buildE(int[][] array, int y, int x)
{
int up = array[y - 1][x];
int down = array[y + 1][x];
int left = array[y][x - 1];
int right = array[y][x + 1];
if(up == 0){
buildETopRow(array);
}
E will be my new array. This method does not work because y does not equal 0, it just doesn't exist but I can't set ints to null either. In the case of an out of bounds error I need the element (up, down, left, or right) that is out of bounds to equal the current element. Is there a way I can still use a for loop for this or do I need to do something else?
If I read this correctly you want to effectively treat the difference of an element on the edge with an element off the edge as 0. If that's true I would write four methods right(), left(), up() and down(), with down() shown below as an example:
/*
* Return the difference between an element an the element below it
*/
public void down(int x, int y) {
if (y == array.length - 1) {
\\ on the bottom edge
return 0;
}
return array[y][x] - array[y + 1][x];
}
And inside your loop you'd calculate:
up(x,y) + down(x,y) + right(x,y) + left(x,y)
or whatever calculation it is you need to sum up.
The easiest way it to surround your array with a border region. So that your x dimension is really width+2.
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
int realWidth = 10;
int realHeight = 10;
int[][] in = new int[(realWidth+2)][(realHeight+2)];
int[][] out = new int[(realWidth+2)][(realHeight+2)];
for (int j = 1;j<realHeight+1;j++)
{
for (int i = 1;i<realWidth+1;i++)
{
int top = in[j-1][i];
int bottom = in[j+1][i];
int left= in[j][i-1];
int right = in[j][i+1];
out[j][i] = operation(top,bottom,left,right);
}
}
}
public static int operation (int top,int bottom,int left,int right)
{
return top+bottom+left+right;
}
}
I'm not totally sure what your question is, but (1) the usual structure for traversing a 2D array is to use nested for loops (one inside the other), and (2) when you want wrap-around counters (e.g. 2, 3, 0, 1, 2, ...) use the remainder operator %.
int numRows = theArray.length;
int numCols = theArray[0].length;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
int right = theArray[(j+1) % numCols];
int down = theArray[(i+1) % numRows];
int left = theArray[(j+numCols-1) % numCols];
int up = theArray[(i+numRows-1) % numCols];
/* right, down, left, and up will be the elements to the right, down,
left, and up of the current element. Npw that you have them, you can
process them however you like and put them in the other array. */
}
}
What the remainder operator A%B does is sets A back to zero once it gets as large as B. Since B is the size of your array, that's exactly when it is too large and will cause an IndexOutOfBounds error. Note: That's not how % works but it's an ok way to think of what it does. To find out more about it you can google it, I found an ok explanation here.
I'm kinda new to programming and need help doing a recursive method.I have a method that picks a random space in a 2d array and then I want to check if the space is free.If the space is free I want to use that space but if it isn't I want to pick a new random space in the 2d array.Thanks
import java.io.* ;
import java.util.ArrayList ;
public class WordSearchPuzzle
{
private char[][] puzzle ;
private ArrayList<String> puzzleWords ;
private int letterCount = 0 ;
private int gridDimensions;
public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
{
this.puzzleWords = userSpecifiedWords ;
}
private void createPuzzleGrid()
{
int i, itemLength;
String item;
for (i = 0; i < puzzleWords.size(); i++) {
item = puzzleWords.get(i);
itemLength = item.length();
letterCount = letterCount + itemLength;
}
gridDimensions = letterCount * 2;
puzzle = new char[gridDimensions][gridDimensions] ;
}
private void generateWordSearchPuzzle()
{
}
public void firstSpace(String Word)
{
int row, column;
row = (int)(Math.random() * gridDimensions +1);
column = (int)(Math.random() * gridDimensions +1);
if(puzzle[row][column] != ' '){
firstSpace();
}
}
The specific issue you mentioned in the comments is because the firstSpace method needs to have a string as a parameter. You should use:
firstSpace(word);
Also be aware that this method doesn't currently return anything so you have no way of knowing which space it chose.
I don't think adding 1 in your index calculations is necessary and could perhaps cause an array out of bounds exception. Although, that depends on your definition of gridDimensions.
The problem you specified in the comments is because the Java compiler was trying to find a method named 'void firstSpace()', this is a different method that 'void firstSpace(String word)'.
public void firstSpace(String word)
{
int row, column;
// No need to add 1, Java arrays are accessed with the first index
// being 0. Math.random() returns from 0 up to but not including 1.0.
// e.g. array size = 50, min index = 0, max index = 49
// Lets say you get very close to 1 e.g. 0.9999, then
// 0.9999 * 50 = 49.995 (after integer truncating you have 49)
row = (int)(Math.random() * gridDimensions);
column = (int)(Math.random() * gridDimensions);
if(puzzle[row][column] != ' ') {
// If this element is not "empty" then run the method again
// using recursion. null might be a better choice to compare
// to depending on how you initialized the array.
firstSpace(word);
} else {
// Otherwise we're finished and we can set the array element
// to the new word.
// (Assumed post condition (you might want to do something else once you
// find a blank index))
puzzle[row][column] = word;
}
}
I am trying to implement an algorithm to clear dead stones in my Go game.
I hear that floodfill is the best to achieve this as using it recursively would be most effiecient and easier to implement.
I am having trouble using it within my code and was wondering how I should go about implementing it.
This is one of my classes, it is pretty self explanatory.
import java.io.*;
public class GoGame implements Serializable {
int size;
char[][] pos; // This is the array that stores whether a Black (B) or White (W) piece is stored, otherwise its an empty character.
public GoGame(int s){
size = s;
}
public void init() {
pos = new char[size][size];
for (int i=0;i<size;i++) {
for (int j=0;j<size;j++) {
pos[i][j] = ' ';
}
}
}
public void ClearAll() {
for (int i=0;i<size;i++) {
for (int j=0;j<size;j++) {
pos[i][j] = ' ';
}
}
}
public void clear(int x, int y) {
pos[x][y]=' ';
}
public void putB(int x, int y) { //places a black stone on the board+array
pos[x][y]='B';
floodfill(x,y,'B','W');
}
public void putW(int x, int y) { //places a white stone on the board+array
pos[x][y]='W';
floodfill(x,y,'W','B');
}
public char get(int x, int y) {
return pos[x][y];
}
public void floodfill(int x, int y, char placed, char liberty){
floodfill(x-1, y, placed, liberty);
floodfill(x+1, y, placed, liberty);
floodfill(x, y-1, placed, liberty);
floodfill(x, y+1, placed, liberty);
}
}
x and y are the coordinates of the square, placed is the character of the stone put down, liberty is the other character
Any help would be amazing!
while the other answers are technically correct, you are also missing a lot more logic related to go. what you need to do is, i think (on a B move):
for each W neighbour of the move:
check that W group to see if it has any liberties (spaces)
remove it if not
flood fill is useful for finding the extent of a group of stones, but your routine needs a lot more than that (i'm simplifying here, and also trying to guess what this routine is used for - see comments below this answer).
given the above, a flood fill that identifies all the stones in a group would be something like this (note that it uses a second array for the fill, because you don't want to be changing pos just to find a group):
public void findGroup(int x, int y, char colour, char[][] mask) {
// if this square is the colour expected and has not been visited before
if (pos[x][y] == colour && mask[x][y] == ' ') {
// save this group member
mask[x][y] = pos[x][y];
// look at the neighbours
findGroup(x+1, y, colour, mask);
findGroup(x-1, y, colour, mask);
findGroup(x, y+1, colour, mask);
findGroup(x, y-1, colour, mask);
}
}
you can call that to identify a single group (and copy it into mask), so it will help you identify the members of a W group that neighbour a B move (for example), but it is only a small part of the total logic you need.
finally, note that if you want to do something with every stone in a group you have two options. you can call a routine like the one above, and then loop over mask to find the group, or you can put the action you want to do directly inside the routine (in which case you still use mask to control the extent of the flood fill in the test && mask[x][y] == ' ' but you don't use it as a result - all the work is done by the time the routine returns).
(programming something to handle go correctly, following all the rules, is actually quite complex - you've got a lot of work ahead... :o)
I'd use false proof for that. Here is how I find captured stones:
private static final int SIZE = 8;
private static final int VACANT = 0; //empty point
private static final int MY_COLOR = 1; //Black
private static final int ENEMY_COLOR = 2; //White
private static final int CHECKED = 50; //Mark for processed points
private static final int OUT = 100; //points out of the board
private static boolean isCaptured(int col, int row, int[][] board) {
boolean result = !isNotCaptured(col, row, board);
cleanBoard(board);
return result;
}
private static boolean isNotCaptured(int col, int row, int[][] board) {
int value = board[col][row];
if (!(value == MY_COLOR || value == CHECKED))
return true;
int top = row < SIZE - 1 ? board[col][row + 1] : OUT;
int bottom = row > 0 - 1 ? board[col][row - 1] : OUT;
int left = col > 0 ? board[col - 1][row] : OUT;
int right = col < SIZE - 1 ? board[col + 1][row] : OUT;
if (top == VACANT || right == VACANT || left == VACANT || bottom == VACANT)
return true;
board[col][row] = CHECKED;
return (top == MY_COLOR && isNotCaptured(col, row + 1, board))
|| (bottom == MY_COLOR && isNotCaptured(col, row - 1, board))
|| (left == MY_COLOR && isNotCaptured(col - 1, row, board))
|| (right == MY_COLOR && isNotCaptured(col + 1, row, board));
}
private static void cleanBoard(int[][] board) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == CHECKED)
board[i][j] = MY_COLOR;
}
}
}
Then you can call method like this:
isCaptured(5, 4, board)
I think that BFS will be better for this case because you need to explore the neighbors first, so that if any of them is captured then the point is captured.
As others pointed out, there is also a "ko rule" in Go which roughly means that you are not allowed to capture back immediately when a single stone is captured (simplified). In summary, you may want to use an existing library for this.
I recommend the brugo repository, which is available in maven.
<!-- https://mvnrepository.com/artifact/be.brugo/brugo -->
<dependency>
<groupId>be.brugo</groupId>
<artifactId>brugo</artifactId>
<version>0.1.0</version>
</dependency>
It roughly works like this.
(warning: code not tested)
// create a starting position
Position position = new Position(boardSize, komi);
// play a move
Intersection whereToPlay = Intersection.valueOf(4,4);
IntStatus colorToPlay = IntStatus.BLACK;
Position position2 = position.play(whereToPlay, colorToPlay);
// watch the result.
IntStatus[][] matrix = position2.getMatrix()
It also contains objects to export to Load/Save SGF. The loading of SGF files does not only support UTF-8 but also Asian encodings. Here is a screenshot that shows how difficult this is to implement yourself:
If you also plan to use javafx, then run this demo: brugo.go.ui.javafx.goban.GobanComponentDemo
Enough to get you started.
I have a for loop that traverses a String, and pulls out 2 characters each time and assigns them to a variable. The code segment I wrote is below:
//parse strings to integers
public int convertStringToInt(String stringOfDigits)
{
int number = 0;
int total = 0;
String subData = "";
for(int n = 0; n < stringOfDigits.length() ; n+=2) //For loop to pick out 2 digit sets of numbers from a string literal
{
subData = stringOfDigits.substring(n, n+2); //pulls a 2 digit substring and assigns it to a variable
number = Integer.parseInt(subData); //converts the substring to an integer
}
}
I need to somehow return the first two characters to a variable (var1 for example), the next two characters to another variable (var2) and so on. Then I need to return all of that so that I can bring it into my main module.
Any help would be very much appreciated!
Edit:
This is my entire program. The answers I was getting were pretty complex for what I think it needs to be. Maybe a simpler solution lies somewhere else. This program prints out a bar graph displaying how many users use each operating system (linux, mac, windows) that I know. The String contains those numbers, and I need to get those separate two digit pairs into the drawBar() method so that it draws the bar with that many users.
/**
* The purpose of this program is to draw a bar graph using turtle graphics
* and object oriented programming techniques based on information from a survey on
* Operating System use.
*
* #author Andrew Hauser (Shibumi)
* #version 1/16/12
*/
import java.awt.*; //imports the awt module
class Survey //Declares the Survey class
{
//Draws a line (or rectangle) from the first set of coordinates to the second set of coordinates.
public static void drawLine(Turtle myrtle, Color color, int penWidth, int x1, int y1, int x2, int y2)
{
myrtle.hide();
myrtle.penUp();
myrtle.setPenColor(color);
myrtle.setPenWidth(penWidth);
myrtle.moveTo(x1, y1);
myrtle.penDown();
myrtle.moveTo(x2, y2);
}//end of method
//Draws the box that the graph is contained in
public static void drawBox(Turtle myrtle, Color color)
{
myrtle.hide();
myrtle.penUp();
myrtle.setPenColor(color);
myrtle.setPenWidth(1);
myrtle.moveTo(100, 10);
myrtle.penDown();
myrtle.moveTo(400, 10);
myrtle.moveTo(400, 250);
myrtle.moveTo(100, 250);
myrtle.moveTo(100, 10);
myrtle.penUp();
}
//Draws the value lines on the outside of the box
public static void drawBoxLines(Turtle myrtle, Color color)
{
myrtle.hide();
myrtle.penUp();
myrtle.setPenWidth(1);
myrtle.moveTo(100, 250);
for(int nums = 0; nums <=10; nums++)
{
myrtle.turnLeft();
myrtle.penDown();
myrtle.forward(10);
myrtle.penUp();
myrtle.backward(10);
myrtle.turnRight();
myrtle.forward(20);
}
}
//Draws a bar for the graph
public static void drawBar(Turtle myrtle, Color color, int value, int x1, int y1)
{
myrtle.hide();
myrtle.penUp();
myrtle.setPenColor(color);
myrtle.setPenWidth(1);
myrtle.moveTo(x1, y1);
myrtle.penDown();
myrtle.forward(value * 10);
myrtle.turnRight();
myrtle.forward(10);
myrtle.turnRight();
myrtle.forward(value * 10);
myrtle.penUp();
myrtle.turnLeft();
myrtle.turnLeft();
}
//parse strings to integers
public int convertStringToInt(String stringOfDigits)
{
int number = 0;
int total = 0;
String subData = "";
for(int n = 0; n < stringOfDigits.length() ; n+=2) //For loop to pick out 2 digit sets of numbers from a string literal
{
subData = stringOfDigits.substring(n, n+2); //pulls a 2 digit substring and assigns it to a variable
number = Integer.parseInt(subData); //converts the substring to an integer
}
}
}
public class SurveyTester //Declares the SurveyTester class
{
//Executes the code.
public static void main(String[] args) //Start of main method
{
World worldObj = new World(); //Makes a new world
Turtle myrtle = new Turtle(0, 0, worldObj); //Makes a new Turtle object
Picture pictureObj = new Picture("Graph_background.png");
worldObj.setPicture(pictureObj);
Survey survey = new Survey(); //Makes a new Survey object
stringData = "031016";
survey.drawBox(myrtle, Color.BLACK);
survey.drawBoxLines(myrtle, Color.BLACK); //Draws lines by twos
survey.drawBar(myrtle, Color.RED, 3, 150, 250); //Bar for linux
survey.drawBar(myrtle, Color.RED, 10, 240, 250); //Bar for Mac
survey.drawBar(myrtle, Color.RED, 16, 330, 250); //Bar for windows
}
}
Help would be super appreciated!
PS. If you want to actually run this to see what it looks like, you'll need the bookClasses Java library, which you can get from my dropbox here: http://db.tt/H8zmyA75
This is why arrays were invented. Put each pair of characters into a
char[][] pairs;
Or perhaps better an
ArrayList<char[]> pairs;
if you can't predict how many in advance.
import java . util . * ;
//parse strings to integers
public int convertStringToInt(String stringOfDigits)
{
int number = 0;
int total = 0;
String subData = "";
List<Integer> numbers = new ArrayList<>();
for(int n = 0; n < stringOfDigits.length() ; n+=2) //For loop to pick out 2 digit sets of numbers from a string literal
{
subData = stringOfDigits.substring(n, n+2); //pulls a 2 digit substring and assigns it to a variable
numbers . add ( Integer.parseInt(subData) ) ; //converts the substring to an integer
}
}
From what I understood from your question you want to create multiple variables based on the size of the string. And that Each two characters in your string represent one Integer.
An Array could be described as a list of variables.
So if you have a 10 character string , those 10 characters represent 5 integers.
So you will need an array of length 5 (the number of variables in the array or 'cells')
To create an array of integers :
int[] arrayName = new int[SIZE];
you will also return the array and handle that as a 'list' of variables
For Example :
public static int [] convertStringToInteger(String toConvert) {
int [] toReturn = new int[toConvert.length()/2];
//Assuming passed strings are of even length
for (int i = 0, j = 0; i < toConvert.length() || j < toReturn.length; i +=2 , j++) {
String temp = toConvert.charAt(i) + "" + toConvert.charAt(i+1);
toReturn [j] = Integer.parseInt(temp);
}
return toReturn;
}
Arrays are very easy to use and understand you can check Arrays in the Java tutorials by oracle here
Hope this Helps.