Why Ain't my Code working?

I’m trying to make a card game, but when I try to get the player’s hand in the correct spot. All 5 cards go to the last spot instead of staying where they suppose to.

here setup

Table enemyT;
TableRow tr;
int s = 75;
ArrayList<Card> deck = new ArrayList<Card>();
ArrayList<Card> hand = new ArrayList<Card>();
int turn = 0;
Feild feild = new Feild();
void setup(){
  size(600,600);
  enemyT = loadTable("zombies.csv","header");
  for (int i = 0; i < 10; i++){
    deck.add(new Card(0));
  }
}

void draw(){
  background(0);
  textSize(20);
  fill(255);
  text("Turn: " + turn, 10, 20);
  text("Hand: " + hand.size(), 10, 40);
  text("Deck: " + deck.size(), 10, 60);
  if (deck.size() > 0){
    fill(255);
    rect(10,520,s,s);
    fill(0);
    textSize(80);
    text("D",20,585);
  }
  feild.update();
  for (Card c: deck){
    c.show();
  }
  for (Card c: hand){
    c.show();
  }
}

here is Card

class Card{
  PVector pos,tcol,rcol,scol;
  String name,type,element;
  int rank,attack,defense;
  boolean inDeck,inHand,inFeild;
  
  Card(int n){
    inDeck = true;
    pos = new PVector(10,520);
    scol = new PVector(255,255,255);
    rcol = new PVector(0,0,0);
    tcol = new PVector(255,255,255);
    /*
    tr = enemyT.getRow(n);
    name = tr.getString("name");
    type = tr.getString("type");
    element = tr.getString("element");
    rank = tr.getInt("rank");
    attack = tr.getInt("attack");
    defense = tr.getInt("defense");
    */
    name = "zombie 1";
    type = "zombie";
    element = "dark";
    rank = 6;
    attack = 2500;
    defense = 1200;
    if (element.equals("dark")){
      scol = new PVector(255,255,255);
      rcol = new PVector(0,0,0);
      tcol = scol;
    }
  }
  
  void show(){
    if (inDeck == false){
      stroke(scol.x,scol.y,scol.z);
      fill(rcol.x,rcol.y,rcol.z);
      rect(pos.x,pos.y,s,s);
      fill(tcol.x,tcol.y,tcol.z);
      textSize(18);
      text(type,pos.x+5,pos.y+20);
    }
  }
}

here is feild. it controls when cards move & place them in correct places

class Feild{
  
  void update(){
    if (turn == 0){
      if (hand.size() < 5){
        Card c = deck.get(0);
        hand.add(c);
      }// hand 5
      if (hand.size() == 5){
        turn = 1;
      }
    }// turn 0
    if (turn == 1){
      for (int i = 0; i < hand.size(); i++){
        Card c = hand.get(i);
        c.pos.x = 80 + (80 * i);
        c.inHand = true;
        c.inDeck = false;
      }
    }// turn 1
  }// update
}// feild
1 Like

For this post, you need to provide instructions of how to play the game to understand what you are trying to do. At least, you could explain in what part of the code you are adjusting the position of the cards and the functionality of each Class. Something brief here helps a ton.

I am providing a modifying version of your code. In your code you are assigning a default position when creating a card. This position should be either the location of the deck or some unset value meaning that position should be set before using the card (I did not do this in the example below).

You are updating the card position if your Feild class. I have update this class to use a while loop and to update the card when it is loaded into the hand. Maybe you do not want a while loop but an if statement? It really depends on what kind of game you are playing. I am assuming you want to have 5 cards in your hand when starting the game and have 5 cards in the hand at all times.

Before moving further in your game, I suggest you change your class from Feild to CardManager so the Class name agrees with its functionality.

Kf

Table enemyT;
TableRow tr;
int s = 75;

ArrayList<Card> deck = new ArrayList<Card>();
ArrayList<Card> hand = new ArrayList<Card>();

int turn = 0;
Feild feild = new Feild();


public void setup() {
  size(600, 600);
  //enemyT = loadTable("zombies.csv", "header");

  for (int i = 0; i <10; i++) {
    deck.add(new Card(i));
  }

  noLoop();
}

public void draw() {
  background(0);
  textSize(20);
  fill(255);
  text("Turn: " + turn, 10, 20);
  text("Hand: " + hand.size(), 10, 40);
  text("Deck: " + deck.size(), 10, 60);

  if (deck.size() > 0) {
    fill(255);
    rect(10, 520, s, s);
    fill(0);
    textSize(80);
    text("D", 20, 585);
  }

  feild.update();

  for (Card c : deck) {
    c.show();
  }
  for (Card c : hand) {
    c.show();
  }
  
  printStatus();
}

void mousePressed() {
  redraw();
}

void keyPressed(){
   turn = (turn+1)%2; //Ensures value is either 1 or 0 after value is increased   
   redraw();
}

void printStatus(){
   println("==============");
   println("Current status");
   println("Turn",turn);
   println("Deck size",deck.size());
   println("Hand size",hand.size());
}


class Card {
  PVector pos, tcol, rcol, scol;
  String name, type, element;
  int rank, attack, defense;
  boolean inDeck, inHand, inFeild;

  Card(int n) {
    inDeck = true;
    pos = new PVector(80*n, 520);
    scol = new PVector(255, 255, 255);
    rcol = new PVector(0, 0, 0);
    tcol = new PVector(255, 255, 255);
    /*
    tr = enemyT.getRow(n);
     name = tr.getString("name");
     type = tr.getString("type");
     element = tr.getString("element");
     rank = tr.getInt("rank");
     attack = tr.getInt("attack");
     defense = tr.getInt("defense");
     */
    name = "zombie 1";
    type = "zombie";
    element = "dark";
    rank = 6;
    attack = 2500;
    defense = 1200;
    if (element.equals("dark")) {
      scol = new PVector(255, 255, 255);
      rcol = new PVector(0, 0, 0);
      tcol = scol;
    }
  }

  public void show() {
    if (inDeck == false) {
      stroke(scol.x, scol.y, scol.z);
      fill(rcol.x, rcol.y, rcol.z);
      rect(pos.x, pos.y, s, s);
      fill(tcol.x, tcol.y, tcol.z);
      textSize(18);
      text(type, pos.x+5, pos.y+20);
    }
  }
}


class Feild {

  public void update() {
    if (turn == 0) {
      int i=0;
      // Next ensures there are cards in the deck before withdrawing a new one
      while (hand.size() < 5 && deck.size>0 ) {  
        Card c = deck.get(0);
        deck.remove(0);
        c.pos.x = 80 * (++i);
        c.inHand = true;
        c.inDeck = false;
        hand.add(c);
      }// hand 5
    }// turn 0

    //if (turn == 1) {
    //  for (int i = 0; i <hand.size(); i++) {
    //    Card c = hand.get(i);
    //    c.pos.x = 80 + (80 * i);
    //    c.inHand = true;
    //    c.inDeck = false;
    //  }
    //}// turn 1
  }// update
}// feild

1 Like

Thanks I only want 5 cards in hand it at least check for 5 cards before the player chooses a card

1 Like