现在我们已经有一能够接收用户输入事件的Button类了,下面我们应该考虑如何实现
游戏中相关的逻辑,猜数字中的
游戏逻辑都比较简单,主要是产生一个4位随机数字且不能重复,其次是根据输入返回给用户结果。我们提供一个Engine类来完成这个工作。
package com.j2medev.numbergame;
import java.util.Random;
public class Engine
{
private int[] answer = new int[4];
private Random random = new Random();
public void init()
{
int[] number = new int[10];
for (int i = 0; i < number.length; i++)
{
number[i] = i;
}
int n = 10;
for (int index = 0; index < answer.length; index++)
{
int r = Math.abs(random.nextInt() % n);
answer[index] = number[r];
number[r] = number[n - 1];
n--;
}
}
public int[] getAnswer()
{
return answer;
}
public int[] queryResult(int[] input)
{
int[] state = new int[2];
int a = 0;
int b = 0;
for (int i = 0; i < answer.length; i++)
{
for (int j = 0; j < answer.length; j++)
{
if ((input[j] ^ answer[i]) == 0)
{
if (i == j)
{
a++;
} else
{
b++;
}
}
}
}
state[0] = a;
state[1] = b;
return state;
}
}
在游戏的运行中,我们有时候需要提示用户它的操作有误,比如输入数字为空,或者显示给用户已经答对了,一个庆祝的界面。当然这些你可以通过MIDP中提供的Alert来完成,我在这里实现了一个简单的界面类,这是一个抽象类,扩展了FullCanvas但是并没有实现paint()方法,把这个方法留给他的字类来实现。
/*
* Created on 2004-12-21
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.j2medev.numbergame;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import com.nokia.mid.ui.FullCanvas;
public abstract class NumScreen extends FullCanvas
{
protected Timer timer = new Timer();
protected Display display;
protected Manager next;
protected int timeout;
protected Font font;
public NumScreen(Display display,Manager next)
{
this.display = display;
this.next = next;
setTimeout(3000);
}
public Font getFont()
{
if(font == null)
{
font = Font.getDefaultFont();
}
return font;
}
public void setFont(Font font)
{
this.font = font;
}
public void setTimeout(int time)
{
this.timeout = time;
}
public void dismiss()
{
timer.cancel();
display.setCurrent(next);
}
public void showNotify()
{
timer.schedule(new TimerTask()
{
public void run()
{
dismiss();
}
},3000);
}
}
基于这个类,我们实现一个SplashScreen用在程序启动的时候的欢迎界面上,还提供一个类CongScreen用于显示用户的成绩和提示。
package com.j2medev.numbergame;
import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class SplashScreen extends NumScreen
{
public SplashScreen(Display display,Manager next)
{
super(display, next);
}
protected void paint(Graphics arg0)
{
Image welcome = null;
try
{
welcome = Image.createImage("/welcome.png");
} catch (IOException e)
{
e.printStackTrace();
}
arg0.drawImage(welcome, 2, 2, Graphics.TOP | Graphics.LEFT);
}
}
package com.j2medev.numbergame;
import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class CongScreen extends NumScreen
{
private String title;
private Mark mark;
private Image image;
private Image star;
private Manager manager;
private int type = CONG;
public static final int CONG = 1;
public static final int WARNING = 2;
public CongScreen(Display display, Manager next)
{
super(display, next);
}
public CongScreen(Display display,Manager next, Mark mark)
{
this(display, next);
this.mark = mark;
}
protected void paint(Graphics g)
{
int width = this.getWidth();
int height = this.getHeight();
if (image == null)
{
try
{
image = Image