2013/02/27

Bingo game

Bingo game. You play against the computer.
This has set up a lot of the touch area.
This is only for iPad within portlait because of screen size.
//--------------------------------------------------------------------------------
/* BINGO
// for X-BASIC for iOS v2.0
//--------------------------------------------------------------------------------

if deviceType()<>DEVICE_IPAD and deviceType()<>DEVICE_IPAD_SIMULATOR then {
    print localizedString("このプログラムはiPad専用です","This program only for iPad.")
    end
}
dim int A(5,11),B(12,2)
dim int keyAreaNo(5,5)
int d,L,T,E,F
int WX,WY
int sx,sy,ex,ey
int OPEND=100:// constant
/*
srand2()
width(64)
getWidth(WX,WY)
setUpScroll(NO):// disable to up scroll (V2.0)
apage(GPAGE0)
vpage(B_TPAGE or B_GPAGE0 or B_GPAGE1,YES)
//display_title()

d=0
dim str menues(1)
str title
int e
repeat
    cls()
    apage(GPAGE1):wipe()
    apage(GPAGE0):wipe()
    gcolor(-TWHITE,0,0,255)
    symbol(128,2,"BINGO GAME","IPAGothic",48)
    gcolor(-THWHITE,0,0,255)
    symbol(129,3,"BINGO GAME","IPAGothic",48)
    gcolor(-THRED,0,0,255)
    box(13,64,748,614)
    tcolor(-THWHITE,0,0,255)
    shuffle():// set numbers
    field_print():// display numbers and set touch area
    print_number(NO)
    /*
    if isLocalizeJapan() then {
        menues={"先手","後手"}
        title="順番選択"
    } else {
        menues={"First move","Passive move"}
        title="Moving order"
    }
    repeat
        L=selectMenu2(title,menues):// -1,0,1
    until L>=0:// -1=BREAK/outside menu touch
    L=L+1:// 0,1->1,2

    sub_cls()
    /*
    for e=0 to 12
        B(e,0)=0
        B(e,1)=0
        B(e,2)=0
    next
    E=0:F=0:T=0
    repeat
        T=T+1
        tcolor(-THWHITE,0,0,255)
        locate(6,17)
        if isLocalizeJapan() then {
            print"第";T;"手"
        } else {
            print"The";T;" hand"
        }
        repeat
            switch L
                case 1:
                    locate(11,19)
                    print localizedString("あなた:","You : ");
                    d=input_num()
                    L=2
                    break
                case 2:
                    locate(38,19)
                    print localizedString("コンピューター:","Computer : ");
                    d=com_num()
                    //beep()
                    L=1
                    break
            endswitch
            int cx=pos()
            int cy=csrlin()
            int ffind=check_num(d)
            if not ffind then {
                // not found 
                // Human side only
                L=1
                beep()
                // input retry
            }
        until ffind
        locate(cx,cy):print d
    until (F>=5) or (E>=5)
    resetALlKeys():// remove touch area
    sub_cls()
    print_number(YES)
    hantei(E,F)
    locate(11,24)
    if isLocalizeJapan() then {
        print "御苦労さまでした"
        menues={"はい","いいえ"}
        title="もう一度しますか?"
    } else {
        print "Cheers for hard work was."
        menues={"YES","NO"}
        title="Try again ?"
    }
    L=selectMenu2(title,menues):// -1=stop,0=YES,1=NO
until L=1 or L=-1
end

//--------------------------------------------------------------------------------
// functions
//--------------------------------------------------------------------------------

func hantei(E;int,F;int)
/* game decision
    tcolor(-THYELLOW,0,0,255)
    locate(10,23)
    if isLocalizeJapan() then {
        print E;" 対";F;"で ";
    }
    if F<E then {
           print localizedString("あなたの勝ち","You win");
    } else {
        if E<F then {
            print localizedString("コンピューターの勝ち","Computer win");
        } else {
            print localizedString("引き分け","Draw");
        }
    }
    if not isLocalizeJapan() then {
        print " in the";E;"-for-";F
    }
endfunc
/*
func int input_num()
/* human's number select
int num
//    kbclr()
    // wait key input
    while not kbhit()
    endwhile
    // if you touch the grid then return keycode as number
    num=inkey()
    return(num)
endfunc
/*
func int check_num(d;int)
/* check number and grid open
int h,x,y,ac,bb,i,j
    for h=1 to 2:// check both
        // get grid coordinate from number
        int ffind=NO
        for i=1 to 5
            for j=1 to 5
                ac=A(i,j+(h-1)*6)
                if ac=d then {
                    x=i:y=j
                    ffind=YES
                    break
                }
            next
            if ffind then break
        next
        if not ffind then {
            // not found
            locate(0,5):print "見つからない"
            return (NO)
        }
        //
        apage(GPAGE1)
        switch h
            case 1:gcolor(-THMAGENTA,0,0,200):break
            case 2:gcolor(-THGREEN  ,0,0,200):break
        endswitch
        calc_grid_gxy(x,y,h-1):// ->sx,sy,ex,ey
        fill(sx,sy,ex,ey)
        apage(GPAGE0)
        //
        A(x,y+(h-1)*6)=ac+OPEND:/* opened symbol
        bb=0
        if x=y   then bb=B(12,h):B(12,h)=bb+1
        if x=6-y then bb=B(11,h):B(11,h)=bb+1
        bb=B(x,h)  :B(x  ,h)=bb+1
        bb=B(y+5,h):B(y+5,h)=bb+1
        x=0
        for i=1 to 12
            x=x+B(i,h)\5
        next
        if (h=1) and (E<x) then {
            locate(11,15)
            if isLocalizeJapan() then {
                print "あなた:";x;"列"
            } else {
                print "You :";x;" lines"
            }
            E=x
            beep2(7)
        } else {
            if (h=2)*(F<x) then {
                locate(38,15)
                if isLocalizeJapan() then {
                    print "コンピューター:";x;"列"
                } else {
                    print "Computer :";x;" lines"
                }
                F=x
                beep2(8)
            }
        }
    next
    // found and processed
    return (YES)
endfunc
/*
func int com_num()
/* computer thinking
int i,j,m,bb,c
    m=0
    for i=1 to 5
        for j=1 to 5
            bb=0
            if A(i,j+6)<OPEND then {
                if i=j   then bb=   1+B(12,2)
                if i=6-j then bb=bb+1+B(11,2)
                bb=bb+2+B(i,2)+B(j+5,2)
                if bb>m then c=A(i,j+6):m=bb
            }
        next
    next
//    locate(0,26):print "com=";c
    return(c)
endfunc
/*
func sub_cls()
/* clear part of screen
    int y
    for y=16 to WY
        locate(0,y):print space$(WX);
    next
endfunc
/*
func shuffle()
/* shuffle the number in grid
int i,j,l,h
int c,x,y
    for l=0 to 1:/* l=0:you , 1=computer
        /* enter number to grid in order
        /* A(i,j) : j=1〜5:yours , 6〜11:computer
        h=0
        for i=1 to 5
            for j=1 to 5
                h=h+1
                A(i,j+l*6)=h
            next
        next
        /* exchange number
        for i=1 to 5
            for j=1 to 5
                x=rand() mod 5+1
                y=rand() mod 5+1+l*6
                c=A(i,j+l*6)
                A(i,j+l*6)=A(x,y)
                A(x,y)=c
            next
        next
    next
endfunc
/*
func print_number(mode;int)
/* display the number in grid
// mode : NO=Human , YES=Computer
int i,j,c
int x,y
    tcolor(-THWHITE,0,0,255)
    for i=1 to 5
        x=(i-1)*5
        for j=1 to 5
            y=j*2+2
            if not mode then {
                // Human
                c=A(i,j)
                if c>OPEND then c=c-OPEND:/* opened grid
                locate(x+ 7,y):print using "##";c
                locate(x+36,y):print "??"
            } else {
                // Computer
                c=A(i,j+6)
                if c>OPEND then c=c-OPEND:/* opened grid
                locate(x+36,y):print using "##";c
            }
        next
    next
endfunc
/*
func field_print()
/* draw field
// In advance that you'll accept the number with shuffle()
int l,i,j
    for l=0 to 1:/* l=0:You , 1=Computer
        if l=0 then {
            tcolor(-THCYAN,0,0,255)
            locate(11,14)
            print localizedString("あなたのマス目","Your grid")
        } else {
            tcolor(-THYELLOW,0,0,255)
            locate(36,14)
            print localizedString("コンピューターのマス目","  Computer grid  ")
        }
        // draw grid
        gcolor(-THMAGENTA,0,0,255)
        for i=1 to 5
            for j=1 to 5
                calc_grid_gxy(i,j,l):// ->sx,sy,ex,ey
                box(sx,sy,ex,ey)
                if l=0 then {
                    // set toucha area
                    // only human area
                    // keyCode is the number
                    keyAreaNo(i,j)=setTouchArea(sx,sy,(ex-sx),(ey-sy),A(i,j),YES)
                    // save touch area No into keyAreaNo()
                }
            next
        next
    next
    tcolor(-THWHITE,0,0,255)
endfunc
/*
func resetALlKeys()
// reset all key touch area (important)
    int i,j
    for i=1 to 5
          for j=1 to 5
            removeTouchArea(keyAreaNo(i,j))
        next
    next
endfunc
/*
func calc_grid_gxy(i;int,j;int,l;int)
// calc graphic coordinate from grid coordinate
int x,y
    x=textX2Gx((i-1)*5+7)+l*340
    y=textY2Gy(j*2+2)
    // below return value (grobal variable)
    ex=textX2Gx((i-1+1)*5+7)+l*340-12
    ey=textY2Gy((j+1)*2+2)-12
    sx=x-12
    sy=y-12
endfunc
/*
func display_title()
/* title screen
int i,x
str s,s1
    wipe()
    lineWidth(2)
    gcolor(-THRED,0,0,255)
    box(61,4,448,128)
    gcolor(-THMAGENTA,0,0,200)
    fill(63,6,446,126)
    s="BINGO"
    for i=0 to 4
        x=i*70+90
        s1=utf8Mid$(s,i+1,1)
        gcolor(-TWHITE,0,0,255)
        symbol(x-11,16,s1,"Times New Roman",64)
        gcolor(-THWHITE,0,0,255)
        symbol(x-9 ,18,s1,"Times New Roman",64)
    next
    /*
    gcolor(-TGREEN,0,0,255)        :fill(112,180,400,320)
    //
    str copyright="Presented by AIG-SOFT"
    str forMachine="for X-BASIC or iOS"
    gcolor(-TWHITE,0,0,255)
    symbol(170,201,copyright,"IPAGothic",16)
    symbol(180,231,forMachine,"IPAGothic",16)
    gcolor(-THWHITE,0,0,255)
    symbol(169,200,copyright,"IPAGothic",16)
    symbol(179,230,forMachine,"IPAGothic",16)
    //
    gcolor(-THRED,0,0,255)
    box(109,178,403,324)
    hitKey()
endfunc
/*
func srand2()
/* random number initialize
int t
    t=val(right$(time$,2))+val(mid$(time$,4,2))
    srand(t*16)
endfunc
/*
func hitKey()
    setFunctionKey(0,localizedString("ここを押してください","Hit This Button"),'!')
    displayFunctionKey(YES,0,0)
    while inkey()=0
    endwhile
    cls()
    displayFunctionKey(NO,0,0)
endfunc

func str localizedString(js;str,es;str)
    if isLocalizeJapan() then return(js)
    return (es)
endfunc

//--------------------------------------------------------------------------------

Zip file is here : XBbingo.zip

No comments:

Post a Comment