×
Namespaces

Variants
Actions
(Difference between revisions)

A simple card game in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Adding missing translation link)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix ArticleMetaData and RevieweApproval)
Line 1: Line 1:
[[Category:Code Examples]][[Category:Java ME]][[Category:Games]]
+
[[Category:Code Snippet]][[Category:Java ME]][[Category:Games]]
 
{{Abstract|This is a very simple Java ME card game. 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.}}
 
{{Abstract|This is a very simple Java ME card game. 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.}}
{{ArticleMetaData
+
{{ArticleMetaData <!-- v1.1 -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
Line 8: Line 8:
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
|capabilities=<!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
+
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
|language=<!-- Language category code for non-English topics - e.g. Lang-Chinese -->
+
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|translated-by= <!-- [[User:XXXX]] -->
|review-timestamp=<!-- After re-review: YYYYMMDD -->
+
|translated-from-title= <!-- Title only -->
|update-by=<!-- After significant update: [[User:username]]-->
+
|translated-from-id= <!-- Id of translated revision -->
|update-timestamp=<!-- After significant update: YYYYMMDD -->
+
|review-by= <!-- After re-review: [[User:username]] -->
|creationdate=20080604
+
|review-timestamp= <!-- After re-review: YYYYMMDD -->
|author=[[User:Vivartpandey]]
+
|update-by= <!-- After significant update: [[User:username]]-->
}}{{ReviewerApproved|timestamp=20090927}}
+
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20080604
 +
|author= [[User:Vivartpandey]]
 +
}}
  
  

Revision as of 05:42, 8 February 2012

This is a very simple Java ME card game. 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.

Article Metadata
Article
Created: vivartpandey (04 Jun 2008)
Last edited: hamishwillee (08 Feb 2012)


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

77 page views in the last 30 days.
×