dimanche 29 mars 2015

How to link images to numbers and descriptions?

I'm having trouble trying with a card game program that I'm writing. The game is basically like War with 4 players, each player gets a card and the highest card wins. The cards are supposed to be arranged from 2-Ace and the rank of the suits goes: clubs, diamonds, hearts, and the spades. I have an algorithm here for the game, I also have a folder that holds the cards. The issue I'm having is trying to connect each of the cards with a number in an arraylist so that when the user presses the deal button, it will deal them each a card and then compare them and print out the winner of the round. Here is my code. Right now I have the UI opening up and the random cards appear in each player box. The issue I'm having again is trying to link those cards to a specific number, and description, and to get the deal button to deal 4 new cards each time. Please help! P.S. I know I have very messy code and a lot of stuff is not being used, these are just things that I have been trying.



import java.io.File;
import java.util.ArrayList;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.control.Label;
import javafx.geometry.*;
import java.util.*;

import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;

import javax.swing.*;

public class War extends Application {
public static void main(String[] args) {
launch(args);
}

List<Integer> deck;
ArrayList<ImageView> imager = new ArrayList<ImageView>();
private int roundCounter;
JButton button = new JButton();

@Override
public void start(Stage primaryStage) {
loadDeck();
loadCards();
//Panes
BorderPane bPain = new BorderPane();
VBox vBox = new VBox(100);
vBox.setPadding(new Insets(5, 5, 5, 5));
HBox buttonBox = new HBox(100);
buttonBox.setPadding(new Insets(25, 25, 25, 75));
buttonBox.setStyle("-fx-background-color: blue");

bPain.setLeft(new P1Pane("PLayer 1"));
bPain.setRight(new P2Pane("PLayer 2"));
BorderPane bPain2 = new BorderPane();
bPain2.setRight(new P3Pane("PLayer 4"));
bPain2.setLeft(new P4Pane("PLayer 3"));

//VBox vBox2 = new VBox(100);
//vBox2.setPadding(new Insets(5, 5, 5, 5));


BorderPane bPain3 = new BorderPane();
DeckPane dPane = new DeckPane("Deck");


bPain3.setCenter(dPane);
bPain3.setMargin(dPane, new Insets(5, 5, 5, 5));
bPain3.setCenter(dPane);

//Buttons
Button pBtn = new Button("PLAY");
Button dBtn = new Button("DEAL");
Button endBtn = new Button("END GAME");
buttonBox.getChildren().addAll(pBtn, dBtn , endBtn);

//vBox.getChildren().add(buttonBox);
vBox.getChildren().addAll(buttonBox, bPain, bPain3, bPain2);
//vBox2.getChildren().add(bPain3);
//vBox.getChildren().add(vBox2);
/*
pBtn.setOnAction(
p -> {System.out.println("Play pressed");});

dBtn.setOnAction(
d -> {
//deals remaining cards
int[] wins=new int[4];
wins[0]=0;wins[1]=0;
wins[2]=0;wins[3]=0;

System.out.println("Round: "+(roundCounter++));
int prev=-1;
int winner=1;
for(int j=0;j<4;j++)
{
System.out.println("Player "+(j+1)+" card: "+deck.get(0));
if(prev<deck.get(0))
{
winner=j+1;
prev=deck.get(0);
}
deck.remove(deck.get(0));
}
System.out.println("Winner is player: " +winner);
System.out.println();
wins[winner-1]++;
//shuffle after each round


//only enabled after play button and there are cards left
System.out.println("Deal pressed");
});

endBtn.setOnAction(
e -> {
//when pressed, popup asks if sure
//disabled after
//resets number of wins
System.out.println("End pressed");
});
*/


//Stage set
primaryStage.setTitle("WAR!!!");
//primaryStage.setScene(new Scene(vBox2, 100, 100));
primaryStage.setScene(new Scene(vBox, 500, 500));

primaryStage.show();
}

public void loadCards() {

String path = "/users/ebrovski/Desktop/card";
String files;
File folder = new File(path);
Image[] images = new Image[100];
File[] listOfFiles = folder.listFiles();
int j = 0;
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].getName().endsWith("png")) {
System.out.println(listOfFiles[i].getAbsolutePath());

files = "file:" + listOfFiles[i].getAbsolutePath();
System.out.println(files);

images[j] = new Image(files, 200, 200, true, true);

j++;
}
}

for(int k = 0; k < deck.size(); k++) {
imager.add(new ImageView(images[deck.get(k)]));

}
}



public void loadDeck() {
deck = new ArrayList<Integer>();
for(int i =0; i < 52; i++) {
deck.add(i);
}
Collections.shuffle(deck);
}


//Player Panes
class P1Pane extends Pane {
public P1Pane(String player) {
Label playerTitle = new Label(player);
getChildren().add(playerTitle);
setStyle("-fx-border-color: black");
//setStyle("-fx-background-color: green");
setPadding(new Insets(75, 1, 1, 1));
getChildren().add(imager.get(10));

}
}

class P2Pane extends Pane {
public P2Pane(String player) {
Label playerTitle = new Label(player);
getChildren().add(playerTitle);
setStyle("-fx-border-color: black");
//setStyle("-fx-background-color: green");
setPadding(new Insets(75, 1, 1, 1));
getChildren().add(imager.get(20));
}
}
class P3Pane extends Pane {
public P3Pane(String player) {
Label playerTitle = new Label(player);
getChildren().add(playerTitle);
setStyle("-fx-border-color: black");
//setStyle("-fx-background-color: green");
setPadding(new Insets(75, 3, 3, 3));
getChildren().add(imager.get(40));

}
}

class P4Pane extends Pane {
public P4Pane(String player) {
Label playerTitle = new Label(player);
getChildren().add(playerTitle);
setStyle("-fx-border-color: black");
//setStyle("-fx-background-color: green");
setPadding(new Insets(75, 3, 3, 3));
getChildren().add(imager.get(30));
}
}


//Deck Pane
class DeckPane extends Pane {
public DeckPane(String deckName) {
Label deckTitle = new Label(deckName);
getChildren().add(deckTitle);

setStyle("-fx-border-color: black");
//setStyle("-fx-background-color: forestgreen");
setPadding(new Insets(75, 3, 3, 3));

}
}
/*
public enum Suit {

SPADES(Color.BLACK, "\u2660"),
HEARTS(Color.RED, "\u2665"),
CLUBS(Color.BLACK, "\u2663"),
DIAMONDS(Color.RED, "\u2666"),
;

Color color;
String name;

Suit( Color color, String name) {
this.color = color;
this.name = name;
}

public Color getColor() {
return color;
}

public String getName() {
return name;
}
}

public enum Rank {

ACE( "A"),
_2( "2"),
_3("3"),
_4("4"),
_5("5"),
_6("6"),
_7("7"),
_8("8"),
_9("9"),
_10("10"),
JACK("J"),
QUEEN("Q"),
KING("K")
;

String name;

Rank( String name) {
this.name = name;
}

public String getName() {
return name;
}
}

class Card extends Pane {



Node frontFace;
Node backFace;

boolean isFaceUp;

double w = 60;
double h = 90;

Suit suit;
Rank rank;

public Card( Suit suit, Rank rank) {

this.suit = suit;
this.rank = rank;

Image image = new Image("http://ift.tt/1ONNMUC");
ImageView iv1 = new ImageView();
iv1.setImage(image);
ImageView iv2 = new ImageView();
iv2.setImage(image);
frontFace = iv1;
backFace = iv2;

getChildren().addAll( frontFace, backFace);

setFaceDown();
}

public void setFaceUp() {

frontFace.setVisible(true);
backFace.setVisible(false);

isFaceUp = true;
}

public void setFaceDown() {

frontFace.setVisible(false);
backFace.setVisible(true);

isFaceUp = false;
}

public boolean isFaceUp() {
return isFaceUp;
}

public Suit getSuit() {
return suit;
}

public Rank getRank() {
return rank;
}


public String toString() {
return suit + " " + rank;
}
*/

}

Aucun commentaire:

Enregistrer un commentaire