Advertisement
1cutcut1

treta

Nov 2nd, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.49 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Card {
  4.     private int type;
  5.     private int health;
  6.     private int magicPower;
  7.  
  8.     public Card(int type, int health, int magicPower) {
  9.         this.type = type;
  10.         this.health = health;
  11.         this.magicPower = magicPower;
  12.     }
  13.  
  14.     public int getType() {
  15.         return type;
  16.     }
  17.  
  18.     public void setType(int type) {
  19.         this.type = type;
  20.     }
  21.  
  22.     public int getHealth() {
  23.         return health;
  24.     }
  25.  
  26.     public void setHealth(int health) {
  27.         this.health = health;
  28.     }
  29.  
  30.     public int getMagicPower() {
  31.         return magicPower;
  32.     }
  33.  
  34.     public void setMagicPower(int magicPower) {
  35.         this.magicPower = magicPower;
  36.     }
  37.  
  38.     public int significance() {
  39.         return health * magicPower;
  40.     }
  41.  
  42.  
  43.     @Override
  44.     public String toString() {
  45.         return String.valueOf(type);
  46.     }
  47. }
  48.  
  49. class SLLNode<E> {
  50.     protected E element;
  51.     protected SLLNode<E> succ;
  52.  
  53.     public SLLNode(E elem, SLLNode<E> succ) {
  54.         this.element = elem;
  55.         this.succ = succ;
  56.     }
  57.  
  58.     @Override
  59.     public String toString() {
  60.         return element.toString();
  61.     }
  62. }
  63.  
  64. class SLL<E> {
  65.     private SLLNode<E> first;
  66.  
  67.     public SLL() {
  68.         this.first = null;
  69.     }
  70.  
  71.     public void deleteList() {
  72.         first = null;
  73.     }
  74.  
  75.     public int size() {
  76.         int ret;
  77.         if (first != null) {
  78.             SLLNode<E> tmp = first;
  79.             ret = 1;
  80.             while (tmp.succ != null) {
  81.                 tmp = tmp.succ;
  82.                 ret++;
  83.             }
  84.             return ret;
  85.         } else
  86.             return 0;
  87.  
  88.     }
  89.  
  90.     @Override
  91.     public String toString() {
  92.         String ret = new String();
  93.         if (first != null) {
  94.             SLLNode<E> tmp = first;
  95.             ret += tmp;
  96.             while (tmp.succ != null) {
  97.                 tmp = tmp.succ;
  98.                 ret += " " + tmp;
  99.             }
  100.         } else
  101.             ret = "Prazna lista!!!";
  102.         return ret;
  103.     }
  104.  
  105.     public void insertFirst(E o) {
  106.         SLLNode<E> ins = new SLLNode<E>(o, first);
  107.         first = ins;
  108.     }
  109.  
  110.     public void insertAfter(E o, SLLNode<E> node) {
  111.         if (node != null) {
  112.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  113.             node.succ = ins;
  114.         } else {
  115.             System.out.println("Dadenot jazol e null");
  116.         }
  117.     }
  118.  
  119.     public void insertBefore(E o, SLLNode<E> before) {
  120.         if (first != null) {
  121.             SLLNode<E> tmp = first;
  122.             if (first == before) {
  123.                 this.insertFirst(o);
  124.                 return;
  125.             }
  126.             while (tmp.succ != before)
  127.                 tmp = tmp.succ;
  128.             if (tmp.succ == before) {
  129.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  130.                 tmp.succ = ins;
  131.             } else {
  132.                 System.out.println("Elementot ne postoi vo listata");
  133.             }
  134.         } else {
  135.             System.out.println("Listata e prazna");
  136.         }
  137.     }
  138.  
  139.     public void insertLast(E o) {
  140.         if (first != null) {
  141.             SLLNode<E> tmp = first;
  142.             while (tmp.succ != null)
  143.                 tmp = tmp.succ;
  144.             SLLNode<E> ins = new SLLNode<E>(o, null);
  145.             tmp.succ = ins;
  146.         } else {
  147.             insertFirst(o);
  148.         }
  149.     }
  150.  
  151.     public E deleteFirst() {
  152.         if (first != null) {
  153.             SLLNode<E> tmp = first;
  154.             first = first.succ;
  155.             return tmp.element;
  156.         } else {
  157.             System.out.println("Listata e prazna");
  158.             return null;
  159.         }
  160.     }
  161.  
  162.     public E delete(SLLNode<E> node) {
  163.         if (first != null) {
  164.             SLLNode<E> tmp = first;
  165.             if (first == node) {
  166.                 return this.deleteFirst();
  167.             }
  168.             while (tmp.succ != node && tmp.succ.succ != null)
  169.                 tmp = tmp.succ;
  170.             if (tmp.succ == node) {
  171.                 tmp.succ = tmp.succ.succ;
  172.                 return node.element;
  173.             } else {
  174.                 System.out.println("Elementot ne postoi vo listata");
  175.                 return null;
  176.             }
  177.         } else {
  178.             System.out.println("Listata e prazna");
  179.             return null;
  180.         }
  181.     }
  182.  
  183.     public SLLNode<E> getFirst() {
  184.         return first;
  185.     }
  186.  
  187.     public SLLNode<E> find(E o) {
  188.         if (first != null) {
  189.             SLLNode<E> tmp = first;
  190.             while (tmp.element != o && tmp.succ != null)
  191.                 tmp = tmp.succ;
  192.             if (tmp.element == o) {
  193.                 return tmp;
  194.             } else {
  195.                 System.out.println("Elementot ne postoi vo listata");
  196.             }
  197.         } else {
  198.             System.out.println("Listata e prazna");
  199.         }
  200.         return first;
  201.     }
  202. }
  203.  
  204. public class MysticalCardGame {
  205.  
  206.     //TODO: implement function
  207.     public static void startDuel(SLL<Card> firstSorcererCards, SLL<Card> secondSorcererCards) {
  208.         SLLNode<Card> move = firstSorcererCards.getFirst();
  209.         SLLNode<Card> max = firstSorcererCards.getFirst();
  210.         while (move != null) {
  211.             if (move.element.significance() > max.element.significance()) {
  212.                 max = move;
  213.             }
  214.             move = move.succ;
  215.         }
  216.  
  217.         SLLNode<Card> move2 = secondSorcererCards.getFirst();
  218.         for (int i = 0; i < secondSorcererCards.size()/2; i++) {
  219.             move2 = move2.succ;
  220.         }
  221.         secondSorcererCards.insertBefore(max.element, move2);
  222.         firstSorcererCards.delete(max);
  223.     }
  224.  
  225.  
  226.     public static void main(String[] args) {
  227.         Scanner scanner = new Scanner(System.in);
  228.         SLL<Card> firstSorcererCards = new SLL<Card>();
  229.         SLL<Card> secondSorcererCards = new SLL<Card>();
  230.  
  231.         for (int i = 0; i < 6; i++) {
  232.             String line = scanner.nextLine();
  233.             String[] parts = line.split("\\s+");
  234.             firstSorcererCards.insertLast(new Card(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
  235.         }
  236.  
  237.         for (int i = 0; i < 6; i++) {
  238.             String line = scanner.nextLine();
  239.             String[] parts = line.split("\\s+");
  240.             secondSorcererCards.insertLast(new Card(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2])));
  241.         }
  242.  
  243.         startDuel(firstSorcererCards, secondSorcererCards);
  244.         System.out.println(firstSorcererCards);
  245.         System.out.println(secondSorcererCards);
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement