Top Banner
1 Object-Oriented Programming (Java), Unit 16 Kirk Scott
94

1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

Dec 28, 2015

Download

Documents

Abel Fowler
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

1

Object-Oriented Programming (Java), Unit 16

Kirk Scott

Page 2: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

2

Programming Project

• Background Information• Wari• Togiz Kumalak• Example Programs• Assignment

Page 3: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

Background Information

• Wari and Togiz Kumalak are two different versions of the same kind of board game.

• This section gives some historical and cultural information about these games. The next two sections describe the rules of play.

• Example code for implementations of Wari is provided.

3

Page 4: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Several different implementation choices are discussed.

• The assignment for the unit is to implement Togiz Kumalak in Java.

• It is the very last thing covered in these overheads.

4

Page 5: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Wari, and games like it, have been extensively studied.

• This first section of the unit contains excerpts from published works on Wari.

• The first source is: Games of the World: How to Make Them, How to Play Them, How They Came to Be

5

Page 6: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Wari is one of many similar board games played in various parts of the world.

• They are generically known as mancala games and have been played for thousands of years in Egypt, where boards have been found carved into the stone of the pyramid of Cheops and the temples at Luxor and Karnak.

6

Page 7: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• The game spread to Asia and Africa, where the Arabs developed certain variations.

• It thus survived through all the epochs of Egyptian history.

• European travelers were introduced to it in the cafes of nineteenth century Cairo, where it was customary for the loser to pay for the coffee drunk during the game.

7

Page 8: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• African slaves brought mancala games to Surinam and the West Indies, where they survive unchanged.

• In some rural areas of Africa today, children play these age-old games on ‘boards’ scooped out of the ground.

8

Page 9: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Here is an excerpt from: Board and Table Games From Many Civilizations

• It provides more historical and cultural background for games like Wari.

9

Page 10: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Professor Flinders Petrie found a rough block of limestone at Memphis containing three rows of fourteen pits which appears to be an early form of Mankala’h.

• The store suggests that pieces were captured and the pits are so small that the pieces were probably beans or seeds.

10

Page 11: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• There are several sets of deeply cut holes in the roofing slabs of the Kurna temple at Thebes, c. 1400 B.C.

• Other sets of boards are cut into the summit of the damaged portion of the great pylon built in Ptolemaic times at the entrance of the temple of Karnak, and also at the Luxor temple.

• The boards consist of two rows of six, seven, and eight saucer-shaped holes, the largest being 3½ in. wide and 1 in. deep.

11

Page 12: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Boards have been found in Arabia dating from before the time of Muhammad, and the followers of the prophet carried variations of the game to the countries influenced by their culture.

• Mancala is used as a generic noun for all the games of the Mankala’h type.

• …

12

Page 13: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• In the New World African slaves played their native games and taught them to their children.

• It is still possible to trace the ancestral origins of some West Indian Negroes by their form of mancala.

• Four variants of Wari are found among the Negroes of Guiana and the Caribbean which match with the games of Dahomey, Togoland, and Nigeria.

13

Page 14: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• The games have several names, one being Awari.

• As an intellectual exercies it is on a level with chess.

• The Negroes play for amusement and the prestige accruing to a good player.

• They will not play for money.

14

Page 15: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Finally, here is an excerpt from: The Oxford History of Board Games

• It provides more background information:

15

Page 16: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Mancala is probably of Black African origin, and Culin rightly characterizes it as Africa’s national board game.

• There is, however, no universally accepted ‘official’ or ‘standard’ version.

• On the contrary, it is a typical folk game—the ludic equivalent of a series of mutually intelligible dialects each of which is standard only for its own locality.

16

Page 17: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Any given variety may have several different names within a single gaming community;

• conversely, the same name, or variants of it, will often denote a number of different but similar games.

• Playing Mancala is as generic an activity as playing cards.

• …

17

Page 18: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Mancala is a game of perfect information, perfect equality, much freedom of significant choice, and hence great skill.

• Westerners will assume at first sight that one player, probably the first to move, has a certain win or at least a draw, and that the play will be slow and drawn-out as each in turn considers every possible move and as many branches down the resultant strategy tree as the human brain can manage.

18

Page 19: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• It is obviously ideal for computer analysis, and researches along this line suggest that a complete solution is imminent.

• Some might eschew Mancala on the ground that it lacks, on one hand, the random elements of Bridge and Backgammon which render them amenable to intuitive play, and, on the other, the two-dimensional structure of Chess which poses more of a challenge to intellectual analysis.

19

Page 20: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• The complexity of Chess lies in its depth, that of Mancala in its length.

• What the westerner tends to ignore, however, is the value of games as a form of social bonding and mental recreation.

• In fact, native practitioners of Mancala play at surprising speed, and experts with deadly accuracy.

20

Page 21: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Even without counting the contents of the most heavily loaded holes, many have a knack of unerringly selecting the best line of play from what appears to be the benefit of intuition derived from experience.

• To play at the sloth of Chess is to miss the point of the game.

21

Page 22: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• As our epigraph shows, the play of Mancala, at least in Africa, is a social activity, to which bystanders contribute so substantially that it is sometimes impossible to distinguish players from kibitzers.

• Some of the larger and more elaborate games are indeed played by two teams commanding either side of a particularly long board.

22

Page 23: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Against this background, however, Lenox-Smith regretfully notes that ‘cultured’ West Africans nowadays tend to regard Mancala as a game for children and peasants, and that some of the boards commercially available in large towns are of the wrong size or feel, suggesting that their producers have lost touch with their cultural roots.

23

Page 24: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• This will be recognized as a well-known social phenomenon in any country dominated by one (in this case ‘The West’) whose culture is perceived as more advantageous and hence more worthy of emulation.

24

Page 25: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

25

Wari

• The board for Wari consists of two rows of six cups each, one row for each player.

• These cups are initially filled with four seeds apiece.

• Each player also has a separate cup for winnings, which starts out empty.

• A representation of the initial state of the board and cups is shown below.

Page 26: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

26

Page 27: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

27

The basic rules of Wari:

• As a player, you pick up the contents of one of the cups on your side of the board.

• You then go counterclockwise around the board, starting with the cup immediately after the one you emptied, dropping one seed at a time into each succeeding cup until there are none left to drop.

• If the last seed dropped goes into one of your opponent’s cups which contains only one or two seeds, this is your win.

• You get to take the two or three seeds which result, and put them into your winnings cup.

Page 28: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• After you finish one play, whether you win any seeds or not, it is then your opponent’s turn.

• The game continues until one player has no seeds left to play.

• If you are the player with seeds left on your side of the board, you get to add them to your winnings.

• The winner of the game is the player with the highest winnings.

28

Page 29: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

29

Togiz Kumalak

• The Togiz Kumalak game board is shown on the next page.

• Following the board, the rules of play are given.

• The board for Togiz Kumalak consists of two rows of nine cups each, one row for each player.

• These cups are initially filled with nine seeds apiece.

Page 30: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Player 1’s side is on top and player 2’s side is on the bottom.

• Each player also has a separate cup for winnings, which starts out empty.

• The winnings cups are in the center of the board.

• A representation of the initial state of the board and cups is shown below.

30

Page 31: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

31

Player 2’s winnings go in this area. They are taken from player 1’s cups,which are on this side of the board.

Player 1’s winnings go in this area. They are taken from player 2’s cups,which are on this side of the board.

Page 32: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

32

The basic rules of Togiz Kumalak:

• Players take turns, and on each turn, you as a player can pick up the seeds from any cup on your side that has some in it.

• If there is only one seed in a cup, you pick that one up.

• If there is more than one seed, then you pick up all but one.

• You then drop the seeds one-by-one in each cup, moving counterclockwise around the board.

Page 33: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

33

The basic rules of Togiz Kumalak, continued:

• If a cup has an even number of seeds in it, it is referred to as closed.

• If it has an odd number in it, it is referred to as open.

• If the last seed you drop is into one of your opponent’s cups which is open, you win all of the seeds in that cup.

• The overall strategy of play is to open your opponent’s cups by leaving an odd count in them, and to close your own by leaving an even count in them.

Page 34: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

34

The basic rules of Togiz Kumalak, continued:

• If the last seed on a play is dropped into one of your opponent’s cups with 2 seeds in it, this cup becomes what is referred to as your home on the opponent’s side of the board.

• You immediately win the 3 seeds in that cup and for all following play, any seed that goes into that cup, whether one played by you or your opponent, goes into your winnings.

• A player can only have one home, so after getting one, the usual rules apply to cups with 2 seeds in them--They are cups with an even count.

Page 35: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Both players can have a home.• The fact that one player has one doesn’t

prevent the other from getting one.• On a physical board, a cup that has

become a home is usually marked with a special game piece that is different from the regular ones.

35

Page 36: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

36

The basic rules of Togiz Kumalak, continued:

• Play ends when one player runs out of seeds on his side of the board.

• When this happens the player with seeds remaining gets to add them to his winnings.

• The player with the most seeds wins.

Page 37: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

37

Example programs:

• WariV1• WariV21• WariV22

Page 38: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

WariV1

• A screenshot of WariV1 is shown on the next overhead.

• It uses the MyTerminalIO class to do I/O.• This means that it produces a little dialog box

when you call getInt(), getDouble(), or getString() for input.

• It also means that a call to println() puts ASCII text output into a graphical window.

• It does not use the System.out.println() black screen of death, although that would be an option. 38

Page 39: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

39

Page 40: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

40

WariV1

• This is a simple program consisting of a single class in one file.

• A UML diagram is given on the next overhead, with the code on the following overheads.

Page 41: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

41

Page 42: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

42

• public class WariV1• {• public static void main(String[] args)• {• /* Declarations */

• int[][] gameboard = new int[2][6];• int[] captured = new int[2];• int handfull;• int whoseturn = 0;• int chosencup;• int currentcup;• int currentside;• boolean goagain = true;• String answer;• int i;• int j;

Page 43: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

43

• /* This is needed for I/O. */

• MyTerminalIO myterminal = new MyTerminalIO();

• /* Array initializations */

• for(i = 0; i <= 1; i++)• {• captured[i] = 0;• for(j = 0; j <= 5; j++)• {• gameboard[i][j] = 4;• }• }

Page 44: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

44

• /* Display the game board. Note that player 0's side is printed "backwards". The board is shown from player 1's perspective. From this point of view, to display player 0's cups correctly, you have to count down from left to right rather than up. The game board is displayed again below. A little more thought about the placement of things in the loop would probably result in an arrangement where this code doesn't have to appear twice. However, this is the inspiration of the moment and it works. */

• myterminal.println("Player 0, captured " + captured[0] + "\n");

• for(j = 5; j >= 0; j--)• {• myterminal.print(" " + gameboard[0][j]);• if(gameboard[0][j] < 10)• myterminal.print(" ");• if(j >0)• myterminal.print(" |");• }

• myterminal.println("\n-----------------------------");

• for(j = 0; j <= 5; j++)• {• myterminal.print(" " + gameboard[1][j]);• if(gameboard[1][j] < 10)• myterminal.print(" ");• if(j < 5)• myterminal.print(" |");• }• myterminal.println("\n\nPlayer 1, captured " + captured[1] + "\n\n");

Page 45: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

45

• /* As long as the players want to play, continue the game.• This version of the code does not determine when the game is• over or who the winner is. The human players have to do that. */

• while(goagain)• {• /* Now prompt for for whoseturn's move. */

• /* This is the way you call the myterminal method for• reading an int. */

• chosencup = myterminal.getInt("\nIt is player " + whoseturn• + "'s move.\nPlease enter the number, from 1 to 6,\n• of the cup you want to play: ");• chosencup--;• myterminal.println();

• /* Now pick up the seeds from the cup and prepare to play. */

• currentcup = chosencup;• currentside = whoseturn;• handfull = gameboard[currentside][chosencup];• gameboard[currentside][chosencup] = 0;

Page 46: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

46

• /* Play the pieces. */

• while(handfull != 0)• {• /* Move to the next cup. Notice how you cycle through the array,

switching from• one side of the board to the other. */• currentcup = (currentcup + 1) % 6;

• if(currentcup == 0)• currentside = 1 - currentside;

• /* Drop a seed. */• gameboard[currentside][currentcup] += 1;

• handfull--;

• /* Capture. */• if(currentside != whoseturn && (gameboard[currentside][currentcup] ==

2• || gameboard[currentside][currentcup] == 3))• {• captured[whoseturn] += gameboard[currentside][currentcup];

• gameboard[currentside][currentcup] = 0;• }• }

Page 47: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

47

• /* Display the game board again. */

• myterminal.println("\n\nPlayer 0, captured " + captured[0] + "\n");

• for(j = 5; j >= 0; j--)• {• myterminal.print(" " + gameboard[0][j]);• if(gameboard[0][j] < 10)• myterminal.print(" ");• if(j >0)• myterminal.print(" |");• }

• myterminal.println("\n-----------------------------");

• for(j = 0; j <= 5; j++)• {• myterminal.print(" " + gameboard[1][j]);• if(gameboard[1][j] < 10)• myterminal.print(" ");• if(j < 5)• myterminal.print(" |");• }

• myterminal.println("\n\nPlayer 1, captured " + captured[1] + "\n\n");

Page 48: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

48

• /* One player's move has come to an end• and it's the other player's turn. */

• whoseturn = 1 - whoseturn;

• answer = myterminal.getString("\nEnter 'no‘• to quit.\nAny other input will continue• play: ");• if(answer.equals("no"))• goagain = false;• }• }• }

Page 49: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

49

WariV21

• This is a program consisting of multiple classes saved in multiple files.

• Its appearance is the same as WariV1 so no screenshot is given.

• A UML diagram is given on the next overhead, with the code on the following overheads.

Page 50: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

50

Page 51: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

51

• /**• This is the program file/Class for the second wari

programming• assignment, the one using classes and objects.• @author Kirk Scott• @version 21• */

• public class WariV21• {• public static void main(String[] args)• {• MyTerminalIO myterminal = new MyTerminalIO();• int playTurn = 1;• int playCup = 1;• boolean playAgain = true;• String answer = null;• BoardV21 playBoard = new BoardV21();

• playBoard.showBoard(myterminal);

Page 52: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

52

• while(playAgain == true)• {• playCup = myterminal.getInt("\nIt is player " +• playTurn + "'s move.\nPlease enter the number,• from 1 to 6,\nof the cup you want to play: ");

• playBoard.moveBoard(playTurn, playCup);• playBoard.showBoard(myterminal);

• if(playTurn == 1)• playTurn = 2;• else• playTurn = 1;

• answer = myterminal.getString("\nEnter 'no' to• quit.\nAny other input will continue play: ");• if(answer.equals("no"))• playAgain = false;• }• }• }

Page 53: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

53

• /**• This class implements the game board for playing wari.• */

• public class BoardV21• {• private CupV21[][] gameBoard = new CupV21[3][7];• private CupV21[] captured = new CupV21[3];

Page 54: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

54

• /**• This constructor sets up the cups in the board, initializing the number

of seeds in each to 4, and correctly labelling them as belonging to player 1 or player 2. It also creates the captured cups, initializing the number of seeds in each to 0 and labelling them as belonging to player 1 and player 2 respectively.

• */

• public BoardV21()• {• captured[1] = new CupV21(0, 1);• captured[2] = new CupV21(0, 2);

• gameBoard[1][6] = new CupV21(4, 1);• gameBoard[2][6] = new CupV21(4, 2);

• for(int i = 5; i > 0; i--)• {• gameBoard[1][i] = new CupV21(4, 1, gameBoard[1][i + 1]);• gameBoard[2][i] = new CupV21(4, 2, gameBoard[2][i + 1]);• }

• gameBoard[1][6].setNextCup(gameBoard[2][1]);• gameBoard[2][6].setNextCup(gameBoard[1][1]);• }

Page 55: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

55

• /**• This method was included for debugging purposes. It prints• the toString() information about each cup in the game board• by going around the board as an array.• */

• public void showBoardArrayStyle(MyTerminalIO myterminal)• {• for(int i = 1; i <= 2; i++)• {• for(int j = 1; j <= 6; j++)• {• myterminal.println(gameBoard[i][j]);• }• }• myterminal.println();• }

Page 56: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

56

• /**• This method was included for debugging purposes. It prints• the toString() information about each cup in the game board• by following the references to the next cup.• */

• public void showBoardNextStyle(MyTerminalIO myterminal)• {• CupV21 tempReference = gameBoard[1][1];

• for(int i = 1; i <= 12; i++)• {• myterminal.println(tempReference);• tempReference = tempReference.getNextCup();• }• myterminal.println();• }

Page 57: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

57

• /**• This method is part of the play. It prints out the the game board in a

formatted way, showing the number of seeds in each cup.• */

• public void showBoard(MyTerminalIO myterminal)• {• CupV21 tempReference;• int tempSeedCount;

• myterminal.println("\n\nPlayer 1, captured " +• captured[1].getSeedCount() + "\n");

• for(int i = 1; i <= 6; i++)• {• tempReference = gameBoard[2][6];

• for(int j = 0; j <= 6 - i; j++)• {• tempReference = tempReference.getNextCup();• }

• tempSeedCount = tempReference.getSeedCount();• myterminal.print(" " + tempSeedCount);• if(tempSeedCount < 10)• myterminal.print(" ");• if(i < 6)• myterminal.print(" |");• }

Page 58: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

58

• myterminal.println("\n-----------------------------");

• tempReference = gameBoard[1][6];

• for(int i = 1; i <= 6; i++)• {• tempReference = tempReference.getNextCup();• tempSeedCount = tempReference.getSeedCount();• myterminal.print(" " + tempSeedCount);• if(tempSeedCount < 10)• myterminal.print(" ");• if(i < 6)• myterminal.print(" |");• }• myterminal.println("\n\nPlayer 2, captured " +• captured[2].getSeedCount() + "\n\n");• }

Page 59: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

59

• /**• This method implements the actual playing of a move in wari.• @param whoseTurn tells which player's turn it is who is playing.• @param whichCup tells which cup the player has chosen to play.• */

• public void moveBoard(int whoseTurn, int whichCup)• {• int handfull = gameBoard[whoseTurn][whichCup].removeSeeds();

• CupV21 tempReference = gameBoard[whoseTurn][whichCup].getNextCup();

• while(handfull != 0)• {• tempReference.addOneSeed();• handfull--;

• if(tempReference.getWhoseCup() != whoseTurn)• {• if(tempReference.getSeedCount() == 2 ||• tempReference.getSeedCount() == 3)• {• captured[whoseTurn].addSomeSeeds(tempReference.removeSeeds());• }• }

• tempReference = tempReference.getNextCup();• }• }• }

Page 60: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

60

• /**• The Cup class is used to create the 12 cups of the wari Board, as well as the two additional cups for keeping a count of the number of seeds captured, one for each player.

• */

• public class CupV21• {• private int seedCount;• private int whoseCup;• private CupV21 nextCup;

Page 61: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

61

• /**• This constructor is used for creating the captured cups.

There are no links between these cups.• @param seedCountin is an integer that initializes the

number of seeds in the cup.• @param whoseCupin is an integer that identifies which

player the cup belongs to, player 1 or player 2.• */

• public CupV21(int seedCountin, int whoseCupin)• {• seedCount = seedCountin;• whoseCup = whoseCupin;• nextCup = null;• }

Page 62: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

62

• /**• This constructor is used for creating the 12 cups that

are part of the wari Board.• @param seedCountin is an integer that initializes the

number of seeds in the cup.• @param whoseCupin is an integer that identifies which

player the cup belongs to, player 1 or player 2.• @param nextCupin contains a reference to another cup on

the board. Not only are the cups related by their position in an array, for good measure they are linked together in their order of play.

• */

• public CupV21(int seedCountin, int whoseCupin, CupV21 nextCupin)

• {• seedCount = seedCountin;• whoseCup = whoseCupin;• nextCup = nextCupin;• }

Page 63: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

63

• /**• This method is an incomplete implementation of the

standard toString() method. It is incomplete because only the count of the number of seeds and who the cup belongs to are printed out. No information on the next cup in the board is printed out.

• @return String is a String containing the count of the number of seeds and a number indicating who the cup belonged to.

• */

• public String toString()• {• return "CupV21[seedCount = " + seedCount• + ", whoseCup = " + whoseCup• + "]";• }

Page 64: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

64

• /**• This method simply returns the count of the number of seeds in the cup.

• @return this is the count of the seeds.• */

• public int getSeedCount()• {• return seedCount;• }

Page 65: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

65

• /**• This method adds one to the count of seeds in a cup. This is used as play progresses and seeds are dropped around the board one by one.

• */

• public void addOneSeed()• {• seedCount++;• }

Page 66: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

66

• /**• This method adds the amount given as a parameter

to the number of seeds in a cup. It is used when a player captures some seeds and they are put into that player's captured cup.

• @param seedsin is the number of seeds to add to the cup.

• */

• public void addSomeSeeds(int seedsin)• {• seedCount += seedsin;• }

Page 67: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

67

• /**• This method simply returns the integer, 1 or 2, telling which player the cup belongs to.

• @return the player the cup belongs to.• */

• public int getWhoseCup()• {• return whoseCup;• }

Page 68: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

68

• /**• This method returns a reference to the next cup in the game board.

• @return the next game cup in the board.• */

• public CupV21 getNextCup()• {• return nextCup;• }

Page 69: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

69

• /**• This method sets the value of the instance

variable that holds the reference to the next cup in the game board. It is called when the Board class constructor is creating the cups and arranging them in an instance of the board.

• @param nextCupin is the reference to the next cup in the board.

• */

• public void setNextCup(CupV21 nextCupin)• {• nextCup = nextCupin;• }

Page 70: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

70

• /**• This method zeros out the count of the number of• seeds in a cup and returns that number. It is• used in play when a capture occurs.• @return the number of seeds that were in the cup.• */

• public int removeSeeds()• {• int temp = seedCount;• seedCount = 0;• return temp;• }• } /* End of class. */

Page 71: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

71

WariV22

• This version of the program illustrates the use of a subclass.

• Honestly, the inclusion of the subclass in the design is of no practical importance.

• Its appearance is the same as WariV1 so no screenshot is given.

• A UML diagram is given on the next overhead, with the code on the following overheads.

Page 72: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

72

Page 73: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

73

• /**• This is the program file/Class for the second wari

programming assignment, the one using classes and objects.

• @author Kirk Scott• @version 22• */

• public class WariV22• {• public static void main(String[] args)• {• MyTerminalIO myterminal = new MyTerminalIO();• int playTurn = 1;• int playCup = 1;• boolean playAgain = true;• String answer = null;• BoardV22 playBoard = new BoardV22();

• playBoard.showBoard(myterminal);

Page 74: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

74

• while(playAgain == true)• {• playCup = myterminal.getInt("\nIt is player " +• playTurn + "'s move.\nPlease enter the number, from• 1 to 6,\nof the cup you want to play: ");

• playBoard.moveBoard(playTurn, playCup);• playBoard.showBoard(myterminal);

• if(playTurn == 1)• playTurn = 2;• else• playTurn = 1;

• answer = myterminal.getString("\nEnter 'no' to• quit.\nAny other input will continue play: ");• if(answer.equals("no"))• playAgain = false;• }• }• }

Page 75: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

75

• /**

• ******************************************************** This is the version that uses the Cup, LinkedCup class hierarchy. ********************************************************

• This class implements the game board for playing wari.• */

• public class BoardV22• {• private LinkedCupV22[][] gameBoard =• new LinkedCupV22[3][7];• private CupV22[] captured = new CupV22[3];

Page 76: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

76

• /**• This constructor sets up the cups in the board, initializing• the number of seeds in each to 4, and correctly labelling• them as belonging to player 1 or player 2. It also creates• the captured cups, initializing the number of seeds in each• to 0 and labelling them as belonging to player 1 and player• 2 respectively.• */

• public BoardV22()• {• captured[1] = new CupV22(0, 1);• captured[2] = new CupV22(0, 2);

• gameBoard[1][6] = new LinkedCupV22(4, 1);• gameBoard[2][6] = new LinkedCupV22(4, 2);

• for(int i = 5; i > 0; i--)• {• gameBoard[1][i] = new LinkedCupV22(4, 1, gameBoard[1][i + 1]);• gameBoard[2][i] = new LinkedCupV22(4, 2, gameBoard[2][i + 1]);• }

• gameBoard[1][6].setNextCup(gameBoard[2][1]);• gameBoard[2][6].setNextCup(gameBoard[1][1]);• }

Page 77: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

77

• /**• This method was included for debugging purposes.

It prints the toString() information about each cup in the game board by going around the board as an array.

• */

• public void showBoardArrayStyle(MyTerminalIO myterminal)

• {• for(int i = 1; i <= 2; i++)• {• for(int j = 1; j <= 6; j++)• {• myterminal.println(gameBoard[i][j]);• }• }• myterminal.println();• }

Page 78: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

78

• /**• This method was included for debugging purposes.

It prints the toString() information about each cup in the game board by following the references to the next cup.

• */

• public void showBoardNextStyle(MyTerminalIO myterminal)

• {• LinkedCupV22 tempReference = gameBoard[1][1];

• for(int i = 1; i <= 12; i++)• {• myterminal.println(tempReference);• tempReference = tempReference.getNextCup();• }• myterminal.println();• }

Page 79: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

79

• /**• This method is part of the play. It prints out the the game board in a

formatted way, showing the number of seeds in each cup.• */

• public void showBoard(MyTerminalIO myterminal)• {• LinkedCupV22 tempReference;• int tempSeedCount;

• myterminal.println("\n\nPlayer 1, captured " +• captured[1].getSeedCount() + "\n");

• for(int i = 1; i <= 6; i++)• {• tempReference = gameBoard[2][6];

• for(int j = 0; j <= 6 - i; j++)• {• tempReference = tempReference.getNextCup();• }

• tempSeedCount = tempReference.getSeedCount();• myterminal.print(" " + tempSeedCount);• if(tempSeedCount < 10)• myterminal.print(" ");• if(i < 6)• myterminal.print(" |");• }

Page 80: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

80

• myterminal.println("\n-----------------------------");

• tempReference = gameBoard[1][6];

• for(int i = 1; i <= 6; i++)• {• tempReference = tempReference.getNextCup();• tempSeedCount = tempReference.getSeedCount();• myterminal.print(" " + tempSeedCount);• if(tempSeedCount < 10)• myterminal.print(" ");• if(i < 6)• myterminal.print(" |");• }• myterminal.println("\n\nPlayer 2, captured " +• captured[2].getSeedCount() + "\n\n");• }

Page 81: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

81

• /**• This method implements the actual playing of a move in wari.• @param whoseTurn tells which player's turn it is who is playing.• @param whichCup tells which cup the player has chosen to play.• */

• public void moveBoard(int whoseTurn, int whichCup)• {• int handfull = gameBoard[whoseTurn][whichCup].removeSeeds();

• LinkedCupV22 tempReference =• gameBoard[whoseTurn][whichCup].getNextCup();

• while(handfull != 0)• {• tempReference.addOneSeed();• handfull--;

• if(tempReference.getWhoseCup() != whoseTurn)• {• if(tempReference.getSeedCount() == 2 ||• tempReference.getSeedCount() == 3)• {• captured[whoseTurn].addSomeSeeds(tempReference.removeSeeds());• }• }

• tempReference = tempReference.getNextCup();• }• }• }

Page 82: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

82

• /**• The Cup class is used to create the two cups for keeping a count of• the number of seeds captured, one for each player.• */

• public class CupV22• {• private int seedCount;• private int whoseCup;

• /**• This constructor is used for creating the captured cups. There are• no links between these cups.• @param seedCountin is an integer that initializes the number of seeds• in the cup.• @param whoseCupin is an integer that identifies which player the cup• belongs to, player 1 or player 2.• */

• public CupV22(int seedCountin, int whoseCupin)• {• seedCount = seedCountin;• whoseCup = whoseCupin;• }

Page 83: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

83

• /**• This method simply returns the count of the number of seeds• in the cup.• @return this is the count of the seeds.• */

• public int getSeedCount()• {• return seedCount;• }•• /**• This method adds one to the count of seeds in a cup. This is• used as play progresses and seeds are dropped around the• board one by one.• */

• public void addOneSeed()• {• seedCount++;• }

Page 84: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

84

• /**• This method adds the amount given as a parameter to the number• of seeds in a cup. It is used when a player captures some seeds• and they are put into that player's captured cup.• @param seedsin is the number of seeds to add to the cup.• */

• public void addSomeSeeds(int seedsin)• {• seedCount += seedsin;• }

• /**• This method simply returns the integer, 1 or 2, telling which• player the cup belongs to.• @return the player the cup belongs to.• */

• public int getWhoseCup()• {• return whoseCup;• }

Page 85: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

85

• /**• This method zeros out the count of the number of seeds in a cup• and returns that number. It is used in play when a capture• occurs.• @return the number of seeds that were in the cup.• */

• public int removeSeeds()• {• int temp = seedCount;• seedCount = 0;• return temp;• }• }

Page 86: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

86

• /**• The LinkedCup class is used to create the 12 cups of the wari Board.• */

• public class LinkedCupV22 extends CupV22• {• private LinkedCupV22 nextCup;

• /**• This constructor takes only two parameters.• @param seedCountin is an integer that initializes the number of seeds• in the cup.• @param whoseCupin is an integer that identifies which player the cup• belongs to, player 1 or player 2.• */

• public LinkedCupV22(int seedCountin, int whoseCupin)• {• super(seedCountin, whoseCupin);• nextCup = null;• }

Page 87: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

87

• /**• This constructor takes three parameters and initializes all of the• instance variables.• @param seedCountin is an integer that initializes the number of seeds• in the cup.• @param whoseCupin is an integer that identifies which player the cup• belongs to, player 1 or player 2.• @param nextCupin contains a reference to another cup on the board.• Not only are the cups related by their position in an array, for• good measure they are linked together in their order of play.• */

• public LinkedCupV22(int seedCountin, int whoseCupin, LinkedCupV22 nextCupin)

• {• super(seedCountin, whoseCupin);• nextCup = nextCupin;• }

Page 88: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

88

• /**• This method returns a reference to the next cup in the game• board.• @return the next game cup in the board.• */

• public LinkedCupV22 getNextCup()• {• return nextCup;• }

• /**• This method sets the value of the instance variable that holds• the reference to the next cup in the game board. It is called• when the Board class constructor is creating the cups and• arranging them in an instance of the board.• @param nextCupin is the reference to the next cup in the board.• */

• public void setNextCup(LinkedCupV22 nextCupin)• {• nextCup = nextCupin;• }• }

Page 89: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

Java Unit 16 Assignment

•  Grading check-off checklist:• 1. You should have a version of Togiz

Kumalak which compiles and runs.• When you prepare to demonstrate this,

recall that if you used MyTerminalIO.java, it should also be in the folder so that your program will compile and run.

89

Page 90: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

•  Assignment:• 1. The assignment for this unit is to implement

the game of Togiz Kumalak.• The implementation should show the current

state of the game using Unicode characters and digits and the MyTerminalIO class.

• It should prompt the users, in turn, for their cup choices, and update the state of the board.

90

Page 91: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• You may or may not use classes and objects in your solution.

• You may also choose not to implement all of the rules of the game.

• For example, it would be possible to write a version that didn’t recognize the case where a player had gotten a “home” on the opponent’s side of the board.

91

Page 92: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• The implementation should recognize the case where one side of the board is empty and the winner is determined by the number of seeds won.

• Notice that unlike the assignments in previous units, no solution is given to this problem.

• If you need guidance, you will have to rely on the sample Wari code that is given.

92

Page 93: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

• Most students find it easy enough to modify the given code so that there are 9 cups on each side of the board and each cup starts with 9 seeds in it.

• Your solution to this assignment will not be checked to make sure that the Togiz Kumalak rule for capturing seeds Kumalak has been implemented in place of the Wari rule.

• However, it's not too soon to do this. • Points for this will be given in a future assignment.

93

Page 94: 1 Object-Oriented Programming (Java), Unit 16 Kirk Scott.

94

The End