Processing 2.0+ War card game- basic

edited November 2014 in Questions about Code

Driving me crazy. Went over and over the code. Have this set up in two parts - Class and program. setting up for 20 to test and debug, but then going to run it to 52 for a full deck. Card 00 is the back of the card.

Am sure I copied it all correctly, but am running in to an error.

unexpected token: PImage

Class section

  class Cards
  {
  int show = 0; // shows the back of the card
  //int cardWidth = 105;
  //int cardHeight = 141;
  PImage  cardImage;
  int cardX = 0;
  int cardY = 0;
  int faceValue = 0;
  int myPoints = 0;

  // card back is 00 so there are '53' cards vs 52

  String[] cardName = {
    "00.png", "01.png", "02.png", "03.png", "04.png", "05.png", "06.png", "07.png", "08.png", "08.png", 
    "09.png", "10.png", "11.png", "12.png", "13.png", "14.png", "15.png", "16.png", "17.png", "18.png", 
    "19.png", "20.png", "21.png", "22.png", "23.png"

    /*, "24.png","25.png","26.png","27.png","28.png",
    "29.png", "30.png","31.png","33.png","33.png, "34.png", "35.png", "36.png", "37.png", "38.png", 
    "39.png", "40.png", "41.png", "44.png", "43.png, "44.png","45.png","46.png","47.png","48.png",
    "49.png","50.png","51.png","52.png","53.png
      */
  };


  Cards (int x, int y, int fv, int mp)
  {
    cardX = x;
    cardy = y;
    faceValue = fv;
    myPoints = mp;  //keeps track of the points J=11, Q=12, K=13
  }
  void disply()
  {

    cardImage= loadImage(cardName[show]); //Display loads the images.
    image(cardImage, cardX, cardY);
  }


  void setX(int newX)  //move the card from the deck to the center and 
  {
    cardX = newX;
    show = faceValue;
  }
  int getPoints()  //gets the points from the card values compare the left to the right.
  {
    return (myPoints);
  }
}

next- Program

//WAR note: change all 20's to 52 for larger number of cards

//-----

void setup()

{

  background(0);

  int myY =75;

  int myX1 = 430;  //each pile has its own x value

  int myX2 = 50;

  size(600, 600);

  fill(255, 0, 0);


  myFont = createFont("Verdana", 48, true);

  for (int i = 0; i<20; i++)  //chn to 52

  {

    y[i] = myY;

    fv[i] = i+1;

    points [i] = currentPoint;

    currentPoint ++;

    if (currentPoint > 10)  //since only doing 10 cards each side - total 20 change this

    {

      currentPoint=1;  //then resetting at one - counts  -10 and then starts over.
      //so the point value matches the face value of the cards. ace =1, 2=2, etc j=11, q=12, k=13
    }


    if (i%2==0)

    {

      x[i] = myX1;

      myY+=2;  //changes the pixels of the cards so they are not all on top of eachother
      // the cards will show slightlu fanned down and to the right with these functions

      myX1+=2;

      myX2+=2;
    } else

    {

      x[i] = myX2;
    }
  }


  shuffle(); // shuffle a couple of times

  shuffle();

  for (int i=0; i<20; i++)

  {

    myCard[i] = new Cards (x[i], y[i], fv[i], points[i]);
  }
}

void draw()

{


  background(0);

  textFont(myFont);

  text(leftPoint, 75, 50);

  text(rightPoint, 515, 50);

  rect(200, 400, 200, 100);

  for (int i=0; i<20; i++)

  {

    myCard[i].display();  //display cards on the screen
  }

  if (leftDeck>19)  //CHANGE means went throug all the cards

  {

    if (leftPoint > rightPoint)

    {

      text("Left Wins!", 250, 350);
    } else if (leftPoint<rightPoint)

    {

      text("Right Wins!", 250, 350);
    } else

    {

      text("you TIED!!!!", 250, 350);
    }
  }
}


void mouseClicked()

{

  if (mouseX<400 && mouseX>200 && mouseY<500 && mouseY>400)

  {
    myCard[leftDeck].setX(190);  //set x value for left card display

    myCard[rightDeck].setX(310); //sets x value for right card display

if (myCard[leftDeck].getPoints()>myCard[rightDeck].getPoints())
{

  leftPoint++;
} else if (myCard[leftDeck].getPoints()<myCard[rightDeck].getPoints())

{
  rightPoint++;
}

leftDeck+=2;
rightDeck+=2;
  }
}
void shuffle()
{
  int temp = 0;

  int tempPoint =0;

  int rand = 0;

  for (int i=0; i<20; i++)  //CHANGE to 52

  {
    rand = int(random(0, 20));  //showing 20 needs to be 52

    temp = fv[i];

    tempPoint = points[i];

    fv[i] = fv[rand];

    points[i] = points[rand];  //have to move the point value with the facevalue

    fv[rand] = temp;

    points[rand] = tempPoint;
  }
}
Tagged:

Answers

  • Answer ✓

    I bet it's the = sign in line 6 of Cards. As it is you're trying assign to a class where you should be just declaring the variable cardImage.

  • edited November 2014 Answer ✓

    I think a Card class to represent 1 card unit would make the code more logical: *-:)
    http://forum.processing.org/two/discussion/2801/picking-cards-at-random-then-excluding-those-from-further-picking-

    Also, resources like loadImage() should be loaded inside setup()! :-B

  • edited November 2014

    thanks

  • edited December 2014

    okay duh ... got my that. Now the program error says it 'can't find anything named "myFont" ' thought we had it preloaded, will probably have to go back and use the font loader... (sigh).... Except when using

    PFont myFont;

    used, it at least went to the next error.

    Thanks!

  • Went back and started from scratch. The instructor gave us this program in a u Tube video and said to 'type in in and get it to working, be sure to check your code.' She apparently ran the full program off screen to show us the 'idea' but never told us the video depiction was missing significant parts of code. Had she done so would have spent significant hours less on looking for typos and more on making the program work, so wasted about 10 hours proofreading vs programming. Had to go back and declare all the global variables first, outside of void setup(). Then go back through and re examine what each line of code did and figure on how it applied to make the program run - another 10 hours plus 4 hours in a class 'cram session' the instructor had with some backup instructors. got it to work finally, but this was supposed to be a beginning course in coding - turned out to be a lot less fun than it was supposed to be. I guess never EVER take a course that is depicted as 'new, trial beginning basic programming course,', run by someone with 20 years of experience.

Sign In or Register to comment.