import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
import java.awt.;
import javax.swing.;
public class imgLoad extends JFrame { public static void main(String[] args) { new imgLoad(); }
Image image;
String imageFile = "images/fish.jpeg";
imgLoad() {
this.setTitle("Background image");
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setSize(600,400);
JPanel panel = new GrPanel();
panel.setBackground(Color.BLUE); //overwritten by the background image
JLabel lblStatus = new JLabel("nothing to report");
lblStatus.setBackground(Color.GRAY.brighter());
lblStatus.setOpaque(true);
//Choose one of the following two:
loadImage();
//loadIcon();
panel.add(lblStatus);
this.add(lblStatus,BorderLayout.NORTH);
this.add(panel,BorderLayout.CENTER);
this.validate();
this.setVisible(true);
}
void loadIcon() {
java.net.URL imageURL = imgLoad.class.getResource(imageFile);
if (imageURL != null) {
ImageIcon icon = new ImageIcon(imageURL);
image = icon.getImage();
} else { System.out.println("null icon"); }
}
void loadImage() {
ImageIcon icon = new ImageIcon(getClass().getResource(imageFile));
image = icon.getImage();
if (image == null) System.out.println("null image");
}
private class GrPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
//draw big image to fill up the screen
if (image != null) g.drawImage(image, 0, 0, (int)(getWidth() * 0.95), (int)(getHeight() * 0.95), Color.RED, this);
//draw the smaller image
if (image != null) g.drawImage(image, 0, 0, 100, 100, this);
}
}
}
class GoFishTrial2 { static final Random rng = new Random(); static private ArrayList<Card> cards; static public Player[] Players;
public static Card draw()
{
return cards.remove(rng.nextInt(cards.size()));
}
public static int deckSize()
{
return cards.size();
}
public static void main(String[] args)
{
System.out.println(" Sounds of sorrow and lament echo throughout the plains.");
System.out.println(" Tensions are at the edges as imminent doom is present.");
System.out.println(" General Hades and you are facing off in an epic battle for the ages.");
System.out.println(" Your nation of Pagnotti is severly dependent on the victory here.");
System.out.println(" This battle determines who receives the port of Dumesville.");
cards = new ArrayList<Card>();
for(int imill=0;imill<4;imill++)
for(Card weapons: Card.values())
cards.add(weapons);
Player Us = new HumanPlayer();
Player Hades = new AIPlayer();
Players = new Player[] {Us, Hades};
while(Players[0].getNumBooks() + Players[1].getNumBooks() < 13)
{
Players[0].haveTurn();
System.out.println("----------");
Players[1].haveTurn();
System.out.println("----------");
}
int yScore = Players[0].getNumBooks(); int aiScore = Players[1].getNumBooks();
if (yScore > aiScore)
System.out.println("Congratulations, you have defended Pagnotti's honor!"+ yScore + " to "+ aiScore +"!");
else if (aiScore > yScore)
System.out.println("General Hades has won! Pagnotti is devastated by your inability to win."+ yScore + " to "+ aiScore +"...");
else
System.out.println("The war is at the edges, and both sides have decided on a draw "+yScore+" each!");
}
}
enum Card { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING; }
abstract class Player { protected ArrayList<Card> hand = new ArrayList<Card>(); private int numBooks;
public Player()
{
for(int imill=0;imill<8;imill++)
fish();
}
public boolean hasGiven(Card cType)
{
return hand.contains(cType);
}
public ArrayList<Card> giveAll(Card cType)
{
ArrayList<Card> x = new ArrayList<Card>();
for(int imill=0;imill<hand.size();imill++)
if (hand.get(imill) == cType)
x.add(hand.get(imill));
for(int cancun=0;cancun<x.size();cancun++)
hand.remove(cType);
return x;
}
protected boolean askFor(Card cType)
{
int tmp = 0;
if (this instanceof HumanPlayer)
tmp = 1;
Player other = GoFishTrial2.Players[tmp];
//Used for the computer's strategy//
if (tmp==1)
((AIPlayer) other).queries.add(cType);
if (other.hasGiven(cType))
{
for(Card c: other.giveAll(cType))
hand.add(c);
return true;
}
else
{
return false;
}
}
protected void fish()
{
if (GoFishTrial2.deckSize() > 0)
hand.add(GoFishTrial2.draw());
else
System.out.println("That is impossible since your inevntory is depleted!");
}
public int getNumBooks()
{
return numBooks;
}
protected Card checkForBooks()
{
for(Card c: hand)
{
int num = 0;
for(Card d: hand)
if (c == d)
num++;
if (num == 4)
{
for(int imill=0;imill<4;imill++)
hand.remove(c);
numBooks++;
return c;
}
}
return null;
}
public abstract void haveTurn();
}
class HumanPlayer extends Player { public void haveTurn() { Scanner scn = new Scanner(System.in); boolean playing = true; do{ Card book = checkForBooks(); if(book != null) System.out.println("You have successfully won a book of " + book + "s!");
if (hand.size() == 0)
{
System.out.print("Your inventory is depleted, you must "); //Go fish
break;
}
else
{
System.out.println("Your current weaponry inventory: ");
for(Card c: hand)
System.out.print(c + " ");
System.out.println();
}
System.out.println("Ask General Hades for what weapon?");
Card req;
try{
req = Card.valueOf(scn.next().toUpperCase());
}
catch(IllegalArgumentException e){ //If what you said is not in Card
System.out.println("You don't have anymore of this weapon!. Try again:");
continue;
}
if(!hand.contains(req))
{
System.out.println("You can't ask for inventory that you dont have! Try again:");
continue;
}
System.out.println("You ask for a " + req);
playing = askFor(req); //If you get card(s), askFor returns true and loops
} while(playing);
System.out.println("Go fish!");
fish();
}
}
class AIPlayer extends Player { public ArrayList<Card> queries = new ArrayList<Card>(); private int age = 0;
public void haveTurn()
{
boolean playing;
do{
Card book = checkForBooks();
if(book != null)
System.out.println("General Hades won a book of " + book + "s...");
if (hand.size() == 0)
{
System.out.print("General Hades inventory is depleted!");
break;
}
Card req = aiMagic();
System.out.println("General Hades is confident he can win. He wants a: " + req);
playing = askFor(req);
age++;
} while(playing);
System.out.println("General Hades goes fishing.");
fish();
}
//The AI's strategy is to first ask for things you asked for, since you have those things.
//Failing that, it chooses at random.
private Card aiMagic()
{
if (age>2)
{
queries.remove(queries.size()-1); //gets rid of oldest entry so it won't
age=0; //get stuck asking for the same thing
}
for(int imill=queries.size()-1; imill>-1; imill--) //(maybe a queue would have been better?)
if (hand.contains(queries.get(imill)))
{
return queries.remove(imill); //once it extracts a card from you, it will stop
} //asking for that card, until YOU ask for it again.
return hand.get(GoFishTrial2.rng.nextInt(hand.size()));
}
}