Answered step by step
Verified Expert Solution
Question
1 Approved Answer
c++ In Part A, you will develop functions to convert a string to a row and column. As you develop them, you will write a
c++
In Part A, you will develop functions to convert a string to a row and column. As you develop them, you will write a main function to test them. Perform the following steps: 1. Create a main. cpp fle for your program. Inside it, put \#includes for and , a using namespace stdz line, and a main function that just returns 0 . 2. Your program does not do anything yet, but it should compile and run without crashing. From now on, try to keep your program in a working state. Debugging is much easier if only one thing is wrong at a time. - Hint: If you are using replit, you can compile with gi+ main,cpp -o parta Then you can execute it with - /parta - Recommendation: Compile the program after every step or two from now on. 3. Print a message at the start of your main function saying that it will test placestrings. 4. Write a function named stringtointeger that take a string as its only parameter and returns an int. Place the function prototype (one line ending with a semi-colon) before the main function and the function itself after the main function. For now, it should just convert the first character in the string to an int and return that. - Hint: You can aocess the individual characters of a string using array syntax. For example, it your string is named str, then str [0] is the first character and str [2. is the second one. - Hint: To convert a single digit character to an integer, subtract the " 0 ' character. For example, " 4+ ' 10 " equals 4 as an int value. So if a char variable named ch contains 4 and you subtract " 0 " from ch and store the result in an integer variable named 3 , then you will store 4 in 1 . 5. Add code to your ma in function to test your stringTolnteger function. Write at least 3 tests on different strings, including at least one with multiple digits. For each test, print a message displaying the string you are converting and the result. 6. Improve the stringTointeger function to convert an entire string of digits to a non-negative integer. For example, " 345" should be converted to 345. - Hint: Use a loop to process the characters sequentially. - Hint: You can use call the . 1ength () function to determine how many characters are in a string variable. A somewhat irrelevant example is: if (1=. Anything less than " 0 ' or greater than " 9 " is not a digit. 9. Improve vour main function so there is at least one test on a bad string, meaning one that contains characters that are not digits. 10. Write a function named placestringtorow that takes a string storing a placestring as its only parameter and returns the row number as an int. The function should ianore the first character in the place-string and attempt to convert the remaining characters to a number. - Reminder: Put the function prototype before the ma in function and the function itself at the end of the file. - Reminder: In a well-formed place-string, all characters except the first one give the row number. For example, if the string is "c13", the row number is 13. Also, in the case of a string with an incorrect first character followed by a number, e.g. " 145 or " 345, the function should still return a row number, e.g., 45 . - Hint: Use-Call your stringto Integer function. - Hint: You can use-call the s substr function to get only part of a string. For example, str. substr (3) returns a copy of str with the first three characters removed. 11. Add code to your main function to test your placestringtoRow function. Write at least 3 tests on different place-strings, including at least one where the row number has multiple digits. For each test, print a message saying the place-string you are testing and the row. 12. Improve your placeStr ingToRow function to handle place-strings that are too short. If the place-string contains fewer than two characters, always retum INVAL.TD_COORDINATE, In your ma in function add a test on an ill-formed placestring. - Hint: You should check the length before doing anything else in the function. If the string is empty and you access position 0 of the string. then your program may crash. 13. Write a function named placestringTocol umn that takes a place-string as its only parameter and returns the column number as an int. If the place-string has no characters, return INVALID COORDINATE. If the first character in the placestring is not an uppercase letter, or if it is either " 1 ' or " 0 ", then return INVALID_COORDINATE. - Reminder: In a well-formed place-string, the column is specified by the first character. If the first character is " A " then the cotumn number is 0 ; it the first character is ' B " then the column number is 1 ; and so forth. - Reminder: The characters " 1 ' and ' 0 ' cannot appear in a wel-formed placestring. 'H' indicates column 7 while ,J ' indicates column 8 . Similarly. " W " indicates column 12 while 15 ' indicates column 13. - Hint: First, check if the place-string has no characters. Then extract the first character and store it in a char variable. Then a check it it is an uppercase letter. Then check if it is ' 1 ' ' or ' 0 '. Convert your char character to an index and return that index. Make sure your program works on characters in the range ' A " to " H ' before continuing. Then improve your function to also handle characters in the range J ' to ' ' N ' and in the range " F ' to " 2 '. - Hint: To check if a character is an uppercase letter, check if it is > " " ' and " 2 '. To check if it is not an uppercase letter, check if it is " 22 ". - Hint: At or near the end of your function, you will have an if _ else if .. else structure to handle the three ranges of valid characters. - Hint: To convert an uppercase letter to a number, subtract ' A ". " A " becomes 0 , "ag' becomes 1, ' C ' becomes 2, etc. 14. Add code to your main function to test your placestringrocol unn function on at least 3 different place-strings, including at least one with an invalid column. Print similar messages as for the other tests. 15. Write a function named isplacestringWe 1 Formed that take a place-string as its only parameter and returns a bool indicating whether the place-string is wellformed. - Hint: Use Call your placestringTopow and placeStringtoColenn functions. If either returns INVALID_COORDINATE, the place-string is in-formed. Otherwise, it is well-formed. 16. Add code to your main function to test your isplacestringliel 1. Formed function on at least 3 different place-strings. Include at one with a well-formed string, one with an invalid row, and one with an invalid column. 17. Rename your ma in. cpp file to mainA, cppx. Do not delete or overwrite it; you will have to hand it in. In Part B, you will convert the functions from Part A for handling place-strings to be a module. The module will consist of one copp implementation file (also referred to as a source file) and one .h interface file (also referred to as a header file). By the end of Part B, your PlaceString module will have functions with the following prototypes: int stringtointeger (const stringt str_in) : int placestringtocolumn (const string 6 place_string_in) : int placestringTorow (const stringk place_string_in): bool isplacestringWel1Formed (const strings place_string_in) : Perform the following steps: 1. Add a new file to your project named Placestzing . h. Make sure to get upper case and lower case exactly right in C++ filenames and C++ programs. At the top of the file, put the following line: Bpragma once This line tells the C++ compiler to only compile this file once even if it happens to be in included multiple times. Also put the same line in as the first line in every other header (.h) file for the rest of the CS 115 course. 2. Add lines to include the string library and using namespace std; to your Placestring , h file. 3. Copy in the INVALID COORDINATE constant and the function prototypes from Part A. This is everything you will need for the PlaceString.h file. 4. Add a new source file to your project named placestring - cpp. Add lines to include the string library and use the std namespace. Do not add the H pragma once line. That line is only for header files. 5. Add another \#inelude to Placestring . cpp to include the Placestring.h header file: *include "placestring.h" - Note: When including standard C++ libraries, use triangle brackets. These tell the compiler to look in the folder where the standard libraries are stored. When including your own header file, put the file name in quotes. These single quotes tell the compiler to look in the same folder as your source file. 6. Copy in the functions from Part A (except main). This is everything you will need for the Placestring + cpp file. 7. If you do not have the word const in front of your parameters in your functions, add it in the function prototypes and the functions themselves (see function prototypes at the beginning of Section B of this assignment). a. Compile your PlaceString module with the zent Placestringl. .pp file provided. You will also need the and Iest Helper, cesc files. Run the resulting program. It should give you full marks (example output). - Hint: g++ Placestring. cpp Testplacestring1. cpp Testlielper. cpp -o partb - Note: Only compile the source files (. cpp), not header files (.h). - Hint: To simplify compiling. you should have all. cpp and . h files for your program together in the same folder. You can download the TestHelper . h and Testhlelper. cpp files from the assignment website or you can make empty files and copy/paste the contents of the files into them. You do not need to edit these files. - Hint: If the test program finds errors in your functions and you dont know how to solve them, go back and look at the instructions in Part A. They may help you. In Part C, you will develop a simple module to determine whether a place is on the board. The module will have its own source and header files. By the end of Part C, your BoardSize module will have one function with the following prototype: bool isonBoard (int row_in, int column_in); Perform the following steps: 1. Add a header file to your project named BoardSize.h. Add the \#pragma once line. - Note: This file does not need any \#includes or using statements. However, if you put them in, they will do no harm. 2. Add an int constant named BOARD_SIZE to the new file. It should have a value of 19. 3. Add the prototype for the isonBoard function (see a few lines above). This is everything you will need for the BoardSize. h file. 4. Add a new source file to your project named BoardSize. cpp. Add lines to \# include your BoardSize.h header into the source file. 5. In BoardSize.cpp, write an implementation for the isonBoard function. It should return true if the specified place is on the board and false otherwise. - Hint: A place is on the board if both the row number given by row_in and the column number given by column_in are at least 0 and strictly less than BOARD_SIZE. Otherwise, it is not on the board. - Hint: You will need four checks. These can be four separate ifs or four checks in the same if joined with && or 1. 6. Test your BoardSize module with the TestBoardSize1. cpp program provided. You will also need the TestHelper. h and TestHelper. cpp files again. Run the resulting program. It should give you full marks (example output). - Hint: g++ BoardSize.cpp TestBoardSizel.cpp TestHelper.cpp -o partc 4. Print another message asking the name of the player. Read the result into a string using getline and then print another message greeting the player by name and telling him/her that he/she will play black. For example: Welcome to my Go game! Enter yout name: A, shidem Hello A. Student. You will play black. The game proper will go atter these opening messages. - Hint: The syntax for get 1.1 ne is as follows: string name; getilne(cin, namel: - Note: The difference between in >> and get line is that ein >> reads che word (ends at a space), while getet in reads the whole line (ends at a newline character). In this case, we want to read the whole line. The get in ine function is corlvenient to use because it reads the newline character from the input but does. not store it in the string. - Warning: There is also a different ein.getline function. It does not work as well, so you should yse call the getline (cin,) one. 5. Print another message at the end of main saying goodbye to the player by name. For example: Goodbye, A. Student. The code for the game proper will go before this closing message. - Hint: Stop and test your program: q++ Boardsize.cpp Placestring+cpp main.cpp -o game These steps deal with setting up a main loop for your program. 6. Between the opening and closing messages, add a main loop for your program. This is a loop that repeatedly obtains and handles input until the player quits the program. Create a bool variable for whether the loop should continue and start it with a value that indicates it should. - Hint: Your main loop can be implemented as a whi Le loop or as a do loop. 7. Inside the main loop, ask the player for a move and read the input into a string using getline. If the move is "quit", change the main loop variable so the loop stops. - Hint: This is a good time to stop and test your program. 8. Otherwise, if the move is "pass", print the message "Black passed" followed on a separate line by the message "White passed". (No marks lost if not on a separate line.) 9. Otherwise, if the move is a well-formed place-string, determine the row and column it represents. If that place is on the board, print "Black played a stone at row RRR, column cCC", where RRR and cCC are the row and column values, followed on a separate line by the message "Whice passed". If the place is not on the board, print "Forbiddent Place fow RRR, colunn cCC is dutside board". - Hint: Use Call the functions from your Placestring and BoardSize modules. - Note: Print "White passed" if the player places a stone on the board, but not if 11. Stoo and test vour orogram. Test it on a place on the board, a place off the board, an ill-formed string, pass, and quit. - Reminder: g++ Boardisize. cpp Placestring. cpp main.cpp to game In Part E, you will develop another module to search an unsorted array of strings. The module will have its own source and header files. - Note: This module will use linear search, which just means checking each element of the array in order. Linear search is the simplest and most versatile searching algorithm. By the end of Part E, your Search module will have functions with the following prototype int linearsearch (const string data_in[], int count_in, const strings value_in); int unsortedFindSmallest (const string data_in [], int count_in): int unsortedFindLargest (const string dat_in[], int count_in); Perform the following steps: 1. Create an appropriately-named header (.h) file for the Search module. You will need to finclude the library and put in the normal using line. Add the three function prototypes shown above. 2. Add an int constant named VALUE_NOT_EOUND with a value of INT_MAX. You will return this constant if the value you search for is not in the array. - Note: INT_MAX is a constant that stores the largest possible int value. You will have to finclude the climits> library. 3. Create an appropriately-named source (. cpp) file for the Search module. You wil need two fincludes and a using line. 4. Write an implementation for the 1 inearsearch function. It should return the index in array data_ in where the value given by parameter value_ in is stored. there is no such value in the array, the function should return the VALUE_NOT_FOUND constant. If there is more than one copy of value_in in data_in, you may return the index of any of them. 5. Write an implementation for the unsortedFindSmallest function. It should return the index of the smallest value in the array named data in. If there is morthan one copy of that value in data_in, you may return the index of any of them. - Note: You do not have to handle the case where data_ in contains no elements. It will be handled in Assignment 2. - Hint: You may want to keep track of both the smallest value (a string) and the index where the smallest value is stored (an int). 6. Write an implementation for the unsortedFindLargest function. It will be simile to unsortedFindSmallest. 7. Test your Search module with the Testsearch1. cop program provided. You will still need the and TestHelper. cke files. Run the resulting program. It should give you full marks (example output). - Hint: g++Search.cpp TestSearch1.cpp TestHelper.cpp -o parte analysis when the program ends. Perform the following steps: 1. Outside the main loop, declare an array of strings to store the player's moves. Give the array a large size, such as 1000 , so that it will not get full. Also add a variable for keeping track of how many moves have been stored in the array and initialize this variable to zero. 2. Whenever the player successfully places a stone, add the place-string representing the player's move to the array. Then increment the count of how many moves have been stored. 3. After the main loop but before the goodbye message, print a message saying how many stones the player ("black") played in total. Then, if the player played at least one stone, print messages giving the smallest place-string (considered as a string value), the largest place-string, and whether the player ever played at place K9. - Reminder: When two strings are compared, they are compared character by character. Thus, "A13" > "B13" because ' 4 ' is greater than ' 1 '. This is what should happen. - Hint: Use Call the functions you developed in Part E. - Reminder: Test your program before handing it in: g++ PlaceString.cpp BoardSize.cpp Search.cpp main.cpp -o game Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started