Othello: Description of Game - University of Delaware



MiniProject: Othello (Classes, Objects, and a smidge of random Numbers)(100 pts, Due Thurs, Sept 17 at midnight)Note: you may work with a partner or you may work alone. If you choose to work with a partner, make sure you both turn in the project, and make sure you include both names on the project. Equally, note your partner’s name in canvas. Please be aware that if your partner flakes on you, you are still responsible for completing the mini project and turning it in on time.Time: This project may take 8-10 hours, and should be worked on in a spread-out manner, as opposed to one sitting. This miniproject is closely tied to week 2 videos and ppts on my web site: (yeah, I’m just gonna keep plugging it until everyone gets used to the idea that that is where I place my course content).Contents TOC \o "1-3" \h \z \u Othello: Description of Game PAGEREF _Toc50496378 \h 1Purpose of this miniproject: PAGEREF _Toc50496379 \h 2Programming Description: Othello PAGEREF _Toc50496380 \h 2Player Class: PAGEREF _Toc50496381 \h 2(6 pts) Creating and implementing the header file and the definition file for the Player Class PAGEREF _Toc50496382 \h 2Othello Class: PAGEREF _Toc50496383 \h 2(4 pts) Creating a header file and a definition file for Othello Class PAGEREF _Toc50496384 \h 2(6 pts) 3 constructors: PAGEREF _Toc50496385 \h 3(3 pts)makemat PAGEREF _Toc50496386 \h 3(8 pts)printmat() PAGEREF _Toc50496387 \h 3(12 pts)placepiece() PAGEREF _Toc50496388 \h 4(20 pts)countandflippieces() PAGEREF _Toc50496389 \h 4(8 pts)ckwin() PAGEREF _Toc50496390 \h 5(20 pts)compplacepiece(player p): PAGEREF _Toc50496391 \h 5(15 pts): Getting the game to work successfully and turning in a screenshot of your running code PAGEREF _Toc50496392 \h 5Extra Credit Options: PAGEREF _Toc50496393 \h 7To Turn in: PAGEREF _Toc50496394 \h 7Sample Output: PAGEREF _Toc50496395 \h 7Othello: Description of GameFor this lab you will be using classes and objects to program the game Othello. For those of you unfamiliar with the game, there is an 8x8 board, and 2 players. Equally, there are pieces that you place on the board. All pieces are identical, and they have one side black, and the other side a color (mine were orange). The 4 middle squares are diagonal black and orange pieces to start with, e.g.,012345670________1________________3___BO___4___OB___5________6________7________One player is black pieces, and one is orange pieces. The two players take turns placing pieces with their color up on the board. Each time they place a piece on the board it must be placed such that at least one of the other players pieces is flipped. To flip a piece, their piece must be directly in a line between an old piece of yours on the board, and the piece you just placed on the board. If a player is unable to place their piece on the board, they forfeit their turn. The winner is the player with the most pieces on the board.Below is a link to a video demonstration of the game (they used black and white instead of black and orange, but you get the idea) of this miniproject:The purpose of this project is to establish a firm foundation in creating and using basic classes and objects. For many of our future data structures we will be using classes and objects. There is a smidge of random number generation thrown in as well (because you can never go wrong with a bit of randomness).Note: If a pop-up box arises that says “Errors exist, do you wish to continue?” Your code DOES NOT COMPILE. Don’t click continue. Fix your code. You will not receive credit for code that does not compile.Thus I am requiring these 2 classes, and I am requiring that the player fields in the Othello class be of type Player. Equally, I am requiring that you basically follow my method outline for the Othello class, although I will allow you to add methods (helper methods, perhaps) and even modify the method parameters. Be aware that any modifications may involve your having to update the playGame method. Programming Description: OthelloYou will be creating 2 classes (one very brief class for the player objects in the game, and one more extensive class for the Othello Game. Thus you will be creating 4 files: Player.hpp, Player.cpp, Othello.hpp, and Othello.cpp (if this all sounds crazy to you, please watch my videos/ppts under week 2 on my web site at (plugging it again!)).I am giving you the contents for the 5th file which should contain the main function, and the playGame method for the Othello class. Player Class:(6 pts) Creating and implementing the header file and the definition file for the Player ClassThe player class consists of 2 fields and 2 constructors. The fields are: a string for the name, A char for the player’s piece (‘B’ or ‘O’);The two constructors are:One with no input parameters, that sets the name field to be something like, “computer” and chooses a piece color for that player (again, either ‘B’ or ‘O’)One with 2 input parameters (a string and a char). This constructor sets the name filed to be the string input parameter and the piece field to be the char parameterOthello Class:(4 pts) Creating a header file and a definition file for Othello ClassThe Othello Class is much more extensive than the Player class, and as such, there is some flexibility (i.e., you may add methods that will act as helper methods, you may modify the parameters for methods, you may even add (or possibly subtract) a field. (Note that for this class I’m giving you the playGame method, so be aware that if you modify the methods/fields too drastically, you will also have to edit the playGame method). For my Othello Class:The fields are:A board matrix (8x8) of chars – this is the otherllo boardA board size (int) which for this game will always be 8;2 players, both of type PlayerA numplayer field (an int). This indicates how many players are “live” and how many are computers. For instance, if the numplayers is 0, the computer is playing against the computer, whereas if the numplayers is 1, a person is playing the computer, and if it is 2, a person is playing another person.The Methods I wrote are:Othello();Othello(string st1, char c);Othello(string st1, char c, string st2, char c2);(6 pts) 3 constructors:0ne setting both players to be computers and choosing which piece goes to each, setting the and numplayers to be 0, one setting one player’s fields to be thee input parameters and the second player to be a computer, and the numplayers to be 1, and the third sets both player’s fields and numplayers to be 2./*(3 pts)makemat This method initializes each element in the list as a blank character ('_').(You need to initialize the matrix so there's not gibberish in each square whenwe start - C++ lets you access gibberish, so it isn't easy to check if a squareis empty)After the matrix has been made, add 'B' and 'O' to the centermost squares toinitialize the board, so you end up with something like this: [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ','B','O',' ',' ',' '], [' ',' ',' ','O','B',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' ']] *//*(6 pts)printmat() this method prints out the matrix, by adding a tab between each square.It also prints out around the edges the index values. This makes selectinga square MUCH EASIER. It should print something that looks like this thefirst time you print the matrix:012345670________1________2________3___BO___4___OB___5________6________7________*//*(12 pts)placepiece() this method takes 2 input parameters as strings: the current player ('player1' or 'player2') andthe player's color ('B', or 'O').It returns nothing, but modifies the boardmatrix (adds the player's piece and calls thecountandflippieces method above to flip the appropriate pieces)Start by having the player input the x and y coordinates of the square where they want toplace their piece.To give you an example of what this would look like to get the coordinates from the player:int x;int y;cout<<"enter the x coordinate on the board: ";cin >> x;cout<<"enter the y coordinate on the board: ";cin >> y;Check to make sure that hte x coordinate and the y coordinate are both valid (notless than 0 or off the edge of the board) and that the boardmatrix at the x-y coordinate doesn'talready have a piece there. If the x or y coordinates are off-limits, or the boardmatrix alreadyhas a piece on it there, ask the usr to enter another set of x/y coordinates and continue to do thisuntil the user chooses valid x/y coordinates.Once a valid x/y coordinate has been chosen, the boardmatrix at the x/y coordinates becomes the player'scolor.Now add the call(s) to the countandflippieces methodto flip the appropriate pieces in each of the 8 directions, keeping track of the total number offlipped pieces.If the total number of flipped pieces is 0, then print out ("Player forfeits turn") and set the squareon the boardmatrix back to ' '.*//*(20 pts)countandflippieces() This method takes as a minimum, the (integer) x and y coordinates of where the playerplaced a piece on the board, and the current player.It might also take a boolean value that determines whether to actually flip thepieces or not and a direction to check in (leftupdiag, leftdowndiag, left,up,etc.)It returns (an integer) the count of the number of pieces flippedNote:My method is 24 lines long and doesn't use helper methods.However, if you want, and think this method would be easier with helper methods, feelfree to add more methods.Note2:I used the boolean value to determine whether to flip the pieces or not. In other words, ifthe computer is playing, you may want to look at a square on the board and determine how manypieces would be flipped if the computer placed a piece there. If, however, a person is placinga piece, or the computer has determined it wants to place a piece in a particular location,then the pieces should be flipped.Testing:To test this method, call it with an x and y coordinate, player, etc. and set the booleanvalue to true. Then use the printmatrix method to print the board and see ifthe appropriate pieces were flipped. Also check the count integer returned to seeif the count is correct.*//*(6 pts)ckwin() This method takes no input parameters.It returns nothing, but prints out who won with the number of pieces on the board with that color,so it might print:Player 1 won with: 33 versus 31Testing: print the boardmatrix using your printboard method above. Then call this method.Check to see whether the results match the board by counting the number of squares on theboard that match the color of the input parameter.*//*******************************************************************************If you've made it this far, you can start testing your code without the next methodIn the playGame method, comment out the call to compplacepiece. Then in main, Use the game instantiator that calls the constructor that sets both players to be humans. Make sure you remove any test cases you used to test your methods (or, anything you commentedOut)*******************************************************************************//*(20 pts)compplacepiece(player p): This method takes as input a playerThis method looks at all possible places that the player's piece could be placed on the boardmatrix, thenuses countandflippieces to calculate how many pieces would be flipped if a piece was placed there;Note: I used False as the input parameter to prevent the pieces from actually being flipped).This method needs to keep track ofmthe x/y coordinates that, if a piece was placed there, would flipthe most pieces.Then, once the x/y coordinates of the location in which the most pieces would be flipped, the methodcountandflippieces is used to actually flip the pieces (so for mine the input parameter was set to True now).If you get the above working, that's worth 15 of the 20 points for this method. However, we wantthe computer to choose randomly among the x/y coordinates that flip the most pieces. So, if thereare 4 x/y locations that all result in flipping 3 pieces, then we want to choose randomly amongthise 4 x/y locations. Have fun!*/(15 pts): Getting the game to work successfully and turning in a screenshot of your running code/*Congratulations! Now that you have your classes and methods working, add in the playGame method.If everything works, you're done! (note: you might have to modify my calls to placepiece, compplacepiece, or ckwin if you'vemodified the input parameters from what I have)*//******************************************************************************///Code I’m giving you://playGame Method:void Othello::playGame(){ makemat(); printmat(); bool play = true; int fullsqrs = 0; player p = p1; bool whichp = true; bool turn = true; if (rand()%2 == 0) { // p1 plays first p = p2; turn = false; whichp = false; } while ((play) && (fullsqrs < 64)){ cout << endl << p.name <<" ("<<p.piece<<") choose your square: "<<endl; if ((numplayers == 2) || ((numplayers == 1) && turn)){ placepiece(p); } else if ((numplayers == 0) || ((numplayers == 1) && (turn == false))){ compplacepiece(p); } turn = !turn; printmat(); if (whichp) { p = p2; whichp = false; } else { p=p1; whichp = true; } fullsqrs+=1; } ckwin();}/* *************************************************************************and code for the main function, which should be in its own file with a .cpp extension, located in the same project folder as your Othello Class and your Player Class files****************************************************************************/#include <iostream>#include <stdlib.h>#include "Othello.hpp"#include <time.h>using namespace std;int main() {srand(time(NULL));Othello game("b1",'B',"s1",'O');Othello game2("b1",'B');Othello game3;game.playGame();}/*******************************************************************************************************************/Extra Credit Options:If you actually have time, there are two extra credit options:(up to 10 pts) – right now the computer’s “intelligence” is solely choosing randomly among the potential squares that flip the most pieces. I was usually able to win simply by picking better pieces. Add some intelligence to the computer so that it picks more wisely(up to 20 pts) – create a better user interface. C++ is known for creating incredibly fast, efficient executable code that takes very little space in memory. It is not known for its user interfaces. However, there are libraries that will allow you to create a better user interface. For extra credit, create a cooler user interface. /*******************************************************************************************************************/To Turn in:1 zipped file with 6 files:Player header and defintiions filesOthello header and definitions filesYour name and your partner’s name on ALL 4 filesThe main (zip it in there)A screenshot of your code working/*******************************************************************************************************************/Sample Output:(This is me playing the computer. I’m horrible at games. It’s embarrassing. But that’s not the point. This is what the game output looks like when I play the computer:)012345670________1________2________3___BO___4___OB___5________6________7________computer (O) choose your square: 012345670________1________2___O____3___OO___4___OB___5________6________7________b1 (B) choose your square: Enter the x coordinate: 2Enter the y coordinate: 2012345670________1________2__BO____3___BO___4___OB___5________6________7________computer (O) choose your square: 012345670________1________2_OOO____3___BO___4___OB___5________6________7________b1 (B) choose your square: Enter the x coordinate: 2Enter the y coordinate: 4012345670________1________2_OOOB___3___BB___4___OB___5________6________7________computer (O) choose your square: 012345670________1________2_OOOOO__3___BO___4___OB___5________6________7________b1 (B) choose your square: Enter the x coordinate: 3Enter the y coordinate: 5012345670________1________2_OOOOO__3___BBB__4___OB___5________6________7________computer (O) choose your square: 012345670________1________2_OOOOO__3___BOO__4___OOO__5________6________7________b1 (B) choose your square: Enter the x coordinate: 3Enter the y coordinate: 6012345670________1________2_OOOOO__3___BBBB_4___OOO__5________6________7________computer (O) choose your square: 012345670________1________2_OOOOOO_3___BBOB_4___OOO__5________6________7________b1 (B) choose your square: Enter the x coordinate: 5Enter the y coordinate: 4012345670________1________2_OOOOOO_3___BBOB_4___OBB__5____B___6________7________computer (O) choose your square: 012345670________1________2_OOOOOO_3___BBOO_4___OOOO_5____B___6________7________b1 (B) choose your square: Enter the x coordinate: 3Enter the y coordinate: 7012345670________1________2_OOOOOO_3___BBBBB4___OOOO_5____B___6________7________computer (O) choose your square: 012345670________1________2_OOOOOOO3___BBBOB4___OOOO_5____B___6________7________b1 (B) choose your square: Enter the x coordinate: 1Enter the y coordinate: 7012345670________1_______B2_OOOOOBB3___BBBOB4___OOOO_5____B___6________7________computer (O) choose your square: 012345670________1_______B2_OOOOOBB3__OOOOOB4___OOOO_5____B___6________7________b1 (B) choose your square: Enter the x coordinate: 3Enter the y coordinate: 1012345670________1_______B2_OOOOOBB3_BBBBBBB4___OOOO_5____B___6________7________computer (O) choose your square: 012345670________1______OB2_OOOOOOB3_BBBBBOB4___OOOO_5____B___6________7________b1 (B) choose your square: Enter the x coordinate: 0Enter the y coordinate: 7012345670_______B1______BB2_OOOOBOB3_BBBBBOB4___OOOO_5____B___6________7________computer (O) choose your square: 012345670_______B1______BB2_OOOOBOB3OOOOOOOB4___OOOO_5____B___6________7________b1 (B) choose your square: Enter the x coordinate: 2Enter the y coordinate: 0012345670_______B1______BB2BBBBBBOB3OOOOOOOB4___OOOO_5____B___6________7________computer (O) choose your square: 012345670_______B1__O___BB2BOOOBBOB3OOOOOOOB4___OOOO_5____B___6________7________b1 (B) choose your square: Enter the x coordinate: 4Enter the y coordinate: 0012345670_______B1__O___BB2BOOOBBOB3BOOOOOOB4B__OOOO_5____B___6________7________computer (O) choose your square: 012345670_______B1__O_O_BB2BOOOOOOB3BOOOOOOB4B__OOOO_5____B___6________7________b1 (B) choose your square: Enter the x coordinate: 0Enter the y coordinate: 4012345670____B__B1__O_B_BB2BOOOBOOB3BOOOBOOB4B__OBOO_5____B___6________7________computer (O) choose your square: 012345670___OB__B1__O_O_BB2BOOOBOOB3BOOOBOOB4B__OBOO_5____B___6________7________b1 (B) choose your square: Enter the x coordinate: 0Enter the y coordinate: 2012345670__BBB__B1__O_O_BB2BOOOBOOB3BOOOBOOB4B__OBOO_5____B___6________7________computer (O) choose your square: 012345670__BBB__B1__O_O_BB2BOOOOOOB3BOOOOOOB4B__OOOO_5____O___6____O___7________b1 (B) choose your square: Enter the x coordinate: 7Enter the y coordinate: 4012345670__BBB__B1__O_B_BB2BOOOBOOB3BOOOBOOB4B__OBOO_5____B___6____B___7____B___computer (O) choose your square: 012345670__BBBO_B1__O_O_BB2BOOOBOOB3BOOOBOOB4B__OBOO_5____B___6____B___7____B___b1 (B) choose your square: Enter the x coordinate: 0Enter the y coordinate: 6012345670__BBBBBB1__O_O_BB2BOOOBOOB3BOOOBOOB4B__OBOO_5____B___6____B___7____B___computer (O) choose your square: 012345670__BBBBBB1__OOO_BB2BOOOOOOB3BOOOBOOB4B__OBOO_5____B___6____B___7____B___b1 (B) choose your square: Enter the x coordinate: 5Enter the y coordinate: 3012345670__BBBBBB1__OBO_BB2BOOBOOOB3BOOBBOOB4B__BBOO_5___BB___6____B___7____B___computer (O) choose your square: 012345670__BBBBBB1__OBO_BB2BOOBOOOB3BOOOBOOB4B_OOOOO_5___BB___6____B___7____B___b1 (B) choose your square: Enter the x coordinate: 0Enter the y coordinate: 1012345670_BBBBBBB1__BBO_BB2BOOBOOOB3BOOOBOOB4B_OOOOO_5___BB___6____B___7____B___computer (O) choose your square: 012345670_BBBBBBB1_OOOO_BB2BOOBOOOB3BOOOBOOB4B_OOOOO_5___BB___6____B___7____B___b1 (B) choose your square: Enter the x coordinate: 5Enter the y coordinate: 5012345670_BBBBBBB1_OOOO_BB2BOOBOOOB3BOOOBOOB4B_OOOOB_5___BBB__6____B___7____B___computer (O) choose your square: 012345670_BBBBBBB1_OOOO_BB2BOOBOOOB3BOOOBOOB4B_OOOOB_5___OOB__6___OB___7____B___b1 (B) choose your square: Enter the x coordinate: 0Enter the y coordinate: 0012345670BBBBBBBB1_BOOO_BB2BOBBOOOB3BOOBBOOB4B_OOBOB_5___OOB__6___OB___7____B___computer (O) choose your square: 012345670BBBBBBBB1_BOOO_BB2BOBBOOOB3BOOBBOOB4B_OOBOO_5___OOOO_6___OB___7____B___b1 (B) choose your square: Enter the x coordinate: 7Enter the y coordinate: 3012345670BBBBBBBB1_BOOO_BB2BOBBOOOB3BOOBBOOB4B_OBBOO_5___BOOO_6___BB___7___BB___computer (O) choose your square: 012345670BBBBBBBB1_BOOO_BB2BOBBOOOB3BOOBOOOB4B_OOBOO_5__OOOOO_6___BB___7___BB___b1 (B) choose your square: Enter the x coordinate: 4Enter the y coordinate: 7012345670BBBBBBBB1_BOOB_BB2BOBBOBOB3BOOBOOBB4B_OOBBBB5__OOOOO_6___BB___7___BB___computer (O) choose your square: 012345670BBBBBBBB1_BOOOOBB2BOBBOOOB3BOOBOOBB4B_OOBBBB5__OOOOO_6___BB___7___BB___b1 (B) choose your square: Enter the x coordinate: 4Enter the y coordinate: 1012345670BBBBBBBB1_BOOOOBB2BBBBOOOB3BBBBOOBB4BBBBBBBB5__BOOOO_6___BB___7___BB___computer (O) choose your square: 012345670BBBBBBBB1OOOOOOBB2BOBBOOOB3BBOBOOBB4BBBOBBBB5__BOOOO_6___BB___7___BB___b1 (B) choose your square: Enter the x coordinate: 5Enter the y coordinate: 7012345670BBBBBBBB1OOOOOOBB2BOBBOOOB3BBOBOOBB4BBBOBBBB5__BBBBBB6___BB___7___BB___computer (O) choose your square: 012345670BBBBBBBB1OOOOOOBB2BOOBOOOB3BBOOOOOB4BBBOOBOB5__BBBOOB6___BB_O_7___BB___b1 (B) choose your square: Enter the x coordinate: 7Enter the y coordinate: 7012345670BBBBBBBB1OBOOOOBB2BOBBOOOB3BBOBOOOB4BBBOBBOB5__BBBBOB6___BB_B_7___BB__Bcomputer (O) choose your square: 012345670BBBBBBBB1OBOOOOBB2BOBBOOOB3BOOOOOOB4BOOOBBOB5_OOOOOOB6___BB_B_7___BB__Bb1 (B) choose your square: Enter the x coordinate: 6Enter the y coordinate: 7012345670BBBBBBBB1OBOOOOBB2BOBBOOOB3BOOOOOOB4BOOOBBOB5_OOOOOBB6___BB_BB7___BB__Bcomputer (O) choose your square: 012345670BBBBBBBB1OBOOOOBB2OOBBOOOB3OOOOOOOB4OOOOBBOB5OOOOOOBB6___BB_BB7___BB__Bb1 (B) choose your square: Enter the x coordinate: 6Enter the y coordinate: 1012345670BBBBBBBB1OBOOOOBB2OBBBOBOB3OBOOBOOB4OBOBBBOB5OBBOOOBB6_B_BB_BB7___BB__Bcomputer (O) choose your square: 012345670BBBBBBBB1OBOOOOBB2OBBBOBOB3OBOOBOOB4OBOBBBOB5OOOOOOBB6_BOBB_BB7___BB__Bb1 (B) choose your square: Enter the x coordinate: 7Enter the y coordinate: 2012345670BBBBBBBB1OBOOOOBB2OBBBOBOB3OBBOBOOB4OBBBBBOB5OOBOOOBB6_BBBB_BB7__BBB__Bcomputer (O) choose your square: 012345670BBBBBBBB1OBOOOOBB2OBBBOBOB3OBBOBOOB4OBBBBBOB5OOBOOOBB6_OOBB_BB7_OBBB__Bb1 (B) choose your square: Enter the x coordinate: 7Enter the y coordinate: 0012345670BBBBBBBB1OBOOOOBB2OBBBOBOB3OBBOBOOB4OBBBBBOB5OOBOOOBB6_BOBB_BB7BBBBB__Bcomputer (O) choose your square: 012345670BBBBBBBB1OBOOOOBB2OBBBOBOB3OBBOBOOB4OBBBBBOB5OOBOOOBB6_BOOOOBB7BBBBB__Bb1 (B) choose your square: Enter the x coordinate: 6Enter the y coordinate: 0012345670BBBBBBBB1BBOOOOBB2BBBBOBOB3BBBOBOOB4BBBBBBOB5BBBOOOBB6BBOOOOBB7BBBBB__Bcomputer (O) choose your square: 012345670BBBBBBBB1BBOOOOBB2BBBBOBOB3BBBOBOOB4BBBBBBOB5BBBOOOOB6BBOOOOOB7BBBBB_OBb1 (B) choose your square: Enter the x coordinate: 7Enter the y coordinate: 5012345670BBBBBBBB1BBOOOOBB2BBBBOBOB3BBBOBOOB4BBBBBBOB5BBBBOBOB6BBOOBBBB7BBBBBBBBcomputer (O) choose your square: computer forfeits turnb1(B) won with: 50 versus 14 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download