Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Thanks for replying!

+9 votes
asked Nov 7 by An Nguyen (270 points)
Hi, thanks a lot for your help!

I didn't put the description in because I thought it was too long, but if it is of any help, here it is:

In this assignment, students need to implement a program in C language to simulate the process of playing the game Gomoku on the console screen through the game rules described above and the tasks described below. Each task is required to write a corresponding function with parameters of which are given in the description of the task request. Before continuing below, students are required to read and understand how the main() and startGame() functions work. It is not recommended to understand the content of the function displayBoard() and lineString(). 5.1 Make a move (3 points) When the user selects the turn-based game mode, the program calls the function startGame(). Next, the program will wait for the user to enter a move that is a position of the board. Gomoku are played on a square board consisting of 15 rows numbered 15 to 1 (top to bottom) and 15 columns ordered from character ’a’ to ’o’ (left to right). A player’s move on the board will be denoted by a combination RowColumn with row and column values within the valid range of the board. Some examples of valid moves: 1a, 3d, 5o, etc. Students are asked to write a function that determines if a player’s input is a valid move or not, and records the move to the board if it is valid. Specifically, the description of the function is as follows: • Function name: makeMove. • Input parameters: – board[][MAX SIZE] (type enum Stone): 2-dimensional array of type enum Stone, storing information about the current state of the board. Introduction to Programming’s assignment – size (type int): The size of the board. – playerMove (char*): String containing the player’s move – isFirstPlayerTurn (bool): Logical variable used to determine whether the current turn is the turn of the first player or not • Function requirements: 1. The function will check if the player’s move is a valid move or not. A valid move is a move that: follows the notation of a move; the row and column values are within the valid range, and the square to be placed on the board is empty. 2. If the move is valid, the function updates the player’s move on the board array based on the isFirstPlayerTurn variable. Otherwise, the function will do nothing. 3. Returns true if the board is updated with a new move, otherwise returns false

5.2 Winning checking(4 points) We continue to observe the function startGame(). After the move is valid and the stone has been placed on the board, the program will continue to check if the player who just made that move has won or not. The winning conditions of the game are clearly described in the section Game rules. Students are asked to write the following function to describe the winning checking process. The function information is as follows: • Function name: hasWon. • Input parameters: – board[][MAX SIZE] (type enum Stone): 2-dimensional array of type enum Stone, storing information about the current state of the board. – size (type int): The size of the board. – isFirstPlayerTurn (bool): Logical variable used to determine whether the current turn is the turn of the first player or not • Function requirements: The function checks if the current state of the board is the winning state of player 1/player 2 (based on the variable isFirstPlayerTurn). • Return result: The function returns the value true if the board is in a winning state. Otherwise, returns false.

5.3 Watch match history (3 points) This section asks students to implement the function of reviewing a game of Gomoku by implementing the function displayHistory(). Know that after a match ends, the match history will be saved in the following format: a0a1a2...ai ...an In which: • ai : Notation of a move • n: Number of moves of the match After this match history mode is selected, the user will enter the match history to watch this match. The program will continue to provide users with 3 ways to control the watching process including: Watch next move, Watch previous move and Stop. SV was asked to write the following function to allow the user to control the replay of the match based on the history entered by the user. The details of the function are as follows: • Function name: displayHistory. • Input parameters: – history (char*): string containing match history – numOfMoves (type int): number of moves of the match • Function requirements: 1. Declare a 2D array of type enum Stone (called game) with size 15x15 to store the match state. Initialize all elements of the array to the value NA. 2. Displays the current state by calling the displayBoard function with the necessary arguments to display the empty board. 3. Prints the string inputCommand to the screen. 4. Reads user input as a character of type char. – If the user enters the character ’n’: Display the board state after playing the next move (continue using the displayBoard function). However, if the current move is the last move, print the string endOfHistory. Then go back to step 4. – If the user enters the character ’p’: Displays the board before playing the current move (using the displayBoard function). However, if the board is currently empty, startOfGame is printed to the screen. Then go back to step 4. – If the user enters the character ’s’: Ends the function. – If the user enters a character other than the three above, the string invalidInput is printed to the screen. Then go back to step 4. • Returns: Nothing • Note: In this assignment, user-entered match history is guaranteed to be always valid and n ¡ 225.

So far, I only have the following code to check if player X wins:

bool win = false;
for(int i = 1; i <= size; ++i){
     for ( int j =1; j <= size; ++j){
         if (isPlayerTurn == 1 && arr[i][j] == X) {
             if (arr[i][j +1] == X && arr[i][j + 2] == X && arr[i][j + 3] == X && arr[i][j + 4] == X) {
                 if (arr[i][j + 5] == X) win = false;
                 else if(arr[i][j + 5] == O && arr[i][j - 1] == O) win = false;
                 else {win = true; break;}
             }
             else if (arr[i + 1][j +1] == X && arr[i + 2][j +2] == X && arr[i + 3][j +3] == X && arr[i + 4][j +4] == X){
                 if (arr[i + 5][j + 5] == X) win = false;
                 else if (arr[i + 5][j + 5] == O) && arr[i - 1][j - 1] == O) win = false;
                 else {win = true; break;}
             }
             else if (arr[i + 1][j] == X && arr[i + 2][j] == X && arr[i + 3][j] == X && arr[i + 4][j] == X){
                 if (arr[i + 5][j] == X) win = false;
                 else if ( arr[i - 1][j] == O && arr[i + 5][j] == O) win = false;
                 else {win = true; break}
             }
             else if (arr[i - 1][j + 1] == X && arr[i - 2][j + 2] == X && arr[i - 3][j + 3] == X && arr[i - 4][j + 4] == X){
                 if (arr[i - 5][j + 5] == X) win = false;
                 else if (arr[i + 1][j - 1] == O && arr[i - 5][j + 5 == O]) win = false;
                 else {win = true; break}
             }
         }
     }
 }
 return win;

I'm not sure if I addressed the array elements correctly, or if the code is generally decent, and without the function for making moves there is no way to check, so could you please review it? It would also be great if you could suggest an algorithm for the other functions, and I will try to write the program myself. My major problem is devising algorithms.

Again, thank you so much!

Hope you have a nice day.

1 Answer

0 votes
answered Nov 7 by Peter Minarik (63,320 points)

Here are some tips below.

After fixing 1-6, your code will compile. But it will not work correctly.

  1. you can take the original code shared in I need to code functions for a gomoku game. Then you can paste in your above code into the body of the bool hasWon(enum Stone board[][15], int size, bool isFirstPlayerTurn) function. This way you can (compile and) RUN the program and see if there are any compilation errors. You can fix these easily as the compiler will tell you what's wrong.
  2. In most programming languages (including C) arrays have 0-based indices. In your code, you assume 1-based indices, so you'll miss the first (0th) row/column and index out of range when trying to index the last row/column, which is the 14th, not the 15th (remember, [0, 14] is your range, not [1, 15]).
  3. To correctly check if a bool expression is true, do not compare it to 1 (which should be correct most of the time) but check if it equals true or just simply make the statement. E.g.: isFirstPlayerTurn == true or just the statement isFirstPlayerTurn (which evaluates to true if isFirstPlayerTurn is not equal to 0).
  4. If the function calls your array board, then keep calling that and not arr.
  5. Make sure you march your opening and closing braces/parentheses. E.g. the line else if (board[i + 5][j + 5] == O) && board[i - 1][j - 1] == O) win = false; is has an extra closing parenthesis that breaks your code.
  6. Make sure your statements are terminated by a semicolon (;), even the break needs one.
  7. Your code is prone to indexing outside of the bounds of your board array. See how you keep adding 1, 2, 3, 4 and 5 to your indices or subtracting them? But your i and j variables range on [0, 14] (correctly, after fixing 1-6 above), but with the +/- 5 you actually index your board on  [-5, 19]. That's incorrect. Review your logic here.
Keep working on it, you're getting there! Good luck!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...