猜数字小游戏[附图和源码]

猜数字小游戏, 随机产生的4位数字, A:位置对,数字也正确. B:数字正确,但是位置不对. 例如 [A:2 B:1]表示当前这四个数字中有两个数字是和结果吻合的, 另一个数字包含在结果内,但是所在位置不对.
还是很简单的, 当时是为了用一下Flex的拖拽功能.

code中引用的gif图片没有上传, 随便下载一套, 按照路径放置之后, 编译就可以了.



verticalAlign="middle" backgroundColor="#FFFFFF" backgroundGradientColors="[#ffffff, #e1e1e1]"
initialize="init()">

import mx.skins.halo.DateChooserIndicator;
import mx.charts.DateTimeAxis;
import mx.formatters.DateBase;
import mx.accessibility.DateChooserAccImpl;
import flash.utils.getTimer;
import mx.charts.AreaChart;
import mx.events.CollectionEvent;
import mx.rpc.events.AbstractEvent;
import mx.events.DragEvent;
import mx.events.ListEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.ItemClickEvent;
import mx.controls.TabBar;
import mx.collections.ICollectionView;
import mx.managers.DragManager;
import mx.core.IUIComponent;


[Bindable][Embed("img/0.gif")]
public var num0:Class;
[Bindable][Embed("img/1.gif")]
public var num1:Class;
[Bindable][Embed("img/2.gif")]
public var num2:Class;
[Bindable][Embed("img/3.gif")]
public var num3:Class;
[Bindable][Embed("img/4.gif")]
public var num4:Class;
[Bindable][Embed("img/5.gif")]
public var num5:Class;
[Bindable][Embed("img/6.gif")]
public var num6:Class;
[Bindable][Embed("img/7.gif")]
public var num7:Class;
[Bindable][Embed("img/8.gif")]
public var num8:Class;
[Bindable][Embed("img/9.gif")]
public var num9:Class;

[Bindable]
public var numSelection_dragable:Boolean = false;
[Bindable]
public var numSelection_dropable:Boolean = false;
[Bindable]
public var resultArea_dragable:Boolean = false;
[Bindable]
public var resultArea_dropable:Boolean = false;

[Bindable]
public var nowTime : String ="init....";
[Bindable]
public var passedTime : Number = 0;
[Bindable]
public var resultAreaArr:ArrayCollection = new ArrayCollection();
[Bindable]
private var guessTrackArr : ArrayCollection =new ArrayCollection();
private var currentStr : String ="";
private var finalResStr : String ="";
private var globalTimer : Timer =new Timer(1000,0);
private var playTimer : Timer =new Timer(1000,0);
private var checkCount: int = 0;

private function init():void{
resultAreaArr.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChangeEvnHander);
globalTimer.addEventListener(TimerEvent.TIMER,doTimer);globalTimer.start();
playTimer.addEventListener(TimerEvent.TIMER,doPlayTimer);
guessTrackArr.addItem("--- guess track ---");
}

private function resetProvderAreaArr():void{
numSelection.dataProvider = [
{label : "zero" , icon : num0},
{label : "one" , icon : num1},
{label : "two" , icon : num2},
{label : "three", icon : num3},
{label : "four" , icon : num4},
{label : "five" , icon : num5},
{label : "six" , icon : num6},
{label : "seven", icon : num7},
{label : "eight", icon : num8},
{label : "nine" , icon : num9}
];
resultAreaArr.removeAll();
}

private function clickEvt(event:ItemClickEvent):void {
var targetComp:TabBar = TabBar(event.currentTarget);
}

private function canDragOrNot() : void{
if (resultArea_dropable==false){
Alert.show("Please click Start button first.");
}
if (this.numSelection.dataProvider.length>=7){
this.numSelection_dragable =true;
}else{
this.numSelection_dragable =false;
}
}

private function convertEngToNum(input : String) : String{
var resultStr : String;

if (input == "one"){
resultStr = "1";
}else if (input == "two"){
resultStr = "2";
}else if (input == "three"){
resultStr = "3";
}else if (input == "four"){
resultStr = "4";
}else if (input == "five"){
resultStr = "5";
}else if (input == "six"){
resultStr = "6";
}else if (input == "seven"){
resultStr = "7";
}else if (input == "eight"){
resultStr = "8";
}else if (input == "nine"){
resultStr = "9";
}else if (input == "zero"){
resultStr = "0";
}
return resultStr;
}

private function btnCheckOnclick() : void{
if (currentStr!="" && currentStr.length==4){
if (finalResStr.length==4 && currentStr.length==4){
checkCount++;
guessTrackArr.addItemAt(checkCount+"- "+currentStr+" -["+ compareStr(finalResStr,currentStr)+"]",0);
cb_guessTrack.focusEnabled=true;
cb_guessTrack.selectedIndex=0;
}
}
}

private function generateFinalResultStr(): String{
var resultStr: String="";
var tmpArr:ArrayCollection = new ArrayCollection();
var tmpNum : Number;
while (tmpArr.length<4){
tmpNum = randRange(0,9);
if (!tmpArr.contains(tmpNum)){
tmpArr.addItem(tmpNum);
}
}
for(var i: int = 0; i resultStr += tmpArr[i].toString();
}
return resultStr;
}

private function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}

public function collectionChangeEvnHander(event : CollectionEvent) : void{
var tmpStr:String = "";
for (var i:int =0; i< this.resultAreaArr.length;i++){
tmpStr += convertEngToNum(resultAreaArr[i].label);
}
currentStr = tmpStr;
}

public function startGame():void{
resetProvderAreaArr();
finalResStr = generateFinalResultStr();
passedTime = 0;
checkCount =0;
playTimer.start();
guessTrackArr.removeAll();
guessTrackArr.addItem("-view track here-");
numSelection_dragable = true;
numSelection_dropable = true;
resultArea_dragable = true;
resultArea_dropable = true;
}

private function doTimer(event:TimerEvent):void{
nowTime = new Date().toLocaleTimeString();
}

private function doPlayTimer(event:TimerEvent):void{
passedTime += 1;
}

public function compareStr(finalStr:String,guessStr:String):String{
var tmpArr : ArrayCollection = new ArrayCollection();
var resultStr : String = "";
var countA : int = 0;
var countB : int = 0;

if (finalStr.length==4 && guessStr.length==4){

for(var i:int=0; i tmpArr.addItem(finalStr.charAt(i));
if (finalStr.charAt(i) == guessStr.charAt(i)){
countA += 1;
}
}
for (var j:int=0;j if (tmpArr.contains(guessStr.charAt(j))){
countB += 1;
}
}
var tmpNum : int = countB - countA;
resultStr = "A:"+countA.toString()+" B:"+tmpNum.toString();
}
if (resultStr=="A:4 B:0"){
Alert.show("Gratulation ! The right answer is :["+guessStr+"] you totally used "+passedTime.toString()+" s and "+checkCount+" tries");
playTimer.stop();
}
return resultStr;
}
]]>




mainArea























height="81" width="216" allowMultipleSelection="false"
dataProvider="{resultAreaArr}"/>







dataProvider="{guessTrackArr}"
selectedIndex="0"/>
click="btnCheckOnclick()"/>











你可能感兴趣的:(猜数字小游戏[附图和源码])