编程辅导 C C++ Java Python MIPS Processing 网络家教 在线辅导

编程家教 远程写代码 Debug 讲解答疑 课后答疑 不是中介,本人直接答疑

微信: ittutor QQ: 14061936 Email: ittutor@qq.com

导航

 Coin Game Strategies

The goal of this assignment is to gain experience with more interesting ways to create systems of cooperating objects.

2 Who Will Play the Coin Game?

Not you, because it’s pretty boring. How about a computer? They’re good at that. In this assignment, you will implement two classes, one that takes on the role of a single coin game player, and one that controls the playing of a game by allowing players to move, in turn, until the game ends, and then reports the winner. (This will most likely not look anything like your imagined multiplayer coin game interface from Problem Set 4.)

2.1 Getting Started

Before writing code of your own, you will want to review the code that is provided:

  • CoinGameStrategy is the interface for a coin game strategy—that is, a class that acts as a coin game player. The interface declares one method, playOneMove(CoinGame). This method takes an instance of a CoinGame and makes one move in the game. (A strategy that attempts to cheat by making more than one move on the given game must be foiled, and we will see how to do so below.)

  • CoinGame is the same coin game interface you’ve seen before.

  • CoinGameController is a skeleton for a class for running coin games by allowing the players to take turns until one wins. The method that performs this job, runCoinGame(CoinGame, CoinGameStrategy...) is left for you to implement (below).

  • SingleMoveAdaptor is a class that wraps a CoinGame object in order to allow only one move to be made. In particulare, if game is a CoinGame, then new SingleMoveAdaptor(game) returns a CoinGameobject that works just like game, but raises an exception if the client attempts to make a move via the new object more than once. This means that we can wrap a game before handing it off to a strategy, and know that the strategy cannot cheat by moving more than once. Each time we want to allow a strategy to make a move on some game, we must allocate a fresh SingleMoveAdaptor object to wrap the same game, yielding a new one-move game object. It would be a good idea to read this class carefully to understand how it works.

  • StrictCoinGame is an implementation of the CoinGame interface with the strict rules. The source isn’t provided, but the compiled class is included in pset05.jar.

  1. Download the pset05.jar JAR file containing the compiled StrictCoinGame class (as well as compiled versions of the other files above that you won’t have to change), and add it to your project’s classpath. (As in Problem Set 4, how to do this will depend on your IDE, and help will be available on Piazza.)

3 Your Task

3.1 A Simple Coin Game Strategy

The strategy you will implement for this assignment is a simple one: find the leftmost coin that can move, and move it as far to the left as possible. Here are some examples of moves that would be made by this strategy:

Before After
"------O-" "O-------"
"-O--OO-O" "O---OO-O"
"O---OO-O" "OO---O-O"
"OO---O-O" "OOO----O"
"OOO----O" "OOOO----"
"OOOO----" no move, because game is over
  1. Write a class SimpleCoinGameStrategy that implements the CoinGameStrategy interface using the simple strategy described above: Move the leftmost movable coin as far to the left as permitted by the rules of the game.
  2. Test your SimpleCoinGameStrategy class sufficiently to assure yourself and your grader that your code is correct. As usual, comprehensive testing must verify that all error cases are handled correctly.

Hint: My entire implementation of SimpleCoinGameStrategy comprises 15 lines of code, not including blanks and comments. If you find your code getting significantly longer, you may want to rethink it.

3.2 The Game Controller

The CoinGameController class is responsible for running games between several players. Its runCoinGame(CoinGame, CoinGameStrategy...) method takes a game and an array of players (represented as strategies). It allows the players in the array to make their moves, from left to right, until one players wins. Then it returns the array index of the winning player.

There are several ways this can go wrong. If the game is over already, or if no players are provided1, then it must throw an exception as specified in the method’s Javadoc. Additionally, this method is responsible to ensure that no player makes more than one move when given its turn, though it need not recover from this condition.

  1. Complete the implementation of the runCoinGame(CoinGame, CoinGameStrategy...) method in theCoinGameController class.
  2. Test your completed runCoinGame(CoinGame, CoinGameStrategy...) method sufficiently to assure yourself and your grader that your code is correct. (You may find the already-defined methodrunCoinGame(String, CoinGameStrategy...) convenient for testing.) As usual, comprehensive testing must verify that all error cases are handled correctly.

Hint: The body of my runCoinGame(CoinGame, CoinGameStrategy...) method is 9 lines of code, not including blanks and comments.

3.3 Bonus

If you wish, you may implement other strategy classes. We don’t give extra credit in CS 3500, but hey, it might be fun.

 

  1.  

相关推荐