the draw() method in my code isn't working properly [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 days ago.
Improve this question
I am making a uno game for a school project. The code displayed below if giving me some issues, and i dont know why.
The code below is suppose to check if the cpu has any cards that match the players card in the middle. If there is a card that matches, the cpu places it down. If there isnt a card that matches, the code checks to see if the cpu has a wild card, if so it places it down, if not then it draws.
For some reason the draw() method is giving me issues, rather than the cpu drawing just 1 card, sometimes it draws 2, 5, 10,etc.
To be clear, i am pretty sure the issue isnt my draw method, i have tested it out.
I assume the issue stems from a problem with my loops in the gamePlayed subclass.
Here is the code
private void game_played(Canvas canvas, MouseEvent event) {
//Setting the horizontal position of the mouse to x
double x = event.getX();
//Setting the Vertical position of the mouse to x
double y = event.getY();
//Setting the defualt card in the Middle
boolean played = false;
for (int i = 0 ; i < human.getHand().size(); i++) {
if (x > 20+75*(i%10) && x < 20+75*(i%10) + 75
&& y > 400+110*(i/10) && y <400+110*(i/10)+110) {
//C becomes the card we clicked
myCard c = human.getHand().get(i);
//C is placed in the middle of the scene
humanMiddle = c;
//We remove the card clicked from our hand
human.getHand().remove(i);
//Breaking the loop
//My code - checking for color match
//If the card in the middle for the human is red
if (humanMiddle.color == myCard.RED) {
//loop through the cpu hand, and see if you find a red card
for (int r = 0; r < cpu.getHand().size(); r++ ) {
//if you find a red card
if (cpu.getHand().get(r).color == myCard.RED) {
//the cpu middle now becomes that card
cpuMiddle = cpu.getHand().get(r);
//we remove the card from the hand
cpu.getHand().remove(r);
//else if, we have a wild card
} else if (cpu.getHand().get(r).value == -1){
//the cpu middle now becomes the wild card
cpuMiddle = cpu.getHand().get(r);
//we remove the wild card from the hand
cpu.getHand().remove(r);
} else {
cpu.draw();
}
}
}
if (humanMiddle.color == myCard.BLUE) {
for (int b = 0; b < cpu.getHand().size(); b++ ) {
if (cpu.getHand().get(b).color == myCard.BLUE) {
cpuMiddle = cpu.getHand().get(b);
cpu.getHand().remove(b);
} else if (cpu.getHand().get(b).value == -1){
cpuMiddle = cpu.getHand().get(b);
cpu.getHand().remove(b);
} else {
cpu.draw();
}
}
}
if (humanMiddle.color == myCard.YELLOW) {
for (int ye = 0; ye < cpu.getHand().size(); ye++ ) {
if (cpu.getHand().get(ye).color == myCard.YELLOW) {
cpuMiddle = cpu.getHand().get(ye);
cpu.getHand().remove(ye);
} else if (cpu.getHand().get(ye).value == -1){
cpuMiddle = cpu.getHand().get(ye);
cpu.getHand().remove(ye);
} else {
cpu.draw();
}
}
}
if (humanMiddle.color == myCard.GREEN) {
for (int g = 0; g < cpu.getHand().size(); g++ ) {
if (cpu.getHand().get(g).color == myCard.GREEN) {
cpuMiddle = cpu.getHand().get(g);
cpu.getHand().remove(g);
} else if (cpu.getHand().get(g).value == -1){
cpuMiddle = cpu.getHand().get(g);
cpu.getHand().remove(g);
} else {
cpu.draw();
}
}
}
played = true;
break;
}
}
here is the draw method
public void draw() {
int value = (int)(Math.random()*13);
int color = (int)(Math.random()*5);
while (value == 11) value = (int)(Math.random()*13);
if (color == myCard.BLACK) {
value = -1;
}
myCard card = new myCard();
card.value = value;
card.color = color;
hand.add(card);
}
I would appreciet any help
I have tried, adding breaks, to stop the loops at Certain points, but it just makes it worst.

Related

When the player hits the food make it disapear

I am new to programing, so bare with me lol. Our teacher has given us a game in which we need to add some elements to it. It's a game where we have a player, some enemies and some food made out of rectangles. If the player hits the food i want the food to spawn somewhere else in the window. Heres is the code where im trying to make it happen:
private void updateFood(){
for(int i = 0; i < food.length; ++i)
{
//Should we follow or move randomly?
//2 out of 3 we will follow..
if(rnd.nextInt(3) < 2)
{
//We follow
int dx = player.getX() - food[i].getX();
int dy = player.getY() - food[i].getY();
if(abs(dx) > abs(dy))
{
if(dx > 0)
{
//Player is to the right - opiste
food[i].moveLeft();
}
else
{
//Player is to the left - opiste
food[i].moveRight();
}
}
else if(dy > 0)
{
//Player is down; - opiste
food[i].moveUp();
}
else
{//Player is up; - opiste
food[i].moveDown();
}
}
else
{
//We move randomly
int move = rnd.nextInt(4);
if(move == 0)
{
//Move right
food[i].moveRight();
}
else if(move == 1)
{
//Move left
food[i].moveLeft();
}
else if(move == 2)
{
//Move up
food[i].moveUp();
}
else if(move == 3)
{
//Move down
food[i].moveDown();
}
}
if(food[i].getX() == player.getX() && food[i].getY() == player.getY()){
food[i].getX() = random(0,width);
food[i].getY() = random(0,height);
}
}
}
the last 10 lines (or so) is where im having the problem.
if(food[i].getX() == player.getX() && food[i].getY() == player.getY()){
food[i].getX() = random(0,width);
food[i].getY() = random(0,height);
With limited info on what methods your food[index] objects have, I'm going to guess .getX() is read-only, meaning you can't set a value to it. Either find a .setX(number) method, or use the existing .moveLeft/Right/Up/Down(number) with a bit of maths.

Draw stops working after if

I'm trying to create a maze with Union Find, but am unable to remove walls.
This is what I have got so far.
private void createMaze (int cells, Graphics g) {
s = new int[cells*cells]; //No unions yet setting all to -1
for(int i = 0; i < cells*cells; ++i){
s[i] = -1;
}
g.setColor(Color.yellow);
random = new Random();
while(breaker){
g.setColor(Color.yellow);
int innerWall = random.nextInt(4)+0;
int randomCellX = random.nextInt(cells-1)+0;
int randomCellY = random.nextInt(cells)+0;
if(randomCellX==cells&&innerWall==2||
randomCellX==0&&innerWall==0||
randomCellY==cells-1&&innerWall==3||
randomCellY==0&&innerWall==1){
continue;
}
else{
int location = randomCellX+(randomCellY*cells);
int neighbour = 0;
if(innerWall==0){
neighbour =location-1;
}
else if(innerWall==1){
neighbour =location-cells;
}
else if(innerWall==2){
neighbour =location+1;
}
else if(innerWall==3){
neighbour =location+cells;
}
int locationRoot =find(location);
int neighbourRoot =find(neighbour);
if(locationRoot==neighbourRoot){
breaker = checkIfDone(s);
}
union(location,neighbour);
drawWall(randomCellX,randomCellY,innerWall,g);
}
}
}
If I remove the
if(randomCellX==cells&&innerWall==2||
randomCellX==0&&innerWall==0||
randomCellY==cells-1&&innerWall==3||
randomCellY==0&&innerWall==1){
continue;
}
It removes the lines fine,but when it is added the walls are not removed. The method is called but doesn't do anything.
It seems some logical mistake have been done obviously. But can not spot specifically as you haven't given the explanation of the logic you have done. Only can help showing the path where the problem might be happening.
Since you can not reach the else block anyway, it is obvious that one or more of these conditions in if are always true.
(randomCellX == cells && innerWall == 2)
(randomCellX == 0 && innerWall == 0)
(randomCellY == cells - 1 && innerWall == 3)
(randomCellY == 0 && innerWall == 1)
That's why you are getting true for the if condition and continuing the loop without doing anything. Ensure the conditions are okay.
If these conditions are right, the next suspect might be these lines:
int innerWall = random.nextInt(4)+0;
int randomCellX = random.nextInt(cells-1)+0;
int randomCellY = random.nextInt(cells)+0;
Check whether these random values range are exactly what you want or not. For example: you are taking random values for randomCellX from 0 to cells-2, but for randomCellY the range is 0 to cells-1.

How to navigate a Barbie in a grid system? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to solve an interesting problem for navigating a Barbie doll inside a grid system. Initially, Barbie is in [0,0] position, means in the intersection of the X and Y axis. Barbie has 3 operations on movement with 'F' for forward, 'R' for right turn (90 degree) and 'L' for left turn (90 degree). Say, if I pass the direction String of "FF", the position should be [0,2]. I solve the problem and the current state of code is as following,
public static void barbiePosition(String str ){
if( str == null || str.length() == 0)
return;
int [] initial = {0,0};
boolean xPos = false, xNeg = false, yPos = true, yNeg = false;
char[] ch = str.toCharArray();
for( char c: ch){
// the initial postion of the robot is towards the positive Y axis
if(c == 'L'){
if(xPos){
xPos = false;
yPos = true;
}
else if ( xNeg){
xNeg = false;
yNeg = true;
}
else if(yPos){
xNeg = true;
yPos = false;
}
else if (yNeg){
yNeg = false;
xPos = true;
}
}
else if ( c == 'R'){
if(xPos){
xPos = false;
yNeg = true;
}
else if ( xNeg){
yPos = true;
xNeg = false;
}
else if(yPos){
yPos = false;
xPos = true;
}
else if (yNeg){
yNeg = false;
xNeg = true;
}
}
else if (c == 'F'){
if(xNeg){
initial[0] -= 1;
}
else if (xPos){
initial[0] += 1;
}
else if (yNeg){
initial[1] -=1;
}
else if( yPos){
initial[1] += 1;
}
}
}
System.out.println(Arrays.toString(initial));
}
The thing is I just don't the solution as it's feels ugly. Is there any better way
to design the algorithm ?
What constitutes 'forward' in your application is a function of the previous move.
Forward simply means you repeat your last move.
If forward is your first move you need to pick a convention that says something like "Barbie is initially moving to the right".

How to detect if two sprites are being touched at the same time?

I tried this:
if(spr.getBoundingRectangles.contain(x,y)){
//do this
}
but how to detect if the other sprite is touched by the second pointer?
EDIT:
for(int i = 0; i < Constants.MAX_POINTERS; i++){
if(Gdx.input.isTouched(i)){
xy.set(Gdx.input.getX(i), Gdx.input.getY(i), 0);
xy1.set(Gdx.input.getX(i), Gdx.input.getY(i), 0);
WorldRenderer.camera.unproject(xy);
WorldRenderer.camera.unproject(xy1);
if(Spr.getBoundingRectangle().contains(xy.x, xy.y) &&
Spr1.getBoundingRectangle().contains(xy.x, xy.y))
score += 1;
}
}
What happens is that xy and xy1 are always the same, when I touch the screen with the second pointer they will just both switch to the new coordinates instead of having two different x,y for both xy and xy1.
you can iterate over all pointers checking whether they are touching the screen and then check if positions overlapping sprites position
final int MAX_POINTERS = 5;
...
for(int i = 0; i < MAX_POINTERS; i++)
{
if( Gdx.input.isTouched(i) )
{
int x = Gdx.input.getX(i);
int y = Gdx.input.getY(i);
if( sprite.getBoundingRectangle().contains(x, y) ) //instead of checking one sprite iterate over sprites array
{
System.out.println("The sprite is touched!");
}
//if... - or just add more ifs
}
}
you need to define max count of pointers to iterate over it - as far as I know Libgdx supports up to 20 pointers
ABOUT EDIT:
Of course they are the same... :) You are placing the same value to vetors. My example above is far more generic that you need - if you know that you have two pointers you can just use:
if( Gdx.input.isTouched(0) && Gdx.input.isTouched(1) ) //because if two pointers are touching screen there is a chance that they are touching two sprites
{
xy.set(Gdx.input.getX(0), Gdx.input.getY(0), 0);
xy1.set(Gdx.input.getX(1), Gdx.input.getY(1), 0);
//checking if pointer 1 is touching sprite 1 and pointer 2 is touching sprite 2 OR VICE VERSA
if( (Spr.getBoundingRectangle().contains(xy.x, xy.y) && Spr1.getBoundingRectangle().contains(xy1.x, xy1.y))
||
(Spr.getBoundingRectangle().contains(xy1.x, xy1.y) && Spr1.getBoundingRectangle().contains(xy.x, xy.y))
)
{
score += 1;
}
}
or just create the function that will return true if all sprites you will pass to it are touched (which actually will can handle more than two sprites)
boolean allTouched(Array<Sprite> sprites)
{
int spritesCount = sprites.size;
int spritesTouched = 0;
for(int i = 0; i < MAX_POINTERS; i++)
{
if( Gdx.input.isTouched(i) )
{
for(Sprite sprite : sprites)
{
if( sprite.getBoundingRectangle().contains(Gdx.input.getX(i), Gdx.input.getY(i)) )
{
spritesTouched++;
sprites.removeValue(sprite, true); //to not doubling the same sprite
}
}
}
}
return spritesCount == spritesTouched ;
}

java: tetris random block generated upon row clear

I'm trying to make the game Tetris in java.
I've gotten it to the point where:
a new block is generated when it hits the floor or its y+1 is not null (meaning there's another block under it)
public void collisionCheck(int x, int y) {
if (activetile.getY() == this.height-2 || getTileAt(x, y+1) != null) {
activetile = new Tile(this, 0, 0);
}
}
A row clears when the bottom row is full of non-null values, or the Tetris pieces (for y = 4 (the floor), loop through x till x = 4 and check if all non-null)
public void checkBottomFull(int x, int y) {
while (getTileAt(x,y) != null) {
say("(" + x + ", " + y +")");
if (x == 3) {
say("row is full");
//replace full row with tiles from above
for (int i = 0; i < 4; i++) {
for (int j = 5; j > 0; j--) {
grid[j][i] = getTileAt(i,j-1);
grid[j-1][i] = null;
}
}
break;
}
x++;
}
}
Right now, I'm using keys to move the block:
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_DOWN) {
activetile.setLocation(activetile.getX(), activetile.getY()+1);
System.out.println("coordinates: " + activetile.getX() + ", " + activetile.getY());
collisionCheck(activetile.getX(),activetile.getY());
checkBottomFull(0,4);
repaint();
}
}
There's two issues I'm having:
1) In the picture you'll notice I've dropped the block all the way to the floor... and the row cleared. After it's cleared, it will generate a block to the top left (x=0, y=1) which I have no control over.
2) On the floor there seems to be a red line... which I'm assuming is a row of blocks hidden by the JFrame... I'm not sure why that's there.
FYI: If you're wondering why grid[j][i] has the rows and columns flipped (aka, why it's not grid[i][j]) is because I instantiated it as grid = new Tile[height][width];
Any thoughts?
Thanks!
It is hard to say what is wrong without actually debugging your app.
But maybe try this one:
public void checkBottomFull(int x, int y) {
while (getTileAt(x,y) != null) {
say("(" + x + ", " + y +")");
if (x == 3) {
say("row is full");
//replace full row with tiles from above
for (int i = 0; i < 4; i++) {
for (int j = 4; j >= 0; j--) {
grid[j][i] = getTileAt(i,j-1);
grid[j-1][i] = null;
}
}
break;
}
x++;
}
}
You have 5 rows (indexed from 0 to 4) and 4 columns (indexed from 0 to 3).
What values of height and width do you pass to:
grid = new Tile[height][width];
Because from what I see you should do something like that:
grid = new Tile[5][4];
Bah,
Turns out in the key event, I needed to check if the bottom was full before checking if there is a collision.
I guess what was happening is, when I was checking collisionCheck(activetile.getX(),activetile.getY()); before checkBottomFull(0,4);, when the bottom was full, it would clear the row and set the current row equal to the row above it: grid[j][i] = getTileAt(i,j-1);, the problem was that collisionCheck was generating a new piece and the that newly generated piece was getting cleared and replaced by checkBottomFull.
Putting the collision check after the checkBottomFull ensures that the newly generated piece won't be replaced if bottom is full.

Categories

Resources