lundi 6 novembre 2017

How to test if a certain item in an ArrayList

Right now I'm doing an if-statement that looks like this:

if(Game.nflteams.subList(0, 1).equals("Arizona Cardinals"))
    // do something like draw their logo to a made JFrame

Every time that Game.nflteams.subList(0, 1) does equal "Arizona Cardinals", or one of the 3 teams I've done this with so far, nothing draws to the screen.
All of my classes are below:
My main class:

public class Football {

    public static void main(String[] args) {
        Game game = new Game();
        game.game();
        GameII gamei = new GameII("Football Sim", 1000, 500);
        gamei.start();
    }

}

My Game class:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Game {

    private static ArrayList<String> teams = new ArrayList<String>();

    private Random random = new Random();

    public void game() {
        teams.add("New England Patriots (0 - 1)");
        teams.add("Dallas Cowboys (0 - 1)");
        teams.add("Oakland Raiders (1 - 0)");
        teams.add("Philadelphia Eagles (0 - 1)");
        teams.add("New York Giants (0 - 1)");
        teams.add("Seattle Seahawks (1 - 0)");
        teams.add("Pittsburgh Steelers (1 - 0)");
        teams.add("Green Bay Packers (1 - 0)");
        teams.add("Denver Broncos (0 - 1)");
        teams.add("San Fransisco 49ers (0 - 1)");
        teams.add("Chicago Bears (1 - 0)");
        teams.add("Minnesota Vikings (0 - 1)");
        teams.add("Carolina Panthers (1 - 0)");
        teams.add("Cleveland Browns (0 - 1)");
        teams.add("Los Angeles Rams (1 - 0)");
        teams.add("Kansas City Cheifs (0 - 1)");
        teams.add("Washington Redskins (1 - 0)");
        teams.add("Atlanta Falcons (1 - 0)");
        teams.add("Baltimore Ravens (1 - 0)");
        teams.add("Houston Texans (0 - 1)");
        teams.add("Detroit Lions (1 - 0)");
        teams.add("New York Jets (0 - 1)");
        teams.add("Los Angeles Chargers (0 - 1)");
        teams.add("Arizona Cardinals (0 - 1)");
        teams.add("Buffalo Bills (1 - 0)");
        teams.add("New Orleans Saints (1 - 0)");
        teams.add("Miami Dolphins (0 - 1)");
        teams.add("Jacksonville Jaguars (1 - 0)");
        teams.add("Cincinatti Bengals (0 - 1)");
        teams.add("Tampa Bay Buccaneers (1 - 0)");
        teams.add("Indianapolis Colts (0 - 1)");
        teams.add("Tennessee Titans (1 - 0)");
        Collections.shuffle(teams);
        int t2touch = (random.nextInt(5) + 1) * 7;
        int t2fielgoal = (random.nextInt(5) + 1) * 3;
        int t2score = t2touch + t2fielgoal;
        int t1touch = random.nextInt(5) * 7;
        int t1fielgoal = random.nextInt(5) * 3;
        int t1score = t1touch + t1fielgoal;
        System.out.println(teams.subList(0, 1) + " (away) " + " vs " + teams.subList(1, 2) + " (home)");
        if(t1score > t2score) {
            System.out.println("The " + teams.subList(0, 1) + " win " 
                    + t1score + " (" + t1touch / 7 + " touchdowns, " + t1fielgoal / 3 + " field goals)" + 
                    " to "
                    + t2score + " (" + t2touch / 7 + " touchdowns, " + t2fielgoal / 3 + " field goals)" + "!");
        } else if(t2score > t1score) {
            System.out.println("The " + teams.subList(1, 2) + " win " 
                    + t2score + " (" + t2touch / 7 + " touchdowns, " + t2fielgoal / 3 + " field goals)" + 
                    " to "
                    + t1score + " (" + t1touch / 7 + " touchdowns, " + t1fielgoal / 3 + " field goals)" + "!");
        } else {
            System.out.println("It's a tie. "
                    + teams.subList(0, 1) + " " + t1score + " (" + t1touch / 7 + " touchdowns, " + t1fielgoal / 3 + " field goals" + 
                    ") to "
                    + teams.subList(1, 2) + " " + t2score + " (" + t2touch / 7 + " touchdowns, " + t2fielgoal / 3 + " field goals");
        }
    }

    public static ArrayList<String> getTeams() {
        return teams;
    }

}

My GameII class:

import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import foot.display.Display;
import foot.gfx.Assets;

public class GameII implements Runnable {

    private Display display;
    public int width, height;
    public String title;

    private boolean running = false;
    private Thread thread;

    private BufferStrategy bs;
    private Graphics g;

    public GameII(String title, int width, int height){
        this.width = width;
        this.height = height;
        this.title = title;
    }

    private void init(){
        display = new Display(title, width, height);
        Assets.init();
    }

    private void tick(){

    }

    private void render(){
        bs = display.getCanvas().getBufferStrategy();
        if(bs == null){
            display.getCanvas().createBufferStrategy(3);
            return;
        }
        g = bs.getDrawGraphics();
        //Clear Screen
        g.clearRect(0, 0, width, height);
        //Draw Here!

        // team 1's
        if(Game.getTeams().subList(0, 1).equals("Arizona Cardinals (0 - 1)"))
            g.drawImage(Assets.cardinals, 100, 100, null);
        if(Game.getTeams().subList(0, 1).equals("Jacksonville Jaguars (1 - 0)))
            g.drawImage(Assets.jaguars, 100, 100, null);
        if(Game.getTeams().subList(0, 1).equals("New England Patriots (0 - 1)"))
            g.drawImage(Assets.patriots, 100, 100, null);

        // team 2's
        if(Game.getTeams().subList(1, 2).equals("Arizona Cardinals (0 - 1)"))
            g.drawImage(Assets.cardinals, 400, 400, null);
        if(Game.getTeams().subList(1, 2).equals("Jacksonville Jaguars (1 - 0)"))
            g.drawImage(Assets.jaguars, 400, 400, null);
        if(Game.getTeams().subList(1, 2).equals("New England Patriots (0 - 1)"))
            g.drawImage(Assets.patriots, 400, 400, null);

        //End Drawing!
        bs.show();
        g.dispose();
    }

    public void run(){

        init();

        while(running){
            tick();
            render();
        }

        stop();

    }

    public synchronized void start(){
        if(running)
            return;
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public synchronized void stop(){
        if(!running)
            return;
        running = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

My Assets class:

import java.awt.image.BufferedImage;

public class Assets {

    private static final int width = 134, height = 100;

    public static BufferedImage cardinals, jaguars, patriots, saints, browns, broncos;

    public static void init(){
        SpriteSheet sheet = new SpriteSheet(ImageLoader.loadImage("/textures/every nfl team.jpg"));

        cardinals = sheet.crop(0, 0, width, height);
        jaguars = sheet.crop(width, 0, width, height);
        patriots = sheet.crop(width * 2, 0, width, height);
        saints = sheet.crop(width * 4, 0, width, height);
        browns = sheet.crop(width * 5, 0, width, height);
        broncos = sheet.crop(width * 6, 0, width, height);
    }
}

My SpriteSheet class:

import java.awt.image.BufferedImage;

public class SpriteSheet {

    private BufferedImage sheet;

    public SpriteSheet(BufferedImage sheet){
        this.sheet = sheet;
    }

    public BufferedImage crop(int x, int y, int width, int height){
        return sheet.getSubimage(x, y, width, height);
    }

}

My ImageLoader class:

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageLoader {

    public static BufferedImage loadImage(String path){
        try {
            return ImageIO.read(ImageLoader.class.getResource(path));
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        return null;
    }

}

and my Display class:

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Display {

    private JFrame frame;
    private Canvas canvas;

    private String title;
    private int width, height;

    public Display(String title, int width, int height){
        this.title = title;
        this.width = width;
        this.height = height;

        createDisplay();
    }

    private void createDisplay(){
        frame = new JFrame(title);
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        canvas = new Canvas();
        canvas.setPreferredSize(new Dimension(width, height));
        canvas.setMaximumSize(new Dimension(width, height));
        canvas.setMinimumSize(new Dimension(width, height));

        frame.add(canvas);
        frame.pack();
    }

    public Canvas getCanvas() {
        return canvas;
    }

}

These are all of my classes and should be enough to reproduce the problem.
Just to say:
In theory/logically, my formula should work.
If you think about it, my steps are completely logical:
1) Make an ArrayList of Strings and add all of the NFL's teams' names to it
2) Shuffle the ArrayList
3) Randomly choose two of those 'teams' to 'play'
4) Test if a specific team is chosen
5) If that is true, draw the logo of that team to the previously made JFrame

Aucun commentaire:

Enregistrer un commentaire