1 / 12

A Tale of Two Cities

A Tale of Two Cities. Magnus Madsen. Software Architecture Project. Winner Strategy. You are asked to implement: where a player has won if he has conquered all cities, i.e. RED has won if BLU controls no cities (and vice versa). public Player getWinner(Collection<City > cities );.

varana
Télécharger la présentation

A Tale of Two Cities

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Tale of Two Cities Magnus Madsen

  2. Software Architecture Project

  3. Winner Strategy You are asked to implement: where a player has won if he has conquered all cities, i.e. RED has won if BLU controls no cities (and vice versa) publicPlayer getWinner(Collection<City> cities); Real world issue: What if there are no cities? => Implementation dependent

  4. Think, but don't speak How would you implement this in Java? In your favorite language? REDhas won if BLU controls no cities (and vice versa)

  5. Solution I  publicPlayer getWinner(GameImpl game) { Player winner = null; Player lastOwner = null; for(CityImpl c : game.getCities()) { if(lastOwner == null) { lastOwner = c.getOwner(); winner = lastOwner; } else{ if (!lastOwner.equals(c.getOwner())) { winner = null; break; }}} returnwinner; }    

  6. Solution II  public Player getWinner(GameImpl game) { List<Player> players = new ArrayList<Player>(); for (CityImpl c : game.getCitiesIterable()) { if (!players.contains(c.getOwner())) { players.add(c.getOwner()); if (players.size() >= 2) break; } } if (players.size() == 1) return players.get(0); else return null; }    

  7. Solution III  public Player computeWinner(GameImpl game) { Player assumedWinner = null; for (CityImpl c : game.getCities().values()) { if (assumedWinner == null) { assumedWinner = c.getOwner(); } else { if (!c.getOwner().equals(assumedWinner)) return null; } } return assumedWinner; }    

  8. Solution IV publicPlayer computeWinner(GameImpl game) { intred = 0; intblue = 0; for(CityImpl c : game.getCities().values()) { if(c.getOwner() == Player.RED) { red++; } else{ blue++; } } if(blue == 0) { returnPlayer.RED; } if(red == 0) { returnPlayer.BLUE; } returnnull; }     

  9. Solution V def getWinner(cities: ParSet[City]): City = { val redCity = cities.exist(c => c.isRed); val blueCity = cities.exist(c => c.isBlue); if (!blueCity) Player.Red else if (!redCity) Player.Blue else null }     

  10. Solution VI def getWinner(cities: ParSet[City]): City = { if (cities.isEmpty) return null; return cities.reduce((c1, c2) => if (c1 == c2) c1 else null ); }     

  11. Solution VII def getWinner(cities: ParSet[City]): Color = { if (cities.isEmpty) return null; val c1 = cities.head.color; if (cities.exists(c2 => c1 != c2.color)) null else c1 }     

  12. Summary Interesting properties: • obvious implementation (i.e. correctness)? • n > 2 players? • early termination? • single pass? • embarrassingly parallel? A variety of interesting solutions: • mutating, accumulating, counting, reducing, ...

More Related