
-----------------------------------
yanko
Tue Jan 20, 2009 3:44 pm

AI in Java for a card game
-----------------------------------
Hey guys, i am still in process of learning java...but this forum rocks, and canada is awesome lol


i kinda need help, We(one of my friends and I) are working on a game called Dajm" you want't find any info about it on the net" we have the hole game working but we Don't know how to make an AI. If you have time and if you want to help us , it would be apritiated ... oh and we are planing to realest it on sourceForge. thanks in advence!!! I could provid  the source code and rules !!!!

-----------------------------------
DemonWasp
Tue Jan 20, 2009 4:01 pm

RE:AI in Java for a card game
-----------------------------------
What does your code look like? We don't necessarily need to see your code, but an idea of what objects you have, what the flow-of-control is like, an similar high-level information can help us point you in the right way.

If you are going to post code, use [ syntax = "java" ] [ / syntax ] , minus the spaces, to highlight your code properly.

-----------------------------------
yanko
Tue Jan 20, 2009 5:09 pm

Re: AI in Java for a card game
-----------------------------------
This is the card class

 

//////===========================================================//////
//////                         DAJM (Dime)                       //////
//////-----------------------------------------------------------//////
//////                         Card.java                         //////
//////                  Version 0.41 Pre-Alpha                   //////
//////                  January 11, 2009 Build                   //////
//////-----------------------------------------------------------//////
//////      Copyright 2008-2009 Drew Burden, Yanko Petrovic      //////
//////-----------------------------------------------------------//////
//////   This file is part of Dajm.                              //////
//////                                                           //////
//////   Dajm is free software: you can redistribute it and/or   //////
//////   modify it under the terms of the GNU General Public     //////
//////   License as published by the Free Software Foundation,   //////
//////   either version 3 of the License, or (at your option)    //////
//////   any later version.                                      //////
//////                                                           //////
//////   Dajm is distributed in the hope that it will be         //////
//////   useful, but WITHOUT ANY WARRANTY; without even the      //////
//////   implied warranty of MERCHANTABILITY or FITNESS FOR A    //////
//////   PARTICULAR PURPOSE.  See the GNU General Public         //////
//////   License for more details.                               //////
//////                                                           //////
//////   You should have received a copy of the GNU General      //////
//////   Public License along with Dajm.  If not, see            //////
//////   .                         //////
//////===========================================================//////

// Best viewed in Notepad++

import java.awt.*;
import javax.swing.*;

/////////////////////////////////////////////////////////////////////////// Card
class Card {
    //=================================================================== fields
    private ImageIcon 	_image;
	private int 		_suit;
	private int 		_face;
	private boolean 	_moveable;
	private boolean 	_display;
    private int			_x;
    private int 		_y;
    
    //============================================================== constructor
    public Card(ImageIcon image, int suit, int face, boolean moveable, boolean display) {
        _image		= image;
		_suit		= suit;
		_face		= face;
		_moveable 	= moveable;
		_display 	= display;
    }
	
	//=================================================================== setImg
    public void setImg(ImageIcon image) {
        _image = image;
    }
    
    //=================================================================== moveTo
    public void moveTo(int x, int y) {
        _x = x;
        _y = y;
    }
    
    //================================================================= contains
    public boolean contains(int x, int y) {
        return (x > _x && x < (_x + getWidth()) && 
                y > _y && y < (_y + getHeight()));
    }
    
    //================================================================= getWidth
    public int getWidth() {
        return _image.getIconWidth();
    }
    
    //================================================================ getHeight
    public int getHeight() {
        return _image.getIconHeight();
    }
    
    //===================================================================== getX
    public int getX() {
        return _x;
    }
    
    //===================================================================== getY
    public int getY() {
        return _y;
    }
	
	//===================================================================== getSuit
	public int getSuit() {
        return _suit;
    }
	
	//===================================================================== getFace
	public int getFace() {
        return _face;
    }
	
	//===================================================================== setMoveable
	public void setMoveable(boolean moveable) {
        _moveable = moveable;
    }
	
	
	//===================================================================== isMoveable
	public boolean isMoveable() {
        return _moveable;
    }
	
	//===================================================================== shouldDisplay
	public boolean shouldDisplay() {
        return _display;
    }
    
    //===================================================================== draw
    public void draw(Graphics g, Component c) {
        _image.paintIcon(c, g, _x, _y);
    }
}


////////////////////////////
//////////  EOF   //////////
////////////////////////////




Edit: Code tag
[syntax="java"]code here[/syntax]

-----------------------------------
yanko
Tue Jan 20, 2009 6:45 pm

Re: AI in Java for a card game
-----------------------------------
here is the cardTable class




//////===========================================================//////
//////                         DAJM (Dime)                       //////
//////-----------------------------------------------------------//////
//////                      CardTable.java                       //////
//////                  Version 0.41 Pre-Alpha                   //////
//////                  January 11, 2009 Build                   //////
//////-----------------------------------------------------------//////
//////      Copyright 2008-2009 Drew Burden, Yanko Petrovic      //////
//////-----------------------------------------------------------//////
//////   This file is part of Dajm.                              //////
//////                                                           //////
//////   Dajm is free software: you can redistribute it and/or   //////
//////   modify it under the terms of the GNU General Public     //////
//////   License as published by the Free Software Foundation,   //////
//////   either version 3 of the License, or (at your option)    //////
//////   any later version.                                      //////
//////                                                           //////
//////   Dajm is distributed in the hope that it will be         //////
//////   useful, but WITHOUT ANY WARRANTY; without even the      //////
//////   implied warranty of MERCHANTABILITY or FITNESS FOR A    //////
//////   PARTICULAR PURPOSE.  See the GNU General Public         //////
//////   License for more details.                               //////
//////                                                           //////
//////   You should have received a copy of the GNU General      //////
//////   Public License along with Dajm.  If not, see            //////
//////   .                         //////
//////===========================================================//////

// Best viewed in Notepad++

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.util.Random;

////////////////////////////////////////////////////////////////////// CardTable
public class CardTable extends JComponent implements MouseListener, MouseMotionListener {
    
    //================================================================ constants
    private final Color BACK = Color.BLACK;		// Used for redraw.
    private final int   TABLE_SIZE = 550;		// Window width and height. *** CAUTION *** Changing this will screw up some stuff!
    

	
    //=================================================================== variables
    //------- Initial image coords.
    private int _initX     = 0;   				// x coord - set from drag.
    private int _initY     = 0;   				// y coord - set from drag.
    
	
    //------- Position in image of mouse press to make dragging look better
    private int _dragFromX = 0;					// Displacement inside image of mouse press.
	private int _dragFromY = 0;					// Displacement inside image of mouse press.

		
	//------- Array location variables
	private int _cardPlace = 0;					// Place in the array of the current card.
	private int _discardPlace = 0;				// Place in the array of the current discard.
	
		
	//------- X, Y Positions
	private int n = 0;         					// Which card in the array (array position).
	private int xPos = 0, yPos = 0;				// Where it should be placed initially.
	private int _oldX = 0, _oldY = 0;			// Used for snapping the card back into place.
	private int _X1, _X2, _Y1, _Y2;				// For placing the rearranged cards.
	
	
	//------- Deck Initialization Variables
	private String suits = "shdc";				// Holds all the suits in a string (each one char long).
	private String faces = "a23456789tjqk";		// Holds all the face values in a string (each one char long).
	private int _suitval;						// Used to plug into the Card creator for returning a value later (checkDajm method).
	private int _faceval;						// Used to plug into the Card creator for returning a value later (checkDajm method).
	private String _prompt = "new";				// Which text do we display at the bottom of the screen? It's a string to limit confusion.
	private boolean _chip = false;				// Is this the first time for the current round that we are painting the dealer chip?
	private int _round = 3;						// Not the round we are in, but rather the amount of cards that each player has...
												// (it starts at 3 since the dealing function adds one to the variable right at the start.
    
	//------- Setup Various Decks
    private Card

-----------------------------------
yanko
Tue Jan 20, 2009 6:48 pm

Re: AI in Java for a card game
-----------------------------------
here is the main()  Dajm class



//////===========================================================//////
//////                         DAJM (Dime)                       //////
//////-----------------------------------------------------------//////
//////                         Dajm.java                         //////
//////                  Version 0.41 Pre-Alpha                   //////
//////                  January 11, 2009 Build                   //////
//////-----------------------------------------------------------//////
//////      Copyright 2008-2009 Drew Burden, Yanko Petrovic      //////
//////-----------------------------------------------------------//////
//////   This file is part of Dajm.                              //////
//////                                                           //////
//////   Dajm is free software: you can redistribute it and/or   //////
//////   modify it under the terms of the GNU General Public     //////
//////   License as published by the Free Software Foundation,   //////
//////   either version 3 of the License, or (at your option)    //////
//////   any later version.                                      //////
//////                                                           //////
//////   Dajm is distributed in the hope that it will be         //////
//////   useful, but WITHOUT ANY WARRANTY; without even the      //////
//////   implied warranty of MERCHANTABILITY or FITNESS FOR A    //////
//////   PARTICULAR PURPOSE.  See the GNU General Public         //////
//////   License for more details.                               //////
//////                                                           //////
//////   You should have received a copy of the GNU General      //////
//////   Public License along with Dajm.  If not, see            //////
//////   .                         //////
//////===========================================================//////

// Best viewed in Notepad++


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

////////////////////////////////////////////////////////////// class Dajm
class Dajm extends JFrame {
	
	public static String _progVersion = "Pre-Alpha";			// Alpha? Beta? RC1? RC2? etc...
	public static double _buildVersion = 0.41;					// Basic version
	public static String _buildDate = "January 11, 2009";		// Build date
	public static CardTable table;
	public static JFrame _about;								// About dialog
	public static JFrame _main;									// Main window
	public static JMenuBar _menuBar;							// The menu bar itself
	public static JMenu _menu;									// The actual menus (reused)
	public static JMenuItem _newMenu;							// The new menu item
	public static JMenuItem _exitMenu;							// The exit menu item
	public static JMenuItem _aboutMenu;							// The about menu item
	public static ClassLoader cldr = Dajm.class.getClassLoader();	// ClassLoader is used to get images from a jar file.
	public static URL imageURL;									// Variable to plug into the class loader.
	
    //===================================================================== main
    public static void main(String

-----------------------------------
yanko
Wed Jan 21, 2009 8:32 am

RE:AI in Java for a card game
-----------------------------------
no one has an idea?

-----------------------------------
DemonWasp
Wed Jan 21, 2009 9:09 am

RE:AI in Java for a card game
-----------------------------------
Keep in mind that most of us are only online during work / school hours.

Your CardTable class appears to be handling both display concerns and game-rules concerns. My brief scan of the code makes it seem like your human player does all their actions by interacting with that, which doesn't lend itself to playing as an AI.

To add an AI, you should probably think about turning user interactions into some kind of Action, which is then immediately issued. The AI could just create the actions itself, on its turn.

I could give a more in-depth discussion if I knew how the game was played.

-----------------------------------
yanko
Wed Jan 21, 2009 9:03 pm

Re: AI in Java for a card game
-----------------------------------
While i appreciate your suggestion, 
by doing what you have suggested, it 
will only serve as a convenience, but not really 
a necessity when it comes to creating our AI.

-----------------------------------
A.J
Wed Jan 21, 2009 10:32 pm

Re: AI in Java for a card game
-----------------------------------
First of all yanko, please attach your code as an attachment as opposed to posting all of it........

As for making an AI, you should keep in mind 2 main things necessary in building an AI of any sort:

1) Your Artificial Intelligence's brain (i.e. the class in charge of computing the AI's moves) must first be structured.

2) You must then decide what algorithm is to be used by the AI to compute the best possible move given a state of the game. This can either be merely pruning every possible move the AI can make (if this is the case, my suggestion is alpha-beta pruning), or it can be following a specific strategy (like for instance in a game like NIM).

After doing this, then you should start structuring other methods that will aid in the thinking/pruning and the performing of the move in the game by the AI

If you can provide me with a brief description of the game, I'll be glad to help in finding an algorithm that suits this particular game.

-----------------------------------
yanko
Thu Jan 22, 2009 6:00 pm

RE:AI in Java for a card game
-----------------------------------
If you want me to repost the src file let me know?  and here is the rules for the game.....                         

 Dajm(dime)
                           
1.  Game starts with 2 players."Players .vs.Pc"

2.  The program deals the cards to Pc and to Player
    until the Pc and player have each 4 cards, also it
    leaves one card uncovered on the discard pile.
    Since Pc dealt, the player plays first.
   
3.  4 cards now has Player (fours of any suit are the wild cards
    for the round). Next round is 5 cards. Then the
    fives of any suit are the wild cards. The rounds go all the way 
    up to 13 cards (where the kings are wild).

4.  Player has to put together at least 3 cards of the same
    face value (depending on what round you're in), or they 
    can get a straight with all of the same suit.
    ex.; H8 H9 H10 -> Ok! 
         H8 A4 H10 -> ok! (4 of any kind is a wild card)
         H8 D9 H10 -> not ok.

5.  The player and Pc can draw from the deck or from the discard pile
    and then they must discard. For example:
         On the round of 4 cards, you draw from the discard pile (one
         card is turned up onto the discard at the beginning) and you
         now have 5 cards. You must now discard to bring your cards 
         back down to 4 cards.

6.  Lastly, you can call Dajm only before you discard since discarding 
    signifies the end of your turn.

-----------------------------------
A.J
Thu Jan 22, 2009 7:52 pm

Re: AI in Java for a card game
-----------------------------------
this is (in my opinion) pretty easy to come up with an algorithm.

all u have to do is take the card on the discard pile and search and see how 'valuable' it is to your current hand

if yes, then take it, if not then use EV (expected value) to calculate the probability of getting a card from the deck that'll help you

so, make an evaluation function that returns how 'valuable' a card given the card you have in you hand

that should make thing a lot easier

so start off by making an evaluation function (which should probably be the most 'challenging' part for you)

If I make am of no use AT ALL to you (which I can be at times....), please ignore me.......

however, if you do need help, it'll be my pleasure in continuing this small 'tutorial' :D

-----------------------------------
yanko
Thu Jan 22, 2009 9:58 pm

RE:AI in Java for a card game
-----------------------------------
but how??
how am I supposed to check the player's or computer's hand to see if they have dajm?

and please , put it in a code like way!!??

-----------------------------------
A.J
Thu Jan 22, 2009 10:30 pm

Re: AI in Java for a card game
-----------------------------------
I am not going to code it out for you........
it is pretty simple, just store the cards in an array (for both the AI and the player alike) and just manually check.

don't check for a Dajm, but rather evaluate your hand and check how valuable a card can be to a particular hand by means of checking how much closer to a dajm you are.

I am saying all of this under the impression that a Dajm is like having some sort of poker hand (like straight or something)

even if i am mistaken, this approach should work

-----------------------------------
yanko
Fri Jan 23, 2009 11:51 am

RE:AI in Java for a card game
-----------------------------------
Well, thankx for your help, we ll try to...
and if anyone can really help us , please do ! thankx

-----------------------------------
A.J
Fri Jan 23, 2009 2:27 pm

Re: AI in Java for a card game
-----------------------------------
k, I don't mind helping you, it is just that I have exams now......
I'll try helping you after that though :D

-----------------------------------
yanko
Fri Jan 23, 2009 6:25 pm

RE:AI in Java for a card game
-----------------------------------
ok.. thankx

-----------------------------------
yanko
Tue Apr 07, 2009 5:43 pm

RE:AI in Java for a card game
-----------------------------------
So A.J.   how are the exams?
