From c49a81ed0a455b3491a85f2978e840f483d328f3 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 2 Oct 2023 19:42:00 +0300 Subject: [PATCH 01/20] Create Duplicate_Encoder.js --- .../Duplicate Encoder/Duplicate_Encoder.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 codewars/Duplicate Encoder/Duplicate_Encoder.js diff --git a/codewars/Duplicate Encoder/Duplicate_Encoder.js b/codewars/Duplicate Encoder/Duplicate_Encoder.js new file mode 100644 index 0000000..e7cc14e --- /dev/null +++ b/codewars/Duplicate Encoder/Duplicate_Encoder.js @@ -0,0 +1,21 @@ +function duplicateEncode(word){ + word = word.toLowerCase(); + + var count = {}; + + + for (var i = 0; i < word.length; i++) { + var char = word[i]; + count[char] = count[char] ? count[char] + 1 : 1; + } + + var result = ''; + + for (var i = 0; i < word.length; i++) { + var char = word[i]; + + result += count[char] === 1 ? '(' : ')'; + } + + return result; +} From d8da82d42215ac27ca0cdd9589443d6b5f841777 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 2 Oct 2023 19:42:56 +0300 Subject: [PATCH 02/20] Create Build_Tower.js --- codewars/Build Tower/Build_Tower.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 codewars/Build Tower/Build_Tower.js diff --git a/codewars/Build Tower/Build_Tower.js b/codewars/Build Tower/Build_Tower.js new file mode 100644 index 0000000..c70e6d6 --- /dev/null +++ b/codewars/Build Tower/Build_Tower.js @@ -0,0 +1,13 @@ +function towerBuilder(nFloors) { + let res = []; + let width = ((nFloors * 2) - 1); + + for (let i = 1; i <= nFloors; i++) { + let space = " ".repeat(nFloors - i); + let star = "*".repeat((i * 2) - 1); + let floor = space + star + space; + res.push(floor); + } + + return res; +} From fe005f0bf50183b2073fa84d45a54ab6d669f7b0 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 2 Oct 2023 19:43:35 +0300 Subject: [PATCH 03/20] Create Sum_of_Digits.js --- .../Sum of Digits - Digital Root/Sum_of_Digits.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 codewars/Sum of Digits - Digital Root/Sum_of_Digits.js diff --git a/codewars/Sum of Digits - Digital Root/Sum_of_Digits.js b/codewars/Sum of Digits - Digital Root/Sum_of_Digits.js new file mode 100644 index 0000000..a8a1307 --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/Sum_of_Digits.js @@ -0,0 +1,14 @@ +function digitalRoot(n){ + var str = '' + n; + while(str.length > 1){ + var char = str.split(''); + var b = char.map(Number); + let res = b.reduce(function(a,b){ + return(a + b); + }) + str = res.toString(10); + } + if (str.length = 1){ + return parseInt(str, 10); + } +} From 8d8afb0fa8da223720639eb4ab6c7cf918d0aae6 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 2 Oct 2023 19:44:10 +0300 Subject: [PATCH 04/20] Create Merge_Two_Arrays.js --- codewars/Merge two arrays/Merge_Two_Arrays.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 codewars/Merge two arrays/Merge_Two_Arrays.js diff --git a/codewars/Merge two arrays/Merge_Two_Arrays.js b/codewars/Merge two arrays/Merge_Two_Arrays.js new file mode 100644 index 0000000..c8da97d --- /dev/null +++ b/codewars/Merge two arrays/Merge_Two_Arrays.js @@ -0,0 +1,12 @@ +function mergeArrays(a, b) { + let res = []; + for(let i = 0; i < Math.max(a.length, b.length); i++){ + if (i < a.length){ + res.push(a[i]); + } + if (i < b.length){ + res.push(b[i]); + } + } + return(res); +} From f3bd14d334300939a0989fd1c7c6c6ddfa08249f Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 2 Oct 2023 19:44:53 +0300 Subject: [PATCH 05/20] Create Moving_Zeros_to_End.js --- .../Moving Zeros To The End/Moving_Zeros_to_End.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 codewars/Moving Zeros To The End/Moving_Zeros_to_End.js diff --git a/codewars/Moving Zeros To The End/Moving_Zeros_to_End.js b/codewars/Moving Zeros To The End/Moving_Zeros_to_End.js new file mode 100644 index 0000000..5d2382a --- /dev/null +++ b/codewars/Moving Zeros To The End/Moving_Zeros_to_End.js @@ -0,0 +1,12 @@ +function moveZeros(arr) { + let res = []; + let zeros = []; + for(let i = 0; i < arr.length; i++) { + if(arr[i] === 0) { + zeros.push(arr[i]); + } else { + res.push(arr[i]); + } + } + return res.concat(zeros); +} From 32414458d2bc247c7f234670fedfd1e81f834942 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 2 Oct 2023 19:50:19 +0300 Subject: [PATCH 06/20] Create Convert_Str_to_Camel_Case.js --- .../Convert_Str_to_Camel_Case.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 codewars/Convert string to camel case/Convert_Str_to_Camel_Case.js diff --git a/codewars/Convert string to camel case/Convert_Str_to_Camel_Case.js b/codewars/Convert string to camel case/Convert_Str_to_Camel_Case.js new file mode 100644 index 0000000..c66e38b --- /dev/null +++ b/codewars/Convert string to camel case/Convert_Str_to_Camel_Case.js @@ -0,0 +1,12 @@ +function toCamelCase(str) { + if (str.length === 0) { + return ""; + } + let words = str.split(/[-_]/); + let res = words[0]; + for (let i = 1; i < words.length; i++) { + res += words[i].charAt(0).toUpperCase() + words[i].slice(1); + } + + return res; +} From b06fb1bcc4737dd4eb620a89e769275eca51ddbb Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 2 Oct 2023 20:03:43 +0300 Subject: [PATCH 07/20] Create Array_Deep_Count.js --- codewars/Array Deep Count/Array_Deep_Count.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 codewars/Array Deep Count/Array_Deep_Count.js diff --git a/codewars/Array Deep Count/Array_Deep_Count.js b/codewars/Array Deep Count/Array_Deep_Count.js new file mode 100644 index 0000000..e99965d --- /dev/null +++ b/codewars/Array Deep Count/Array_Deep_Count.js @@ -0,0 +1,13 @@ +function deepCount(a) { + let count = 0; + + for (let i = 0; i < a.length; i++) { + if (Array.isArray(a[i])) { + count += deepCount(a[i]) + 1; + } else { + count++; + } + } + + return count; +} From ced212631add2a1c20e71b5fc257a49204a44a0e Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 2 Oct 2023 20:15:42 +0300 Subject: [PATCH 08/20] Create Sum_Of_Pairs.js --- codewars/Sum of pairs/Sum_Of_Pairs.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 codewars/Sum of pairs/Sum_Of_Pairs.js diff --git a/codewars/Sum of pairs/Sum_Of_Pairs.js b/codewars/Sum of pairs/Sum_Of_Pairs.js new file mode 100644 index 0000000..3a50ead --- /dev/null +++ b/codewars/Sum of pairs/Sum_Of_Pairs.js @@ -0,0 +1,15 @@ +function sumPairs(arr, target) { + let indices = new Map(); + + for (let i = 0; i < arr.length; i++) { + let complement = target - arr[i]; + + if (indices.has(complement)) { + return [complement, arr[i]]; + } + + indices.set(arr[i], i); + } + + return undefined; +} From 15062f9813245290d938f478143f416d8754958b Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 6 Nov 2023 16:53:33 +0300 Subject: [PATCH 09/20] Create cat.ts --- rpgsaga/saga/src/cat.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 rpgsaga/saga/src/cat.ts diff --git a/rpgsaga/saga/src/cat.ts b/rpgsaga/saga/src/cat.ts new file mode 100644 index 0000000..55665ba --- /dev/null +++ b/rpgsaga/saga/src/cat.ts @@ -0,0 +1,37 @@ +export class Cat { + private age: number; + private name: string; + private breed: string; + + constructor(age: number, name: string, breed: string) { + this.age = age; + this.name = name; + this.breed = breed; + } + set Age(age: number){ + this.age = age; + } + + get Age(): number { + return this.age; + } + + set Name(name: string){ + this.name = name; + } + + get Name(): string { + return this.name; + } + + set Breed(breed: string){ + this.breed = breed; + } + + get Breed(): string { + return this.breed; + } + getInfo(): string { + return `Имя: ${this.name}; Порода: ${this.breed}; Возраст: ${this.age}.` + } +} From 26acc05354f0e23787cd460ca6203c8ad6ca9199 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 6 Nov 2023 17:20:55 +0300 Subject: [PATCH 10/20] Update cat.ts --- rpgsaga/saga/src/cat.ts | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/rpgsaga/saga/src/cat.ts b/rpgsaga/saga/src/cat.ts index 55665ba..f3b7059 100644 --- a/rpgsaga/saga/src/cat.ts +++ b/rpgsaga/saga/src/cat.ts @@ -1,37 +1,46 @@ export class Cat { - private age: number; - private name: string; - private breed: string; + private theAge: number; + private theName: string; + private theBreed: string; + private theWeight: number; - constructor(age: number, name: string, breed: string) { - this.age = age; - this.name = name; - this.breed = breed; + constructor(age: number, name: string, breed: string, weight: number) { + this.theAge = age; + this.theName = name; + this.theBreed = breed; + this.theWeight = weight; } set Age(age: number){ - this.age = age; + this.theAge = age; } get Age(): number { - return this.age; + return this.theAge; } set Name(name: string){ - this.name = name; + this.theName = name; } get Name(): string { - return this.name; + return this.theName; } set Breed(breed: string){ - this.breed = breed; + this.theBreed = breed; } get Breed(): string { - return this.breed; + return this.theBreed; + } + set Weight(weight: number){ + this.theWeight = weight; + } + + get Weight(): number{ + return this.theWeight; } getInfo(): string { - return `Имя: ${this.name}; Порода: ${this.breed}; Возраст: ${this.age}.` + return `Имя: ${this.theName}; Порода: ${this.theBreed}; Возраст: ${this.theAge}; Вес:${this.theWeight}.` } } From 33981329d3ea5c9001e7f4e312a87d18305d7de2 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 6 Nov 2023 17:33:53 +0300 Subject: [PATCH 11/20] Update index.ts --- rpgsaga/saga/src/index.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 7805559..b909f87 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,16 +1,20 @@ -import { Phone } from './phone'; +import { Cat } from './cat'; -const first = new Phone('+7900-000 000 (123)', 1990, 'Телефон 1'); -first.year = 1998; +const Cat1 = new Cat (7, 'Кеша', 'Шотландский вислоухий'); +Cat1.Age = 7; +Cat1.Weight = 5; +Cat1.Breed = 'Шотландский вислоухий'; -first.year = -1998; -first.call('12345'); -first.endCall(); +const Cat2 = new Cat (undefined, 'Кисик', 'Британский'); +Cat2.Age = 6; +Cat2.Weight = 7; +Cat2.Breed = 'Британский'; -const second = new Phone('+799900000', -5); -// second.name = 'Телефон 2'; -console.log(second.year); -second.call('12345'); -second.endCall(); +const Cat3 = new Cat (15, 'Юми', 'Русский голубой'); +Cat2.Age = 3; +Cat2.Weight = 4; +Cat2.Breed = 'Русский голубой'; -console.log(first, second, Phone.phoneCount); +console.log(Cat1.getInfo()) +console.log(Cat2.getInfo()) +console.log(Cat3.getInfo()) From 0e986cc24bb23438a0a167481d73f5fb9b255d34 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Mon, 6 Nov 2023 17:46:18 +0300 Subject: [PATCH 12/20] Create cat.spec.ts --- rpgsaga/saga/tests/cat.spec.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 rpgsaga/saga/tests/cat.spec.ts diff --git a/rpgsaga/saga/tests/cat.spec.ts b/rpgsaga/saga/tests/cat.spec.ts new file mode 100644 index 0000000..b6b130d --- /dev/null +++ b/rpgsaga/saga/tests/cat.spec.ts @@ -0,0 +1,34 @@ +import { Cat } from '../src/cat'; + +describe('Testing cat constructor', () => { + it('Cat should be created', () => { + const cat1 = new Cat(7, 'Кеша', 'Шотландский вислоухий', 5); + expect(cat1.Age).toEqual(7); + expect(cat1.Name).toEqual('Кеша'); + expect(cat1.Breed).toEqual('Шотландский вислоухий'); + expect(cat1.Weight).toEqual(5); + }); +}); + +describe('Testing cat methods', () => { + it('Cat new age value', () => { + const cat1 = new Cat(7, 'Кеша', 'Шотландский вислоухий', 5); + cat1.Age = 7; + expect(cat1.Age).toEqual(7); + }); + it('Cat new name value', () => { + const cat1 = new Cat(7, 'Кеша', 'Шотландский вислоухий', 5); + cat1.Name = 'Кеша'; + expect(cat1.Name).toEqual('Кеша'); + }); + it('Cat new weight', () => { + const cat1 = new Cat(7, 'Кеша', 'Шотландский вислоухий', 5); + cat1.Weight = 5; + expect(cat1.Weight).toEqual(5); + }); + it('Cat new breed', () => { + const cat1 = new Cat(7, 'Кеша', 'Шотландский вислоухий', 5); + cat1.Breed = 'Шотландский вислоухий'; + expect(cat1.Breed).toEqual('Шотландский вислоухий'); + }); +}); From 8bb529f2fd6c0448e35d2570d5e918dbe2bcb7c3 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:42:54 +0300 Subject: [PATCH 13/20] Update cat.ts --- rpgsaga/saga/src/cat.ts | 52 +++++++++++------------------------------ 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/rpgsaga/saga/src/cat.ts b/rpgsaga/saga/src/cat.ts index f3b7059..3b91d8e 100644 --- a/rpgsaga/saga/src/cat.ts +++ b/rpgsaga/saga/src/cat.ts @@ -1,44 +1,20 @@ export class Cat { - private theAge: number; - private theName: string; - private theBreed: string; - private theWeight: number; + Age: number; + Name: string; + Breed: string; + Weight: number; constructor(age: number, name: string, breed: string, weight: number) { - this.theAge = age; - this.theName = name; - this.theBreed = breed; - this.theWeight = weight; - } - set Age(age: number){ - this.theAge = age; - } - - get Age(): number { - return this.theAge; - } - - set Name(name: string){ - this.theName = name; - } - - get Name(): string { - return this.theName; - } - - set Breed(breed: string){ - this.theBreed = breed; - } - - get Breed(): string { - return this.theBreed; - } - set Weight(weight: number){ - this.theWeight = weight; - } - - get Weight(): number{ - return this.theWeight; + this.Age = age; + this.Name = name; + this.Breed = breed; + this.Weight = weight; + if (age <= 0 || age > 30){ + throw new Error("Invalid age"); + } + if (weight <= 0 || weight > 30){ + throw new Error("invalid weight") + } } getInfo(): string { return `Имя: ${this.theName}; Порода: ${this.theBreed}; Возраст: ${this.theAge}; Вес:${this.theWeight}.` From b24426d1ea1bffcaa30d5e29f64400f68adc3062 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:44:25 +0300 Subject: [PATCH 14/20] Update cat.spec.ts --- rpgsaga/saga/tests/cat.spec.ts | 60 ++++++++++++++++------------------ 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/rpgsaga/saga/tests/cat.spec.ts b/rpgsaga/saga/tests/cat.spec.ts index b6b130d..e1c124c 100644 --- a/rpgsaga/saga/tests/cat.spec.ts +++ b/rpgsaga/saga/tests/cat.spec.ts @@ -1,34 +1,32 @@ import { Cat } from '../src/cat'; -describe('Testing cat constructor', () => { - it('Cat should be created', () => { - const cat1 = new Cat(7, 'Кеша', 'Шотландский вислоухий', 5); - expect(cat1.Age).toEqual(7); - expect(cat1.Name).toEqual('Кеша'); - expect(cat1.Breed).toEqual('Шотландский вислоухий'); - expect(cat1.Weight).toEqual(5); - }); -}); - -describe('Testing cat methods', () => { - it('Cat new age value', () => { - const cat1 = new Cat(7, 'Кеша', 'Шотландский вислоухий', 5); - cat1.Age = 7; - expect(cat1.Age).toEqual(7); - }); - it('Cat new name value', () => { - const cat1 = new Cat(7, 'Кеша', 'Шотландский вислоухий', 5); - cat1.Name = 'Кеша'; - expect(cat1.Name).toEqual('Кеша'); - }); - it('Cat new weight', () => { - const cat1 = new Cat(7, 'Кеша', 'Шотландский вислоухий', 5); - cat1.Weight = 5; - expect(cat1.Weight).toEqual(5); - }); - it('Cat new breed', () => { - const cat1 = new Cat(7, 'Кеша', 'Шотландский вислоухий', 5); - cat1.Breed = 'Шотландский вислоухий'; - expect(cat1.Breed).toEqual('Шотландский вислоухий'); - }); +describe('Testing Cat constructor', () => { + it('Cat should be created', () => { + const first = new Cat(8, "Кеша", "Шотландский вислоухий", 7); + expect(first.age).toEqual(8); + expect(first.name).toEqual("Кеша"); + expect(first.breed).toEqual("Шотландский вислоухий"); + expect(first.weight).toEqual(7); + }); + it('Cat with empty name', () => { + const first = new Cat(8, "", "Шотландский вислоухий", 7 ); + expect(first.age).toEqual(8); + expect(first.name).toBeUndefined(); + expect(first.breed).toEqual("Шотландский вислоухий"); + expect(first.weight).toEqual(7); + }); + it('Cat age lower than bound', () => { + const first = new Cat(0, "", "Шотландский вислоухий", 7 ); + expect(first.age).toEqual(1); + expect(first.name).toBeUndefined(); + expect(first.breed).toEqual("Шотландский вислоухий"); + expect(first.weight).toEqual(7); + }); + it('Cat age higher than bound', () => { + const first = new Cat(65, "", "Шотландский вислоухий", 7 ); + expect(first.age).toEqual(29); + expect(first.name).toBeUndefined(); + expect(first.breed).toEqual("Шотландский вислоухий"); + expect(first.weight).toEqual(7); + }); }); From 631414dd2c8492e51fda8dbd69bc62b8717d74fa Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:45:20 +0300 Subject: [PATCH 15/20] Update index.ts --- rpgsaga/saga/src/index.ts | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index b909f87..b2b41ad 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,20 +1,5 @@ import { Cat } from './cat'; -const Cat1 = new Cat (7, 'Кеша', 'Шотландский вислоухий'); -Cat1.Age = 7; -Cat1.Weight = 5; -Cat1.Breed = 'Шотландский вислоухий'; +const Cat1 = new Cat (8, 'Кеша', 'Шотландский вислоухий', 7); -const Cat2 = new Cat (undefined, 'Кисик', 'Британский'); -Cat2.Age = 6; -Cat2.Weight = 7; -Cat2.Breed = 'Британский'; - -const Cat3 = new Cat (15, 'Юми', 'Русский голубой'); -Cat2.Age = 3; -Cat2.Weight = 4; -Cat2.Breed = 'Русский голубой'; - -console.log(Cat1.getInfo()) -console.log(Cat2.getInfo()) -console.log(Cat3.getInfo()) +Cat1.getInfo() From eb4b98ef6a447af486fd1ef91f08c01632554ab1 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:47:29 +0300 Subject: [PATCH 16/20] Create Classes.ts --- rpgsaga/saga/src/Classes.ts | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 rpgsaga/saga/src/Classes.ts diff --git a/rpgsaga/saga/src/Classes.ts b/rpgsaga/saga/src/Classes.ts new file mode 100644 index 0000000..e6f6694 --- /dev/null +++ b/rpgsaga/saga/src/Classes.ts @@ -0,0 +1,49 @@ +import {Player} from "./Player"; +import {spec} from "node:test/reporters"; + +export class Paladin extends Player{ + constructor(name: string, specialization = 'Паладин', AbilityName: string = "Кара") { + super(name, specialization, AbilityName); +} + attackDamage(player: Player, strength = this.Strength){ + player.Health -= strength; + return strength; + } + abilityDamage (player: Player): number{ + this.attackDamage(player, this.Strength * 1.3) + return Math.floor(this.Strength * 1.3) + } +} +export class Sorcerer extends Player{ + constructor(name: string, specialization: string = 'Волшебник', AbilityName: string = "Ледяной туман") { + super(name, specialization, AbilityName); + } + + attackDamage(player: Player, strength = this.Strength): number{ + player.Health -= strength; + return strength; + } + abilityDamage(player: Player): number{ + player.Stun = true; + return 0; + } +} +export class Ranger extends Player { + constructor(name: string, specialization: string = 'Следопыт', AbilityName: string = "Град огненных стрел") { + super(name, specialization, AbilityName); + } + attackDamage(player: Player, strength = this.Strength): number{ + player.Health -= strength; + return strength; + } + abilityDamage(player: Player): number{ + if (this.AbilityUsed){ + this.attackDamage(player); + return this.attackDamage(player); + }else{ + this.AbilityUsed = true; + player.BurnDamage = 3; + return 0; + } + } +} From dc011e9deeaf9c8f9cc80defc5760cc6cc2c5bef Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:48:00 +0300 Subject: [PATCH 17/20] Create Game.ts --- rpgsaga/saga/src/Game.ts | 115 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 rpgsaga/saga/src/Game.ts diff --git a/rpgsaga/saga/src/Game.ts b/rpgsaga/saga/src/Game.ts new file mode 100644 index 0000000..32443a0 --- /dev/null +++ b/rpgsaga/saga/src/Game.ts @@ -0,0 +1,115 @@ +import {Player} from "./Player"; +import {randomInt} from "./Player"; +import {Paladin, Ranger, Sorcerer} from "./Classes"; +import {Logger} from "./Logger"; + +export class Game{ + players: Player[] = [] + private playersNames = ["Чилчак", "Туовен", "Сонот", "Ролвод", "Зогрид"] + playersCount: number; + constructor(playersCount: number) { + this.playersCount = playersCount; + for(let i = 0; i < playersCount; i++){ + const playerType = randomInt(0, 2) + const playerName = randomInt(0, 4) + let player: Player + switch (playerType) { + case 0: + player = new Paladin(this.playersNames[playerName]) + break + case 1: + player = new Ranger(this.playersNames[playerName]) + break + case 2: + player = new Sorcerer(this.playersNames[playerName]) + } + + // @ts-ignore + this.players.push(player) + } + + } + battle(p1: Player, p2: Player): any { + let turn = false; //сходил ли первый игрок + while (p1.Health > 0 && p2.Health > 0) { + const attackingPlayer = turn ? p2 : p1; + const defendingPlayer = turn ? p1 : p2; + // if (!turn) { + // const attackingPlayer = p1; + // const defendingPlayer = p2; + // } else { + // const attackingPlayer = p2; + // const defendingPlayer = p1; + // } + if (attackingPlayer.Stun){ + attackingPlayer.Stun = false; + }else{ + if (randomInt(0,1)){ + if (attackingPlayer.AbilityUsed){ + let damage = attackingPlayer.abilityDamage(defendingPlayer); + damage += defendingPlayer.BurnDamage; + Logger.abilityDamage(attackingPlayer, defendingPlayer, damage); + if (defendingPlayer.Health <= 0){ + Logger.death(defendingPlayer); + return attackingPlayer; + } + }else{ + let damage = attackingPlayer.attackDamage(defendingPlayer) + Logger.attackDamage(attackingPlayer, defendingPlayer, damage); + if(defendingPlayer.Health <= 0){ + Logger.death(defendingPlayer); + return attackingPlayer; + } + } + }else{ + let damage = attackingPlayer.attackDamage(defendingPlayer); + if (defendingPlayer.BurnDamage){ + damage += defendingPlayer.BurnDamage; + } + Logger.attackDamage(attackingPlayer, defendingPlayer, damage); + if(defendingPlayer.Health <= 0){ + Logger.death(defendingPlayer); + return attackingPlayer; + } + } + } + } + turn = !turn; + } + start(){ + let count = 1; + let shuffledPlayers = shuffle(this.players); + while(shuffledPlayers.length > 1){ + const winner: Player[] = []; + console.log(`Битва ${count}`); + for(let i = 0; i < shuffledPlayers.length - 1; i += 2){ + shuffledPlayers[i].Health = shuffledPlayers[i].BasicHealth; + shuffledPlayers[i + 1].Health = shuffledPlayers[i + 1].BasicHealth; + winner.push(this.battle(shuffledPlayers[i], shuffledPlayers[i + 1])) + } + shuffledPlayers = winner; + count += 1; + } + } +} + +export function shuffle(array: T[]): T[] { + let currentIndex = array.length, randomIndex; + + // While there remain elements to shuffle. + while (currentIndex != 0) { + + // Pick a remaining element. + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex--; + + // And swap it with the current element. + [array[currentIndex], array[randomIndex]] = [ + array[randomIndex], array[currentIndex]]; + } + + return array; +} + + + From a8644d70de3b555a5a55d492d59f1ed83bc93ee1 Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:48:26 +0300 Subject: [PATCH 18/20] Create Logger.ts --- rpgsaga/saga/src/Logger.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 rpgsaga/saga/src/Logger.ts diff --git a/rpgsaga/saga/src/Logger.ts b/rpgsaga/saga/src/Logger.ts new file mode 100644 index 0000000..bc2eaed --- /dev/null +++ b/rpgsaga/saga/src/Logger.ts @@ -0,0 +1,20 @@ +import {Player} from "./Player"; + +export class Logger{ + static attackDamage(p1: Player, p2: Player, damage: number){ + console.log(`${p1.Name} наносит урон ${damage} врагу${p2.Name}.`); + } + static abilityDamage(p1: Player, p2: Player, damage: number){ + console.log(`${p1.Name} использует способность ${p1.AbilityName} и наносит ${damage} врагу ${p2.Name}.`); + + } + static death(player: Player){ + console.log(`${player.Name} повержен!`); + } + static roundStart(p1: Player, p2:Player){ + console.log(`${p1.Specialization} ${p1.Name} против ${p2.Specialization} ${p2.Name}`) + } + static stun(player: Player){ + console.log(`${player.Name} замерзает и пропускает ход`); + } +} From aa3ada18b6376520d93b618327cd11b2cfa3587e Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:49:01 +0300 Subject: [PATCH 19/20] Create Player.ts --- rpgsaga/saga/src/Player.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 rpgsaga/saga/src/Player.ts diff --git a/rpgsaga/saga/src/Player.ts b/rpgsaga/saga/src/Player.ts new file mode 100644 index 0000000..957eb9e --- /dev/null +++ b/rpgsaga/saga/src/Player.ts @@ -0,0 +1,26 @@ +export function randomInt(min: number, max: number){ + return Math.floor(Math.random() * (max - min + 1) + min); +} +export abstract class Player{ + BasicHealth: number; + Health: number; + Name: string; + Strength: number; + Specialization: string; + Stun: boolean = false; + BurnDamage: number = 0; + AbilityUsed: boolean = false; + AbilityName: string; + + + protected constructor(Name: string, Specialization: string, AbilityName: string) { + this.BasicHealth = randomInt(75, 100); + this.Name = Name; + this.Health = this.BasicHealth; + this.Strength = randomInt(5, 15); + this.Specialization = Specialization; + this.AbilityName = AbilityName; + } + abstract attackDamage(player: Player): number; + abstract abilityDamage(player: Player): number; +} From 4b80d600ad1822f912ad44fe85e6331dde7936ec Mon Sep 17 00:00:00 2001 From: EgoriusTheGreat2 <117865797+EgoriusTheGreat2@users.noreply.github.com> Date: Fri, 12 Jan 2024 17:50:00 +0300 Subject: [PATCH 20/20] Update index.ts --- rpgsaga/saga/src/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index b2b41ad..51e47d6 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,5 +1,8 @@ import { Cat } from './cat'; +import {Game} from "./Game"; const Cat1 = new Cat (8, 'Кеша', 'Шотландский вислоухий', 7); - Cat1.getInfo() + +const game = new Game(8); +game.start();