draw multiple lines with canvas - java

public static void main(String[] args) {
Picture myPic = new Picture(600, 600);
Graphics canvas = myPic.getOffScreenGraphics();
canvas.setColor(Color.WHITE);
canvas.fillRect(0, 0, 600, 600);
canvas.setColor(Color.BLACK);
int x1;
int y1;
int x2;
int y2;
int heading;
x1 = 300;
y1 = 300;
x2 = 300;
y2 = 300;
heading = 0;
String keyboard = JOptionPane.showInputDialog(null,
"Enter your input");
if (keyboard != null) {
if (keyboard.isEmpty() || keyboard.contains(" ")) {
JOptionPane.showMessageDialog(
null,
"Please enter an input",
"Error",
JOptionPane.ERROR_MESSAGE);
main(args);
}
int counth = 0;
int countg = 0;
int countPlus = 0;
int countMinus = 0;
int countK = 0;
int countR = 0;
int countG = 0;
int countB = 0;
int countC = 0;
int countO = 0;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == '+') {
if (heading == 0) {
heading = 1;
} else if (heading == 1) {
heading = 2;
} else if (heading == 2) {
heading = 3;
} else if (heading == 3) {
heading = 0;
}
}
}
countPlus++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == '-') {
if (heading == 0) {
heading = 3;
} else if (heading == 1) {
heading = 0;
} else if (heading == 2) {
heading = 1;
} else if (heading == 3) {
heading = 2;
}
}
}
countMinus++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'h' || keyboard.charAt(i) == 'f') {
if (heading == 0) {
counth++;
int k = 10 * (counth);
canvas.drawLine(x1, y1, x2, y2 + k);
} else if (heading == 1) {
counth++;
int k = 10 * (counth);
canvas.drawLine(x1, y1, x2 + k, y2);
} else if (heading == 2) {
counth++;
int k = 10 * (counth);
canvas.drawLine(x1, y1, x2, y2 - k);
} else if (heading == 3) {
counth++;
int k = 10 * (counth);
canvas.drawLine(x1, y1, x2 - k, y2);
} else if (heading > 3 || heading < 0) {
JOptionPane.showMessageDialog(
null,
"Your heading is greater than 3 or less than 0",
"Error",
JOptionPane.ERROR_MESSAGE);
main(args);
}
}
}
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'K') {
canvas.setColor(Color.BLACK);
}
}
countK++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'R') {
canvas.setColor(Color.RED);
}
}
countR++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'G') {
canvas.setColor(Color.GREEN);
}
}
countG++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'B') {
canvas.setColor(Color.BLUE);
}
}
countB++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'C') {
canvas.setColor(Color.CYAN);
}
}
countC++;
for (int i = 0; i < keyboard.length(); i++) {
if (keyboard.charAt(i) == 'O') {
canvas.setColor(Color.BLACK);
}
}
countO++;
System.out.println("h = " + counth);
System.out.println("g = " + countg);
System.out.println("+ = " + countPlus);
System.out.println("- = " + countMinus);
System.out.println("K = " + countK);
System.out.println("R = " + countR);
System.out.println("G = " + countG);
System.out.println("B = " + countB);
System.out.println("C = " + countC);
System.out.println("O = " + countO);
myPic.repaint();
} else {
System.exit(0);
}
}
I'm trying to draw some lines based on user input.
The inputs are h and f for moving forward 10 pixels (both do the same thing) and + or - to change the direction the line will move 90 degrees either clockwise for + or counterclockwise for - (default is moving up).
For example, if a user inputs: "hhh" the line will move up 30 pixels.
The problem I am having is I want the user to be able to input hhh+hhh and it draw a line 30 pixels upwards then turns and draws 30 more pixels but to the right. However in my program when I input hhh+hhh it just makes one straight line 60 pixels to the right.
So my question is: how can I get it to draw a line turn when it sees either + or - in the specified direction and draw another line.

Code completely untested but I suggest you:
Delete the two loops for + and -.
for(int i =0; i < keyboard.length(); i++)
{
if(keyboard.charAt(i) == '+')
heading++;
if(keyboard.charAt(i) == '-')
heading--;
if (heading < 0)
heading = 3;
if (heading > 3)
heading = 0;
if(keyboard.charAt(i) == 'h' || keyboard.charAt(i) == 'f')
//Snip rest of heading code here
}
The issue you are having is that you are looping through the code multiple times independent of processing the line drawing code.
While this may not resolve your issue it may get you on the right track.

Related

How to express a player on a board in a connect four game

The numbers entered in selectline are well implemented up to the second line of the board.
But it doesn't work from the third line.
What should I do?
The following is part of my code
As shown in the picture below, it should be printed on the third line, the fourth line...
I am printed only until the second line.
enter image description here
public class Practice1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Board board = new Board(13, 6);
SelectLine selLine = new SelectLine();
for (int i = 0; i < 10; i++) {
selLine.input();
board.put(selLine);
}
}
}
public class SelectLine {
int line;
public void input() {
System.out.print("Select Line : ");
Scanner sc = new Scanner(System.in);
line = sc.nextInt();
}
public int getY() {
return line;
}
}
public class Board {
char[][] string;
char[][] om;
int width;
int height;
int count;
public Board(int width, int height) {
count = 0;
string = new char[height][width];
this.width = width;
this.height = height;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == 0 && j == 0) {
string[i][j] = '┌';
} else if (j % 2 == 1) {
string[i][j] = '─';
} else if (i == 0 && j == width - 1) {
string[i][j] = '┐';
} else if (i == height - 1 && j == 0) {
string[i][j] = '└';
} else if (i == height - 1 && j == width - 1) {
string[i][j] = '┘';
} else if (i == 0) {
string[i][j] = '┬';
} else if (j == 0) {
string[i][j] = '├';
} else if (i == height - 1) {
string[i][j] = '┴';
} else if (j == width - 1) {
string[i][j] = '┤';
} else
string[i][j] = '┼';
}
}
}
public void print() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++)
System.out.print(string[i][j]);
System.out.println();
}
}
public void put(SelectLine setLine) {
int y = setLine.getY();
int x = 6;
if (count % 2 == 0) {
if (string[x - 1][(y - 1) * 2] == '●' || string[x - 1][(y - 1) * 2] == '○') {
x -= 1;
string[x - 1][(y - 1) * 2] = '●';
count++;
} else {
string[x - 1][(y - 1) * 2] = '●';
count++;
}
} else {
if (string[x - 1][(y - 1) * 2] == '●' || string[x - 1][(y - 1) * 2] == '○') {
x -= 1;
string[x - 1][(y - 1) * 2] = '○';
count++;
} else {
string[x - 1][(y - 1) * 2] = '○';
count++;
}
}
this.print();
}
}
}
This isn't going to be the answer you're looking for, but hopefully will point you in the right direction.
// At this point Y = the selected column
// and X = 6 (presumably the end/bottom)
if (count % 2 == 0) {
if (string[x - 1][(y - 1) * 2] == '●' || string[x - 1][(y - 1) * 2] == '○') {
x -= 1; // Right here X is now 5
string[x - 1][(y - 1) * 2] = '●'; // and now you're setting string[4][]
count++;
} else {
string[x - 1][(y - 1) * 2] = '●'; // and now you're setting string [5][]
count++;
}
}
You see how you're never going to be able to set `string[0..3] which are the top 4 lines? What would happen if you did something like this:
// Only determine the piece in one spot.
// Because after this point it doesn't matter what colour it is
// you're just going to find the next open spot from the bottom up
char piece = (count %2 == 0) ? 'r' : 'w';
// Now check all your lines from the bottom
for (int x = 6; x < 0; x--) {
// does !string [x-1][y calc] contain 'r' or 'w'
// string [x-1][y calc] = piece;
// break;
}
// print/wins/etc.
Starting at the bottom, you work your way up until the spot doesn't contain r or w and then place it there.

How do I combine an Output of two for loops into a "table" format?

I used two for loops to convert miles to km and km to miles respectively for selected values. However the issue I am facing is that the output for the first for loop is not side by side with the output of the second table. Appreciate some help on this!
public static double miletoKilometer(double mile) {
double conversion = mile * 1.609;
return conversion;
}
public static double kilometerToMile(double km) {
double conversion2 = km / 1.609;
return conversion2;
}
public static void main(String[] args) {
int mileInput = 0;
double kmOutput = 0;
int kmInput = 0;
double mileOutput = 0;
int displayRow1 = 0;
int displayRow2 = 0;
System.out.print("Miles\tKilometres\tKilometres\tMiles \n");
for (int i = 0; i < 11; i++) {
if (i == 1 || i == 2 || i == 9 || i == 10) {
mileInput = i;
System.out.printf("\n" + i);
kmOutput = miletoKilometer(mileInput);
System.out.printf("\t %.3f\n", kmOutput);
}
}
for (int j = 0; j < 66; j++) {
if (j == 20 || j == 25 || j == 60 || j == 65) {
kmInput = j;
System.out.printf("\n\t\t " + j);
mileOutput = kilometerToMile(kmInput);
System.out.printf("\t\t%.3f", mileOutput);
}
}
}
Current Output:
enter image description here
Changing your loop to match the code below should put everything in the right order. Not the most elegant solution but it gets the job done.
int j = 0;
for (int i = 0; i < 11; i++) {
if (i == 1 || i == 2 || i == 9 || i == 10) {
mileInput = i;
System.out.printf("\n" + i);
kmOutput = miletoKilometer(mileInput);
System.out.printf("\t %.3f", kmOutput);
for (; j < 66; j++) {
if (j == 20 || j == 25 || j == 60 || j == 65) {
kmInput = j;
System.out.printf("\t\t " + j);
mileOutput = kilometerToMile(kmInput);
System.out.printf("\t\t%.3f\n", mileOutput);
j++;
break;
}
}
}
}

Stack Overflow Error in a Self Calling Function in Java (Number of Islands)

I did some research on what causes a stack overflow errors, and I can conclude it is being caused by a recursive function in a program that is supposed to "count the number of islands" in an array. I understand what is causing the issue, but not sure why this is happening, or my main question is what to actually do about it. I found that if I slow down the program by having it repeatedly printing out something to the console, it works, but it takes forever to complete. Is there a way I can keep the program speed without the error, or a better way to solve the problem (search up "number of islands" to find the problem). Also, the array is two dimensional with a size of 1050 by 800.
public class NumOfIslands {
static boolean[][] dotMap = new boolean[1050][800];
static boolean visited[][] = new boolean[1050][800];
static int total = 0;
public static void main(String args[]) {
defineArrays();
run();
}
public static void findObjects(int xCord, int yCord) {
for(int y = yCord - 1; y <= yCord + 1; y++) {
for(int x = xCord - 1; x <= xCord + 1; x++) {
if(x > -1 && y > -1 && x < dotMap[0].length && y < dotMap.length) {
if((x != xCord || y != yCord) && dotMap[x][y] == true && visited[x][y] != true) {
visited[x][y] = true;
findObjects(x,y);
//System.out.println("test");
}
}
}
}
}
public static void defineArrays() {
for(int y = 0; y < 800; y++) {
for(int x = 0; x < 1050; x++) {
dotMap[x][y] = true;
}
}
}
public static int run() {
//dotMap = DisplayImage.isYellow;
System.out.println(dotMap.length + " " + dotMap[0].length);
int objects = 0;
for(int y = 439; y < 560/*dotMap[0].length*/; y++) {
for(int x = 70; x < 300/*dotMap.length*/; x++) {
if(dotMap[x][y] == true && visited[x][y] != true) {
visited[x][y] = true;
objects++;
findObjects(x,y);
}
}
}
System.out.println("total" + total);
System.out.println(objects);
return objects;
}
}
StackOverflowError reasons. In your example each call to findObjects adds 2 variables to the stack int x and int y from loops.
One of the fastest solution:
class Solution {
int m, n;
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
m = grid.length;
n = grid[0].length;
int counter = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
visit(grid, i, j);
counter++;
}
}
}
return counter;
}
public void visit(char[][] grid, int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n) {
return;
}
if (grid[i][j] == '0') {
return;
}
grid[i][j] = '0';
visit(grid, i - 1, j);
visit(grid, i + 1, j);
visit(grid, i, j - 1);
visit(grid, i, j + 1);
}
}
All recursive algorithms can be implemented with loops. One of the example is below. The Solution implements BFS (Breadth-first search) algorithm, more details on wikipedia.
class Solution {
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int nr = grid.length;
int nc = grid[0].length;
int num_islands = 0;
for (int r = 0; r < nr; ++r) {
for (int c = 0; c < nc; ++c) {
if (grid[r][c] == '1') {
++num_islands;
grid[r][c] = '0'; // mark as visited
Queue<Integer> neighbors = new LinkedList<>();
neighbors.add(r * nc + c);
while (!neighbors.isEmpty()) {
int id = neighbors.remove();
int row = id / nc;
int col = id % nc;
if (row - 1 >= 0 && grid[row-1][col] == '1') {
neighbors.add((row-1) * nc + col);
grid[row-1][col] = '0';
}
if (row + 1 < nr && grid[row+1][col] == '1') {
neighbors.add((row+1) * nc + col);
grid[row+1][col] = '0';
}
if (col - 1 >= 0 && grid[row][col-1] == '1') {
neighbors.add(row * nc + col-1);
grid[row][col-1] = '0';
}
if (col + 1 < nc && grid[row][col+1] == '1') {
neighbors.add(row * nc + col+1);
grid[row][col+1] = '0';
}
}
}
}
}
return num_islands;
}
}
the problem is in this function
public static void findObjects(int xCord, int yCord) {
for(int y = yCord - 1; y <= yCord + 1; y++) {
for(int x = xCord - 1; x <= xCord + 1; x++) {
if(x > -1 && y > -1 && x < dotMap[0].length && y < dotMap.length) {
if((x != xCord || y != yCord) && dotMap[x][y] == true && visited[x][y] != true) {
visited[x][y] = true;
findObjects(x,y);
//System.out.println("test");
}
}
}
}
}`
at here you are builiding a stack of recursive calls to findobjects and ultimately it has no termination condition so it ends up at infinite stacks of findobjects, so my solution is if you are just checking that if x and y varaibles are not equal and visited[x][y] is not true then there is no need to call for recursion just comment the recursive call, because your loop already do what you want the recursive call to do.
public static void findObjects(int xCord, int yCord) {
for(int y = yCord - 1; y <= yCord + 1; y++) {
for(int x = xCord - 1; x <= xCord + 1; x++) {
if(x > -1 && y > -1 && x < dotMap[0].length && y < dotMap.length) {
if((x != xCord || y != yCord) && dotMap[x][y] == true && visited[x][y] != true) {
visited[x][y] = true;
//findObjects(x,y);
//System.out.println("test");
}
}
}
}
}

Java: Tic-Tac-Toe - How To AVOID Repeated Cell From Being Input By User?

How do I prevent the same tic tac toe coordinate from being inputted by the user?
The user input is taken at the main method in the Game Class.
The tic tac toe cells with [x, y] coordinates ranging from (0-2) can be either:
0(_), 1 (X) or 2 (O)
Grid Class with alpha beta search tree pruning algorithm
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Grid {
List<Cell> availableCells;
int[][] board = new int[3][3];
Scanner scan = new Scanner(System.in);
// Set limit to search tree depth
int treeDepth = 9;
List<CellsAndScores> rootsChildrenScore = new ArrayList<>();
public int score() {
int score = 0;
// Check all columns
for (int j = 0; j < 3; ++j) {
int X = 0;
int O = 0;
for (int i = 0; i < 3; ++i) {
if (board[i][j] == 0) {
} else if (board[i][j] == 1) {
X++;
} else {
O++;
}
}
score += changeInScore(X, O);
}
// Check all rows
for (int i = 0; i < 3; ++i) {
int X = 0;
int O = 0;
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0) {
} else if (board[i][j] == 1) {
X++;
} else {
O++;
}
}
score += changeInScore(X, O);
}
int X = 0;
int O = 0;
// Check diagonal (first)
for (int i = 0, j = 0; i < 3; ++i, ++j) {
if (board[i][j] == 1) {
X++;
} else if (board[i][j] == 2) {
O++;
} else {
}
}
score += changeInScore(X, O);
X = 0;
O = 0;
// Check Diagonal (Second)
for (int i = 2, j = 0; i > -1; --i, ++j) {
if (board[i][j] == 1) {
X++;
} else if (board[i][j] == 2) {
O++;
} else {
}
}
score += changeInScore(X, O);
return score;
}
private int changeInScore(int X, int O) {
int change;
if (X == 3) {
change = 100;
} else if (X == 2 && O == 0) {
change = 10;
} else if (X == 1 && O == 0) {
change = 1;
} else if (O == 3) {
change = -100;
} else if (O == 2 && X == 0) {
change = -10;
} else if (O == 1 && X == 0) {
change = -1;
} else {
change = 0;
}
return change;
}
public int alphaBetaMinimax(int alpha, int beta, int depth, int turn) {
if (beta <= alpha) {
System.out.println("Pruning at tree depth = " + depth + " alpha: " + alpha + " beta: " + beta);
if (turn == 1)
return Integer.MAX_VALUE;
else
return Integer.MIN_VALUE;
}
if (depth == treeDepth || gameOver()) {
return score();
}
List<Cell> cellsAvailable = getAvailableStates();
if (cellsAvailable.isEmpty()) {
return 0;
}
if (depth == 0) {
rootsChildrenScore.clear();
}
int maxValue = Integer.MIN_VALUE, minValue = Integer.MAX_VALUE;
for (int i = 0; i < cellsAvailable.size(); ++i) {
Cell cell = cellsAvailable.get(i);
int currentScore = 0;
if (turn == 1) {
placeAMove(cell, 1);
currentScore = alphaBetaMinimax(alpha, beta, depth + 1, 2);
maxValue = Math.max(maxValue, currentScore);
// Set alpha
alpha = Math.max(currentScore, alpha);
if (depth == 0) {
rootsChildrenScore.add(new CellsAndScores(currentScore, cell));
}
} else if (turn == 2) {
placeAMove(cell, 2);
currentScore = alphaBetaMinimax(alpha, beta, depth + 1, 1);
minValue = Math.min(minValue, currentScore);
// Set beta
beta = Math.min(currentScore, beta);
}
// reset board
board[cell.x][cell.y] = 0;
// Do not evaluate the rest of the branches after search tree is pruned
if (currentScore == Integer.MAX_VALUE || currentScore == Integer.MIN_VALUE)
break;
}
return turn == 1 ? maxValue : minValue;
}
public boolean gameOver() {
// Game is over is someone has won, or board is full (draw)
return (hasXWon() || hasOWon() || getAvailableStates().isEmpty());
}
public boolean hasXWon() {
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == 1)
|| (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] == 1)) {
// System.out.println("X Diagonal Win");
return true;
}
for (int i = 0; i < 3; ++i) {
if (((board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] == 1)
|| (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] == 1))) {
// System.out.println("X Row or Column win");
return true;
}
}
return false;
}
public boolean hasOWon() {
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == 2)
|| (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] == 2)) {
// System.out.println("O Diagonal Win");
return true;
}
for (int i = 0; i < 3; ++i) {
if ((board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] == 2)
|| (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] == 2)) {
// System.out.println("O Row or Column win");
return true;
}
}
return false;
}
public List<Cell> getAvailableStates() {
availableCells = new ArrayList<>();
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0) {
availableCells.add(new Cell(i, j));
}
}
}
return availableCells;
}
public void placeAMove(Cell Cell, int player) {
board[Cell.x][Cell.y] = player; // player = 1 for X, 2 for O
}
public Cell returnBestMove() {
int MAX = -100000;
int best = -1;
for (int i = 0; i < rootsChildrenScore.size(); ++i) {
if (MAX < rootsChildrenScore.get(i).score) {
MAX = rootsChildrenScore.get(i).score;
best = i;
}
}
return rootsChildrenScore.get(best).cell;
}
public void displayBoard() {
System.out.println();
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0)
System.out.print("_" + " ");
if (board[i][j] == 1)
System.out.print("X" + " ");
if (board[i][j] == 2)
System.out.print("O" + " ");
}
System.out.println("");
}
System.out.println();
}
public void resetGrid() {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
board[i][j] = 0;
}
}
}
}
Cell class
class Cell {
int x, y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}
}
class CellsAndScores {
int score;
Cell cell;
CellsAndScores(int score, Cell cell) {
this.score = score;
this.cell = cell;
}
}
Game Class with main method - takes user input
import java.util.Random;
public class Game {
public static void main(String[] args) {
Grid grid = new Grid();
Random random = new Random();
grid.displayBoard();
System.out.print("Who moves first? [1]Computer(X) [2]User(O): ");
int turn = grid.scan.nextInt();
if (turn == 1) {
Cell p = new Cell(random.nextInt(3), random.nextInt(3));
grid.placeAMove(p, 1);
grid.displayBoard();
}
while (!grid.gameOver()) {
int x = 0, y = 0;
System.out.print("Please enter an x coordinate [0-2]: ");
x = grid.scan.nextInt();
System.out.print("Please enter an y coordinate [0-2]: ");
y = grid.scan.nextInt();
Cell userMove = new Cell(y, x);
grid.placeAMove(userMove, 2); // 2 for O and O is the user
grid.displayBoard();
if (grid.gameOver())
break;
grid.alphaBetaMinimax(Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 1);
for (CellsAndScores pas : grid.rootsChildrenScore)
System.out.println("Cell: " + pas.cell + " Score: " + pas.score);
grid.placeAMove(grid.returnBestMove(), 1);
grid.displayBoard();
}
if (grid.hasXWon()) {
System.out.println("Unfortunately, you lost!");
grid.resetGrid();
} else if (grid.hasOWon()) {
System.out.println("You win!");
grid.resetGrid();
} else {
System.out.println("It's a draw!");
grid.resetGrid();
}
}
}
My answer would be to add a boolean check method into your Grid.java class and then in your main method - call this boolean check method before the placeAMove() method.
For example, in your Grid.java class, adding the following method:
/*
* Return true if space is ok to use.
*/
public boolean isMoveOK(Cell cell) {
return board[cell.x][cell.y] == 0;
}
This way, using your pre-existing 0/1/2 values that keep track of empty/X/O space values, you may provide a check to see if the space value is zero or not.
This would be one way to use it in your main method, to answer your question of, 'How do I prevent the same tic tac toe coordinate from being inputted by the user?'
Cell userMove = new Cell(y, x);
if (grid.isMoveOK(userMove)) {
grid.placeAMove(userMove, 2); // 2 for O and O is the user
} else {
System.out.println("Please try a different space/cell");
continue;
}
grid.displayBoard();
if (grid.gameOver())
break;
In this way, I am skipping the remaining loop code in your main method loop, until there's a valid open space. (When there is, then the program should proceed to check for winning values or whether to proceed playing)
Hope this answers your question! :)
Cheers

Libgdx Generate RogueLIke Dungeon - Algorithm Error

I have used Libgdx to generate a dungeon out of keyboard characters. I have decided to print out the array as a text file.
However this is what I got:
Furthermore, It isn't consistent
I don't get what is wrong?
I checked over my algorithm and didn't find anything wrong.
Here is my code:
public class test1 extends ApplicationAdapter {
SpriteBatch batch;
Texture img;
int X = 50;
int Y = 25;
////////// # wall
////////// . ground
char[][][] mapChars = new char[1000][1000][1000];
private void genDung() {
int clearance = 4;
for (int i = 0; i < Y; i++) {
for (int j = 0; j <= X; j++) {
if(j == X)
mapChars[0][i][j] = '\n';
else
mapChars[0][i][j] = '#';
}
}
int roomCount = MathUtils.random(2, 2);
int[] roomPosX = new int[roomCount];
int[] roomPosY = new int[roomCount];
int[] roomCenterPosX = new int[roomCount];
int[] roomCenterPosY = new int[roomCount];
int[] roomSizeX = new int[roomCount];
int[] roomSizeY = new int[roomCount];
for (int i = 0; i < roomCount; i++) {
int attempts = 0;
while(true) {
boolean rePosition = false;
roomPosX[i] = MathUtils.random(1, X-1);
roomPosY[i] = MathUtils.random(1, Y-1);
roomSizeX[i] = MathUtils.random(2, 12);
roomSizeY[i] = MathUtils.random(2, 8);
for(int j = 0; j <= i; j++) {
if(i != j) {
if(roomPosX[i] >= roomPosX[j] && roomPosX[i] <= (roomPosX[j] + roomSizeX[j] + clearance)) {
if(roomPosY[i] >= roomPosY[j] && roomPosY[i] <= (roomPosY[j] + roomSizeY[j] + clearance)) {
rePosition = true;
break;
}
}
if((roomPosX[i]+roomSizeX[i]) >= roomPosX[j] && (roomPosX[i]+roomSizeX[i]) <= (roomPosX[j] + roomSizeX[j] + clearance)) {
if((roomPosY[i]+roomSizeY[i]) >= roomPosY[j] && (roomPosY[i]+roomSizeY[i]) <= (roomPosY[j] + roomSizeY[j] + clearance)) {
rePosition = true;
break;
}
}
if((roomPosX[i]) >= roomPosX[j] && (roomPosX[i]) <= (roomPosX[j] + roomSizeX[j] + clearance)) {
if((roomPosY[i]+roomSizeY[i]) >= roomPosY[j] && (roomPosY[i]+roomSizeY[i]) <= (roomPosY[j] + roomSizeY[j] + clearance)) {
rePosition = true;
break;
}
}
if((roomPosX[i]+roomSizeX[i]) >= roomPosX[j] && (roomPosX[i]+roomSizeX[i]) <= (roomPosX[j] + roomSizeX[j] + clearance)) {
if((roomPosY[i]) >= roomPosY[j] && (roomPosY[i]) <= (roomPosY[j] + roomSizeY[j] + clearance)) {
rePosition = true;
break;
}
}
}
else if(roomPosX[j] + roomSizeX[j] >= X-1){
rePosition = true;
}
else if(roomPosY[j] + roomSizeY[j] >= Y-1){
rePosition = true;
}
}
attempts++;
if(attempts >= 10000) break;
if(!rePosition) break;
}
}
for(int r = 0; r < roomCount; r++) {
for (int a = roomPosX[r]; a <= (roomPosX[r] + roomSizeX[r]); a++) {
for (int b = roomPosY[r]; b <= (roomPosY[r] + roomSizeY[r]); b++) {
mapChars[0][b][a] = '.';
}
}
}
Gdx.app.log("roomCount", String.valueOf(roomCount)+"\n\n\n");
for(int i =0; i< roomCount; i++) {
roomCenterPosX[i] = roomPosX[i] + roomSizeX[i]/2;
roomCenterPosY[i] = roomPosY[i] + roomSizeY[i]/2;
Gdx.app.log("room", String.valueOf(i)+"\n");
Gdx.app.log("roomPosX", String.valueOf(roomPosX[i]));
Gdx.app.log("roomPosY", String.valueOf(roomPosY[i]));
Gdx.app.log("roomSizeX", String.valueOf(roomSizeX[i]));
Gdx.app.log("roomSizeY", String.valueOf(roomSizeY[i])+"\n");
Gdx.app.log("RoomCenterPosX", String.valueOf(roomCenterPosX[i]));
Gdx.app.log("RoomCenterPosY", String.valueOf(roomCenterPosY[i])+"\n\n");
}
int difference = X;
int[] roomNum = new int[2];
for(int i = 0; i < roomCount; i++) {
for(int j = 0; j < roomCount; j++) {
if(i != j) {
if(abs(roomCenterPosX[i] - roomCenterPosX[j]) < difference) {
difference = abs(roomCenterPosX[i] - roomCenterPosX[j]);
roomNum[0] = i;
roomNum[1] = j;
}
}
}
}
Gdx.app.log("FarthestRooms", String.valueOf(roomNum[0]));
Gdx.app.log("FarthestRooms", String.valueOf(roomNum[1]));
int differenceX = X;
int differenceY = Y;
int[] connectRooms = new int[2];
// int[] roomsConnected = new int[roomCount];
connectRooms[0] = MathUtils.random(0, roomCount - 1);
// roomsConnected[0] = connectRooms[0];
int count;
for(int i = 0; i < roomCount-1; i++) {
int j;
while(true) {
connectRooms[1] = MathUtils.random(0, roomCount - 1);
/* while (true) {
connectRooms[1] = MathUtils.random(0, roomCount - 1);
count = 0;
for (j = 0; j < i; j++) {
if (connectRooms[1] != roomsConnected[j] && connectRooms[0] != roomsConnected[j]){
count++;
}
}
if(count >= i-2)
break;
}*/
if(connectRooms[0] != connectRooms[1])
break;
}
// roomsConnected[i+1] = connectRooms[1];
differenceX = roomCenterPosX[connectRooms[0]] - roomCenterPosX[connectRooms[1]];
differenceY = roomCenterPosY[connectRooms[0]] - roomCenterPosY[connectRooms[1]];
if(roomCenterPosX[connectRooms[0]] < roomCenterPosX[connectRooms[1]])
differenceX *= -1;
if(roomCenterPosY[connectRooms[0]] < roomCenterPosY[connectRooms[1]])
differenceY *= -1;
int k;
try {
if (differenceX > 0) {
for (k = 0; k < differenceX; k++) {
mapChars[0][roomCenterPosY[i]][roomCenterPosX[i] + k] = '.';
}
} else if (differenceX < 0) {
for (k = 0; k > differenceX; k--) {
mapChars[0][roomCenterPosY[i]][roomCenterPosX[i] + k] = '.';
}
} else k = 0;
if (differenceY < 0) {
for (int z = 0; z > differenceY; z--) {
mapChars[0][roomCenterPosY[i] + z][roomCenterPosX[i] + k] = '.';
}
} else if (differenceY > 0) {
for (int z = 0; z < differenceY; z++) {
mapChars[0][roomCenterPosY[i] + z][roomCenterPosX[i] + k] = '.';
}
} else {
}
}
catch (ArrayIndexOutOfBoundsException e) {
Gdx.app.log("Non Fatal Exception", String.valueOf(e));
}
Gdx.app.log("Connect", String.valueOf(connectRooms[0]));
Gdx.app.log("Connect", String.valueOf(connectRooms[1]));
Gdx.app.log("DifferenceX", String.valueOf(differenceX));
Gdx.app.log("DifferenceY", String.valueOf(differenceY)+"\n");
}
for(int q = 0; q < Y; q++) {
mapChars[0][q][X] = '\n';
}
for(int w = 0; w < Y; w++) {
mapChars[0][w][X-1] = '#';
}
for(int e = 0; e < Y; e++) {
mapChars[0][Y-1][e] = '#';
}
}
private void export() {
if(Gdx.files.isLocalStorageAvailable()) {
FileHandle fileHandle = Gdx.files.local("map.txt");
if(Gdx.files.local("map.txt").exists())
fileHandle.writeString("", false);
for(int i = 0; i<= Y; i++) {
for (int j = 0; j <= X; j++) {
fileHandle.writeString(""+mapChars[0][i][j] , true);
}
}
}
}
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
// genMap();
// for(int i = 0; i< 4; i++)
// refineMap();
genDung();
export();
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, 0, 0);
batch.end();
if(Gdx.input.isTouched()) {
// genMap();
// for(int i = 0; i< 4; i++)
// refineMap();
genDung();
export();
}
}
}
I want the two rooms to connect properly every time.
As you can see, one of the time, the rooms connected
The other time the rooms don't connect.
Thanks in advance
The whole thing can be simplified and definitely needs refactoring. Regarding the 2 room connection - check this piece carefully
differenceX = roomCenterPosX[connectRooms[0]] - roomCenterPosX[connectRooms[1]];
differenceY = roomCenterPosY[connectRooms[0]] - roomCenterPosY[connectRooms[1]];
if(roomCenterPosX[connectRooms[0]] < roomCenterPosX[connectRooms[1]])
differenceX *= -1;
if(roomCenterPosY[connectRooms[0]] < roomCenterPosY[connectRooms[1]])
differenceY *= -1;
As #kiheru pointed out it is equivalent to Math.abs(roomCenterPosX[connectRooms[0]] - roomCenterPosX[connectRooms[1]]) (same for Y). So you always "dig" to right and down, never up or left.
Drop that invertion and have fun with the rest of your algorithm :)

Categories

Resources