Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cat class update and rpg saga #9

Open
wants to merge 20 commits into
base: Frolov_Egor_Alekseevich
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions codewars/Array Deep Count/Array_Deep_Count.js
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions codewars/Build Tower/Build_Tower.js
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions codewars/Convert string to camel case/Convert_Str_to_Camel_Case.js
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions codewars/Duplicate Encoder/Duplicate_Encoder.js
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions codewars/Merge two arrays/Merge_Two_Arrays.js
Original file line number Diff line number Diff line change
@@ -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);
}
12 changes: 12 additions & 0 deletions codewars/Moving Zeros To The End/Moving_Zeros_to_End.js
Original file line number Diff line number Diff line change
@@ -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);
}
14 changes: 14 additions & 0 deletions codewars/Sum of Digits - Digital Root/Sum_of_Digits.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
15 changes: 15 additions & 0 deletions codewars/Sum of pairs/Sum_Of_Pairs.js
Original file line number Diff line number Diff line change
@@ -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;
}
49 changes: 49 additions & 0 deletions rpgsaga/saga/src/Classes.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
115 changes: 115 additions & 0 deletions rpgsaga/saga/src/Game.ts
Original file line number Diff line number Diff line change
@@ -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<T>(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;
}



20 changes: 20 additions & 0 deletions rpgsaga/saga/src/Logger.ts
Original file line number Diff line number Diff line change
@@ -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} замерзает и пропускает ход`);
}
}
26 changes: 26 additions & 0 deletions rpgsaga/saga/src/Player.ts
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions rpgsaga/saga/src/cat.ts
Original file line number Diff line number Diff line change
@@ -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}.`
}
}
20 changes: 6 additions & 14 deletions rpgsaga/saga/src/index.ts
Original file line number Diff line number Diff line change
@@ -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();
Loading