Continue this assignment using the code and follow the intructions below #includ
ID: 3797630 • Letter: C
Question
Continue this assignment using the code and follow the intructions below
#include <stdio.h> //This is a statement which tells the compiler to insert the contents of stdio at that particular place.
#include <stdlib.h> // This is the standard library in c programming
#define BOARD_SIZE 9
void printboard(char array[][BOARD_SIZE]); // Prototyping a function and initializing it.
char array[BOARD_SIZE][BOARD_SIZE];
int x,y,x1,x2,y1,y2;
int horizontal,vertical,diagonal;
char ch;
int main(void) //This is basically just the name of main function which is predefined function in C library and void implies no input arguments.
{
float player_1_score; // This declaring the player 1 score as a float.
float player_2_score; // This declaring the player 2 score as a float.
float increment; // This declaring the increment score as a float.
char player_1_first_name[20]; // returns the character value player 1 first name
char player_1_last_name[20]; // returns the character value
char player_2_first_name[20]; // returns the character value
char player_2_last_name[20]; // returns the character value
setbuf(stdout,NULL); // used for windows
printf("Enter player1's first name?:"); // Prompt user to enter player 1 first name
scanf("%s",player_1_first_name); //scanf reads the input value and saves it.
printf("Enter player1's last name:"); // Prompt user to enter last name
scanf("%s", player_1_last_name); //scanf reads the input value and saves it
printf("Enter player2's first name"); // Prompt user to enter first name
scanf("%s", player_2_first_name); //scanf reads the input value and saves it.
printf("Enter player2's last name"); // prompt user to enter player 2 first name.
scanf("%s", player_2_last_name); // scanf reads the input value and saves it.
printf("Enter player1's score"); // prompts user to enter player'1s score.
scanf("%f", &player_1_score); //scanf reads the input value and saves it.
printf("Enter player2's score"); // prompts user to enter player2's score.
scanf("%f", &player_2_score); //scanf reads the input value and saves it.
printf("Enter size to increment scores scores by:"); // prompts user to enter the size to increment scores
scanf("%f", &increment); //scanf reads the input value and saves it.
player_1_score = player_1_score + increment; // Mathematical operation which shows the addition of the increment to player1's score
player_2_score = player_2_score + increment; // Mathematical operation which shows the addition of the increment to player1's score
printf("%s %s's score is: %.1f ", player_1_first_name, player_1_last_name,player_1_score); // The output on the screen given by print f
printf("%s %s's score is: %.1f ", player_2_first_name, player_2_last_name,player_2_score); // The output on the screen given by print f
while(1) // This means the loop
{
//fill the board with .
for(x=0;x<BOARD_SIZE;x++) // for Loop with condition of the Board size and the Inner loop which is the x goes faster than outer
{
for(y=0;y<BOARD_SIZE;y++) // for loop for the y axis with the condition and its relation to the board size
{
array[x][y]='.'; // This is the line of code that is responsible for the dot on the board in the output.
}
}
printboard(array); //print the board
printf(" Enter first X coordinate : "); // Prompts user to enter the first X coordinate
scanf("%d",&x1); //scanf reads the input value
printf(" Enter first Y coordinate : ");
scanf("%d",&y1); //scanf reads the input value
printf(" Enter second X coordinate : ");
scanf("%d",&x2); // scanf reads the input value
printf(" Enter second Y coordinate : ");
scanf("%d",&y2); //scanf reads the input value
array[y1-1][x1-1]='X'; // replacing the dot by x and filling the board on the input
array[y2-1][x2-1]='X';
if(x1==x2) // The test if it is vertically aligned
vertical=1;
else
vertical=0;
if(y1==y2) // The test if it is horizontally aligned
horizontal=1;
else
horizontal=0;
if(x1+y1==x2+y2 || abs(x1-y1)==abs(x2-y2)) //The test if it is diagonally aligned
diagonal=1;
else
diagonal=0;
printf(" Horizonatlly Alligned = %d",horizontal); // The code determines the number associated with the horizontal
printf(" Vertically Alligned = %d",vertical); // The code determines the number associated with the horizontal
printf(" Diagonally Alligned = %d",diagonal); // The code determines the number associated with the horizontal
printboard(array); //The code prints out the result board
printf(" Enter an integer to continue(q to exit)"); // Prompts the User to enter an integer
fflush(stdin); // flushes the output
ch=getchar(); // gets the character
if(ch=='q') // if condition
break; // since running a while 1 loop we need a break point
}
return 0; // exit code
}
void printboard(char array[][BOARD_SIZE]) // Prints the board size on the output
{
int x,y; // separate function and needs a new initialization
printf(" "); // new line statement
for(x=1;x<=BOARD_SIZE;x++) // for loop condition
printf(" %d",x); // prints the integer numbers on the horizontal axis
for(x=0;x<BOARD_SIZE;x++) // For Condition
{
printf(" %d",x+1); // enter a new line and integer in that new role
for(y=0;y<BOARD_SIZE;y++) // for Loop condition
printf(" %c",array[x][y]); // prints output of function
}
}
#include <stdio.h> //Standard Library
#include <stdlib.h>
//Board Size Constant
#define BOARD_SIZE 9
//Player CONSTANTS
#define PLAYER1 1
#define PLAYER2 2
//Alignment Constants
#define HOR_ALIGN 3
#define VERT_ALIGN 4
#define DIAG_ALIGN 5
int getAlignment(int, int, int, int); //Returns Alignment Constant
void printBoard(int, int, int, int); //Prints Game Board
int validateInput();
For this assignment, re-use your Half-Semester Project (Week 20 source file (You may copy and paste the source code to a new project) Add the additional functionality described below: This week you will be creating a couple of functions that will help organize your code for the remaining weeks of the project. Start by creating macros for the different alignment types (horizontal, vertical, diagonal). Next create a function called getAlignment that uses the code you've already written to find the alignment of two points (this function should take in 2 points). Additionally, this function should return the alignment macros created earlier. Replace the code you copied with a call to the function instead. Next create a function called "printBoard" that uses the code you've already written to print a board with two points (this function should take in 2 points). Replace the code you copied with a call to the function instead. Create yet another function called "validatelnput" that gets the X or Y coordinate of a point with scanfO and validates the input. It should not take any parameters but return an integer. This function should be called at least 4 times per loop. Next, you may delete the code asking the user for each players scores, as well as how much to increment them by. Go ahead and set the starting score values to 2 for each player.Explanation / Answer
//Modified code as per given changes:
#include <stdio.h> //This is a statement which tells the compiler to insert the contents of stdio at that particular place.
#include <stdlib.h> // This is the standard library in c programming
#define BOARD_SIZE 9
#define PLAYER1 1
#define PLAYER2 2
#define HOR_ALIGN 3
#define VERT_ALIGN 4
#define DIAG_ALIGN 5
int getAlignment(int, int, int, int); //Returns Alignment Constant
void printBoard(int, int, int, int); //Prints Game Board
int validateInput(); //validate the entered coordinate
void printboard(char array[][BOARD_SIZE]); // Prototyping a function and initializing it.
char array[BOARD_SIZE][BOARD_SIZE];
int x,y,x1,x2,y_1,y2;
int horizontal,vertical,diagonal;
char ch;
int main(void) //This is basically just the name of main function which is predefined function in C library and void implies no input arguments.
{
float player_1_score=2; // This declaring the player 1 score as a float and set to the default score to 2
float player_2_score=2; // This declaring the player 2 score as a float and set to the default score to 2
float increment=1; // This declaring the increment score as a float and set it to 1
char player_1_first_name[20]; // returns the character value player 1 first name
char player_1_last_name[20]; // returns the character value
char player_2_first_name[20]; // returns the character value
char player_2_last_name[20]; // returns the character value
setbuf(stdout,NULL); // used for windows
printf("Enter player1's first name?:"); // Prompt user to enter player 1 first name
scanf("%s",player_1_first_name); //scanf reads the input value and saves it.
printf("Enter player1's last name:"); // Prompt user to enter last name
scanf("%s", player_1_last_name); //scanf reads the input value and saves it
printf("Enter player2's first name"); // Prompt user to enter first name
scanf("%s", player_2_first_name); //scanf reads the input value and saves it.
printf("Enter player2's last name"); // prompt user to enter player 2 first name.
scanf("%s", player_2_last_name); // scanf reads the input value and saves it.
player_1_score = player_1_score + increment; // Mathematical operation which shows the addition of the increment to player1's score
player_2_score = player_2_score + increment; // Mathematical operation which shows the addition of the increment to player1's score
printf("%s %s's score is: %.1f ", player_1_first_name, player_1_last_name,player_1_score); // The output on the screen given by print f
printf("%s %s's score is: %.1f ", player_2_first_name, player_2_last_name,player_2_score); // The output on the screen given by print f
while(1) // This means the loop
{
//fill the board with .
for(x=0;x<BOARD_SIZE;x++) // for Loop with condition of the Board size and the Inner loop which is the x goes faster than outer
{
for(y=0;y<BOARD_SIZE;y++) // for loop for the y axis with the condition and its relation to the board size
{
array[x][y]='.'; // This is the line of code that is responsible for the dot on the board in the output.
}
}
printboard(array); //print the board
int res;
printf(" Enter first X coordinate : "); // Prompts user to enter the first X coordinate
res=validateInput();
x1=res;
printf(" Enter first Y coordinate : ");
res=validateInput();
y_1=res;
printf(" Enter second X coordinate : ");
res=validateInput();
x2=res;
printf(" Enter second Y coordinate : ");
res=validateInput();
y2=res;
//Function call to print the board with given points
printBoard(x1-1,y_1-1,x2-1,y2-1);
int result=getAlignment(x1,y_1,x2,y2);
if(result==HOR_ALIGN)
printf(" Horizonatlly Alligned "); // The code determines the number associated with the horizontal
else if(result==VERT_ALIGN)
printf(" Vertically Alligned "); // The code determines the number associated with the horizontal
else
printf(" Diagonally Alligned "); // The code determines the number associated with the horizontal
printboard(array); //The code prints out the result board
printf(" Enter an integer to continue(q to exit)"); // Prompts the User to enter an integer
ch=getchar(); // gets the character
if(ch=='q') // if condition
break; // since running a while 1 loop we need a break point
fflush(stdin); // flushes the output
}
return 0; // exit code
}
void printboard(char array[][BOARD_SIZE]) // Prints the board size on the output
{
int x,y; // separate function and needs a new initialization
printf(" "); // new line statement
for(x=1;x<=BOARD_SIZE;x++) // for loop condition
printf(" %d",x); // prints the integer numbers on the horizontal axis
for(x=0;x<BOARD_SIZE;x++) // For Condition
{
printf(" %d",x+1); // enter a new line and integer in that new role
for(y=0;y<BOARD_SIZE;y++) // for Loop condition
printf(" %c",array[x][y]); // prints output of function
}
}
//This function checks the points alignment whether it is horizontally or vertically or diagonally aligned
// and returns the defined macros
int getAlignment(int x1,int y_1,int x2,int y2)
{
if(x1==x2) // The test if it is vertically aligned
return VERT_ALIGN;
else if(y_1==y2) // The test if it is horizontally aligned
return HOR_ALIGN;
else if(x1+y_1==x2+y2 || abs(x1-y_1)==abs(x2-y2)) //The test if it is diagonally aligned
return DIAG_ALIGN;
}
//Function to print the Game board with the given 2 points coordinates
void printBoard(int x1,int y_1,int x2,int y2)
{
array[y_1][x1]='X';
array[y2][x2]='X';
}
//Function to check the given coordinate is valid or not
//It prompts the user to enter valid coordinate
//and return the valid input coordinate
int validateInput()
{
int coordinate;
while(1)
{
scanf("%d",&coordinate);
if(coordinate>0 && coordinate<=BOARD_SIZE)
return coordinate;
printf("Enter valid coordinate ");
}
}
//OUTPUT :
G580:~/codes/schegg$ gcc Board.c
G580:~/codes/schegg$ ./a.out
Enter player1's first name?:E
Enter player1's last name:R
Enter player2's first nameT
Enter player2's last nameY
E R's score is: 3.0
T Y's score is: 3.0
1 2 3 4 5 6 7 8 9
1 . . . . . . . . .
2 . . . . . . . . .
3 . . . . . . . . .
4 . . . . . . . . .
5 . . . . . . . . .
6 . . . . . . . . .
7 . . . . . . . . .
8 . . . . . . . . .
9 . . . . . . . . .
Enter first X coordinate : 3
Enter first Y coordinate : 4
Enter second X coordinate : 6
Enter second Y coordinate : 7
Diagonally Alligned
1 2 3 4 5 6 7 8 9
1 . . . . . . . . .
2 . . . . . . . . .
3 . . . . . . . . .
4 . . X . . . . . .
5 . . . . . . . . .
6 . . . . . . . . .
7 . . . . . X . . .
8 . . . . . . . . .
9 . . . . . . . . .
Enter an integer to continue(q to exit)
1 2 3 4 5 6 7 8 9
1 . . . . . . . . .
2 . . . . . . . . .
3 . . . . . . . . .
4 . . . . . . . . .
5 . . . . . . . . .
6 . . . . . . . . .
7 . . . . . . . . .
8 . . . . . . . . .
9 . . . . . . . . .
Enter first X coordinate : 77
Enter valid coordinate
5
Enter first Y coordinate : 3
Enter second X coordinate : 87
Enter valid coordinate
2
Enter second Y coordinate : 4
Diagonally Alligned
1 2 3 4 5 6 7 8 9
1 . . . . . . . . .
2 . . . . . . . . .
3 . . . . X . . . .
4 . X . . . . . . .
5 . . . . . . . . .
6 . . . . . . . . .
7 . . . . . . . . .
8 . . . . . . . . .
9 . . . . . . . . .
Enter an integer to continue(q to exit)
1 2 3 4 5 6 7 8 9
1 . . . . . . . . .
2 . . . . . . . . .
3 . . . . . . . . .
4 . . . . . . . . .
5 . . . . . . . . .
6 . . . . . . . . .
7 . . . . . . . . .
8 . . . . . . . . .
9 . . . . . . . . .
Enter first X coordinate :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.