Assignment 3 Code Review

24 March 2003 - CS 509


Assignment 3 Code Review


Design


The Solution

candidates = play_all_games(hand);
legal = smaller = false;

for (i = 0; i < candidates.size(); i++)
  if target == candidates[i]
    legal = true
  else if candidates[i] < target
    smaller = true  


Playing All The Games

tvec play_all_games(hand)

  tvec candidates, final
  candidates.push_back(tableau(hand))

  while !candidates.empty()
    tableau t = candidates.back()
    candidates.pop_back()
    tvec new_tabs = possible_next_tabs(t)
    if (new_tabs.empty())
      final.push_back(t)
    else
      candidates.insert(
	candidates.end(),
	new_tabs.begin(), new_tabs.end())

  return final


But, Wait a Minute


Can It Really Be That Bad?


Now What?


The Original Problem Reconsidered


Finding A Smaller Tableau

static bool find_smaller(
  tableau tgt, deck hand, tableau & smlr)

  tvec candidates
  candidates.push_back(tableau(hand))

  do
    smlr = candidates.back()
    if smlr < tgt
      return true

    candidates.pop_back()

    tvec new_tabs = 
      possible_next_tableaus(smlr)
    candidates.insert(
      candidates.end(),
      new_tabs.begin(), new_tabs.end())
    std::sort(
      candidates.rbegin(), candidates.rend())
  while !candidates.empty()

  return false


Does Sorting Help?


Target Tableau Search


Compatible Tableaus


Finding A Target Tableau

bool find_target(tableau target, deck hand)

  tableau t
  tvec candidates
  candidates.push_back(tableau(hand))

  do
    do
      t = candidates.back()
      candidates.pop_back()
    while !target.compatible(t) and 
          !candidates.empty()

    if t == target
      return true
    if !target.compatible(t)
      return false

    const tvec new_tabs = 
      possible_next_tableaus(t)
    candidates.insert(
      candidates.end(),
      new_tabs.begin(), new_tabs.end())
  while !candidates.empty()

  return false


Does Compatibility Pruning Help?


Pruning Duplicate Tableaus

static bool find_legal_dp(
  const tableau & target, const deck & hand)

  tvec candidates;
  candidates.push_back(t);
  strset considered;

  while (!candidates.empty())
    t = candidates.back()
    candidates.pop_back()

    if (t == target)
      return true

    tvec nt = possible_next_tableaus(t)
    for b = nt.begin(); b != nt.end(); b++
      std::string s = b->as_string()
      if considered.count(s) == 0
        considered.insert(s)
        candidates.back_insert(*b);

  return false


Does Duplicate Pruning Help?


Duplicate And Compatibility Pruning

if target.compatible(*b) and
   (considered.count(s) == 0)
  considered.insert(s)
  candidates.push_back(*b)


The Test Cases

  1. Empty input.

  2. Garbage input.

  3. Irreducible deck.

  4. Fully collapsible deck.

  5. Non-optimal tableau.

  6. Recycled tableau.


The Results


Line Statistics - Newlines

0001002003005006001300
01111
101
20
301
4011
501
60
70
801
901
Totals1141121


Line Statistics - Semicolons

000100200300
01111
101
2011
301
401
501
60
701
80
90
Totals2522


Procedure Statistics

totalpages per procedure
procs123
541
642
853
871
18144
221642
25214


Follow Directions


Don't Kill Yourself

void Cards::out()
  unsigned i
  for i = 0; i < deal.size(); i += 2
    cout << deal[i] << deal[i+1] << " "
    cout << endl << endl
  for i = 0; i < piles.size(); i++
    cout << piles[i] << endl


Ouch

for int i = 0; i <= line.size(); i++
  if !isspace(line[i])
    // whatever


Understand the Problem


Omit Needless Code


Duplicate Code is Bad Code


No Magic Numbers

int findidx(string & s)
 int i=-1, j = -1;

 switch (s[0]) 
  case 'c' : i = CLUB   ; break;
  case 'd' : i = DIAMOND; break;
  case 'h' : i = HEART  ; break;
  case 's' : i = SPADE  ; break;
  default : break;

 switch (s[1])
  case 'a' : j=1; break;
  case 't' : j=10; break;
  case 'j' : j=11; break;
  case 'q' : j=12; break;
  case 'k' : j=13; break;
  default :  j=(s[1] - '0');

 assert ( i >= CLUB && i <= SPADE );
 assert ( j > 0 && i < 13 );

 int idx = i*13+j;
 return idx;


Points To Remember


This page last modified on 5 April 2003.