1 / 11

Chomp

Chomp. How is the game played. Human player goes first choose a square, all to the right and down are “eaten” computer takes a turn whoever is forced to take the last square (with the X) loses. All complete but 2 classes. How the game works. starts with BoardGame applet

viveca
Télécharger la présentation

Chomp

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. Chomp

  2. How is the game played • Human player goes first • choose a square, all to the right and down are “eaten” • computer takes a turn • whoever is forced to take the last square (with the X) loses.

  3. All complete but 2 classes

  4. How the game works • starts with BoardGame applet • init method creates: • game object • the board • the players • adds a display field and the board to the applet’s content pane • chooses 1st player • calls player makeMove method.

  5. The first thing to do is • understand the program as it is. • Look at • position • human player • computer player • game • game board

  6. Position: simplest class public class Position { private int row, col; /** * Constructor */ public Position(int r, int c) { row = r; col = c; } /** * Returns this position's row */ public int getRow() { return row; } /** * Returns this position's column */ public int getCol() { return col; } } used by players, board and strategy

  7. Strategy • only used by Computer player • stores moves • 2 ways to move • random • with strategy try to play without discovering the strategy the computer uses first!!

  8. The ChompGame class public class ChompGame extends CharMatrix keeps track of board’s configuration implements the moves does not display the board – done by ChompBoard is a “model” of the game only ChompGame and Strategy know anything about the rules of the game.

  9. Char Matrix • Needed for ChompGame • Finish and test this class first. • No need to add any extra constructors or methods. • How will you test this to be sure it works? CharMatrix c = new CharMatrix(3,4,’x’); create a 3 row 4 column matrix of x’s c.clearRect(1,3,2,4); clear from row 1 to row 2col 3 to col 4

  10. Board Game Applet • top class • derived from JApplet • complete except the hasMoved method. • init method creates: • game object • the board • the players • adds a display field and the board to the applet’s content pane. • Very general. Doesn’t care what game is played or how players make their moves as long as they take turns.

  11. Board Game has moved method • Called by each player when that player completes its current move. • First: updates to next player. • Second: Either displays a winning message if the game is over or else displays a prompt and has the other player to make the next move • Needed because a player does not complete the move right away when make Move is called • human player awaits a mouse click • computer player waits until display stops flashing

More Related