...

View Full Version : Trying to print my board methods to output.



edd21
03-16-2012, 06:08 PM
I am trying to print the board to output after each change. The first change sets the entire board to one symbol. The second change sets the board to alternating symbols. I need the printBoard() method to only print the board, not modify it.

Any help would be greatly appreciated.





import java.util.Scanner;

public class Board
{
static int i; //variable to set rows and column equal to the same
static String sym; //symbol used to set entire array to same symbol
static String sym1; //symbol used with sym2 to alternate and create checkboard
static String sym2; //symbol used with sym1 to create checkerboard
static String sym3; //used in the insertSymbol method
static int r; //used in insertSymbol method. Represents row
static int c; //used in the insertSymbol method. represents column.
static Scanner in; //scanner variable called in
static String [][] Board; //declaring a string array called Board

public void setSize() //method to set the size of the array
{
System.out.println("Please enter a whole number to represent" +
"the length and width of your board.");
i=in.nextInt();
Board= new String[i][i];
}

public void placeSymbol() //method to place a symbol in every cell of the array
{
System.out.println("Please enter any character and" +
"this character will fill the board.");
sym = in.next();
for(int column = 0; column <i; column++)
for(int row = 0; row<i; row++)
Board[row][column]=sym;
}

public void placeSymbols() //method to place two symbols alternating in the array
{
int test;
System.out.println("Please enter a Character.");
sym1=in.next();
System.out.println("Please enter another character.");
sym2=in.next();
for (int row = 0; row < i; row++)
{
for (int col = 0; col < i; col++)
{

test = row + col;
if (test % 2 == 0)
{
Board[row][col] = sym1; // add character at even array position
}
else
{
Board[row][col] = sym2; // add character at odd array position
}
}
}
}
public void insertSymbol() //method to insert a specific symbol into a specific address of the array
{
System.out.println("Please enter the row number you"+
"want to insert at.");
r=in.nextInt();
System.out.println("Please enter the column number"+
"you want to insert at.");
c=in.nextInt();
System.out.println("Please enter the character you want"+
"inserted at that address.");
sym3=in.next();
Board[r][c]=sym3;
}

public void printBoard()
{
//Board someBoard=new Board();
/*String output = "";
for (int row = 0; row < i; row++) {
for (int col = 0; col < i; col++) {
output += sym;
}
output += "\n";
}*/
//someBoard.Board();
System.out.println(Board);

}

public static void main(String[] args)
{
//int action; //variable action used in switch staement
in = new Scanner(System.in);
boolean done = false;
Board someBoard=new Board();

System.out.println("Follow the instructions \n");

while (done!=true)
{
someBoard.printBoard();
someBoard.setSize();
someBoard.placeSymbol();
someBoard.printBoard();
someBoard.placeSymbols();
someBoard.insertSymbol();
someBoard.printBoard();
done = true;

}


}
}

Fou-Lu
03-16-2012, 10:59 PM
According to this, the printboard() method doesn't do anything other than print the pointer address for the primitive array. Everything else has been commented out, and what is there won't modify the array itself, but won't show what's in it either.
So I'm not quite sure what your question here is? The loops in that method are the right idea, but you need to draw from the board[row][col], not from just the sym.

edd21
03-17-2012, 12:24 AM
Here is my improved code. However I do currently have one issue. When I print my alternating symbols method, its prints the board twice. I am not sure why. Earlier I attempted to put that portion of the method in a while loop using a count to try and end it but that did not work out for me. Any more help would be greatly appreciated.

Thanks.




import java.util.Scanner;

public class Board
{
static int i; //variable to set rows and column equal to the same
static String sym; //symbol used to set entire array to same symbol
static String sym1; //symbol used with sym2 to alternate and create checkboard
static String sym2; //symbol used with sym1 to create checkerboard
static String sym3; //used in the insertSymbol method
static int r; //used in insertSymbol method. Represents row
static int c; //used in the insertSymbol method. represents column.
static Scanner in; //scanner variable called in
static String [][] BoardArray; //declaring a string array called BoardArray
static int test;
static int boardCount = 0;

public void setSize() //method to set the size of the array
{
System.out.println("Please enter a whole number to represent" +
" the length and width of your board.");
i=in.nextInt();
BoardArray= new String[i][i];
}

public void placeSymbol() //method to place a symbol in every cell of the array
{

System.out.println("Please enter any character and" +
" this character will fill the board.");
sym = in.next();
for(int column = 0; column <i; column++)
for(int row = 0; row<i; row++)
BoardArray[row][column]=sym;
}

public void alternateSymbols() //method to place two symbols alternating in the array
{
System.out.println("Please enter a Character.");
sym1=in.next();
System.out.println("Please enter another character.");
sym2=in.next();

for (int row = 0; row < i; row++)
{
for (int col = 0; col < i; col++)
{
test = row + col; // adds each position...
if (test % 2 == 0)
{
BoardArray[row][col] = sym1; // add character at even array position
}
else
{
BoardArray[row][col] = sym2; // add character at odd array position
}
System.out.print(" " + BoardArray[row][col]); // Prints my alternate output
}
System.out.println(" ");
}
}
public void insertSymbol() //method to insert a specific symbol into a specific address of the array
{
System.out.println("Please enter the row number you"+
" want to insert at.");
r=in.nextInt();
r--;
System.out.println("Please enter the column number"+
" you want to insert at.");
c=in.nextInt();
c--;
System.out.println("Please enter the character you want"+
" inserted at that address.");
sym3=in.next();
BoardArray[r][c]=sym3;


}

public void printBoard()
{
for(int i = 0; i < BoardArray.length; i++)
{
for(int j = 0; j < BoardArray.length; j++)
{
System.out.print(" " + BoardArray[i][j]);
}
System.out.println();
}
}




public static void main(String[] args)
{
in = new Scanner(System.in);
boolean done = false;
Board someBoard=new Board();

System.out.println("Follow the instructions \n");

while (done!=true)
{

someBoard.setSize(); // Sets size of board.
someBoard.placeSymbol(); // Sets character of board.
someBoard.printBoard(); // Prints single character board.

someBoard.alternateSymbols(); // Places alternating symbols on board.

someBoard.printBoard(); // Prints alternating symbol board.
someBoard.insertSymbol(); // Gets symbol, row, and col to insert into board.
someBoard.printBoard(); // Prints new selected symbol board.
done = true;
}
}
}

Fou-Lu
03-17-2012, 01:12 AM
Alternate symbols method also prints the board manually. Remove those prints.
That method is better, but your inner loop should check for BoardArray[i].length.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum