Go Fish/Java: Difference between revisions

no edit summary
(Creation)
 
No edit summary
Line 9:
<lang Java>import java.util.ArrayList; //Java's Collection Framework is really what is
import java.util.Scanner; //powering the whole game: don't underestimate it.
import java.util.Random;
 
public class GoFish
{
static final Random rng = new Random();
static private ArrayList<Card> cards;
static public Player[] Players;
Line 17 ⟶ 19:
public static Card draw()
{
return cards.remove((int)(Mathrng.randomnextInt()*cards.size()));
}
 
Line 61 ⟶ 63:
abstract class Player
{
protected ArrayList<Card> hand = new ArrayList<Card>();
private int numBooks;
 
public Player()
{
hand = new ArrayList<Card>();
for(int i=0;i<8;i++)
fish();
Line 154 ⟶ 155:
{
Scanner scn = new Scanner(System.in);
Card req, book; boolean playing = true;
while(playing)do{
Card book = checkForBooks();
{
book = checkForBooks();
if(book != null)
System.out.println("You got a book of " + book + "s!");
Line 176:
System.out.println("Ask opponent for what card?");
 
hand = new ArrayList< Card>() req;
try{
req = Card.valueOf(scn.next().toUpperCase());
Line 192 ⟶ 193:
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();
Line 200 ⟶ 201:
class AIPlayer extends Player
{
public ArrayList<Card> queries = new ArrayList<Card>();
private int age = 0;
 
public AIPlayer()
{
queries = new ArrayList<Card>();
age = 0;
}
 
public void haveTurn()
{
Card req, book; boolean playing = true;
while (playing)do{
Card book = checkForBooks();
{
book = checkForBooks();
if(book != null)
System.out.println("Your opponent got a book of " + book + "s...");
Line 222 ⟶ 216:
break;
}
Card req = aiMagic();
System.out.println("Your opponent asks for cards by the name of " + req);
playing = askFor(req);
age++;
} while(playing);
System.out.println("Your opponent goes fishing.");
fish();
Line 245 ⟶ 239:
return queries.remove(i); //once it extracts a card from you, it will stop
} //asking for that card, until YOU ask for it again.
return hand.get((int)(MathGoFish.rng.randomnextInt()*hand.size()));
}
}</lang>
Anonymous user