Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Real Brainer question, looking for a simpler answer.

Status
Not open for further replies.

m3the01

Technical User
Feb 21, 2002
41
CA
Hello just working on a little game, with the setup

1 2 3
4 5 6
7 8 9


Each of the numbers are actually colored squares in the programs with string literals in the for of numbers on them as listed. Now what what i am trying to do is find a formula which i could use in an if/ else statement which would validate moves correctly. Not looking for the obvious solution already do a long if/else statement. Looking more for a math formula. Best i can figure is that we could break them into 3 columns, each number has a difference of 3 vertically in the first/second/third columns, and difference of 1 horizontally. Can anyone think of a formula that i could use, which would only allow moves in horizontal or vertical, crossing moves are illegal. For instancs square 5 can only move too 2,4,6,8.

Any help would be great, already done a long if/else statement. Notlooking for that type of answer. If/else will still be used in the validation of moves just not 20 or so lines of it.


thanks again
 
sorry just read over my thread, typing real bad today
 
Can you assign a row/column to each position (so the upper left would be 1,1 and the lower right be 3,3), then you could use something like

validMove = (Math.abs(thisRow - nextRow) + Math.abs(thisCol - next Col) = 1)

This is assuming that you can't click on a cell that moves you off the board.
 
would really know how to do that, but thanks for the input.


anyone else?

 
This is based on some mathematical information regarding your problem, but it is still in the form of an if-else, but it is fairly short.

x is your current position, and y is the move in question

Code:
if (x%2 == y%2)
   System.out.println("illegal move  " + x + " to " + y);
else if (((x-1)%3 == 0 && y == (x-1)) ||
          (x%3 == 0 && y == (x+1)))
   System.out.println("illegal move  " + x + " to " + y);
else if (y < (x-3) || y > (x+3))
   System.out.println(&quot;illegal move  &quot; + x + &quot; to &quot; + y);
else
   System.out.println(&quot;LEGAL MOVE  &quot; + x + &quot; to &quot; + y);
 
import java.util.*;
import java.io.*;
import java.awt.*;
import java.applet.Applet;

public class Board extends Applet {

// Constants
private static final Color emptySquareColor = Color.white;
private static final int defaultEmpty = 5;
private static final int boardSize = 3;
private static final String int1 = &quot;1&quot;;
private static final String int2 = &quot;2&quot;;
private static final String int3 = &quot;3&quot;;
private static final String int4 = &quot;4&quot;;
private static final String int5 = &quot;5&quot;;
private static final String int6 = &quot;6&quot;;
private static final String int7 = &quot;7&quot;;
private static final String int8 = &quot;8&quot;;
private static final String int9 = &quot;9&quot;;
private static final int intNumber2 = 2;
private static final int intNumber4 = 4;
// instance variables
private Panel row1, row2, row3, boardPanel;
private Button square1, square2, square3, square4, square5;
private Button square6, square7, square8, square9;
private Color topRowColor, middleRowColor, bottomRowColor;
private int emptySquare;


// Constructor
public Board(Color top, Color middle, Color bottom) {

this.topRowColor = top;
this.middleRowColor = middle;
this.bottomRowColor = bottom;







}

// Instance methods
private Panel makeRowPanel( LayoutManager lm) {
//Create a Panel. Set the LayoutManager to FlowLayout (centered). Return a reference to the Panel.

Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
return p;





}

private Button makeSquare(String squareNumber) {
// Create and return a button with the given String as its Label

Button buttons = new Button(squareNumber);
buttons.setFont(new Font(&quot;Courier&quot;,Font.BOLD,15));
return buttons;






}

public void paintSquares( ) {
// Set the colours of the squares according to their row colours and set variable emptySquare to default

this.square1.setBackground(this.topRowColor);
this.square2.setBackground(this.topRowColor);
this.square3.setBackground(this.topRowColor);
this.square4.setBackground(this.middleRowColor);
this.square5.setBackground(this.emptySquareColor);
this.square6.setBackground(this.middleRowColor);
this.square7.setBackground(this.bottomRowColor);
this.square8.setBackground(this.bottomRowColor);
this.square9.setBackground(this.bottomRowColor);

}

private void fillRows( ) {
// Create the squares and their labels, add them to the appropriate rows 1, 2 or 3

this.row1 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,Board.intNumber4,Board.intNumber2));
this.row1.add(this.square1 = makeSquare(Board.int1));
this.row1.add(this.square2 = makeSquare(Board.int2));
this.row1.add(this.square3 = makeSquare(Board.int3));

this.row2 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,Board.intNumber4,Board.intNumber2));
this.row2.add(this.square4 = makeSquare(Board.int4));
this.row2.add(this.square5 = makeSquare(Board.int5));
this.row2.add(this.square6 = makeSquare(Board.int6));

this.row3 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,Board.intNumber4,Board.intNumber2));
this.row3.add(this.square7 = makeSquare(Board.int7));
this.row3.add(this.square8 = makeSquare(Board.int8));
this.row3.add(this.square9 = makeSquare(Board.int9));

paintSquares();




}

private void assembleRows( ) {
// Arranges the rows into a single Panel (using GridLayout)

fillRows();
this.boardPanel = new Panel(new GridLayout(3,1));
this.boardPanel.add(this.row1);
this.boardPanel.add(this.row2);
this.boardPanel.add(this.row3);


}

public Button getSquare(String number) {
// Returns the square (i.e. Button) with the given label

Button getButton = new Button(number);
return getButton;



}

public void changeRowColor(int rowNumber, Color newColor) {
// Change the current colour of the row with the given number 1, 2, 3....

if(rowNumber == 1){
this.square1.setBackground(newColor);
this.square2.setBackground(newColor);
this.square3.setBackground(newColor);
}
if(rowNumber == 2){
this.square4.setBackground(newColor);
this.square5.setBackground(emptySquareColor);
this.square6.setBackground(newColor);

}
if(rowNumber == 3){
this.square7.setBackground(newColor);
this.square8.setBackground(newColor);
this.square9.setBackground(newColor);

}



}

private void changeSquares(Button coloredSquare, Button emptySquare) {
// Interchanges the colours of the given squares

Color storage = coloredSquare.getBackground();
coloredSquare.setBackground(emptySquare.getBackground());
emptySquare.setBackground(storage);








}

public void move(String squareLabel) {
// If the move is valid, move the square with the given label into the empty space

int intString = Integer.parseInt(squareLabel.trim());




THIS IS WHERE I WOULD NEED TO PUT IT PLEASE AN IDEA WHERE TO START WOULD BE GREAT



}

public Panel getBoard( ){
// Return a reference to the Panel into which all 3 rows have been assembled.

assembleRows();
return this.boardPanel;



}

} // end of Board class
 
Here is the actual code which i will be using, the move() method is where i need to carry ou this task. An idea where to start would be great. Havent used the % either, does it mean 2/3 remainder 2 or 3/2 remainder 1. Not really sure, the output is not illegal move but know move will take place. Thanks for the help.
When a legal move is made the square switch color.
thans again




import java.util.*;
import java.io.*;
import java.awt.*;
import java.applet.Applet;

public class Board extends Applet {

// Constants
private static final Color emptySquareColor = Color.white;
private static final int defaultEmpty = 5;
private static final int boardSize = 3;
private static final String int1 = &quot;1&quot;;
private static final String int2 = &quot;2&quot;;
private static final String int3 = &quot;3&quot;;
private static final String int4 = &quot;4&quot;;
private static final String int5 = &quot;5&quot;;
private static final String int6 = &quot;6&quot;;
private static final String int7 = &quot;7&quot;;
private static final String int8 = &quot;8&quot;;
private static final String int9 = &quot;9&quot;;
private static final int intNumber2 = 2;
private static final int intNumber4 = 4;
// instance variables
private Panel row1, row2, row3, boardPanel;
private Button square1, square2, square3, square4, square5;
private Button square6, square7, square8, square9;
private Color topRowColor, middleRowColor, bottomRowColor;
private int emptySquare;


// Constructor
public Board(Color top, Color middle, Color bottom) {

this.topRowColor = top;
this.middleRowColor = middle;
this.bottomRowColor = bottom;







}

// Instance methods
private Panel makeRowPanel( LayoutManager lm) {
//Create a Panel. Set the LayoutManager to FlowLayout (centered). Return a reference to the Panel.

Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
return p;





}

private Button makeSquare(String squareNumber) {
// Create and return a button with the given String as its Label

Button buttons = new Button(squareNumber);
buttons.setFont(new Font(&quot;Courier&quot;,Font.BOLD,15));
return buttons;






}

public void paintSquares( ) {
// Set the colours of the squares according to their row colours and set variable emptySquare to default

this.square1.setBackground(this.topRowColor);
this.square2.setBackground(this.topRowColor);
this.square3.setBackground(this.topRowColor);
this.square4.setBackground(this.middleRowColor);
this.square5.setBackground(this.emptySquareColor);
this.square6.setBackground(this.middleRowColor);
this.square7.setBackground(this.bottomRowColor);
this.square8.setBackground(this.bottomRowColor);
this.square9.setBackground(this.bottomRowColor);

}

private void fillRows( ) {
// Create the squares and their labels, add them to the appropriate rows 1, 2 or 3

this.row1 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,Board.intNumber4,Board.intNumber2));
this.row1.add(this.square1 = makeSquare(Board.int1));
this.row1.add(this.square2 = makeSquare(Board.int2));
this.row1.add(this.square3 = makeSquare(Board.int3));

this.row2 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,Board.intNumber4,Board.intNumber2));
this.row2.add(this.square4 = makeSquare(Board.int4));
this.row2.add(this.square5 = makeSquare(Board.int5));
this.row2.add(this.square6 = makeSquare(Board.int6));

this.row3 = makeRowPanel(new FlowLayout(FlowLayout.LEFT,Board.intNumber4,Board.intNumber2));
this.row3.add(this.square7 = makeSquare(Board.int7));
this.row3.add(this.square8 = makeSquare(Board.int8));
this.row3.add(this.square9 = makeSquare(Board.int9));

paintSquares();




}

private void assembleRows( ) {
// Arranges the rows into a single Panel (using GridLayout)

fillRows();
this.boardPanel = new Panel(new GridLayout(3,1));
this.boardPanel.add(this.row1);
this.boardPanel.add(this.row2);
this.boardPanel.add(this.row3);


}

public Button getSquare(String number) {
// Returns the square (i.e. Button) with the given label

Button getButton = new Button(number);
return getButton;



}

public void changeRowColor(int rowNumber, Color newColor) {
// Change the current colour of the row with the given number 1, 2, 3....

if(rowNumber == 1){
this.square1.setBackground(newColor);
this.square2.setBackground(newColor);
this.square3.setBackground(newColor);
}
if(rowNumber == 2){
this.square4.setBackground(newColor);
this.square5.setBackground(emptySquareColor);
this.square6.setBackground(newColor);

}
if(rowNumber == 3){
this.square7.setBackground(newColor);
this.square8.setBackground(newColor);
this.square9.setBackground(newColor);

}



}

private void changeSquares(Button coloredSquare, Button emptySquare) {
// Interchanges the colours of the given squares

Color storage = coloredSquare.getBackground();
coloredSquare.setBackground(emptySquare.getBackground());
emptySquare.setBackground(storage);








}

public void move(String squareLabel) {
// If the move is valid, move the square with the given label into the empty space

int intString = Integer.parseInt(squareLabel.trim());




THIS IS WHERE I WOULD NEED TO PUT IT PLEASE AN IDEA WHERE TO START WOULD BE GREAT



}

public Panel getBoard( ){
// Return a reference to the Panel into which all 3 rows have been assembled.

assembleRows();
return this.boardPanel;



}

} // end of Board class
 
dont worry about it i think i got it, thanks again for all your help/



bye for now
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top