source: src/main/java/onetomany/bargainingchipsgame/Bundle.java@ 265

Last change on this file since 265 was 263, checked in by Faria Nassiri Mofakham, 5 years ago

Offer class updated; Message enum created; parapoloid tried in GUI; some small changes in some previous classes (Bundle, stack,.. ); UF_Silly renamed to UF_Crazy; started to add role to UF classes...

File size: 5.3 KB
RevLine 
[233]1/**
2 * Bundle class
3 */
[253]4package onetomany.bargainingchipsgame;
[233]5
[240]6import java.util.List;
7
[233]8import java.util.ArrayList;
[236]9import java.util.Arrays;
[240]10import java.util.Iterator;
[233]11
12/**
13 * Offers are exchanged in Bundle formats.
14 * A Bundle is a set of several stacks, {s_1, ..., s_n}, where no two stacks are of the same color.
[243]15 * A stack can be aggregated into a bundle, if at the same color of one of the Bundle's stacks.
[233]16 * Two bundles can also be aggregated into a new bundle, where every two stacks of the same colors from both bundles are aggregated into one stack in the new bundle.
17 * Aggregation of a bundle with the empty stack or empty bundle is itself.
18 *
19 * @author Faria Nassiri-Mofakham
20 *
21 */
[240]22public class Bundle implements Iterable<Stack>
[239]23{
[233]24 private List<Stack> bundle;
25
26 public Bundle()
27 {
28 this.bundle = new ArrayList<Stack>();
29 }
30
31 /**
32 * @return the bundle
33 */
[239]34 public List<Stack> getBundle()
35 {
[233]36 return bundle;
37 }
38
39 /**
40 * @param b the bundle to set
41 */
[239]42 public void setBundle(List<Stack> b)
43 {
[233]44 this.bundle = b;
45 }
46
47 public void addStack(Stack s)
48 {
49 bundle.add(s);
50 }
51
52 @Override
53 public String toString()
54 {
55 return bundle.toString();
56 }
57
58 public int size()
59 {
60 if (bundle != null)
61 {
62 return getBundle().size();
63 }
64 else return 0;
65 }
66
67 public Stack getStack(int i)
68 {
69 return getBundle().get(i);
70 }
71
[240]72 public Iterator<Stack> iterator()
[233]73 {
[240]74 return bundle.iterator();
[233]75 }
76
[236]77
[243]78 public Double getPrice()
79 {
80 double sum=0.0;
81 for (Stack t: bundle)
82 sum += t.getPrice();
83 return sum;
84 }
85
[236]86 /**
87 *
88 * bundle "+" bundle
89 *
90 * @param b, this method adds bundle b to `this' bundle
91 * @return the aggregated bundle
92 * where, any two stacks of the same colors are aggregated as a new stack into the new bundle, otherwise, they are just added into the new bundle.
93 *
94 * This method just calls stack '+' stack aggregation operation (has no need to bundle "+" stack aggregation operation.
95 */
[243]96 public Bundle aggregateWith(Bundle b) //This WORKED WELL although it is not in nice programming style (without calling aggregate with stack, below !
[236]97 {
98 if (b!=null && this!=null)
99 {
100 Bundle l=new Bundle();
101 Boolean[] f1 = new Boolean[b.size()]; //flag for b bundle stack added
102 Arrays.fill(f1, Boolean.FALSE);
103 Boolean[] f2 = new Boolean[this.size()]; //flag for `this' bundle stack added
104 Arrays.fill(f2, Boolean.FALSE);
105
106
107 for (int i=0;i<b.size();i++) //loop over bundle b stacks
108 {
109 Stack s=b.getStack(i);
110
111 for (int j=0; j<this.size();j++) //loop over `this' bundle stacks
112 {
113 Stack t= this.getStack(j);
[243]114 if (!f1[i] && !f2[j] && t.getColor()==s.getColor())
[236]115 {
116 l.addStack(t.aggregateWith(s)); //aggregate these two stacks (b's i-th and this j-th) into the new bundle l
117 f1[i]=Boolean.TRUE;
118 f2[j]=Boolean.TRUE;
119 }
120 }
121 if (!f1[i])
122 l.addStack(s); // so put i-th stack of bundle b directly into the new bundle
123 }
124 for (int j=0;j<this.size();j++)
125 if (!f2[j])
126 l.addStack(this.getStack(j)); // so put j-th stack of `this' bundle directly into the new bundle
127 return l;
128 }
129 return this;
130 }
131
[239]132
[243]133 // previous try for calling two methods both with using iterators
[239]134
[240]135 /**
136 * bundle "+" bundle : second method (nested iterators by the other method), doesn't work correctly.
137 *
138 * @param b, this method adds bundle b to `this' bundle
139 * @return the aggregated bundle
140 * where, any two stacks of the same colors are aggregated as a new stack into the new bundle, otherwise, they are just added into the new bundle.
141 *
142 * This method calls bundle "+" stack aggregation operation.
143 */
[243]144 public Bundle aggregateWith333(Bundle b)
145 {
146 if (b!=null && this!=null)
147 {
[263]148 //Bundle l=new Bundle();
149
[243]150 for (Stack s : b) //loop over bundle b stacks
[263]151 this.aggregateWith333(s); //aggregate stacks of b into `this' bundle by calling aggregateWith(stack), below
152 }
[243]153 return this;
154 }
155
156
157
158 /**
159 * bundle '+' stack
160 *
161 * @param s, this method adds stack s to `this' bundle
162 * @return the aggregated bundle
163 * where, stack s is aggregated with a stack of the same color in `this' bundle and the new stack is added into the new bundle;
164 * otherwise, stack s is just added into the new bundle.
165 *
166 * This method is called by
167 */
168 public Bundle aggregateWith333(Stack s)
169 {
[263]170 Bundle b=new Bundle();
171
[243]172 if (s!=null && this!=null)
173 {
174 Boolean f=Boolean.FALSE; //flag for stack s added
[263]175
176 for (Stack t : bundle) //loop over `this' bundle stacks
177 {
178 if (t.hasSameColorAs(s)) //if the stacks t and s are of the same colors
[243]179 {
[263]180 System.out.println("\n hey it is t "+t+" and it is s "+s);
181 b.addStack(s.aggregateWith(t)); //aggregate stacks t and s and then add to the new bundle
182 System.out.println("\n hey1 it is b "+b);
[243]183 f=true;
184 }
185 else
[263]186 {b.addStack(t); // so put the stack of `this' bundle directly into the new bundle
187 System.out.println("\n hey2 it is b "+b);
188
189 }
[243]190 }
191 if (!f) //s not aggregated with any stack in `this' bundle
[263]192 {b.addStack(s); // so put stack s directly into the new bundle
193 System.out.println("\n hey3 it is b "+b);
194 }
[243]195 return b;
196 }
[263]197 return b;
[243]198 }
199
200
201
[233]202}
Note: See TracBrowser for help on using the repository browser.