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; +} 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; +} 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; +} 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; +} 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); +} 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); +} 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); + } +} 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; +} 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; + } + } +} 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; +} + + + 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} замерзает и пропускает ход`); + } +} 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; +} diff --git a/rpgsaga/saga/src/cat.ts b/rpgsaga/saga/src/cat.ts new file mode 100644 index 0000000..3b91d8e --- /dev/null +++ b/rpgsaga/saga/src/cat.ts @@ -0,0 +1,22 @@ +export class Cat { + Age: number; + Name: string; + Breed: string; + Weight: number; + + constructor(age: number, name: string, breed: string, weight: number) { + 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}.` + } +} diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 7805559..51e47d6 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,16 +1,8 @@ -import { Phone } from './phone'; +import { Cat } from './cat'; +import {Game} from "./Game"; -const first = new Phone('+7900-000 000 (123)', 1990, 'Телефон 1'); -first.year = 1998; +const Cat1 = new Cat (8, 'Кеша', 'Шотландский вислоухий', 7); +Cat1.getInfo() -first.year = -1998; -first.call('12345'); -first.endCall(); - -const second = new Phone('+799900000', -5); -// second.name = 'Телефон 2'; -console.log(second.year); -second.call('12345'); -second.endCall(); - -console.log(first, second, Phone.phoneCount); +const game = new Game(8); +game.start(); diff --git a/rpgsaga/saga/tests/cat.spec.ts b/rpgsaga/saga/tests/cat.spec.ts new file mode 100644 index 0000000..e1c124c --- /dev/null +++ b/rpgsaga/saga/tests/cat.spec.ts @@ -0,0 +1,32 @@ +import { Cat } from '../src/cat'; + +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); + }); +});