Skip to content
This repository was archived by the owner on Feb 2, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
# Проект 4
osx-project-3
=============

## Один из двух вариантов на ваш выбор:

### Вариант 1. Собственное приложение
1. Необходимо закончить разработку приложения и выложить исполняемый файл (готовый продукт)
2. Ваше приложение должно запускаться.
3. Нужно сдать весь проект (project-файл, файлы-ресурсы, .xib-файл и так далее – всю папку).

### Вариант 2. iDNA

1. Добавить локализацию (русский язык или английский если изначально был русский).
2. Добавить всплывающие окна для подтверждения открытия, сохранения файлов и закрытия приложения.
3. Сохранять настройки эволюции в системе (user defaults) и загружать их при новом запуске приложения.
4. Использовать движение мыши в качестве источника случайности. Перед стартом эволюции пользователю должен совершить несколько движений мышью; координаты курсора будут использоваться в качестве некоторых исходных данных для генератора случайных чисел. Сбор этих координат должен отоброжаться прогресс-баром.

OS X Development Course, Project 3
373 changes: 373 additions & 0 deletions iDNA.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions iDNA/Cell+hybrid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Cell+mutator.h
// dna
//
// Created by Igor Pavlov on 03.11.12.
// Copyright (c) 2012 Igor Pavlov. All rights reserved.
//

#import "Cell.h"


@interface Cell (hybrid)


+ (Cell*) makeHybridWith:(Cell*)a andWith:(Cell*)b;


@end
110 changes: 110 additions & 0 deletions iDNA/Cell+hybrid.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// Cell+mutator.m
// dna
//
// Created by Igor Pavlov on 03.11.12.
// Copyright (c) 2012 Igor Pavlov. All rights reserved.
//

#import "Cell+hybrid.h"


@implementation Cell (hybrid)


+ (Cell*) makeHybrid0With:(Cell*)a andWith:(Cell*)b
{
const NSUInteger dnaLength = [a->dna length];

NSMutableString *newDna = [NSMutableString stringWithCapacity:dnaLength];

[newDna appendString:[a->dna substringToIndex:dnaLength/2]];
[newDna appendString:[b->dna substringFromIndex:dnaLength/2]];

return [[Cell alloc] initWithDnaString:newDna];
}


+ (Cell*) makeHybrid1With:(Cell*)a andWith:(Cell*)b
{
const NSUInteger dnaLength = [a->dna length];

NSMutableString *newDna = [NSMutableString stringWithCapacity:dnaLength];

for (NSUInteger i = 0; i != dnaLength; ++i)
{
const NSUInteger curPercent = 100*i/(dnaLength - 1);
if (curPercent & 1)
[newDna appendString:[b->dna substringWithRange:NSMakeRange(i, 1)]];
else
[newDna appendString:[a->dna substringWithRange:NSMakeRange(i, 1)]];
}

return [[Cell alloc] initWithDnaString:newDna];
}


+ (Cell*) makeHybrid2With:(Cell*)a andWith:(Cell*)b
{
const NSUInteger dnaLength = [a->dna length];

NSMutableString *newDna = [NSMutableString stringWithCapacity:dnaLength];

const NSUInteger i0 = 20*dnaLength/100;
const NSUInteger l1 = 60*dnaLength/100;
const NSUInteger i2 = i0 + l1;

[newDna appendString:[a->dna substringToIndex:i0]];
[newDna appendString:[b->dna substringWithRange:NSMakeRange(i0, l1)]];
[newDna appendString:[a->dna substringFromIndex:i2]];

return [[Cell alloc] initWithDnaString:newDna];
}


+ (Cell*) makeHybrid3With:(Cell*)a andWith:(Cell*)b
{
const NSUInteger dnaLength = [a->dna length];

NSMutableString *newDna = [NSMutableString stringWithCapacity:dnaLength];

for (NSUInteger i = 0; i != dnaLength; ++i)
{
if (i & 1)
[newDna appendString:[b->dna substringWithRange:NSMakeRange(i, 1)]];
else
[newDna appendString:[a->dna substringWithRange:NSMakeRange(i, 1)]];
}

return [[Cell alloc] initWithDnaString:newDna];
}


+ (Cell*) makeHybridWith:(Cell*)a andWith:(Cell*)b
{
if (!a || !b)
return nil;

if ([a->dna length] != [b->dna length])
return nil;

const NSUInteger method = arc4random_uniform(4);
switch (method)
{
case 0:
return [Cell makeHybrid0With:a andWith:b];

case 1:
return [Cell makeHybrid1With:a andWith:b];

case 2:
return [Cell makeHybrid2With:a andWith:b];

case 3:
return [Cell makeHybrid3With:a andWith:b];
}

return nil;
}

@end
16 changes: 16 additions & 0 deletions iDNA/Cell+mutator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Cell+mutator.h
// dna
//
// Created by Igor Pavlov on 03.11.12.
// Copyright (c) 2012 Igor Pavlov. All rights reserved.
//

#import "Cell.h"


@interface Cell (mutator)

- (void) mutate:(NSUInteger)percent;

@end
76 changes: 76 additions & 0 deletions iDNA/Cell+mutator.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// Cell+mutator.m
// dna
//
// Created by Igor Pavlov on 03.11.12.
// Copyright (c) 2012 Igor Pavlov. All rights reserved.
//

#import "Cell+mutator.h"


@implementation Cell (mutator)


- (void) mutate:(NSUInteger)percent
{
const NSUInteger maxPercent = 100;

// проверка на правильность аргумента
if (percent > maxPercent)
@throw [NSException exceptionWithName:NSRangeException reason:@"argument is out of [0..100] range" userInfo:nil];

// если задано изменение 0%, то ничего делать не надо, сразу выход
if (0 == percent)
return;

const NSUInteger dnaLength = [dna length];

// создать массив флагов и зарезервировать в нем место
NSMutableArray *mutationFlags = [NSMutableArray arrayWithCapacity:dnaLength];

// количество символов к мутации
const NSUInteger mutationIndexCount = dnaLength*percent/maxPercent;

// заполнить флаги
for (NSUInteger i = 0; i != mutationIndexCount; ++i)
[mutationFlags addObject:[NSNumber numberWithBool:YES]];

for (NSUInteger i = mutationIndexCount; i != dnaLength; ++i)
[mutationFlags addObject:[NSNumber numberWithBool:NO]];

// если мутация затрагивает менее 100% символов, перемешать флаги для случайности изменений
if (mutationIndexCount < dnaLength)
{
// перемешивание
for (NSUInteger i = 0; i != mutationIndexCount; ++i)
{
const NSUInteger j = i + arc4random_uniform((u_int32_t)(dnaLength - i));
[mutationFlags exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}

const NSString *nucleotides = [Cell getNucleotides];
const NSUInteger nucleotideCount = [nucleotides length];

NSMutableString *newDna = [NSMutableString stringWithCapacity:dnaLength];

for (NSUInteger i = 0; i != dnaLength; ++i)
{
NSString *c = [dna substringWithRange:NSMakeRange(i, 1)];
if ([[mutationFlags objectAtIndex:i] boolValue])
{
const NSUInteger curNucleotideIndex = [nucleotides rangeOfString:c].location;
NSAssert(NSNotFound != curNucleotideIndex, @"curNucleotideIndex = %lu", curNucleotideIndex);
const NSUInteger newNucleotideIndex = (curNucleotideIndex + 1 + arc4random_uniform((u_int32_t)(nucleotideCount - 1))) % nucleotideCount;
NSAssert(newNucleotideIndex < nucleotideCount, @"newNucleotideIndex = %lu", newNucleotideIndex);
NSAssert(newNucleotideIndex != curNucleotideIndex, @"newNucleotideIndex = %lu", newNucleotideIndex);
c = [nucleotides substringWithRange:NSMakeRange(newNucleotideIndex, 1)];
}
[newDna appendString:c];
}

dna = newDna;
}

@end
24 changes: 24 additions & 0 deletions iDNA/Cell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Cell.h
// dna
//
// Created by Igor Pavlov on 02.11.12.
// Copyright (c) 2012 Igor Pavlov. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface Cell : NSObject <NSCopying, NSCoding>
{
NSString *dna;
}

+ (NSString*) getNucleotides;

- (id) init __attribute__((unavailable("init not available")));
- (id) initWithSize:(NSUInteger)size;
- (id) initWithDnaString:(NSString*)dnaStr;
- (NSUInteger) hammingDistance:(Cell*)otherCell;

@end
Loading