Answer the following questions and fill out the remaining code where indicated (
ID: 3620139 • Letter: A
Question
Answer the following questions and fill out the remaining code where indicated (Fill this out) below.1. In the getMove function in Connect4.c, numTurns is declared as a static integer. What does the keyword static mean for function-local variables? (If you’re not sure, you may be able to figure it out from how it is used.)
2. As you can see at the top of Connect4.c, the game board is also declared with the keyword static. The functions getVerticalWin, getHorizontalWin, and getDiagonalWin are also declared as static. What does static mean for functions and global variables?
3. You may have noticed that the game board is not a simple two-dimensional array of ints. Instead it is a single-dimensional array. We know the board is two-dimensional, where is the other dimension inside of our board data-structure?
4. In CSE240.h, we define a Bool type, and a few key constants within that type. What are the possible values of a Bool by this definition? What do the other values not defined as constants mean if used as the predicate expression in an if statement?
/*
CSE 240 Assignment 2
Connect Four
Your Name goes here
Your email address goes here.
This programs allows a user to play the game connect four.
*/
#include <stdio.h>
#include "Connect4.h"
#include "CSE240.h"
/***********************************************************************/
/* Global Data */
/***********************************************************************/
/* The Connect4 Game Board */
static Connect4_Column board[NUM_COLS];
/***********************************************************************/
/* Board Management */
/***********************************************************************/
/* Determine if dropping a piece in the given column is possible.
Returns:
- true if move is valid,
- false otherwise
*/
Bool isValidMove(int colNum) {
/* FILL THIS IN */
return TRUE;
}
/* Update the board to reflect the given piece dropped in the
given column.
*/
void makeMove(int toPlace, Connect4Space piece) {
/* FILL THIS IN */
}
/* Check if the board lacks any more open spaces to drop into.
Returns:
- true if the board has no more open spaces
- false otherwise
*/
Bool boardFull() {
int c;
/* check each column to see if it is full */
for(c = 0; c < NUM_COLS; c++) {
if(board[c].numPieces < COL_HEIGHT) {
/* we can stop when we see a short column */
return FALSE;
}
}
/* no short columns means the board is full*/
return TRUE;
}
/* Initializes the board to empty spaces */
void clearBoard() {
int c, r;
for(c = 0; c < NUM_COLS; c++) {
board[c].numPieces = 0;
for(r = 0; r < COL_HEIGHT; r++) {
board[c].pieces[r] = Empty;
}
}
}
/***********************************************************************/
/* Input/Output */
/***********************************************************************/
/* Prompt the player and collect the column that they would like to drop
a piece into.
Returns the column number that the user would like to play in.
*/
int getMove(Bool isMaroonTurn) {
static int numTurns = 0;
do {
int moveColumn = -1;
printBoard();
printf("%s, it is turn %d of the game. "
"Into what column would you like to drop a piece? (0 to 6): ",
isMaroonTurn ? "Maroon" : "Gold",
numTurns);
/* FILL IN LINE HERE TO READ AN INT FROM THE PLAYER */
/* END FILL IN */
if(isValidMove(moveColumn)) {
numTurns++;
return moveColumn;
} else {
printf("I'm sorry, you cannot drop into that column. ");
}
} while(TRUE);
}
/* Prints the board for the user to see. See the assignment writeup
for sample output of this function. Your implementation should match
ours exactly. */
void printBoard() {
/* FILL THIS IN */
}
/***********************************************************************/
/* Checking for a win */
/***********************************************************************/
/* Check for a win by four in-a-row vertically in a column.
Returns:
- MaroonWins if there is four in-a-column for maroon.
- GoldWins if there is four in-a-column for gold.
- None if there is neither
- (this function never returns Draw)
*/
static Connect4Winner getVerticalWin() {
/* FILL THIS IN */
return None;
}
/* Check for a win by four in-a-row horizontally.
Returns:
- MaroonWins if there is four in-a-row for maroon.
- GoldWins if there is four in-a-row for gold.
- None if there is neither
- (this function never returns Draw)
*/
static Connect4Winner getHorizontalWin() {
/* FILL THIS IN */
return None;
}
/* Check for a win by four in-a-row diagonally.
Returns:
- MaroonWins if there is four in-a-diagonal for maroon.
- GoldWins if there is four in-a-diagonal for gold.
- None if there is neither
- (this function never returns Draw)
*/
static Connect4Winner getDiagonalWin() {
/* FILL THIS IN */
return None;
}
/* Check for a win on the game board, either by row, column, or diagonal.
Returns:
- MaroonWins if there is a win for maroon.
- GoldWins if there is a win for gold.
- None if there is neither
- Draw if there is neither and the game board is full.
*/
Connect4Winner getWinner() {
Connect4Winner winner = getVerticalWin();
if(winner != None && winner != Draw) {
return winner;
}
winner = getHorizontalWin();
if(winner != None && winner != Draw) {
return winner;
}
winner = getDiagonalWin();
if(winner != None && winner != Draw) {
return winner;
}
/* if we get this far, then we need to check if there is a draw */
if(boardFull()) {
return Draw;
}
/* otherwise, there is no winner yet */
return None;
}
/***********************************************************************/
/* Game driver - makes the game actually go. */
/***********************************************************************/
void playGame() {
Connect4Winner winner = getWinner();
Bool maroonTurn = TRUE;
while(winner == None) {
/* get the move from the player */
int toPlace = getMove(maroonTurn);
/* place the move into the board */
makeMove(toPlace, maroonTurn ? Maroon : Gold);
/* check for a win */
winner = getWinner();
/* switch to the other player */
maroonTurn = !maroonTurn;
/* rinse, repeat */
}
printBoard();
switch(winner) {
case Draw:
printf("Wow, it was a tie! Good job! ");
break;
case MaroonWins:
printf("Maroon won, go Maroon! ");
break;
case GoldWins:
printf("Gold won, go Gold! ");
break;
default: /* None shouldn't happen */
printf("This is unpossible! How can there not be a winner? ");
break;
}
printf("Thanks for playing! Goodbye! ");
}
int main(void) {
/* initialize the board to empty spaces */
clearBoard();
playGame();
return 0;
}
Explanation / Answer
1. In the getMove function in Connect4.c, numTurns is declared as a static integer. What does the keyword static mean for function-local variables? (If you’re not sure, you may be able to figure it out from how it is used.) A static function-local variable exists after the function exits, but is still visible only within the function. In C, static functions are limited to the scope of the source file, whereas non-static functions can be accessed globally. A global variable is accessible in every scope. 3. You may have noticed that the game board is not a simple two-dimensional array of ints. Instead it is a single-dimensional array. We know the board is two-dimensional, where is the other dimension inside of our board data-structure? 4. In CSE240.h, we define a Bool type, and a few key constants within that type. What are the possible values of a Bool by this definition? What do the other values not defined as constants mean if used as the predicate expression in an if statement? A bool has two values: true or false. When used in an if statement, the value of the bool directly determines whether the statement is executed.For example:
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.