How do I create the card game of WAR on processing?

I'm supposed to create a program for my CST class using images. I'm using arrays and I figured out how to atleast display one player's cards and have it randomize. here is my code.

PImage[] images = new PImage[13];
int imageIndex = 0;

void setup(){
  size(500,500);
  background(255);
// load images into array
// images from freeware.esoterica.free.fr
//
// loading images into an array from an array of filenames
String[] filenames = {"c1.png","c2.png","c3.png","c4.png","c5.png","c6.png","c7.png","c8.png","c9.png","c10.png","cj.png","ck.png","cq.png"};
for (int i = 0; i < filenames.length; i++) {
  images[i] = loadImage(filenames[i]);
}
}

void draw(){
  //displays first image
  image(images[imageIndex],0,0);
}

void mousePressed(){
  //Get a new card when the mouse button is clicked
  imageIndex = int(random(images.length));
}

How do I make a second deck of cards, and have that player be the computer's cards? How do I set up arguements and make it so that the higher number/card would win?

Tagged:

Answers

  • Can you edit your post to properly format your code?

    It's hard to answer general "how do I do this" type questions, other than by pointing you to google and the Processing reference. What exactly are you confused about?

  • See Processing forum rules and advices

    Had to format code and to move to proper category.

  • This is more effort than I was expecting. I'm done. Do what you can with this.

    class Card {
      int c;
      Card(int cc) {
        c=cc;
      }
    }
    
    ArrayList<Card> a=new ArrayList();
    ArrayList<Card> b=new ArrayList();
    ArrayList<Card> x=new ArrayList();
    PImage card, back;
    
    void setup() {
      size(360, 390);
      // Temp deck storage.
      int[] c=new int[52];
      // Initial temp deck.
      for (int i=0;i<52;c[i]=i++);
      // Shuffle temp deck.
      for (int i=0;i<52;i++) {
        int r=int(random(52));
        int t=c[i];
        c[i]=c[r];
        c[r]=t;
      }
      // Deal temp deck.
      for (int i=0;i<52;i++) {
        if (i%2==0) {
          a.add(new Card(c[i]));
        } 
        else {
          b.add(new Card(c[i]));
        }
      }
      // Create card image - you could load this from a file instead...
      PGraphics pg=createGraphics(80, 115, P2D);
      pg.beginDraw();
      pg.background(0, 0, 0, 0);
      pg.fill(255);
      pg.stroke(0);
      pg.arc(10, 10, 20, 20, PI, PI+HALF_PI);
      pg.arc(70, 10, 20, 20, PI+HALF_PI, TWO_PI);
      pg.arc(10, 105, 20, 20, HALF_PI, PI);
      pg.arc(70, 105, 20, 20, 0, HALF_PI);
      pg.noStroke();
      pg.rect(10, 0, 60, 115);
      pg.rect(0, 10, 80, 95);
      pg.stroke(0);
      pg.line(10, 0, 70, 0);
      pg.line(10, 114, 70, 114);
      pg.line(0, 10, 0, 105);
      pg.line(79, 10, 79, 105);
      pg.endDraw();
      card=pg.get();
      pg.beginDraw();
      pg.stroke(0);
      for (int i=0;i<12;i++) {
        for (int j=0;j<19;j++) {
          pg.fill((i+j)%2==0?color(0, 0, 255):color(128, 128, 255));
          pg.rect(10+5*i, 10+5*j, 5, 5);
        }
      }
      pg.endDraw();
      back=pg.get();
      imageMode(CENTER);
      simulateGame();
    }
    
    void simulateGame() {
      int counter = 0;
      int toGive = 3;
      while (a.size ()>0&&b.size()>0 && counter<1000) {
        counter++;
        println( "Player plays a card: " + cardName(a.get(0).c) );
        println( "Computer plays a card: " + cardName(b.get(0).c) );
        x.add( new Card(a.get(0).c) );
        a.remove(0);
        x.add( new Card(b.get(0).c) );
        b.remove(0);
        if ( x.get(x.size()-2).c%13 == x.get(x.size()-1).c%13 ) {
          println("OH NO, A TIE! WAR IS DECLARED!");
          int gaveA = 0;
          for (int i=0;i<toGive&&a.size()>0;i++) {
            x.add( new Card(a.get(0).c) );
            a.remove(0);
            gaveA++;
          }
          int gaveB = 0;
          for (int i=0;i<toGive&&b.size()>0;i++) {
            x.add( new Card(b.get(0).c) );
            b.remove(0);
            gaveB++;
          }
          println("Player commits " + gaveA + " cards to the war effort.");
          println("Computer commits " + gaveB + " cards to the war effort.");
          toGive+=2;
        } 
        else {
          if ( x.get(x.size()-2).c%13 > x.get(x.size()-1).c%13 ) {
            // Player wins hand.
            toGive=3;
            println("Player wins " + x.size() + " cards.");
            while (x.size ()>0) {
              a.add( new Card(x.get(0).c) );
              x.remove(0);
            }
          } 
          else {
            // Computer wins hand.
            toGive=3;
            println("Computer wins " + x.size() + " cards.");
            while (x.size ()>0) {
              b.add( new Card(x.get(0).c) );
              x.remove(0);
            }
          }
        }
        println("");
      }
      if (a.size() == 0) { 
        println("COMPUTER WON!");
      }
      if (b.size() == 0) { 
        println("PLAYER WON!");
      }
      if (counter==1000) { 
        println("I'M SICK OF THIS GAME! GAME OVER!");
      }
    }
    
    
    void draw() {
      // You can... improve on this... maybe...
      background(0, 128, 0);
      fill(0);
      image(back, width/4, 2*height/3);
      image(card, width/2, 2*height/3);  
      image(back, 3*width/4, height/3);
      image(card, width/2, height/3);
    }
    
    String[] suits = { 
      "Hearts", "Spades", "Clubs", "Diamonds"
    };
    String[] ranks = { 
      "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"
    };
    String cardName(int c) {
      return( "" + ranks[c%13] + " of " + suits[c/13] + " (" + c + ")" );
    }
    
  • Full code solutions are academic dishonest and can get students a failing grade or even kicked out of school.

  • True re: full code solutions. But some people need them to learn. Sometimes a line or two of code from the exaple tells the researcher what to do., and can make their program work But an instructor isnt stupid. He/she gets to know what a student can do and when he/she has been driving a chevy and then turns up in a cadillac the instructor knows. If the student can't come up with the methodology then there's trouble. I assume most people are honest. if they aren't then too bad for them. they WILL get caught. It's THEIR responsibility.

  • @waynewal This debate has been had before on this forum, to the point that teachers have begged us to be more strict about full code solutions. I don't think you're speaking as an instructor, so it's a bit unreasonable to assume what's easy or not for an instructor.

    The point is that even without worrying about academic dishonesty, full code solutions also rob the original poster of working through the process of breaking down a problem into smaller pieces and then thinking through each piece one at a time, which is the real point of homework like this.

    The original poster is now worse off than they were before they posted. If we're really here to help people and not just race to dump code, then that should bother us.

Sign In or Register to comment.