Chương trình cho phép chạy ảnh như ảnh động bằng cách dùng biến NSTimer kết hợp với nhiều hình ảnh để tạo ảnh động giả
- Xử lý kết quả:
+ Thêm tiền cược nếu thắng
+ Bớt tiền nếu thua
+ Không bắt đầu nếu không đủ tiền cược
- Lưu dữ liệu của người dùng:
+ Lưu lại dữ liệu tiền của người dùng (sử dụng user default để lưu dữ liệu lại mỗi khi có sự thay đổi) có thể load lại khi bật lại chương trình.
- Quản lý giao diện:
+ Khóa giao diện: Khóa giao diện không tương tác khi xí ngầu quay
+ Mở khóa giao diện: Sau khi xí ngầu đã có điểm cố định
+ Cập nhật giao diện: Cập nhật lại giao diện khi có thay đổi

* Các control:
- Chương trình đưa vào để cho các đối tượng khác tham khảo về cách sử dụng hàm delegate.
- Cách viết một
* Những điểm lưu ý của chương trình:
- Đây là Xcode 4.5.2 nên có những điểm mới so với Xcode 4.2 và 4.3
- Việc khai báo lại biến ở file .m là không cần thiết, các lớp đã được khai báo ở file .h có thể được gọi lại khi thêm (_) ở phía trước. Cải tiến này rất đáng giá khi thêm bớt những thuộc tính trong một view không cần phải xóa khai báo ở nhiều nơi nữa.
- Các controller giờ có các canh theo vị trí tương đối dễ dàng hơn cho việc xử lý và thể hiện giao diện
* Giao diện của chương trình:
- Một hình ảnh (ImageView) để thể hiện hình ảnh của Xí ngầu (Dice)
- Button

III. Kết luận:
- Đây là chương trình mẫu có thể phát triển lên mạnh mẽ hơn nữa trong tương lai để có thêm những chương trình mạnh mẽ hơn do Việt Nam tự viết.
- Chương trình có thể được nâng cấp lên vớu nhiều trò chơi và chức năng mạnh mẽ hơn nữa để đem lại sự tham khảo tốt hơn cho những lập trình viên mới vào nghề chưa có kinh nghiệm trong việc phát triển chương trình.

* Coding:
File.h


Mã:
//
//  MainMindViewController.h
//  ShakingDiceShowing
//
//  Created by Huy Nguyen on 11/21/12.
//  Copyright (c) 2012 HuyNguyen. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MainMindViewController : UIViewController
{
int TongTienHienCo;
int TienCuoc;

NSTimer *timer;
}
@property (retain, nonatomic) IBOutlet UIImageView *XiNgauMot;
@property (retain, nonatomic) IBOutlet UILabel *lblDiemCuoc;
@property (retain, nonatomic) IBOutlet UILabel *lblTongTienHienCo;
@property (retain, nonatomic) IBOutlet UISegmentedControl *segDatCuoc;
@property (retain, nonatomic) IBOutlet UILabel *txtNoiDungThongBao;
@property (retain, nonatomic) IBOutlet UIButton *btnShake;
@property (retain, nonatomic) IBOutlet UIButton *btnSet50;
@property (retain, nonatomic) IBOutlet UIButton *btnSet100;
@property (retain, nonatomic) IBOutlet UIButton *btnGetMoney;

- (IBAction)Set50:(id)sender;
- (IBAction)Set100:(id)sender;
- (IBAction)GetMore:(id)sender;

- (IBAction)DoXiNgau:(id)sender;

- (void) LockScreen;
- (void) UnLockScreen;
- (void) UpdateScreen;
@end
File.m

Mã:
//
//  MainMindViewController.m
//  ShakingDiceShowing
//
//  Created by Huy Nguyen on 11/21/12.
//  Copyright (c) 2012 HuyNguyen. All rights reserved.
//

#import “MainMindViewController.h”

@interface MainMindViewController ()

@end

@implementation MainMindViewController

static int count;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)dealloc {
[_XiNgauMot release];
[_lblDiemCuoc release];
[_lblTongTienHienCo release];
[_segDatCuoc release];
[_txtNoiDungThongBao release];
[_btnShake release];
[_btnSet50 release];
[_btnSet100 release];
[_btnGetMoney release];
[super dealloc];
}

- (IBAction)Set50:(id)sender {
_lblDiemCuoc.text = @”50″;
}

- (IBAction)Set100:(id)sender {
_lblDiemCuoc.text = @”100″;
}

- (IBAction)GetMore:(id)sender {
TongTienHienCo += 100;
[self LuuTruMoney];
[self UpdateScreen];
}

- (IBAction)DoXiNgau:(id)sender {
if (TongTienHienCo <= 0){
UIAlertView *thongBao = [[UIAlertView alloc] initWithTitle:@”Notice” message:@”You don’t have enough money to bet!!” delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil];
[thongBao show];
return;
}

TienCuoc = [[NSString stringWithFormat:_lblDiemCuoc.text] intValue];

if (TienCuoc > TongTienHienCo){
UIAlertView *thongBao = [[UIAlertView alloc] initWithTitle:@”Notice” message:@”You don’t have enough money to bet!!” delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil];
[thongBao show];
return;
}

if (TienCuoc == 0){
UIAlertView *thongBao = [[UIAlertView alloc] initWithTitle:@”Notice” message:@”You need to bet something!!” delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil];
[thongBao show];
return;
}

timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(QuayXucXac) userInfo:nil repeats:YES];
}

- (void) QuayXucXac {
[self LockScreen];

int diemSoRandomMot = arc4random()%6 +1;

NSString *hinhXucXacMot = [NSString stringWithFormat:@"%i",diemSoRandomMot];

_XiNgauMot.image = [UIImage imageNamed:hinhXucXacMot];

int ChonDatCuoc = [_segDatCuoc selectedSegmentIndex];

count++;

if (count >=9){
count = 0;

[timer invalidate];
int soDiem = diemSoRandomMot;

if (ChonDatCuoc == 0){
if (soDiem > 3){
_txtNoiDungThongBao.text = [[NSString alloc] initWithFormat:@”Win: %i”, TienCuoc];
TongTienHienCo = TongTienHienCo + TienCuoc;
UIAlertView *thongbaoThang = [[UIAlertView alloc] initWithTitle:@”Notice” message:@”Win” delegate:self cancelButtonTitle:@”Get money” otherButtonTitles:nil];
[thongbaoThang show];
} else {
_txtNoiDungThongBao.text = [[NSString alloc] initWithFormat:@”Lose: %i”, TienCuoc];
TongTienHienCo = TongTienHienCo – TienCuoc;
UIAlertView *thongbaoThang = [[UIAlertView alloc] initWithTitle:@”Notice” message:@”Lose” delegate:self cancelButtonTitle:@”Let’s bet again” otherButtonTitles:nil];
[thongbaoThang show];
}
} else {
if (soDiem < 3){
_txtNoiDungThongBao.text = [[NSString alloc] initWithFormat:@”Win: %i”, TienCuoc];
TongTienHienCo = TongTienHienCo + TienCuoc;
UIAlertView *thongbaoThang = [[UIAlertView alloc] initWithTitle:@”Notice” message:@”Win” delegate:self cancelButtonTitle:@”Get Money” otherButtonTitles:nil];
[thongbaoThang show];
} else {
_txtNoiDungThongBao.text = [[NSString alloc] initWithFormat:@”Lose: %i”, TienCuoc];
TongTienHienCo = TongTienHienCo – TienCuoc;
UIAlertView *thongbaoThang = [[UIAlertView alloc] initWithTitle:@”Notice” message:@”Lose” delegate:self cancelButtonTitle:@”Let’s bet again” otherButtonTitles:nil];
[thongbaoThang show];
}
}

[self UnLockScreen];
[self UpdateScreen];
}

_lblTongTienHienCo.text = [NSString stringWithFormat:@"%i", TongTienHienCo];
[self LuuTruMoney];
}

- (void) LuuTruMoney{
int tienNguoiChoi = TongTienHienCo;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:tienNguoiChoi forKey:@"TienNguoiChoi"];
[defaults synchronize];
}

- (void) LockScreen{
_btnShake.enabled = NO;
_segDatCuoc.enabled = NO;
_btnSet50.enabled = NO;
_btnSet100.enabled = NO;
}

- (void) UnLockScreen{
_btnShake.enabled = YES;
_segDatCuoc.enabled = YES;
_btnSet50.enabled = YES;
_btnSet100.enabled = YES;
}

- (void) UpdateScreen{
_lblTongTienHienCo.text = [NSString stringWithFormat:@"%i", TongTienHienCo];
}

@end
* Cuối cùng đây là code mẫu cho mọi người tham khảo:

Source for this: http://www.mediafire.com/download.php?svd1naicqw9a1dn

Google Drive Link: https://docs.google.com/open?id=0B38...015VkMtVmUxSGM

Product: LapKan Company Product (LK)
Coder: Huygamer
Blog: huygamer.wordpress.com