0
|
1 |
// vigyazz6! autplayer |
|
2 |
// |
|
3 |
|
1
|
4 |
use core::{fmt, panic}; |
0
|
5 |
use std::collections::VecDeque; |
|
6 |
use rand::Rng; |
|
7 |
use std::mem; |
|
8 |
|
|
9 |
fn main() { |
1
|
10 |
game(); |
0
|
11 |
} |
|
12 |
|
|
13 |
#[derive(Debug)] |
|
14 |
struct Card { |
|
15 |
value: i8, |
|
16 |
points: i8, |
|
17 |
} |
|
18 |
|
|
19 |
impl Card { |
|
20 |
fn new(value: i8)->Self { |
|
21 |
|
|
22 |
let mut points = 0; |
|
23 |
if value % 10 == 5 { |
|
24 |
// ends with 5 = 2 point |
|
25 |
points = 2; |
|
26 |
// println!("*5 add 1, val={}, pt={}", value, points); |
|
27 |
} |
|
28 |
|
|
29 |
if value % 10 == 0 { |
|
30 |
// ends with 0 = 3 point |
|
31 |
points = 3; |
|
32 |
// println!("*0 add 2, val={}, pt={}", value, points); |
|
33 |
} |
|
34 |
|
|
35 |
if value % 10 == value / 10 { |
|
36 |
// same numbers = 5 points (55=7) |
|
37 |
points += 5; |
|
38 |
// println!("NN add 5, val={}, pt={}", value, points); |
|
39 |
} |
|
40 |
|
|
41 |
if points == 0 { |
|
42 |
points = 1; |
|
43 |
} |
|
44 |
|
|
45 |
Card { |
|
46 |
value, |
|
47 |
points, |
|
48 |
} |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
impl fmt::Display for Card { |
|
53 |
fn fmt( &self, f: &mut fmt::Formatter ) -> fmt::Result { |
|
54 |
write!(f, "(Card {}, points {})", self.value, self.points) |
|
55 |
} |
|
56 |
} |
|
57 |
|
|
58 |
impl PartialEq for Card { |
|
59 |
fn eq(&self, other: &Self) -> bool { |
|
60 |
self.value == other.value |
|
61 |
} |
|
62 |
} |
|
63 |
|
1
|
64 |
impl Eq for Card {} |
|
65 |
|
|
66 |
impl PartialOrd for Card { |
|
67 |
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
|
68 |
match self.value.partial_cmp(&other.value) { |
|
69 |
Some(core::cmp::Ordering::Equal) => {None} |
|
70 |
ord => return ord, |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
impl Ord for Card { |
|
76 |
fn cmp(&self, other: &Self) -> std::cmp::Ordering { |
|
77 |
self.value.cmp(&other.value) |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
|
|
82 |
#[derive(Debug)] |
0
|
83 |
struct Deck { |
|
84 |
content: VecDeque<Card>, |
|
85 |
} |
|
86 |
|
|
87 |
impl Deck { |
|
88 |
fn new_empty() -> Self { |
|
89 |
Deck { |
|
90 |
content: VecDeque::new(), |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
fn new() -> Self { |
|
95 |
let content = (1..104).into_iter().map( |n| Card::new(n) ).collect(); |
|
96 |
Deck { |
|
97 |
content, |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
fn shuffle( &mut self ) { |
|
102 |
let mut rng = rand::thread_rng(); |
|
103 |
|
|
104 |
// shufflers: |
|
105 |
// * naive: swap cards n times |
|
106 |
// * kgb: half the deck, take 1..4 cards sequentially from each |
|
107 |
// * grin: take 1..6 from front and put at bottom |
|
108 |
|
|
109 |
// naive shuffle: exchange random cards |
1
|
110 |
for _i in 1..=500 { |
|
111 |
let c1 = rng.gen_range(0 .. self.content.len()); |
|
112 |
let c2 = rng.gen_range(0 .. self.content.len()); |
0
|
113 |
if c1 != c2 { |
|
114 |
self.content.swap(c1, c2); |
|
115 |
} |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
// get top card from deck |
|
120 |
fn pop( &mut self ) -> Option<Card> { |
|
121 |
self.content.pop_front() |
|
122 |
} |
|
123 |
|
|
124 |
// put a card into the bottom of the deck |
|
125 |
fn push( &mut self, c: Card ) { |
|
126 |
self.content.push_back(c); |
|
127 |
} |
|
128 |
|
|
129 |
fn len( &self ) -> usize { |
|
130 |
self.content.len() |
|
131 |
} |
|
132 |
|
|
133 |
fn get_nth( &mut self, n: usize ) -> Card { |
|
134 |
if let Some(c) = self.content.remove(n) { |
|
135 |
c |
|
136 |
} else { |
|
137 |
panic!("get_nth: index {} out of bounds ({})!", n, self.content.len()); |
|
138 |
} |
|
139 |
} |
|
140 |
} |
|
141 |
|
1
|
142 |
#[derive(Debug)] |
0
|
143 |
struct Player { |
|
144 |
name: String, |
|
145 |
hand: Deck, |
|
146 |
pile: Vec<Card>, |
|
147 |
game_point: i32, |
|
148 |
total_point: i32, |
|
149 |
rows_busted: i32, |
|
150 |
} |
|
151 |
|
|
152 |
impl Player { |
|
153 |
fn new(name: String)->Self { |
|
154 |
Player { |
|
155 |
name, |
|
156 |
hand: Deck::new_empty(), |
|
157 |
pile: Vec::new(), |
|
158 |
game_point: 0, |
|
159 |
total_point: 0, |
|
160 |
rows_busted: 0, |
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 |
// get one card from th dealer |
|
165 |
fn get_card( &mut self, card: Card ) { |
|
166 |
self.hand.push(card); |
|
167 |
} |
|
168 |
|
|
169 |
// throw a card from hand to the table |
|
170 |
fn throw_card( &mut self )->Card { |
|
171 |
if let Some(c) = self.hand.pop() { |
|
172 |
c |
|
173 |
} else { |
|
174 |
panic!("throw_card: Player {} has no card in hand!", self.name); |
|
175 |
} |
|
176 |
} |
|
177 |
|
|
178 |
// get a busted row of cards |
1
|
179 |
fn get_pile( &mut self, cards: VecDeque<Card> ) { |
0
|
180 |
for c in cards.into_iter() { |
|
181 |
self.game_point += c.points as i32; |
|
182 |
self.pile.push(c); |
|
183 |
} |
|
184 |
self.rows_busted += 1; |
|
185 |
} |
|
186 |
|
|
187 |
// ask the player their score |
|
188 |
fn tell_points( self ) -> i32 { |
|
189 |
self.game_point |
|
190 |
} |
|
191 |
|
|
192 |
// give back cards from the pile |
|
193 |
fn give_pile( &mut self ) -> Vec<Card> { |
|
194 |
mem::take( &mut self.pile ) |
|
195 |
// same effect: |
|
196 |
// self.pile.drain(..).collect() |
|
197 |
|
|
198 |
// very cumbersome manual fiddling (also reverted...) |
|
199 |
/* let mut throw: Vec<Card> = Vec::new(); |
|
200 |
for _i in 0 .. self.pile.len() { |
|
201 |
throw.push( self.pile.pop().unwrap() ); |
|
202 |
} |
|
203 |
throw |
|
204 |
*/ |
|
205 |
} |
|
206 |
|
|
207 |
// I can do this just because I *throw away* c! |
|
208 |
// doesn't work if I want to use it. |
|
209 |
/* fn _gimme_pile(self)->Self { |
|
210 |
for c in &self.pile { |
|
211 |
println!("Throw {} ", c); |
|
212 |
} |
|
213 |
self |
|
214 |
} |
|
215 |
*/ |
|
216 |
fn close_round( &mut self ) { |
|
217 |
if self.hand.len() > 0 { |
|
218 |
panic!("Closing round when {} has {} cards in hand", self.name, self.hand.len()); |
|
219 |
} |
|
220 |
|
|
221 |
if self.pile.len() > 0 { |
|
222 |
panic!("Closing round when {} stil have pile with {} cards", self.name, self.pile.len()); |
|
223 |
} |
|
224 |
|
|
225 |
self.total_point += self.game_point; |
|
226 |
self.game_point = 0; |
|
227 |
} |
|
228 |
} |
|
229 |
|
|
230 |
// a row of cards on the table (max 5) |
1
|
231 |
#[derive(Debug)] |
0
|
232 |
struct Row { |
|
233 |
cards: VecDeque<Card>, |
|
234 |
} |
|
235 |
|
|
236 |
impl Row { |
|
237 |
const MAX_LEN: usize = 5; |
|
238 |
|
|
239 |
fn new() -> Self { |
|
240 |
Row { |
|
241 |
cards: VecDeque::new(), |
|
242 |
} |
|
243 |
} |
|
244 |
|
|
245 |
fn push_or_collect( &mut self, card: Card ) -> Option<VecDeque<Card>> { |
|
246 |
if self.cards.len() < Self::MAX_LEN { |
|
247 |
self.cards.push_back(card); |
|
248 |
None |
|
249 |
|
|
250 |
} else { |
|
251 |
// row overflow |
|
252 |
let row_cards = mem::take( &mut self.cards ); |
|
253 |
self.cards.push_back(card); |
|
254 |
if self.cards.len() != 1 { |
|
255 |
panic!("New row must have one card, not {}", self.cards.len()); |
|
256 |
} |
|
257 |
Some(row_cards) |
|
258 |
} |
|
259 |
} |
|
260 |
|
|
261 |
fn last_card_value(&self) -> i8 { |
|
262 |
println!("last_card_value: cards {:?}, len {}", self.cards, self.cards.len()); |
|
263 |
self.cards.get( self.cards.len()-1 ).unwrap().value |
|
264 |
} |
|
265 |
} |
|
266 |
|
1
|
267 |
#[derive(Debug)] |
0
|
268 |
struct PlayerCard { |
1
|
269 |
player_id: i32, |
0
|
270 |
card: Card, |
|
271 |
} |
|
272 |
|
1
|
273 |
impl PlayerCard { |
|
274 |
fn get_player(&self) -> i32 { |
|
275 |
self.player_id |
|
276 |
} |
|
277 |
} |
|
278 |
|
|
279 |
impl PartialEq for PlayerCard { |
|
280 |
fn eq(&self, other: &Self) -> bool { |
|
281 |
self.card == other.card |
|
282 |
} |
|
283 |
} |
|
284 |
|
|
285 |
impl PartialOrd for PlayerCard { |
|
286 |
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
|
287 |
match self.card.partial_cmp(&other.card) { |
|
288 |
Some(core::cmp::Ordering::Equal) => {None} |
|
289 |
ord => return ord, |
|
290 |
} |
|
291 |
} |
|
292 |
} |
|
293 |
|
|
294 |
#[derive(Debug)] |
0
|
295 |
struct Table { |
|
296 |
rows: Vec<Row>, |
|
297 |
player_cards: Vec<PlayerCard>, // owned by a player |
|
298 |
} |
|
299 |
|
1
|
300 |
impl Table { |
|
301 |
fn new(row_cards: Vec<Card>) -> Self { |
|
302 |
let mut rows = Vec::new(); |
|
303 |
for card in row_cards { |
|
304 |
// create a new row then put a card into it |
|
305 |
let mut row = Row::new(); |
|
306 |
if let Some(c) = row.push_or_collect(card) { |
|
307 |
panic!("Freshly created row overflowed"); |
|
308 |
} |
|
309 |
rows.push( row ); |
|
310 |
} |
0
|
311 |
|
1
|
312 |
Table { |
|
313 |
rows, |
|
314 |
player_cards: Vec::new(), |
|
315 |
} |
|
316 |
} |
|
317 |
|
|
318 |
fn lay_player_card( &mut self, card: Card, player_id: i32 ) { |
|
319 |
self.player_cards.push( PlayerCard { player_id, card } ); |
|
320 |
} |
|
321 |
|
|
322 |
fn sort_cards( &mut self ) { |
|
323 |
self.player_cards.sort_by( |a,b| b.card.cmp(&a.card) ); |
|
324 |
} |
|
325 |
|
|
326 |
fn get_smallest_player_card( &mut self ) -> PlayerCard { |
|
327 |
self.player_cards.pop().expect("out of player cards on table") |
|
328 |
} |
|
329 |
|
|
330 |
fn get_closest_row( &self, pcard: &PlayerCard ) -> Option<i8> { |
|
331 |
// get the row id with last card closest smaller to players' |
|
332 |
todo!() |
|
333 |
} |
|
334 |
|
|
335 |
fn put_card_into_row( &mut self, pcard: PlayerCard, row_id: i8 ) -> Option<VecDeque<Card>> { |
|
336 |
self.rows[row_id as usize].push_or_collect(pcard.card) |
|
337 |
} |
|
338 |
} |
0
|
339 |
|
|
340 |
|
|
341 |
fn game() { |
1
|
342 |
let mut cnt_shuffle = 0; |
|
343 |
|
|
344 |
let mut deck = Deck::new(); |
|
345 |
|
|
346 |
let player_names = vec![ "grin", "moni", "icbalint", "orsi", "topi", "kgb", "zsu", "csilla" ]; |
|
347 |
let mut players: Vec<Player> = player_names.iter().map( |n| Player::new(n.to_string()) ).collect(); |
|
348 |
|
|
349 |
let player_count = players.len(); // pc - 1 |
|
350 |
|
|
351 |
for _cnt_game in 1..=2 { |
|
352 |
deck.shuffle(); |
|
353 |
cnt_shuffle += 1; |
|
354 |
|
|
355 |
// dealing |
|
356 |
for i in 1..=10 { |
|
357 |
for player in 0 .. player_count { |
|
358 |
players[player].get_card( deck.pop().expect("Deck is empty while dealing to players") ); |
|
359 |
} |
|
360 |
} |
|
361 |
|
|
362 |
// we need 5 crds from deck |
|
363 |
let mut cards = Vec::new(); |
|
364 |
for i in 1..=5 { |
|
365 |
cards.push( deck.pop().expect("deck empty before starting the game") ); |
|
366 |
} |
|
367 |
println!("We push 5 cards to rows: {:?}\n", cards); |
|
368 |
let mut table = Table::new(cards); |
|
369 |
|
|
370 |
// DEBUG |
|
371 |
println!("Table: {:?}\n", table); |
|
372 |
println!("PLayers: {:?}\n", players); |
|
373 |
println!("Deck: {:?}\n", deck); |
|
374 |
|
|
375 |
|
|
376 |
// playing |
|
377 |
for turn in 1..=10 { |
0
|
378 |
|
1
|
379 |
// everyone puts a card face down |
|
380 |
for player in 0 .. player_count { |
|
381 |
let player_id: i32 = player.try_into().unwrap(); |
|
382 |
// get a card from the player |
|
383 |
let topcard = players[player].throw_card(); |
|
384 |
// put it on the table ("turned face down") |
|
385 |
table.lay_player_card( topcard, player_id ); |
|
386 |
} |
|
387 |
|
|
388 |
// process cards |
|
389 |
table.sort_cards(); |
|
390 |
|
|
391 |
let smallest = table.get_smallest_player_card(); |
0
|
392 |
|
1
|
393 |
let closest_row = table.get_closest_row(&smallest); |
|
394 |
match closest_row { |
|
395 |
Some(rowid) => { |
|
396 |
let player_id: usize = smallest.player_id.try_into().unwrap(); |
|
397 |
let overflow = table.put_card_into_row(smallest, rowid); |
|
398 |
if let Some(cards) = overflow { |
|
399 |
// row is full, got pile |
|
400 |
// player gets pile, card gets into row head |
|
401 |
players[ player_id ].get_pile( cards ); |
|
402 |
} |
|
403 |
}, |
|
404 |
None => { |
|
405 |
// card too small, need to pick row! |
|
406 |
todo!(); |
|
407 |
} |
|
408 |
} |
|
409 |
|
|
410 |
} |
|
411 |
|
|
412 |
// end of round |
|
413 |
|
|
414 |
// recollect deck |
|
415 |
|
|
416 |
panic!("FInish now."); |
0
|
417 |
} |
|
418 |
} |
|
419 |
|
|
420 |
#[cfg(test)] |
|
421 |
mod tests { |
1
|
422 |
use core::panic; |
|
423 |
use std::collections::VecDeque; |
|
424 |
|
|
425 |
use rand::Rng; |
|
426 |
|
0
|
427 |
use crate::{Card, Player, Row}; |
|
428 |
|
|
429 |
#[test] |
|
430 |
fn card_values() { |
|
431 |
let card_values = vec![1,2,5,10,33,55,77]; |
|
432 |
let card_points = vec![1,1,2,3, 5, 7, 5]; |
|
433 |
for i in 1 .. card_values.len() { |
|
434 |
let c = Card::new( card_values[i] ); |
|
435 |
let p = c.points; |
|
436 |
assert!(p == card_points[i], "card={} card points={} i={} expected point={}", card_values[i], p, i, card_points[i]); |
|
437 |
} |
|
438 |
} |
|
439 |
|
|
440 |
#[test] |
|
441 |
fn player_take_pile() { |
|
442 |
// create a player |
|
443 |
let mut p = Player::new("bob".to_string()); |
|
444 |
// create a pile |
1
|
445 |
let mut pile = VecDeque::new(); |
0
|
446 |
let mut refpile = Vec::new(); |
|
447 |
for i in 5..10 { |
|
448 |
let c = Card::new(i); |
1
|
449 |
pile.push_back(c); |
0
|
450 |
let c = Card::new(i); |
|
451 |
refpile.push(c); |
|
452 |
} |
|
453 |
// add the pile to player |
|
454 |
p.get_pile(pile); |
|
455 |
assert!( p.rows_busted == 1 ); |
|
456 |
|
|
457 |
// get back the pile from player |
|
458 |
// p = p.gimme_pile(); |
|
459 |
let pile = p.give_pile(); |
|
460 |
|
|
461 |
// the pile we got shall be same as the pile we gave |
|
462 |
// this check is O(n^2), doesn't matter for less than 100 items |
|
463 |
assert!( pile.iter().all( |item| refpile.contains(item)) ); |
|
464 |
|
1
|
465 |
let mut pile = VecDeque::new(); |
|
466 |
for i in 4..=9 { |
0
|
467 |
let c = Card::new(i); |
1
|
468 |
pile.push_back(c); |
0
|
469 |
} |
|
470 |
p.get_pile(pile); |
|
471 |
assert!( p.rows_busted == 2 ); |
|
472 |
} |
|
473 |
|
|
474 |
#[test] |
|
475 |
fn row_push() { |
|
476 |
let mut row = Row::new(); |
|
477 |
let mut refcard = Vec::new(); // reference vec to check |
|
478 |
for i in 1..=7 { |
|
479 |
let cval = i+5; |
|
480 |
let card = Card::new(cval); |
|
481 |
// push a card into the row |
|
482 |
if let Some(cards) = row.push_or_collect(card) { |
|
483 |
// got the overflow |
|
484 |
println!("Got overflow row at {}!", i); |
|
485 |
assert!( i == 6, "Overflow at wrong position: {} != 6", i ); |
|
486 |
// we need to get the proper vec |
|
487 |
assert!( cards.iter().all( |item| refcard.contains(item) ), "Got cards {:?}", cards ); |
|
488 |
} else { |
|
489 |
println!("push success {}", i); |
|
490 |
} |
|
491 |
// remember the correct vec for checking |
|
492 |
let card = Card::new(cval); |
|
493 |
refcard.push(card); |
|
494 |
|
|
495 |
// check card value |
|
496 |
assert!( row.last_card_value() == cval, "Last card value mismatch: got {} vs expected {}", row.last_card_value(), cval ); |
|
497 |
} |
|
498 |
assert!( row.cards.len() == 2, "Row contains wrong amount of cards: {}", row.cards.len() ); |
|
499 |
} |
1
|
500 |
|
|
501 |
#[test] |
|
502 |
fn sort_cards() { |
|
503 |
let mut cards: Vec<Card> = Vec::new(); |
|
504 |
let mut rng = rand::thread_rng(); |
|
505 |
for i in 1..50 { |
|
506 |
let n = rng.gen_range(1..104); |
|
507 |
cards.push( Card::new(n) ); |
|
508 |
} |
|
509 |
cards.sort(); |
|
510 |
|
|
511 |
for i in 1..cards.len() { |
|
512 |
assert!( cards[i-1].value <= cards[i].value, "Bad ordering: {} > {}", cards[i-1].value,cards[i].value ); |
|
513 |
} |
|
514 |
} |
|
515 |
|
0
|
516 |
} |
|
517 |
|
|
518 |
/* |
|
519 |
|
|
520 |
- 1-104 lap, |
|
521 |
- *5 - 2 |
|
522 |
- *0 - 3 |
|
523 |
- NN - 5 |
|
524 |
- 55 - 7 |
|
525 |
- deck |
|
526 |
- jatekosok; kezben tartott lapok; elvitt lapok; pontszamok; counter: elvitt sorok, okrok, total pont |
|
527 |
-- keveres (keveresek szama) |
|
528 |
-- osztas: mindenki 10 lap |
|
529 |
-- start sorok: 5 kartya |
|
530 |
- jatek (jatekok szama) |
|
531 |
-- mindenki a felso lapot kiteszi |
|
532 |
-- szamsorrendben felkerulnek, aki viszi, viszi |
|
533 |
--- ha kisebb, akkor dontes |
|
534 |
---- ha van legkevesebb, viszi |
|
535 |
---- ha tobb min van, random valaszt |
|
536 |
- osszesites |
|
537 |
-- okrok, elvitt sorok |
|
538 |
-- keveresek szama, jatekok szama, eltelt ido |
|
539 |
- deck osszegyujtes |
|
540 |
|
|
541 |
|
|
542 |
*/ |