For this assignment, re-use your Half-Semester Project (Week 3) and opy paste th
ID: 3818584 • Letter: F
Question
For this assignment, re-use your Half-Semester Project (Week 3) and opy paste the code below into a new project).
#include //Standard Library
#include
//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();
int main(void){
char player1FirstName[20]; //Player 1 Name
char player1LastName[20];
char player2FirstName[20]; //Player 2 Name
char player2LastName[20];
float player1Score = 2; //Player 1 Score
float player2Score = 2; //Player 2 Score
int playersTurn = PLAYER1; //Player 1 always go first
//Receiving Player Names
printf("Enter Player 1's First Name: ");
scanf("%s", player1FirstName); //Notice that '&' is not used when taking in
characters
printf("Enter Player 1's Last Name: ");
scanf("%s", player1LastName);
printf("Enter Player 2's First Name: ");
scanf("%s", player2FirstName);
printf("Enter Player 2's Last Name: ");
scanf("%s", player2LastName);
//Display the game board
for(int y=0; y<=BOARD_SIZE; y++){
//Print Top Row of Board
if(y == 0){
printf(" ");//Justify the top to the right 3 spaces
Page 2 of 4
hw4.c 2/24/17, 1:07 PM
//Print Top Column Numbers
for(int x=1; x<=BOARD_SIZE; x++){
printf("%2d", x);
}
}else{ //Printing Each Row of the Game Board
printf("%2d ", y); // Print Leading Row Number
//Depending on state of space, print out either '.', 'W', or 'B'
for(int x=1; x<=BOARD_SIZE; x++){
printf(" ."); //Else Print FREE SPACE
}
}
printf(" "); //Start New Row
}
int status;
do{
//Printing out Player Names and Scores
printf(" "); //Start a new line
printf("%s %s's Score (B): %.1f ", player1FirstName, player1LastName,
player1Score);
printf("%s %s's Score (W): %.1f ", player2FirstName,
player2LastName, player2Score);
if(playersTurn == PLAYER1){
printf("%s %s, it is your turn.", player1FirstName, player1LastName
);
}
if(playersTurn == PLAYER2){
printf("%s %s, it is your turn.", player2FirstName, player2LastName
);
}
printf(" ");
//Testing Point Location Math. Later Used For Validatin Player Moves
int x1, x2, y1, y2;
printf(" ");
printf("Enter first X coordinate: ");
x1 = validateInput();
printf("Enter first Y coordinate: ");
y1 = validateInput();
do{
printf("Enter second X coordinate: ");
x2 = validateInput();
printf("Enter second Y coordinate: ");
y2 = validateInput();
if(x1 == x2 && y1 == y2){
printf("Second coordinate cannot be the same as first. ");
}
}while(x1 == x2 && y1 == y2);
switch(getAlignment(x1, y1, x2, y2)){
Page 3 of 4
hw4.c 2/24/17, 1:07 PM
case HOR_ALIGN:
printf("Points are horizontally aligned. ");
break;
case VERT_ALIGN:
printf("Points are vertically aligned. ");
break;
case DIAG_ALIGN:
printf("Points are diagonally aligned. ");
break;
default:
printf("Points are not aligned. ");
}
printf(" ");
printBoard(x1, y1, x2, y2);
//Switch Players Turns
playersTurn = playersTurn == PLAYER1 ? PLAYER2 : PLAYER1;
printf("Enter an integer to continue (q to exit): ");
}while(scanf("%d", &status));
return 0;
}
//Finds Possible Alignment Between Two Points
int getAlignment(int x1, int y1, int x2, int y2){
if(y1 == y2) //Horizontally Aligned
return HOR_ALIGN;
if(x1 == x2) //Vertically Aligned
return VERT_ALIGN;
if(abs(x1 - x2) == abs(y1 - y2))
return DIAG_ALIGN; //Diagonally Aligned
return 0; //Return nothing if points aren't aligned
}
void printBoard(int x1, int y1, int x2, int y2){
//Display the game board
for(int y=0; y<=BOARD_SIZE; y++){
//Print Top Row of Board
if(y == 0){
printf(" ");//Justify the top to the right 3 spaces
//Print Top Column Numbers
for(int x=1; x<=BOARD_SIZE; x++){
printf("%2d", x);
}
}else{ //Printing Each Row of the Game Board
printf("%2d ", y); // Print Leading Row Number
//Depending on state of space, print out either '.', 'W', or 'B'
for(int x=1; x<=BOARD_SIZE; x++){
if((x == x1 && y == y1) || (x == x2 && y == y2)){
printf(" X");
}
else
printf(" ."); //Else Print FREE SPACE
Page 4 of 4
hw4.c 2/24/17, 1:07 PM
}
}
printf(" "); //Start New Row
}
}
int validateInput(){
int num;
while(scanf("%d", &num) != 1 || num < 1 || num > BOARD_SIZE){
while(getchar() !=' '){} // clear the input buffer if the user enter
non-numeric chars.
printf("Invalid Input. Enter again: ");
}
while(getchar() !=' '){} // clear the input buffer again // for 6.5
return num;
}
Add the additional functionality described
below:
This week you will create a game board array that will save player moves. Start by
declaring this array as a 2D-array of integers with size BOARD_SIZE (at this time make
sure BOARD_SIZE is equal to 8). Create a set of macros that will define whether a space
on the game board is an EMPTY_SPACE, P1_SPACE, or P2_SPACE. An example of the
#define directives is as follows:
//Board Size Constant
#define BOARD_SIZE 8
//Board Space Constants
#define EMPTY_SPACE 0
#define P1_SPACE 1
#define P2_SPACE 2
//Player Constants
#define PLAYER1 1
#define PLAYER2 2
You will use these macros as values in your game board array. Set every cell in the game
board array to EMPTY_SPACE. Then set indices [3][3] and [4][4] to P1_SPACE and indices
[3][4] and [4][3] to P2_SPACE.
Now modify your printBoard function to take the game board array as the only
parameter. Modify the function to print out 'B' (for P1_SPACE), 'W' (for P2_SPACE), and
'.' (for EMPTY_SPACE) using your macros defined earlier. Finally, modify the code in your
program where you call the printBoard function to now take the 2D game board array as a
parameter.
Next, you may delete the code in your game loop that asks for a second coordinate, as
well as the print statements that print the alignment of the coordinates. You will only be
asking for one coordinate from now on (See sample output for example).
Finally create a function called playMove, which takes the x and y coordinates, the
current player’s turn variable, and the 2D game board array as parameters and returns an
integer. The function should set the board space at index [x][y] to the appropriate space
macro defined from earlier (P1_SPACE or P2_SPACE) depending on the player variable
passed in. This function should return 1 for now. We will modify this function more in the
coming weeks (as of right now, players will be able to overwrite previous moves. We will
change this later).
The prototypes of the functions are as follows:
int getAlignment(int, int, int, int);//Returns Alignment Constant
void printBoard(int[BOARD_SIZE][BOARD_SIZE])//Prints Game Board
int validateInput();
int playMove(int, int, int, int [BOARD_SIZE][BOARD_SIZE]);
The output has been given below.....
Sample output: Enter Player 1's First Name: John Enter Player 1's Last Name: Smith Enter Player 2's First Name: Jill Enter Player 2's Last Name: Tucker 1 2 3 4 5 6 7 8 BW W B John Smith's Score (B): 2.0 Jill Tucker's Score (W): 2.0 John Smith, it is your turn Enter X coordinate -1 Invalid Input Enter again 9 Invalid Input Enter again 0 Invalid Input Enter again 1 Enter Y coordinate: 3 1 2 3 4 5 6 7 8 3 B BW W B Enter an integer to continue (q to exit): 10 John Smith's Score (B): 2.0 Jill Tucker's Score (W) 2.0 Jill Tucker, it is your turn Enter X coordinate: 4.5 Enter Y coordinate: 2 1 2 3 4 5 6 7 8 3 B BW W B Enter an integer to continue (q to exit): qExplanation / Answer
#include<stdio.h>
#include<string.h>
//Board Size Constant
#define BOARD_SIZE 8
//Player CONSTANTS
#define PLAYER1 1
#define PLAYER2 2
//Board Space Constants
#define EMPTY_SPACE 0
#define P1_SPACE 1
#define P2_SPACE 2
void printBoard(int game[][BOARD_SIZE]); //Prints Game Board
int playMove(int x_cor, int y_cor, int playersTurn, int game[][BOARD_SIZE]);
int validateInput();
int main()
{
int x1, y1;
int x=0 , y=0 ,game[BOARD_SIZE][BOARD_SIZE];
char player1FirstName[20]; //Player 1 Name
char player1LastName[20];
char player2FirstName[20]; //Player 2 Name
char player2LastName[20];
float player1Score = 2; //Player 1 Score
float player2Score = 2; //Player 2 Score
int playersTurn = PLAYER1, status; //Player 1 always go first
for(y=0;y < BOARD_SIZE;y++){
for(x=0;x < BOARD_SIZE ; x++)
game[y][x] = EMPTY_SPACE;
}
game[3][3] = game[4][4] = P1_SPACE;
game[3][4] = game[4][3] = P2_SPACE;
printf("Enter Player 1's First Name: ");
scanf("%s", player1FirstName); //Notice that '&' is not used when taking in characters
printf("Enter Player 1's Last Name: ");
scanf("%s", player1LastName);
printf("Enter Player 2's First Name: ");
scanf("%s", player2FirstName);
printf("Enter Player 2's Last Name: ");
scanf("%s", player2LastName);
for( y=0; y<=BOARD_SIZE; y++){
if(y == 0){
printf(" ");//Justify the top to the right 3 spaces
for( x=1; x<=BOARD_SIZE; x++){
printf("%2d", x);
}
}
else{
printf("%2d ", y); // Print Leading Row Number
for( x=1; x<=BOARD_SIZE; x++){
if(game[y-1][x-1] == EMPTY_SPACE)
printf(" .");
else if(game[y-1][x-1] == P1_SPACE){
printf(" B");
}
else if (game[y-1][x-1] == P2_SPACE){
printf(" W");
}
}
}
printf(" ");
}
do{
printf(" "); //Start a new line
printf("%s %s's Score (B): %.1f ", player1FirstName, player1LastName,player1Score);
printf("%s %s's Score (W): %.1f ", player2FirstName,player2LastName, player2Score);
if(playersTurn == PLAYER1){
printf("%s %s, it is your turn.", player1FirstName, player1LastName);
}
if(playersTurn == PLAYER2){
printf("%s %s, it is your turn.", player2FirstName, player2LastName);
}
printf(" ");
printf(" ");
printf("Enter X coordinate: ");
x1 = validateInput();
printf("Enter Y coordinate: ");
y1 = validateInput();
playMove(x1,y1,playersTurn,game);
printf(" ");
printBoard(game);
//Switch Players Turns
playersTurn = (playersTurn == PLAYER1) ? PLAYER2 : PLAYER1;
printf("Enter an integer to continue (q to exit): ");
}while(scanf("%d", &status));
return 0;
}
void printBoard(int game[][BOARD_SIZE]){
int x = 0, y =0;
//Display the game board
for( y=0; y<=BOARD_SIZE; y++){
if(y == 0){
printf(" ");//Justify the top to the right 3 spaces
for( x=1; x<=BOARD_SIZE; x++){
printf("%2d", x);
}
}
else{
printf("%2d ", y); // Print Leading Row Number
for( x=1; x<=BOARD_SIZE; x++){
if(game[y-1][x-1] == P1_SPACE){
printf(" B");
}
else if( game[y-1][x-1] == P2_SPACE)
printf(" W");
else
printf(" ."); //Else Print FREE SPACE
}
}
printf(" "); //Start New Row
}
}
int playMove(int x_cor, int y_cor, int playersTurn, int game[][BOARD_SIZE])
{
if(playersTurn == PLAYER1)
game[x_cor - 1][y_cor -1] = P1_SPACE;
else if(playersTurn == PLAYER2)
game[x_cor - 1][y_cor - 1] = P2_SPACE;
return 1;
}
int validateInput()
{
int num;
while(scanf("%d", &num) != 1 || num < 1 || num > BOARD_SIZE){
while(getchar() !=' '){} // clear the input buffer if the user enter non-numeric chars.
printf("Invalid Input. Enter again: ");
}
while(getchar() !=' '){} // clear the input buffer again // for 6.5
return num;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.