In: Computer Science
Complete the project functions for this week. These functions are main_menu(), column_full() and get_move(). To complete these functions, first remove the library function call from the function in the template (main_menu_lib, etc), then replace that with your own code to complete the function. You can test your functions separate from the project by creating a small main() function that calls them (with appropriate inputs), or you can compile the entire project and see if it works.main_menu(): This function displays the main menu to the user, which should say "Welcome to the Connect 4 Game", and then prompt the user to ask if they would like to start a new game (press 'n'), load a game (press 'l') or quit (press 'q'). If the user selects new game, the main_menu function should return 1. If they select load game, it should return 2, and if they choose quit it should return -1. If any other input is entered, the user should be prompted to re-enter their choice until a valid input is chosen.column_full(): This function takes the entire board (a COLS x ROWS array of ints) as input as well as a single integer representing the column number to be checked. Note that this input is between 1 and COLS, not0 and COLS-1 so does not correspond exactly to the array dimension.Your function should then check if the specified column is full or not, and return 1 if full, 0 otherwise. The column is full if every element in the column is non-zero.get_move(): This function takes the entire board (a COLS x ROWS array of ints) as input, and then reads a column number from the user, checks that the supplied column is valid (between 1 and COLS), not full (using the column_full() function you've already written) and then returns the valid column number. If the input is invalid or the column is full, an appropriate error message should be displayed and the user asked to enter another column.
TEMPLATE FILE:
#include #include #include #include #include #include "connect4.h" int main ( void ){ int option ; Game g ; // intitialise random seed srand(time(NULL)); while (( option = main_menu()) != -1 ){ if ( option == 1 ){ // setup a new game setup_game ( &g ) ; // now play this game play_game ( &g ) ; } else if ( option == 2 ){ // attempt to load the game from the save file if ( load_game ( &g, "game.txt" ) == 0 ){ // if the load went well, resume this game play_game ( &g ) ; } else { printf ( "Loading game failed.\n") ; } } else if ( option == -1 ){ printf ( "Exiting game, goodbye!\n") ; } } } // WEEK 1 TASKS // main_menu() // column_full() // get_move() // displays the welcome screen and main menu of the game, and prompts the user to enter an option until // a valid option is entered. // Returns 1 for new game, 2 for load game, -1 for quit int main_menu ( void ){ // Dipslay Welcome message // Continue asking for an option until a valid option (n/l/q) is entered // if 'n', return 1 // if 'l', return 2 // if 'q', return -1 // if anything else, give error message and ask again.. return main_menu_lib () ; } // Returns TRUE if the specified column in the board is completely full // FALSE otherwise // col should be between 1 and COLS int column_full ( int board[COLS][ROWS], int col ){ // check the TOP spot in the specified column (remember column is between 1 and COLS, NOT 0 and COLS-1 so you'll need to modify slightly // if top spot isn't empty (0 is empty) then the column is full, return 1 // otherwise, return 0 return column_full_lib ( board, col ) ; } // prompts the user to enter a move, and checks that it is valid // for the supplied board and board size // Returns the column that the user has entered, once it is valid (1-COLS) // note that this value is betweeen 1 and COLS (7), NOT between 0 and 6!! // If the user enters 'S' or 's' the value -1 should be returned, indicating that the game should be saved // If the user enters 'Q' or 'q' the value -2 should be returned, indicating that the game should be abandoned int get_move ( int board[COLS][ROWS] ){ // repeat until valid input is detected: // read a line of text from the user // check if the user has entered either 's' (return -1) or 'q' (return -2) // if not, read a single number from the inputted line of text using sscanf // if the column is valid and not full, return that column number // otherwise, give appropriate error message and loop again return get_move_lib ( board ) ; } // END OF WEEK 1 TASKS
#ifndef CONNECT4_H #define CONNECT4_H 1 #define ROWS 6 // height of the board #define COLS 7 // width of the board (values of 9 are going to display poorly!!) // These lines detect what sort of compiler you are using. This is used to handle the time delay // function wait() in various operating systems. Most OS will use sleep(), however for windows it is // Sleep() instead. #ifdef _WIN32 #include <windows.h> #else #include <unistd.h> #endif typedef struct { int player1, player2 ; // variables for each player - 1 for human, 0 for computer player int board[COLS][ROWS] ; // the game board. 0 for empty space, 1 for player 1, 2 for player 2 // Note that row 0 is the TOP row of the board, not the bottom! // column 0 is on the left of the board int turn ; // whose turn it is, 1 or 2 int winner ; // who has won - 0 for nobody, 1 for player 1, 2 for player 2 } Game ; // displays the welcome screen and main menu of the game, and prompts the user to enter an option until // a valid option is entered. // Returns 1 for new game, 2 for load game, -1 for quit int main_menu ( void ) ; // displays the board to the screen int display_board ( int[COLS][ROWS] ) ; // sets up the game to a new state // prompts the user if each player should be a human or computer, and initialises the relevant fields // of the game structure accordingly int setup_game ( Game *g ) ; // Returns TRUE if the specified column in the board is completely full // FALSE otherwise // col should be between 1 and COLS int column_full ( int[COLS][ROWS], int col ) ; // plays a game until it is over int play_game ( Game *g ) ; // prompts the user to enter a move, and checks that it is valid // for the supplied board and board size // Returns the column that the user has entered, once it is valid (1-COLS) // note that this value is betweeen 1 and COLS (7), NOT between 0 and 6!! // If the user enters 'S' or 's' the value -1 should be returned, indicating that the game should be saved // If the user enters 'Q' or 'q' the value -2 should be returned, indicating that the game should be abandoned int get_move ( int[COLS][ROWS] ) ; // calcualtes a column for the computer to move to, using artificial "intelligence" // The 'level' argument describes how good the computer is, with higher numbers indicating better play // 0 indicates very stupid (random) play, 1 is a bit smarter, 2 smarter still, etc.. int computer_move ( int[COLS][ROWS], int colour, int level ) ; // adds a token of the given value (1 or 2) to the board at the // given column (col between 1 and COLS inclusive) // Returns 0 if successful, -1 otherwise int add_move ( int b[COLS][ROWS], int col, int colour ) ; // determines who (if anybody) has won. Returns the player id of the // winner, otherwise 0 int winner ( int[COLS][ROWS] ) ; // determines if the board is completely full or not int board_full ( int[COLS][ROWS] ) ; // saves the game to the specified file. The file is text, with the following format // player1 player2 turn winner // board matrix, each row on a separate line // Example: // //1 0 1 0 player 1 human, player 2 computer, player 1's turn, nobody has won //0 0 0 0 0 0 0 board state - 1 for player 1's moves, 2 for player 2's moves, 0 for empty squares //0 0 0 0 0 0 0 //0 0 0 2 0 0 0 //0 0 0 2 0 0 0 //0 2 1 1 1 0 0 //0 2 2 1 1 2 1 int save_game ( Game g, char filename[] ) ; // loads a saved game into the supplied Game structure. Returns 0 if successfully loaded, -1 otherwise. int load_game ( Game *g, char filename[] ) ; // waits for s seconds - platform specific! THIS FUNCTION IS INCLUDED IN THE LIBRARY, NO NEED TO WRITE IT! void wait_seconds ( int s ) ; // library versions of functions. Exactly the same behaviour done by course staff. Please just call these if you have not completed your version as yet. int display_board_lib ( int[COLS][ROWS] ) ; int setup_game_lib ( Game *g ) ; int column_full_lib ( int[COLS][ROWS], int col ) ; int play_game_lib ( Game *g ) ; int get_move_lib ( int[COLS][ROWS] ) ; int add_move_lib ( int b[COLS][ROWS], int col, int colour ) ; int winner_lib ( int[COLS][ROWS] ) ; int board_full_lib ( int[COLS][ROWS] ) ; int computer_move_lib ( int[COLS][ROWS], int colour, int level ) ; int save_game_lib ( Game g, char filename[] ) ; int load_game_lib ( Game *g, char filename[] ) ; int main_menu_lib ( void ) ; #endif
int main_menu ( void ) { int returns, i = 0; char start_answer[20]; // welcome message printf(" WELCOME TO CONNECT4\n"); while (i < 1) { printf("Please select an option: \n"); printf("(N): NEW GAME!\n"); printf("(L): LOAD GAME!\n"); printf("(Q): QUIT GAME\n"); printf("\nTYPE: "); fgets(start_answer, 20, stdin); if ( start_answer[0] == 'N' ) { returns = 1; i++; } if ( start_answer[0] == 'L' ) { returns = 2; i++; } if ( start_answer[0] == 'Q' ) { returns = -1; i++; } if ( start_answer[0] != 'N' && start_answer[0] != 'L' && start_answer[0] != 'Q') { printf("error\n%s is NOT a valid character\n\n", start_answer ); } } return (returns); } int get_move ( int board[COLS][ROWS] ) { char answer[20]; int cols = 0, check, ans; while(1) { printf("ENTER A MOVE: "); fgets(answer, COLS, stdin); sscanf(answer,"%d", &check); if (answer[0] == 's' || answer[0] =='S') { return -1; } else if (answer[0] == 'q' || answer[0] == 'Q') { return -2; } else if (check >= 1 && check <= 7 ) { ans = (column_full(board, check)); if (ans == 0) { return (check); } else { printf("Column is full\n"); } } } } int column_full ( int board[COLS][ROWS], int col ) { if (board[col - 1][0] == 0) { return 0; } else { return 1; } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.