diff --git a/rpgsaga/saga/src/SagaRPG/Classes/Archer/Archer.ts b/rpgsaga/saga/src/SagaRPG/Classes/Archer/Archer.ts new file mode 100644 index 0000000..23b94a8 --- /dev/null +++ b/rpgsaga/saga/src/SagaRPG/Classes/Archer/Archer.ts @@ -0,0 +1,32 @@ +import { Player } from '../../Player/player'; +import { Status } from "../../Player/Ability"; +export enum Archer_Shape { + Hunter = 'Hunter', + Raider = 'Raider', + Elemental_Mage = 'Elemental_Mage', +} + +export class Archer extends Player { + _healthpool: number; + _hp: number; + _power: number = 10; + //_hitchance: number = 85; + //_suppress: number = 25; + Shape: Archer_Shape; + constructor(Shape: Archer_Shape, name: string, hp: number) { + super(name); + this.Shape = Shape; + this._hp = hp; + this._healthpool = hp; + } + ability(){ + return ['ability', 7, 3, Status.Ignite] + } + attack() { + return ['damage',this._power]; + } + + public toString(): string { + return "(Archer) " + this._name; + } +} diff --git a/rpgsaga/saga/src/SagaRPG/Classes/Mage/Mage.ts b/rpgsaga/saga/src/SagaRPG/Classes/Mage/Mage.ts new file mode 100644 index 0000000..4416a86 --- /dev/null +++ b/rpgsaga/saga/src/SagaRPG/Classes/Mage/Mage.ts @@ -0,0 +1,34 @@ +import { Player } from '../../Player/player'; +import { Status } from "../../Player/Ability"; +export enum Mage_Shape { + Inquisitor = 'Inquisitor', + Forbidden = 'Forbidden', + Occultist = 'Occultist', +} + +export class Mage extends Player { + _healthpool: number; + _hp: number; + _power: number = 12; + //_hitchance:number = 90; + //_evade:number = 10; + Shape: Mage_Shape; + constructor(Shape: Mage_Shape, name: string, hp: number) { + super(name); + this.Shape = Shape; + this._hp = hp; + this._healthpool = hp; + } + + ability(){ + return ['ability', 0, 2, Status.Freeze] + } + + attack() { + return ['damage',this._power]; + } + + public toString(): string { + return "(Mage) " + this._name; + } +} diff --git a/rpgsaga/saga/src/SagaRPG/Classes/Warrior/Warrior.ts b/rpgsaga/saga/src/SagaRPG/Classes/Warrior/Warrior.ts new file mode 100644 index 0000000..67df29e --- /dev/null +++ b/rpgsaga/saga/src/SagaRPG/Classes/Warrior/Warrior.ts @@ -0,0 +1,35 @@ +import { Player } from '../../Player/player'; +import { Status } from "../../Player/Ability"; +export enum Warrior_Shape { + Juggernaut = 'Juggernaut', + Marauder = 'Marauder', + Slayer = 'Slayer', +} + +export class Warrior extends Player { + _healthpool: number; + _hp: number; + _power: number = 8; + //_hitchance:number = 80; + //_block:number = 10; + Shape: Warrior_Shape; + constructor(Shape: Warrior_Shape, name: string, hp: number) { + super(name); + this.Shape = Shape; + this._hp = hp; + this._healthpool = hp; + } + ability(){ + return ['ability', 4, 6, Status.Bleed] + } + + attack() { + return ['damage',this._power]; + } + + public toString(): string { + return "(Warrior) " + this._name; + } + +} + diff --git a/rpgsaga/saga/src/SagaRPG/Logger/logger.ts b/rpgsaga/saga/src/SagaRPG/Logger/logger.ts new file mode 100644 index 0000000..312a1de --- /dev/null +++ b/rpgsaga/saga/src/SagaRPG/Logger/logger.ts @@ -0,0 +1,29 @@ +import { Player } from "../Player/player"; + +export class Logger{ + static WriteWinner(winner: Player){ + console.log() + console.log(`${winner} win this game`) + console.log() + } + static WritebattleNumber(number){ + console.log(`Battle № ${number}`) + } + static WritebattleMembers(battleMembers: Player[]){ + console.log(`${battleMembers[0]} VS ${battleMembers[1]}`); + } + + static WriteDeath(looser: Player): void { + console.log(`${looser} dead`); + } + static WriteAction(firstplayer, secondplayer, MoveAction){ + if (MoveAction[0] == 'damage') { + console.log((`${firstplayer} deal ${MoveAction[1]} damage to enemy ${secondplayer}`)); + }else{ + console.log((`${firstplayer} imposed ${MoveAction[3]} on ${secondplayer}`)); + } + } + static WriteDagameFromAbility(player){ + console.log(`${player} get ${player.CheckStatus()[2]} damage from enemy's ability`) + } +} \ No newline at end of file diff --git a/rpgsaga/saga/src/SagaRPG/Player/Ability.ts b/rpgsaga/saga/src/SagaRPG/Player/Ability.ts new file mode 100644 index 0000000..e8a8326 --- /dev/null +++ b/rpgsaga/saga/src/SagaRPG/Player/Ability.ts @@ -0,0 +1,6 @@ +export enum Status{ + Ignite = 'Ignite', + Freeze = 'Freeze', + Bleed = 'Bleed', + None = 'None' +} \ No newline at end of file diff --git a/rpgsaga/saga/src/SagaRPG/Player/Grid-generator/Grid-generator.ts b/rpgsaga/saga/src/SagaRPG/Player/Grid-generator/Grid-generator.ts new file mode 100644 index 0000000..68e694a --- /dev/null +++ b/rpgsaga/saga/src/SagaRPG/Player/Grid-generator/Grid-generator.ts @@ -0,0 +1,19 @@ +import { Random } from "../../random/randomNum"; +import { generator} from "../Player_generator/Player_generator"; +import { Player } from "../player"; + +//classes +export enum classes { + Archer = 'Archer', + Warrior = 'Warrior', + Mage = 'Mage', + } +const classes_array: Array = [classes.Archer, classes.Warrior, classes.Mage]; + + export function Grid_generator(player_quantity):Player[]{ + let player_grid:Player[] = [] + for (let index = 0; index < player_quantity; index++) { + player_grid.push(generator(classes_array[Random(0,2)])) + } + return player_grid +} \ No newline at end of file diff --git a/rpgsaga/saga/src/SagaRPG/Player/Player_generator/Player_generator.ts b/rpgsaga/saga/src/SagaRPG/Player/Player_generator/Player_generator.ts new file mode 100644 index 0000000..f8e21e2 --- /dev/null +++ b/rpgsaga/saga/src/SagaRPG/Player/Player_generator/Player_generator.ts @@ -0,0 +1,28 @@ +import { Random } from '../../random/randomNum'; +import { Warrior, Warrior_Shape } from '../../Classes/Warrior/Warrior'; +import { Mage, Mage_Shape } from '../../Classes/Mage/Mage'; +import { Archer, Archer_Shape } from '../../Classes/Archer/Archer'; +import { Player } from '../player'; +import { classes } from '../Grid-generator/Grid-generator'; +//Shape +const A_Shape: Archer_Shape[] = [Archer_Shape.Hunter, Archer_Shape.Raider, Archer_Shape.Elemental_Mage]; +const M_Shape: Mage_Shape[] = [Mage_Shape.Forbidden, Mage_Shape.Inquisitor, Mage_Shape.Occultist]; +const W_Shape: Warrior_Shape[] = [Warrior_Shape.Juggernaut, Warrior_Shape.Marauder, Warrior_Shape.Slayer]; +//names +const names: string[] = ['Sergey', 'Danilla', 'Nikola', 'Artem', 'Denis', 'Karina', 'Evgenia', 'Elena', 'Egor']; + +export function generator(player_class: classes) { + let player: Player; + switch (player_class) { + case classes.Archer: + player = new Archer(A_Shape[Random(0, 2)], names[Random(0, 8)], Random(90, 110)); + break; + case classes.Warrior: + player = new Warrior(W_Shape[Random(0, 2)], names[Random(0, 8)], Random(110, 130)); + break; + default: + player = new Mage(M_Shape[Random(0, 2)], names[Random(0, 8)], Random(70, 90)); + break; + } + return player; +} diff --git a/rpgsaga/saga/src/SagaRPG/Player/player.ts b/rpgsaga/saga/src/SagaRPG/Player/player.ts new file mode 100644 index 0000000..02f7b7d --- /dev/null +++ b/rpgsaga/saga/src/SagaRPG/Player/player.ts @@ -0,0 +1,46 @@ +import { stat } from "fs"; +import { Status } from "../Player/Ability"; +export class Player { + _hp: number; + _power: number; + _healthpool: number; + _name: string; + _status: [Status, number, number?] = [Status.None, 0, 0]; + //_hitchance: number; + constructor(name: string) { + this._name = name; + } + + ability(){ + return ['ability', 6, 3, Status.Ignite] + } + attack() { + return ['damage',this._power]; + } + DeathOrAlive(): boolean { + if (this._hp <= 0) { + return true; + } else { + return false; + } + } + getDamage(damage: number): boolean { + this._hp = this._hp - damage; + return this.DeathOrAlive(); + } + restore():void { + this._hp = this._healthpool; + this._status = [Status.None, 0, 0] + } + + CheckStatus(){ + return this._status + } + getStatus(status){ + this._status = status + } + TakeDamageFromAbility(){ + this._healthpool -= this.CheckStatus[1] + this.CheckStatus[1] - 1 + } +} diff --git a/rpgsaga/saga/src/SagaRPG/battle/battle.ts b/rpgsaga/saga/src/SagaRPG/battle/battle.ts new file mode 100644 index 0000000..bd70192 --- /dev/null +++ b/rpgsaga/saga/src/SagaRPG/battle/battle.ts @@ -0,0 +1,67 @@ +import { Player } from '../Player/player'; +import { Grid_generator } from '../Player/Grid-generator/Grid-generator'; +import { Logger } from '../Logger/logger'; +import { Random } from '../random/randomNum'; +import { Status } from '../Player/Ability'; +import { stat } from 'fs'; +export class battle { + static Start(player_quantity): void { + let grid: Player[] = Grid_generator(player_quantity); + battle.Play(grid); + } + static Play(grid: Player[]): void { + let length = grid.length; + for (let index = 1; index < length; index++) { + Logger.WritebattleNumber(index); + battle.Playround(grid, length); + } + Logger.WriteWinner(grid[0]); + } + static Playround(grid: Player[], length): void { + let RoundMembers = [grid[0], grid[1]]; + Logger.WritebattleMembers(RoundMembers); + let winner = battle.Playbattle(RoundMembers); + grid.push(winner); + grid.splice(0, 2); + } + static Playbattle(RoundMembers): Player { + for (let index = 0; true; index++) { + if (RoundMembers[index % 2].CheckStatus()[0] == Status.Freeze && RoundMembers[index % 2].CheckStatus()[1] != 0) { + console.log(`${RoundMembers[index % 2]} skip move`); + RoundMembers[index % 2].CheckStatus()[1] -= 1; + }else { + if (RoundMembers[index % 2].CheckStatus()[0] == Status.Ignite || RoundMembers[index % 2].CheckStatus()[0] == Status.Bleed && RoundMembers[index % 2].CheckStatus()[1] < 0) { + + RoundMembers[index%2].TakeDamageFromAbility() + Logger.WriteDagameFromAbility(RoundMembers[index%2]) + } + let PlayerMode = RoundMembers[index % 2].DeathOrAlive(); + if (PlayerMode) { + return RoundMembers[(index + 1) % 2]; + } + let MoveAction = battle.PlayerAction(RoundMembers[index % 2]); + if (MoveAction[0] == 'damage') { + PlayerMode = RoundMembers[(index + 1) % 2].getDamage(MoveAction[1]); + Logger.WriteAction(RoundMembers[index % 2], RoundMembers[(index + 1) % 2], MoveAction); + } else { + RoundMembers[(index + 1) % 2].getStatus([MoveAction[3], MoveAction[2], MoveAction[1]]); + Logger.WriteAction(RoundMembers[index % 2], RoundMembers[(index + 1) % 2], MoveAction); + } + if (PlayerMode) { + Logger.WriteDeath(RoundMembers[(index + 1) % 2]); + RoundMembers[index % 2].restore(); + return RoundMembers[index % 2]; + } + } + } + } + + static PlayerAction(player): void { + let chance = Random(0, 10); + if (chance <= 1) { + return player.ability(); + } else { + return player.attack(); + } + } +} diff --git a/rpgsaga/saga/src/SagaRPG/random/randomNum.ts b/rpgsaga/saga/src/SagaRPG/random/randomNum.ts new file mode 100644 index 0000000..c7ffb2f --- /dev/null +++ b/rpgsaga/saga/src/SagaRPG/random/randomNum.ts @@ -0,0 +1,3 @@ +export function Random(min: number, max: number): number{ + return Math.round(Math.random() * (max - min) + min); + } \ No newline at end of file diff --git a/rpgsaga/saga/src/classTask/mouse.ts b/rpgsaga/saga/src/classTask/mouse.ts new file mode 100644 index 0000000..05d8d82 --- /dev/null +++ b/rpgsaga/saga/src/classTask/mouse.ts @@ -0,0 +1,42 @@ +export enum Mode { + ALIVE = "Alive", + DIED = "Died" +} +export class Mouse { + _healthpool: number = 100; + private _age: number; + _color: string; + _mode: Mode = Mode.ALIVE; + constructor(year: number, color: string) { + this.year = year; + this._color = color; + }; + set year(year: number) { + this._age = year > 0 && year < 4 ? year : 0 + } + get year(): number { + return this._age + } + hit(): string { + this._healthpool -= 10 + if (this._healthpool > 0) { + return `Вы ударили мышь, теперь у неё ${this._healthpool} здоровья` + } else { + this._mode = Mode.DIED + return "Мышь сдохла" + } + } + heal(): string { + this._healthpool = 100 + return `Теперь здоровье мыши в норме и составляет 100 единиц` + } + paint(color): string { + if (this._color == color) { + return `Зачем вы красите ${this._color} в ${color}` + } else { + this._color = color + return `Теперь мышь имеет ${color} цвет` + } + } + +} diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 7805559..4747045 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,16 +1,3 @@ -import { Phone } from './phone'; - -const first = new Phone('+7900-000 000 (123)', 1990, 'Телефон 1'); -first.year = 1998; - -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); +import { Grid_generator } from "./SagaRPG/Player/Grid-generator/Grid-generator"; +import { battle } from "./SagaRPG/battle/battle"; +console.log(battle.Start(2)) \ No newline at end of file diff --git a/rpgsaga/saga/src/phone.ts b/rpgsaga/saga/src/phone.ts deleted file mode 100644 index d310d29..0000000 --- a/rpgsaga/saga/src/phone.ts +++ /dev/null @@ -1,28 +0,0 @@ -export class Phone { - private aYear: number; - phoneNumber: string; - - static phoneCount = 0; - - constructor(number: string, year: number, public name?: string) { - Phone.phoneCount += 1; - this.phoneNumber = number; - this.year = year; - this.name = name; - } - - call(number: string) { - console.log(`Making a call from ${this.phoneNumber} to ${number}`); - } - endCall() { - console.log('Ending call'); - } - - set year(year: number) { - this.aYear = year >= 1900 && year < 2023 ? year : this.aYear ?? 1900; - } - - get year(): number { - return this.aYear; - } -} \ No newline at end of file diff --git a/rpgsaga/saga/src/polymorphism/animal.ts b/rpgsaga/saga/src/polymorphism/animal.ts new file mode 100644 index 0000000..60a08cb --- /dev/null +++ b/rpgsaga/saga/src/polymorphism/animal.ts @@ -0,0 +1,16 @@ +export enum Mode { + ALIVE = "Alive", + DIED = "Died" +}; +export abstract class Animal { + _mode: Mode = Mode.ALIVE; + _color: string; + _healthpool: number = 100; + constructor(color: string) { + this._color = color; + }; + sound(): string { + return "Мы не можем понять, кто это" + } +}; + diff --git a/rpgsaga/saga/src/polymorphism/chinchilla.ts b/rpgsaga/saga/src/polymorphism/chinchilla.ts new file mode 100644 index 0000000..f1fe43d --- /dev/null +++ b/rpgsaga/saga/src/polymorphism/chinchilla.ts @@ -0,0 +1,23 @@ +import { Animal } from "./animal"; +import { Mode } from "./animal"; +export class chinchilla extends Animal { + private _age: number; + constructor(year: number, color: string) { + super(color); + this.year = year; + }; + set year(year: number) { + this._age = year > 0 && year < 20 ? year : 0; + }; + + get year(): number { + return this._age; + } + + sound(): string { + return `Фыр-фыр`; + }; + toString(): string { + return "Это Шиншила"; + }; +}; \ No newline at end of file diff --git a/rpgsaga/saga/src/polymorphism/fox.ts b/rpgsaga/saga/src/polymorphism/fox.ts new file mode 100644 index 0000000..a570eb3 --- /dev/null +++ b/rpgsaga/saga/src/polymorphism/fox.ts @@ -0,0 +1,34 @@ +import { Animal } from "./animal"; +import { Mode } from "./animal"; +export class Fox extends Animal { + private _age: number; + constructor(year: number, color: string) { + super(color); + this.year = year; + }; + set year(year: number) { + this._age = year > 0 && year < 25 ? year : 0; + }; + + get year(): number { + return this._age; + }; + + sound(): string { + return `Миу`; + }; + + toString(): string { + return "Это Лиса"; + }; + + hit(): string { + this._healthpool -= 10; + if (this._healthpool > 0) { + return `Вы ударили лису, теперь у неё ${this._healthpool} здоровья` + } else { + this._healthpool = 100; + return "У лисы осталось 0 здоровья, однако боги исцелили её"; + }; + }; +}; \ No newline at end of file diff --git a/rpgsaga/saga/tests/example.spec.ts b/rpgsaga/saga/tests/example.spec.ts deleted file mode 100644 index 8c44f23..0000000 --- a/rpgsaga/saga/tests/example.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -describe('Example', () => { - it('should return 5 as result of 2 and 3 sum', () => { - const a = 3; - const b = 2; - expect(a + b).toEqual(5); - }); -}); diff --git a/rpgsaga/saga/tests/mouse.spec.ts b/rpgsaga/saga/tests/mouse.spec.ts new file mode 100644 index 0000000..ba43b5c --- /dev/null +++ b/rpgsaga/saga/tests/mouse.spec.ts @@ -0,0 +1,40 @@ +import { Mouse } from '../src/classTask/mouse'; +import { Mode } from "../src/classTask/mouse"; + +xdescribe('Testing Mouse constructor', () => { + it('Mouse should be created with incorrect age', () => { + const mouse = new Mouse(6, "Красный"); + expect(mouse.year).toEqual(0); + expect(mouse._healthpool).toEqual(100); + expect(mouse._color).toEqual("Красный"); + expect(mouse._mode).toEqual(Mode.ALIVE); + }); + it("Mouse should be created", () => { + const mouse = new Mouse(2, "Чёрный"); + expect(mouse.year).toEqual(2); + expect(mouse._healthpool).toEqual(100); + expect(mouse._color).toEqual("Чёрный"); + expect(mouse._mode).toEqual(Mode.ALIVE); + }); +}); + +xdescribe('Testing Mouse methods', () => { + it("Method hit", () => { + const mouse = new Mouse(2, "Чёрный"); + for (let index = 1; index < 10; index++) { + mouse.hit() + expect(mouse._healthpool).toEqual(100 - index * 10) + }; + }); + it("Method heal", () => { + const mouse = new Mouse(2, "Чёрный"); + mouse.heal(); + expect(mouse._healthpool).toEqual(100) + }); + it("Method color with same color and not", () => { + const mouse = new Mouse(2, "Чёрный"); + expect(mouse.paint("Красный")).toEqual("Теперь мышь имеет Красный цвет"); + expect(mouse.paint("Красный")).toEqual("Зачем вы красите Красный в Красный"); + }) +}); + diff --git a/rpgsaga/saga/tests/phone.spec.ts b/rpgsaga/saga/tests/phone.spec.ts deleted file mode 100644 index 64c38b0..0000000 --- a/rpgsaga/saga/tests/phone.spec.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { Phone } from '../src/phone'; - -describe('Testing phone constructor', () => { - it('Phone should be created', () => { - const first = new Phone('+7900-000 000 (123)', 1990, 'Телефон 1'); - expect(first.phoneNumber).toEqual('+7900-000 000 (123)'); - expect(first.year).toEqual(1990); - expect(first.name).toEqual('Телефон 1'); - }); - it('Phone with empty name', () => { - const first = new Phone('+7900-000 000 (123)', 1990); - expect(first.phoneNumber).toEqual('+7900-000 000 (123)'); - expect(first.year).toEqual(1990); - expect(first.name).toBeUndefined(); - }); - it('Phone year lower than bound', () => { - const first = new Phone('+7900-000 000 (123)', 1899); - expect(first.phoneNumber).toEqual('+7900-000 000 (123)'); - expect(first.year).toEqual(1900); - expect(first.name).toBeUndefined(); - }); - it('Phone year higher than bound', () => { - const first = new Phone('+7900-000 000 (123)', 2023); - expect(first.phoneNumber).toEqual('+7900-000 000 (123)'); - expect(first.year).toEqual(1900); - expect(first.name).toBeUndefined(); - }); -}); - -describe('Testing phone methods', () => { - it('Phone year set valid value', () => { - const first = new Phone('+7900-000 000 (123)', 2000, 'Телефон 1'); - first.year = 1991; - expect(first.year).toEqual(1991); - }); - it('Phone year lower than valid value', () => { - const first = new Phone('+7900-000 000 (123)', 2000, 'Телефон 1'); - first.year = 1899; - expect(first.year).toEqual(2000); - }); - it('Phone year higher than valid value', () => { - const first = new Phone('+7900-000 000 (123)', 2000, 'Телефон 1'); - first.year = 2989; - expect(first.year).toEqual(2000); - }); -}); diff --git a/rpgsaga/saga/tests/polymorphism.spec.ts b/rpgsaga/saga/tests/polymorphism.spec.ts new file mode 100644 index 0000000..cbbd36c --- /dev/null +++ b/rpgsaga/saga/tests/polymorphism.spec.ts @@ -0,0 +1,38 @@ + +import { Animal } from "../src/polymorphism/animal"; +import { Mode } from "../src/polymorphism/animal"; +import { Fox } from "../src/polymorphism/fox"; +import { chinchilla } from "../src/polymorphism/chinchilla"; + +describe('Testing Animals constructor', () => { + it('Animals should be created with incorrect age', () => { + const Evgenia = new chinchilla(200, "Белая"); + const Karina = new Fox(200, "Чёрная"); + expect(Evgenia.year).toEqual(0) + expect(Karina.year).toEqual(0) + expect(Evgenia._color).toEqual("Белая") + expect(Karina._color).toEqual("Чёрная") + }); + + it("Animals should be created", () => { + const Evgenia = new chinchilla(19, "Белая"); + const Karina = new Fox(19, "Чёрная"); + expect(Evgenia.year).toEqual(19) + expect(Karina.year).toEqual(19) + }); +}); + +describe('Testing Animals methods', () => { + it("Method toString", () => { + const Evgenia = new chinchilla(19, "Белая"); + const Karina = new Fox(19, "Чёрная"); + expect(`${Evgenia}`).toEqual("Это Шиншила"); + expect(`${Karina}`).toEqual("Это Лиса"); + }); + it("Method sound", () => { + const Evgenia = new chinchilla(19, "Белая"); + const Karina = new Fox(19, "Чёрная"); + expect(Karina.sound()).toEqual("Миу"); + expect(Evgenia.sound()).toEqual("Фыр-фыр"); + }); +}); \ No newline at end of file diff --git a/rpgsaga/saga/tsconfig.json b/rpgsaga/saga/tsconfig.json index 03a0e5a..46ca4e2 100644 --- a/rpgsaga/saga/tsconfig.json +++ b/rpgsaga/saga/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { - "types": ["jest", "node"] + "types": ["jest", "node"], + "target":"ES6" } } \ No newline at end of file