You Are Here:

Community: Wiki

This page was last modified on 27 September 2009, at 07:37.

A simple card game

From Forum Nokia Wiki

Reviewer Approved   

Contents

Overview

This is a very simple card game in Java ME. On screen there are two cards facing down. Users bet some amount of his bank and click on the show button. If your card is greater than computers card, you will win double amount or you loose your amount. And take another two cards.

Source Code

cardcanvas.java

import java.io.IOException;
import java.util.Random;
 
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
 
 
public class cardcanvas extends Canvas implements CommandListener {
 
String[] card=new String[]{"clubs-a-75.png","clubs-k-75.png","clubs-q-75.png","clubs-j-75.png","clubs-2-75.png","clubs-3-75.png","clubs-4-75.png","clubs-5-75.png","clubs-6-75.png","clubs-7-75.png","clubs-8-75.png","clubs-9-75.png","clubs-10-75.png"
,"diamonds-a-75.png","diamonds-k-75.png","diamonds-q-75.png","diamonds-j-75.png","diamonds-2-75.png","diamonds-3-75.png","diamonds-4-75.png","diamonds-5-75.png","diamonds-6-75.png","diamonds-7-75.png","diamonds-8-75.png","diamonds-9-75.png","diamonds-10-75.png"
,"hearts-a-75.png","hearts-k-75.png","hearts-q-75.png","hearts-j-75.png","hearts-2-75.png","hearts-3-75.png","hearts-4-75.png","hearts-5-75.png","hearts-6-75.png","hearts-7-75.png","hearts-8-75.png","hearts-9-75.png","hearts-10-75.png"
,"spades-a-75.png","spades-k-75.png","spades-q-75.png","spades-j-75.png","spades-2-75.png","spades-3-75.png","spades-4-75.png","spades-5-75.png","spades-6-75.png","spades-7-75.png","spades-8-75.png","spades-9-75.png","spades-10-75.png"};
int[] cardrank=new int[]{14,13,12,11,2,3,4,5,6,7,8,9,10,14,13,12,11,2,3,4,5,6,7,8,9,10,14,13,12,11,2,3,4,5,6,7,8,9,10,14,13,12,11,2,3,4,5,6,7,8,9,10};
Image imgcomp=null;
Image imguser=null;
int bet=100;
int ubal=1000;
int cbal=1000;
 
boolean firsttime=true;
boolean bnew=true;
boolean bshow=false;
Command cmdbet=new Command("BET", Command.BACK, 1 );
Command cmdshow=new Command("show", Command.ITEM, 2 );
Command cmdexit=new Command("EXIT", Command.ITEM, 4 );
Command cmdnew=new Command("NEW", Command.ITEM, 3 );
int ucard,ccard;
boolean flag1=true;
boolean flag2=false;
boolean flag3=false;
 
String sUser;
public cardcanvas(String m){
 
sUser = m;
 
this.addCommand(cmdbet);
this.addCommand(cmdshow);
this.addCommand(cmdexit);
this.addCommand(cmdnew);
this.setCommandListener(this);
//System.out.print(m_Midlet);
}
 
public int rancard(){
 
Random generator = new Random();
generator.setSeed(System.currentTimeMillis());
float f = generator.nextFloat();
 
return (int)(f*52.0f)%100;
}
 
protected void paint(Graphics g) {
 
// TODO Auto-generated method stub
g.setColor(0xf0b0b0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
 
if(bnew){
try {
imgcomp = Image.createImage("/suit/back.png");
imguser= Image.createImage("/suit/back.png");
 
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bnew=false;
}
if(bshow){
displaycard();
 
g.drawString("",6 ,115, Graphics.TOP|Graphics.LEFT);
if(ucard>ccard){
g.setColor(0x000000);
g.drawString("u win the game",70 ,150, Graphics.TOP|Graphics.LEFT);
ubal=ubal+2*bet;
repaint();
}
 
if(ucard==ccard){
g.drawString(" ITS A TIE",70 ,150, Graphics.TOP|Graphics.LEFT);
ubal=ubal+bet;
cbal=cbal+bet;
repaint();
}
if(ucard<ccard){
g.setColor(0x000000);
g.drawString(" u lost the game",70 ,150, Graphics.TOP|Graphics.LEFT);
cbal=cbal+2*bet;
repaint();
}
bshow=false;
}
g.drawImage(imgcomp,6,6, Graphics.TOP|Graphics.LEFT);
g.drawImage(imguser,156,6, Graphics.TOP|Graphics.LEFT);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString(sUser,6 ,115, Graphics.TOP|Graphics.LEFT);
g.drawString("COMPUTER ",150 ,115, Graphics.TOP|Graphics.LEFT);
g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED, Font.SIZE_SMALL));
g.drawString(sUser+"'s BANK:$"+ubal,5 ,250, Graphics.TOP|Graphics.LEFT);
g.drawString("COMPUTER BANK:$"+cbal,110 ,250, Graphics.TOP|Graphics.LEFT);
g.drawString(sUser+"'s BET:$"+bet,90 ,200, Graphics.TOP|Graphics.LEFT);
}
 
 
public void displaycard(){
 
int index=rancard();
int index1;
ucard=cardrank[index];
try {
imgcomp = Image.createImage("/suit/"+card[index]);
 
while(true){
index1=rancard();
if(index==index1 && cardrank[index]==cardrank[index1])
continue;
else
break;
 
}
//index=rancard();
ccard=cardrank[index1];
imguser= Image.createImage("/suit/"+card[index1]);
 
} catch (IOException e) {
 
System.out.print("image file not found");
}
//cardmidlet cm=new cardmidlet();
//String name=cm.name();
//g.drawString("image demo",getWidth()/2-10 ,getHeight()/2, Graphics.TOP|Graphics.LEFT);
 
}
 
protected void keyPressed(int keyCode){
if(keyCode==-1){
bet=bet+5;
repaint();
}
if(keyCode==-2){
bet=bet-5;
repaint();
}
//repaint();
}
 
public void commandAction(Command com, Displayable arg1) {
// TODO Auto-generated method stub
if(com==cmdshow && flag2){
bshow=true;
flag2=false;
flag3=true;
repaint();
 
 
}
if(com==cmdnew & flag3){
bnew=true;
flag3=false;
flag1=true;
repaint();
 
}
if(com==cmdbet && flag1){
ubal=ubal-bet;
cbal=cbal-bet;
flag2=true;
flag1=false;
repaint();
}
 
}
 
}

cardmidlet.java

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
 
public class cardmidlet extends MIDlet implements CommandListener {
private Display display;
private Form form;
private List menuList ;
String[] elements=new String[]{"New Game","Credit","Help","About"};
private Command selectCommand;
Alert alert;
private Command fnext=new Command("NEXT",Command.ITEM,1);
private Command fback=new Command("BACK",Command.BACK,1);
private TextField uname;
public String nname;
 
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
 
protected void pauseApp() {
 
 
}
public cardmidlet() {
menuList();
 
}
 
public void menuList(){
 
display = Display.getDisplay(this);
menuList = new List("Menu", List.IMPLICIT, elements, null);
selectCommand=new Command("open",Command.ITEM,1);
menuList.setSelectCommand(selectCommand);
menuList.setCommandListener(this);
 
}
 
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(menuList);
}
 
public void about(){
alert = new Alert("Option Selected", "This game is created by"+"\n"+" vivart pandey.", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.INFO);
display.setCurrent(alert);
}
 
public void newgame(){
display=Display.getDisplay(this);
uname=new TextField("enter your name","",50,TextField.ANY);
form =new Form("new user");
form.append(uname);
form.addCommand(fnext);
form.addCommand(fback);
form.setCommandListener(this);
display.setCurrent(form);
 
}
 
public String name(){
nname=uname.getString();
return nname;
}
 
public void commandAction(Command com, Displayable arg1) {
 
if(com==selectCommand){
if(menuList.getSelectedIndex()==3)
about();
if(menuList.getSelectedIndex()==0)
newgame();
 
}
if(com==fback){
display.setCurrent(menuList);
}
if(com==fnext){
display = Display.getDisplay(this);
cardcanvas cd=new cardcanvas(this.name());
 
display.setCurrent(cd);
}
}
 
}

visit my site http://www.vimviv.com

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fFreeE5fandE5fContributionE5fJME45E5fGamesX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZgamesQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ