i tried using this where ros and cols is an integer which is equals to 8

if (i == ros || j == cols) {
										
if (i == ros && j > cols) {
											cols++;
										
} else if (i == ros && j < cols) {
											cols--;
										
} else if (j == cols && i > ros) {
											ros++;
										
} else if (j == cols && i < ros) {
											ros--;
										
}

									
										
int k = secret.nextInt();
										
Tile boardtile = playersarray[currentPlayer].gettile(k);								
										
b.add(i, j, boardtile);
										System.out.println("");
										b.Print_Board();
										System.out.println("");
										playersarray[currentPlayer].add_Tile(boardtile);
																						playersarray[currentPlayer].gettiles().remove(k);

Dani AI

Generated

As hinted, clearer names and a small, dedicated validator will make this reliable. Treat each player's placement as a move that (1) sets an anchor, (2) determines orientation once a second tile is placed, and (3) is validated for straightness, contiguity (no gaps), and adjacency to existing tiles. Do not mutate the anchor coordinates while checking — compute differences (dr, dc) and derive orientation from them.

Algorithm (plain):

  • Keep the board as a 2D array and collect the coordinates of tiles placed this move (use row/col, not i/j).
  • If move size == 1, allow only when that single tile touches an existing tile (or center rule for first move).
  • If move size > 1, require all tiles share the same row or the same column. The second tile fixes orientation.
  • Ensure contiguity: find min and max along the moving axis and check every cell between is either an already-placed board tile or included in the current move (no empty gaps).
  • Require at least one of the new tiles to be adjacent to an existing board tile (to attach to words already on board).

Example Java-style validator (minimal core):

enum Orientation { UNSET, HORIZONTAL, VERTICAL }

static class Pos { final int r,c; Pos(int r,int c){this.r=r;this.c=c;}
  public boolean equals(Object o){ return (o instanceof Pos)&&((Pos)o).r==r&&((Pos)o).c==c; }
  public int hashCode(){ return r*31 + c; }
}

boolean validateMove(Tile[][] board, Set<Pos> move) { ... } // implement steps above:
  // determine orientation from two tiles,
  // check same row/col, check contiguity across min..max,
  // ensure adjacency to existing tiles.

Troubleshooting notes: rename i/j to row/col and ros/cols to anchorRow/anchorCol; avoid changing anchor during validation; only remove a tile from the player's rack after the move passes validation; test edge cases (single-tile attachment, placing between two existing tiles, board edges). This approach yields predictable Scrabble-like placement rules and will be easier to debug than incrementing ros/cols inside the validation logic.

Recommended Answers

All 2 Replies

Member Avatar for Member #901355

hm, I for one can not understand you question on this.
For 1: You have bad variable names.
2: well, maybe 1 is enough

basically i in this case is next row and j is next column
k is the index of letter i want to place on the board..
on first occasion my code places the letter on 8th row and 8th column of the board (middle basically)
on second occasion my code asks user where he wants to place the letter on the board thts where i and j comes in.
so basically i want my code to do as exactly as a Scrabble game online where you can only place letters next to letters you already have placed on the board either horizontally or vertically..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.