A quick overview of my program: I am making the game War. So when the play button is clicked, two new cards show up with random values. Which ever card has the higher value wins, and a rectangle appears in the middle of the window. As the game goes on, who ever wins has more rectangles pushed to their side (either to the left or right depending on which card wins), but the trick is that if for example the card on the left wins the first three, then the card on the right wins 1, a rectangle is removed, meaning only 2 are going to the left. If the right wins again, another is removed, etc.
The rectangle is created in a separate class called Bar, and here is what I have to create an array with a rectangle that goes to the left.
Bar newBar = new Bar(300,250);
counter = d++;
newBar = new Bar(300-(counter*30), 250);
if(numDi >= bars.length){ //You want it to always be higher
Bar[] temp = new Bar[bars.length +1];
for(int i = 0; i < bars.length; i++){
temp[i] = bars[i];
}
bars = temp; //increments bars by 1
}
bars[numDi++] = newBar;
The idea I have is that I could create an if statement which checks who won and also who won the last game. And if the opposing player won the last game, I would need to subtract a rectangle.
But how would I go about subtracting the rectangle?
And as a side note, I can not use any sort of lists.
You can use a single integer counter. When the right side wins increment it by 1 and when the left side wins decrement it by 1. This way you can easily test with an if statement who has been winning lately and control what happens depending on who wins.
If you can't use a list or queue then I would suggest using two separate arrays, one for the left side and one for the right side. You can increment and decrement them according to the overall score counter.
Related
I'm creating a solitaire game using java swing gui. I'm trying to make this whole program very object-oriented. I've just learned java, but since it's my first language, I wasn't taught OOP and how to implement it into my code design (although I'm trying to learn this myself, as java is an object-oriented language).
I've created a 3d array within the constructor (but I'm not sure if I should put this in a separate method that is designated for the initial game state (which is alike for how the game has to be successfully completed)) for the card suit, colour, and value.
I want to either associate a value to a
each card in the deck between the image and the array, or be able to have an image be directly associated with an array so it's possible for the program to know the correct and legal sequencing in the gameplay.
all the cards require a back and front facing card so either is displayed depending on position & game state.
(unrelated, but I'm also making a layered pane for the depth of each card which will be important for game state)
i've previously learned how to display images in java with swing using imageIcon and attaching them to buttons or labels. I'm not sure if using imageIO would be better. Maybe a 3d array isn't the right choice? Any guidance/help is greatly appreciated. Thank you :)
the 3d array looks like this
String [][][] deck = new String[13][2][2]; //only 2 elements for suits in each colour, out of 4 suits
String[] numbers = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
String[] colors = {"Red", "Black"}; //z
String[] suits = {"Spade", "Club", "Heart", "Diamond"}; //y
}
for (int n = 0; n < 13; n++) { //start with value -> colour -> suit
for (int c = 0; c < 2; c++) {
for (int s = 0; s < 2; s++) {
if (c == 0) { //if c==0 then suit != 1 or 2
deck[n][c][s] = colors[c] + " " + numbers[n] + " of " + suits[s + 2];
//bc adding 2 makes y be [2] or [4] bc those suits are red //combines array to form y#x in each z
} else {
deck[n][c][s] = colors[c] + " " + numbers[n] + " of " + suits[s];
// black card ==1 and it c==1, you wont add 2 to the value of [s], so black can only be [0] or [1] (spade and club)
}
}
}
}
little bit of info about some of my ideas for how the game will be organized:
the game will roughly look like this.
The circle "1" is where the unshuffled deck/array will start (initial gamestate), and this is also what it will look like when the player successfully solves the game (initial gamestate==final gamestate) so each suit has a designated start and finish position.
the array (the deck) will be shuffled by using Donald Knuth's shuffling algorithm.
after multiple iterations of shuffling, I'll divide the cards (elements in the array) into 2 piles: the hand pile (24 cards not yet played), the tableau pile (where the player plays to place the cards in sequential order of high-low and black/red or red/black)
this part is important-
when the player taps a button or label of a cards image, a MouseListener is used to get the position where it was clicked, the program searches for which card value & colour occupies that position, and then the program will look for legal moves that are available for the player.
If you want to make it object oriented you should probably make a class called Card with fields number, color and suit. Don't forget to have separation of concerns. Separate the presentation layer from the business layer. If you end up writing code for which you can't even tell what's front end code and what's back end code then that's bad. Your front end can have a CardDisplayer class, which is an adapter class to the Card class. CardDisplayer takes in a Card and it draws it on the screen.
Your front end sends messages to the back end when the player makes a move. Your back end should not make any changes to the data that front end has. Your back end should message back the front end with legal moves or with the new state of the game.
You also need a game loop. When the game finishes the player should go back to some sort of a start menu.
I have a problem regarding drawing multiple of the same objects. They end on top of each other, and their "Object values" even get added together e.g. if a zombie has "2" movement value and there is added 2 into the current level. Then when zombies are moved, they move "4" each. This does not happen if two different objects are moved e.g. Zombie and Dragon. I have created the following code based on my project - for a simpler overview. However, the behavior is the same.
It should be mentioned the objects of the same type have the same "HashCode". I have also made comments and println to help figure out the problem. Each of the 5 objects have different position (When set). However, they are rendered on top of each other and merged (Movement).
EnemyDatabase enemyDatabase;
ArrayList<Enemy> enemiesInlevel = new ArrayList<Enemy>();
void setup()
{
size(600, 600);
enemyDatabase = new EnemyDatabase();
enemyDatabase.enemies.add(new Enemy("Zombie", 3));
//If dragon is added - Then it can draw both - But only once
//enemyDatabase.enemies.add(new Enemy("Dragon", 10));
//Add 10 zombies to the level
for(int i=0; i< 5;i++)
{
//5 Zombies are created and succesfully added to the database
enemiesInlevel.add(enemyDatabase.enemies.get((int)random(0, enemyDatabase.enemies.size())));;
}
for(int i=0; i<enemiesInlevel.size();i++)
{
//Sets the position of all 5 - Gives random position
Enemy enemy = enemiesInlevel.get(i);
enemy.SetPosition();
}
}
void draw()
{
background(255, 200, 0);
//Draw 5 enemies - Of index 0 (Zombie)
for(int i=0; i<enemiesInlevel.size();i++)
{
Enemy tempEnemy = enemiesInlevel.get(i);
tempEnemy.draw();
//It runs 5 times - print returns 5 total objects in enemiesInlevel
}
}
class EnemyDatabase
{
ArrayList<Enemy> enemies = new ArrayList<Enemy>();
}
class Enemy
{
String enemyName;
int enemyHealth;
PVector enemyPosition;
public Enemy(String name, int hp)
{
enemyName = name;
enemyHealth = hp;
}
public void SetPosition()
{
enemyPosition = new PVector(random(0, width), random(0, height));
//Each of the objects has a random position
println(enemyPosition);
}
public void draw()
{
rect(enemyPosition.x, enemyPosition.y, 25, 25);
}
}
UPDATE
Here is an image of the problem
As the image shows from the output in the image line: The enemies have different positions when added to the array (5 different enemies) However, as the image shows, it still only displays one - And this happens because all of the sudden the positions is the same. I even tried to give a "Random position" in the "DrawImage" all of the enemies still end up having the same position
Hope you guys can figure it out. I certainly have a hard time - I usually don't work in processing Thank you
DrSatan's comment is correct. You're only ever adding a single Enemy to your EnemiesDatabase. So when you select 5 random enemies, you're really just selecting the same enemy 5 times.
It might look like they have different positions in your print statements, but that's because you're printing out the position every time you set it. You're setting it five times, but only the last position really counts for anything.
In other words, you're taking a single instance of Enemy, moving it around the screen 5 times, and printing out the position each time. But then when you draw it, only the last position you moved it to is what you see.
You need to refactor your code so you add more enemies to your EnemyDatabase, but please note that you're going to have similar problems no matter how many enemies are in your EnemyDatabsae, because you don't have any logic that makes sure you don't select the same instance twice. Even if you had 100 enemies in your database, nothing is stopping your random function from selecting the same enemy 5 times.
If I were you, I'd rethink my design. I don't really see the reason to have a separate EnemyDatabase class at all. Maybe just create a function that adds X random enemies to the level, where X is a parameter? That's up to you, but your fundamental problem here is that you only have a single instance of Enemy.
I'm creating a game with a score system similar to the one in Flappy Bird.
if you didn't get it
When the player successfully passes the right and left
obstacle I want my score variable to increase by 1. I don't know the logic behind it though.
Right now I have (this is inside a for loop checking every obstacle in an update method):
'over' making sure the score only increases by 1.
if (bird.getPosition().y > obstacle.getPosLeft().y) {
if (!over) {
over = true;
score++;
}
and when the obstacle gets off the screen (yes, y is 0 at the bottom, not the top):
if (obstacle.getPosLeft().y + Obstacle.OBSTACLE_HEIGHT < 0){
obstacle.reposition(obstacle.getPosLeft().y + (OBSTACLE_SPACING + Obstacle.OBSTACLE_HEIGHT)* OBSTACLE_COUNT);
over = false;
}
The problem is that you can pass 2 obstacle pairs before the first one gets re-positioned which means that you only get the 2nd point after reposition() is called for obstacle 1.
How would you fix this problem?
Just make a "obstacle.over" boolean so that each has its own state.
I am making a chess game and so far everything is good and I am writing the rules of each piece now. The problem is that a for loop is acting oddly. Its for the bishop so x is going down and y is going up. At this point it acts oddly whenever I try to add the point to a possible move
for (int i = 0; i < 8; i++) {
Point newLoc = new Point(x-i, y+i);
if(team.equals("white")) {
if(containsPiece(newLoc)) {
if(ChessBoard.black.containsKey(newLoc)) {
possibilities.put(newLoc, rating);
break;
}
else {
break;
}
} else
possibilities.put(newLoc, rating);
}
containsPiece() is working just fine and possibilities is the HashMap I am storing the possible moves in.
The way I see it it should be working perfect because if the tile at newLoc is white it shouldn't add it to the possible moves and stop getting any moves after it in that direction. Does anyone see why it seems to abandon all the previous possible moves added to possibilities
i should start in 1, not 0, since when i==0, newLoc is the position of the bishop ((x-0,y+0)), so you break from the loop, since the bishop is a white tile.
Im creating an 'Avoid the Blocks' game, and for this i need to move a character around a grid (2D array) using the keys GHKJ. Every x amount of turns (decreasing as the level increases) a shadow must appear, then that shadow becomes a block and if the player moves into that bloack they lose a life.
Most of this is done for me other than the seemingly simple taski of getting the blocks to appear, here is my code so far for the falling blocks:
public void rocked(){
int rockInit = turn;
if(rockInit > 1){
int save = turn;
System.out.println(turn + " ");
B.board[ran.nextInt(12)][ran.nextInt(12)] = shadow;
if(save == turn - 3){
B.board[rockLocX][rockLocY] = rock;
}
}
}
The system.println is simply for debugging purposes, checking the values are being accesed. Turn is increased by 1 for every move the player makes, ran.nextInt(12) is a randomly generated number between 0 and 11, and B.board is the playing board.
It looks like you're never changing "save" after you initialize it to "turn". So then when you check if(save == turn-3), it will always be false, and so never move the block in. If you want to keep track of how many turns have passed, I would recommend a private instance variable "int turnsPassed" that you can increment each turn. Then for each level, you can check if (turnsPassed % x == 0), where x is as you've described it. Hope that helps!